Skip to content

[pages-shared] Collapse repeated request slashes and remove patchUrl - #16962

Draft
ericclemmons wants to merge 3 commits into
mainfrom
pages-shared-url-standard
Draft

ericclemmons wants to merge 3 commits into
mainfrom
pages-shared-url-standard

Conversation

@ericclemmons

@ericclemmons ericclemmons commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Fixes FUNFETTI-ASSET-WORKER-5M (internal).

Summary

Pages' production asset server still runs on the original Workers URL parser (compatibility_date = "2021-11-02"). That parser:

  • collapses repeated slashes, so Pages has always matched _redirects, _headers and assets against /secure/page when the request is //secure/page;
  • throws Invalid base URL string. when request.url has a malformed percent-escape and is used as a redirect base, so /old?sub=%RAND% returns 500 instead of the redirect.

The fix for the 500s is to enable url_standard on Pages' asset server (pages-infra!1257). That needs no change here. But testing the flag on its own showed a regression: Pages has never seen repeated slashes, because the original parser collapsed them before pages-shared read the path. With url_standard, //old, //path1 and //secure/page stop matching /old, /path1 and /secure/*, and return 404 (column ② below). Customers' existing links with // would break.

This PR moves that collapsing into pages-shared, so behavior no longer depends on which parser the runtime uses. It also removes asset-server/patchUrl, a workaround for the original parser that Pages' asset server is the only one still importing (checked across GitLab and GitHub):

 generateHandler({ request, ... })
+  request = collapseRepeatedPathSlashes(request)   // "//a///b" -> "/a/b"; unchanged requests are reused
   url = new URL(request.url)
   staticRedirectsMatcher()                        // keyed by pathname
   generateRedirectsMatcher()({ request })
   headersMatcher({ request })
   findAssetEntryForPath(pathname)
 packages/pages-shared/asset-server/
 ├── handler.ts
-└── patchUrl.ts

The slashes are collapsed once, on the request, so every consumer sees the same path. Collapsing only for the asset lookup, as decodePath does in workers-shared, would let //secure/page skip /secure/* _headers rules. That is the same class of problem #16603 fixed for Pages Functions middleware.

patchUrl is not a pure helper. Importing it replaces the global URL with a wrapper that calls encodeURI on the input, which is what made the original parser accept malformed escapes like /100%. It also returns every property through decodeURIComponent, so url.toString() and url.searchParams throw for any code in the Worker. Removing it is a breaking change: a consumer on the original parser that drops the import without enabling url_standard starts returning 500 for malformed escapes (column ③ below). The changeset is minor (breaking for this pre-1.0 package), so consumers on ^0.13.x only pick it up when they bump deliberately.

