diff --git a/.changeset/pages-shared-collapse-request-slashes.md b/.changeset/pages-shared-collapse-request-slashes.md new file mode 100644 index 0000000000..acf6da6c95 --- /dev/null +++ b/.changeset/pages-shared-collapse-request-slashes.md @@ -0,0 +1,9 @@ +--- +"@cloudflare/pages-shared": minor +--- + +Collapse repeated slashes in request paths and remove the `asset-server/patchUrl` module + +The original Workers URL parser collapsed repeated slashes, so Pages has always matched `_redirects`, `_headers` and assets against paths such as `//secure/page` as if they were `/secure/page`. The WHATWG URL parser keeps them, so the asset server now collapses them itself before matching. This keeps `_headers` rules applying to those paths, and makes the `wrangler pages dev` behavior match production. + +`asset-server/patchUrl` worked around the original URL parser and is no longer needed. Asset servers that import it should enable the `url_standard` compatibility flag instead. That flag also stops requests with malformed percent-escapes, such as `?sub=%RAND%`, failing with `Invalid base URL string.` when they match a redirect rule. diff --git a/packages/pages-shared/__tests__/asset-server/handler.test.ts b/packages/pages-shared/__tests__/asset-server/handler.test.ts index 79995b739a..bb890db97a 100644 --- a/packages/pages-shared/__tests__/asset-server/handler.test.ts +++ b/packages/pages-shared/__tests__/asset-server/handler.test.ts @@ -195,6 +195,55 @@ describe("asset-server handler", () => { } }); + describe("repeated slashes in the request path", () => { + const metadata = createMetadataObject({ + deploymentId: "mock-deployment-id", + redirects: { + invalid: [], + rules: [ + { from: "/old", to: "/secure/page", status: 301, lineNumber: 1 }, + ], + }, + headers: { + invalid: [], + rules: [ + { + path: "/secure/*", + headers: { "x-frame-options": "DENY" }, + unsetHeaders: [], + }, + ], + }, + }) as Metadata; + const findAssetEntryForPath = async (path: string) => + path === "/secure/page" ? "secure-page" : null; + + test.for(["//secure/page", "/secure//page", "//secure///page"])( + "serves the asset and applies _headers rules for %s", + async (path, { expect }) => { + const { response } = await getTestResponse({ + request: `https://example.com${path}`, + metadata, + findAssetEntryForPath, + }); + + expect(response.status).toBe(200); + expect(response.headers.get("x-frame-options")).toBe("DENY"); + } + ); + + test("matches _redirects rules", async ({ expect }) => { + const { response } = await getTestResponse({ + request: "https://example.com//old?keep=1", + metadata, + findAssetEntryForPath, + }); + + expect(response.status).toBe(301); + expect(response.headers.get("Location")).toBe("/secure/page?keep=1"); + }); + }); + test("Match exact pathnames, before any HTML redirection", async ({ expect, }) => { @@ -1243,7 +1292,7 @@ describe("asset-server handler", () => { expect(response.status).toBe(302); expect(response.headers.get("Location")).toBe( - `${protocol}://other.example//images/logo.png?next=https://example.com/a//b#section//one` + `${protocol}://other.example/images/logo.png?next=https://example.com/a//b#section//one` ); } ); @@ -1270,7 +1319,7 @@ describe("asset-server handler", () => { "/old/*", "/old//images/logo.png", "https://other.example/:splat", - "https://other.example//images/logo.png", + "https://other.example/images/logo.png", ], ])( "normalizes the pathname of %s -> %s (%s)", @@ -1287,6 +1336,28 @@ describe("asset-server handler", () => { } ); + test.for([ + ["/old", 301, "/new"], + ["/rewrite", 200, null], + ] as const)( + "matches %s with malformed percent-escapes in the query", + async ([from, status, location], { expect }) => { + const { response } = await getTestResponse({ + request: `https://foo.com${from}?sub=%RAND%`, + metadata: createMetadataObjectWithRedirects([ + { from, to: "/new", status }, + ]), + findAssetEntryForPath: async (path) => + path === "/new" ? "new" : null, + }); + + expect(response.status).toBe(status); + expect(response.headers.get("Location")).toBe( + location && `${location}?sub=%RAND%` + ); + } + ); + test("it should redirect uri-encoded paths", async ({ expect }) => { const { response, spies } = await getTestResponse({ request: "https://foo.com/some%20page", diff --git a/packages/pages-shared/asset-server/handler.ts b/packages/pages-shared/asset-server/handler.ts index 9403218d84..6a9f151656 100644 --- a/packages/pages-shared/asset-server/handler.ts +++ b/packages/pages-shared/asset-server/handler.ts @@ -188,6 +188,7 @@ export async function generateHandler< caches, waitUntil, }: HandlerContext) { + request = collapseRepeatedPathSlashes(request); const url = new URL(request.url); const { protocol, host, search } = url; let { pathname } = url; @@ -809,6 +810,26 @@ export function parseQualityWeightedList(list = "") { return items; } +/** + * Collapse repeated slashes in the request pathname. + * + * The original Workers URL parser collapsed them, so Pages has always matched + * `_redirects`, `_headers` and assets against the collapsed path. The WHATWG + * parser keeps them, so `//secure/page` would otherwise skip `/secure/*` rules. + * + * @param request The incoming request. + * @returns The same request, or a copy whose pathname has no repeated slashes. + */ +function collapseRepeatedPathSlashes(request: Request): Request { + const url = new URL(request.url); + if (!url.pathname.includes("//")) { + return request; + } + + url.pathname = url.pathname.replace(/\/{2,}/g, "/"); + return new Request(url, request); +} + function isCacheable(request: Request) { return !request.headers.has("authorization") && !request.headers.has("range"); } diff --git a/packages/pages-shared/asset-server/patchUrl.ts b/packages/pages-shared/asset-server/patchUrl.ts deleted file mode 100644 index b60f7020e0..0000000000 --- a/packages/pages-shared/asset-server/patchUrl.ts +++ /dev/null @@ -1,21 +0,0 @@ -(globalThis as unknown as Record).URL = (function (globalURL) { - PatchedURL.prototype = globalURL.prototype; - PatchedURL.createObjectURL = globalURL.createObjectURL; - PatchedURL.revokeObjectURL = globalURL.revokeObjectURL; - - return PatchedURL as unknown as typeof globalURL; - - function PatchedURL(input: string, base?: string | URL) { - const url = new globalURL(encodeURI(input), base); - - return new Proxy(url, { - get(target, prop) { - return globalThis.decodeURIComponent( - (target as unknown as Record)[prop] - ); - }, - }); - } -})(URL); - -export {};