Repository navigation
[pages-shared] Collapse repeated request slashes and remove patchUrl - #16962
Draft
ericclemmons wants to merge 3 commits into
Draft
ericclemmons wants to merge 3 commits into
ericclemmons wants to merge 3 commits into
Conversation
🦋 Changeset detectedLatest commit: 60deac1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Contributor
|
LGTM |
@cloudflare/autoconfig
@cloudflare/build-output-utils
@cloudflare/codemods
@cloudflare/config
@cloudflare/containers-shared
create-cloudflare
@cloudflare/deploy-helpers
@cloudflare/kv-asset-handler
miniflare
@cloudflare/pages-functions
@cloudflare/pages-shared
@cloudflare/runtime-types
@cloudflare/unenv-preset
@cloudflare/vite-plugin
@cloudflare/vitest-plugin
@cloudflare/workers-auth
@cloudflare/workers-editor-shared
@cloudflare/workers-utils
wrangler
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:_redirects,_headersand assets against/secure/pagewhen the request is//secure/page;Invalid base URL string.whenrequest.urlhas 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_standardon 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 beforepages-sharedread the path. Withurl_standard,//old,//path1and//secure/pagestop matching/old,/path1and/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 removesasset-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.tsThe slashes are collapsed once, on the request, so every consumer sees the same path. Collapsing only for the asset lookup, as
decodePathdoes inworkers-shared, would let//secure/pageskip/secure/*_headersrules. That is the same class of problem #16603 fixed for Pages Functions middleware.patchUrlis not a pure helper. Importing it replaces the globalURLwith a wrapper that callsencodeURIon the input, which is what made the original parser accept malformed escapes like/100%. It also returns every property throughdecodeURIComponent, sourl.toString()andurl.searchParamsthrow for any code in the Worker. Removing it is a breaking change: a consumer on the original parser that drops the import without enablingurl_standardstarts returning 500 for malformed escapes (column ③ below). The changeset isminor(breaking for this pre-1.0 package), so consumers on^0.13.xonly pick it up when they bump deliberately.Evidence
Before: with the handler change reverted, the new
repeated slashes in the request pathtests fail (4 failed). They cover//secure/page,/secure//pageand//secure///page, each serving the asset with the_headersrule applied, and//old?keep=1redirecting to/secure/page?keep=1.After:
pnpm vitest run __tests__/asset-serverinpackages/pages-sharedpasses (89 passed, 1 todo).tscand 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._redirectshas/old(301),/rw(200 rewrite) and/dyn/:x(302), all pointing to/path1._headerssetsX-Frame-Optionson/secure/*.patchUrl, original parserpatchUrl+url_standardurl_standard/path1//path1/secure/page//secure/page/caf%C3%A9/100%25/old/path1/path1/path1/path1//old/path1/path1/path1//dyn//a/path1/path1/path1/old?sub=RAND/path1?sub=RAND/old?sub=%RAND%/path1?sub=%RAND%/path1?sub=%RAND%/rw?sub=%RAND%/dyn/a?sub=%RAND%/path1?sub=%RAND%/path1?sub=%RAND%/path1?sub=%RAND%/100%/a%zz④ 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 (droppingpatchUrlon the original parser) turns every malformed escape into a 500.The new
matches %s with malformed percent-escapes in the querytests 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 enablesurl_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 giveother.example/images/logo.png, notother.example//images/logo.png. Pages production already returns the collapsed form, sowrangler pages devnow 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-sharedconsumerswrangler pages dev: paths with repeated slashes now match_redirects,_headersand assets the way production does.@cloudflare/pages-shared/dist/asset-server/patchUrlbreaks at build time after upgrading to 0.14. It has to remove the import and enableurl_standardin 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.