Evidence

  • Before: with the handler change reverted, the new repeated slashes in the request path tests fail (4 failed). They cover //secure/page, /secure//page and //secure///page, each serving the asset with the _headers rule applied, and //old?keep=1 redirecting to /secure/page?keep=1.
    After: pnpm vitest run __tests__/asset-server in packages/pages-shared passes (89 passed, 1 todo). tsc and oxlint are clean.

  • Before/after in Pages' asset server: I ran the asset server bundle in Miniflare (compatibility date 2021-11-02) in four setups. The manifest has /path1, /café, /100% and /secure/page. _redirects has /old (301), /rw (200 rewrite) and /dyn/:x (302), all pointing to /path1. _headers sets X-Frame-Options on /secure/*.

    Request ① Today: 0.13.202 + patchUrl, original parser ② 0.13.202 + patchUrl + url_standard ③ This PR, original parser ④ This PR + url_standard
    /path1 200 200 200 200
    //path1 200 404 200 200
    /secure/page 200 + XFO 200 + XFO 200 + XFO 200 + XFO
    //secure/page 200 + XFO 404 200 + XFO 200 + XFO
    /caf%C3%A9 200 200 200 200
    /100%25 200 200 200 200
    /old 301 → /path1 301 → /path1 301 → /path1 301 → /path1
    //old 301 → /path1 404 301 → /path1 301 → /path1
    //dyn//a 302 → /path1 404 302 → /path1 302 → /path1
    /old?sub=RAND 301 → /path1?sub=RAND same same same
    /old?sub=%RAND% 500 301 → /path1?sub=%RAND% 500 301 → /path1?sub=%RAND%
    /rw?sub=%RAND% 500 200 500 200
    /dyn/a?sub=%RAND% 500 302 → /path1?sub=%RAND% 500 302 → /path1?sub=%RAND%
    /path1?sub=%RAND% 200 200 500 200
    /100% 200 200 500 200
    /a%zz 404 404 500 404

    ④ is the target: it matches ① everywhere except the three malformed-escape redirects and rewrites, which stop returning 500. ② and ③ show why the flag and this PR have to ship together. The flag alone breaks // paths, and this PR alone (dropping patchUrl on the original parser) turns every malformed escape into a 500.

  • The new matches %s with malformed percent-escapes in the query tests pass with or without this change, because these tests already run in workerd with the standard parser. They document the behavior Pages gets once it enables url_standard.

  • Three expectations from [pages-shared] Normalize relative redirect targets consistently #16527 changed: /old//images/logo.png → https://other.example/:splat (and the scheme-less variants) now give other.example/images/logo.png, not other.example//images/logo.png. Pages production already returns the collapsed form, so wrangler pages dev now matches it. Explicit // in the target's query and fragment is still preserved.

Merge Danger

Door: two-way

Reverting restores the previous handler and patchUrl.ts. The consumer bump in Pages' asset server (pages-infra!1257) is a separate, progressive release.

Blast Radius: pages-shared consumers

  • wrangler pages dev: paths with repeated slashes now match _redirects, _headers and assets the way production does.
  • Anything importing @cloudflare/pages-shared/dist/asset-server/patchUrl breaks at build time after upgrading to 0.14. It has to remove the import and enable url_standard in the same release, or requests with malformed escapes return 500. The only known importer is Pages' asset server, which does both in pages-infra!1257.

  • Tests
    • Tests included/updated
    • Automated tests not possible - manual testing has been completed as follows:
    • Additional testing not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: this keeps Pages' existing production URL handling and removes an internal module used only by Pages' asset server.

@changeset-bot

changeset-bot Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 60deac1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@cloudflare/pages-shared Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ask-bonk

ask-bonk Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

LGTM

github run

@pkg-pr-new

pkg-pr-new Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
@cloudflare/autoconfig

npm i https://pkg.pr.new/@cloudflare/autoconfig@16962

@cloudflare/build-output-utils

npm i https://pkg.pr.new/@cloudflare/build-output-utils@16962

@cloudflare/codemods

npm i https://pkg.pr.new/@cloudflare/codemods@16962

@cloudflare/config

npm i https://pkg.pr.new/@cloudflare/config@16962

@cloudflare/containers-shared

npm i https://pkg.pr.new/@cloudflare/containers-shared@16962

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@16962

@cloudflare/deploy-helpers

npm i https://pkg.pr.new/@cloudflare/deploy-helpers@16962

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@16962

miniflare

npm i https://pkg.pr.new/miniflare@16962

@cloudflare/pages-functions

npm i https://pkg.pr.new/@cloudflare/pages-functions@16962

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@16962

@cloudflare/runtime-types

npm i https://pkg.pr.new/@cloudflare/runtime-types@16962

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@16962

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@16962

@cloudflare/vitest-plugin

npm i https://pkg.pr.new/@cloudflare/vitest-plugin@16962

@cloudflare/workers-auth

npm i https://pkg.pr.new/@cloudflare/workers-auth@16962

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@16962

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@16962

wrangler

npm i https://pkg.pr.new/wrangler@16962

commit: 60deac1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Untriaged

Development

Successfully merging this pull request may close these issues.

1 participant