Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: try to serve assets from unencoded and encoded paths #6728

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions packages/miniflare/src/plugins/assets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,9 @@ const walk = async (dir: string) => {
*/

const [pathHash, contentHash] = await Promise.all([
hashPath(encodeFilePath(relativeFilepath, path.sep)),
hashPath(
encodeFilePath(filepath, path.sep) + filestat.mtimeMs.toString()
),
hashPath(normalizeFilePath(relativeFilepath)),
// used absolute filepath here so that changes to the enclosing asset folder will be registered
hashPath(filepath + filestat.mtimeMs.toString()),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note what goes into contentHash doesn't actually matter, as long as it is uniquely associated with that filepath and updates with file changes. Just removed the filepath encoding because it was unnecessary and because normalizeFilePath() expects a relative filepath now.

]);
manifest.push({
pathHash,
Expand Down
62 changes: 51 additions & 11 deletions packages/workers-shared/asset-worker/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ export const handleRequest = async (
) => {
const { pathname, search } = new URL(request.url);

const decoded = pathname
.split("/")
.map((x) => decodeURIComponent(x))
.join("/");
const intent = await getIntent(decoded, configuration, exists);
const decodedPathname = decodePath(pathname);
const intent = await getIntent(decodedPathname, configuration, exists);

if (!intent) {
return new NotFoundResponse();
Expand All @@ -36,11 +33,14 @@ export const handleRequest = async (
return new MethodNotAllowedResponse();
}

const decodedDestination = intent.redirect ?? decoded;
const encodedDestination = decodedDestination
.split("/")
.map((x) => encodeURIComponent(x))
.join("/");
const decodedDestination = intent.redirect ?? decodedPathname;
const encodedDestination = encodePath(decodedDestination);

/**
* The canonical path we serve an asset at is the decoded and re-encoded version.
* Thus we need to redirect if that is different from the decoded version.
* We combine this with other redirects (e.g. for html_handling) to avoid multiple redirects.
*/
if (encodedDestination !== pathname || intent.redirect) {
emily-shen marked this conversation as resolved.
Show resolved Hide resolved
return new TemporaryRedirectResponse(encodedDestination + search);
}
Expand Down Expand Up @@ -370,7 +370,6 @@ const htmlHandlingForceTrailingSlash = async (
return {
asset: { eTag: exactETag, status: 200 },
redirect: null,
file: pathname,
};
} else if (
(redirectResult = await safeRedirect(
Expand Down Expand Up @@ -653,3 +652,44 @@ const safeRedirect = async (

return null;
};
/**
*
* +===========================================+===========+======================+
* | character type | fetch() | encodeURIComponent() |
* +===========================================+===========+======================+
* | unreserved ASCII e.g. a-z | unchanged | unchanged |
* +-------------------------------------------+-----------+----------------------+
* | reserved (sometimes encoded) | unchanged | encoded |
* | e.g. [ ] @ $ ! ' ( ) * + , ; = : ? # & % | | |
* +-------------------------------------------+-----------+----------------------+
* | non-ASCII e.g. ü. and space | encoded | encoded |
* +-------------------------------------------+-----------+----------------------+
*
* 1. Decode incoming path to handle non-ASCII characters or optionally encoded characters (e.g. square brackets)
* 2. Match decoded path to manifest
* 3. Re-encode the path and redirect if the re-encoded path is different from the original path
*
* If the user uploads a file that is already URL-encoded, that is accessible only at the (double) encoded path.
* e.g. /%5Bboop%5D.html is served at /%255Bboop%255D only
*
* */

/**
* Decode all incoming paths to ensure that we can handle paths with non-ASCII characters.
*/
const decodePath = (pathname: string) => {
return pathname
.split("/")
.map((x) => decodeURIComponent(x))
.join("/");
};
/**
* Use the encoded path as the canonical path for sometimes-encoded characters
* e.g. /[boop] -> /%5Bboop%5D 307
*/
const encodePath = (pathname: string) => {
return pathname
.split("/")
.map((x) => encodeURIComponent(x))
.join("/");
};
10 changes: 7 additions & 3 deletions packages/workers-shared/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { isAbsolute, sep } from "node:path";
import { getType } from "mime";

/** normalises sep for windows */
export const normalizeFilePath = (filePath: string, sep: string) => {
const encodedPath = filePath.split(sep).join("/");
/** normalises sep for windows and prefix with `/` */
export const normalizeFilePath = (relativeFilepath: string) => {
if (!isAbsolute(relativeFilepath)) {
emily-shen marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(`Expected relative path`);
}
const encodedPath = relativeFilepath.split(sep).join("/");
return "/" + encodedPath;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/workers-shared/utils/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"lib": ["es2021"],
"module": "NodeNext",
"moduleResolution": "nodenext",
"types": ["@cloudflare/workers-types/experimental"],
"types": ["@cloudflare/workers-types/experimental", "@types/node"],
"noEmit": true,
"isolatedModules": true,
"allowSyntheticDefaultImports": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/experimental-assets.ts
GregBrimble marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export const buildAssetManifest = async (dir: string) => {
`Ensure all assets in your assets directory "${dir}" conform with the Workers maximum size requirement.`
);
}
manifest[normalizeFilePath(relativeFilepath, path.sep)] = {
manifest[normalizeFilePath(relativeFilepath)] = {
hash: hashFile(filepath),
size: filestat.size,
};
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.