[workers-shared] Fix _redirects/_headers rules silently dropped when placeholders share a prefix - #14890
Open
chinesepowered wants to merge 1 commit into
Conversation
…ules Substituting on the raw `:name` text let a shorter placeholder match inside a longer one sharing its prefix. For `/p/:id/:id_2` this emitted the `id` capture group twice, and the resulting SyntaxError was swallowed by generateRulesMatcher's catch, so the rule was silently dropped. The same collision corrupted replacer's output. Both now do a single regex-driven pass over whole placeholder matches.
🦋 Changeset detectedLatest commit: 0091dc8 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 |
workers-devprod
requested review from
a team and
jamesopstad
and removed request for
a team
July 28, 2026 02:15
Contributor
|
Codeowners approval required for this PR:
Show detailed file reviewers
|
@cloudflare/autoconfig
@cloudflare/config
create-cloudflare
@cloudflare/deploy-helpers
@cloudflare/kv-asset-handler
miniflare
@cloudflare/pages-shared
@cloudflare/unenv-preset
@cloudflare/vite-plugin
@cloudflare/vitest-pool-workers
@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 #14887.
When two placeholders in a
_redirectsor_headersrule share a prefix — e.g.:idand:id_2— the rule is silently dropped at runtime. Both bugs come from the same cause: substituting on a raw:namestring rather than on a placeholder-aware match, so a shorter name matches inside a longer one.generateRuleRegExpbuilt an invalid regex. Splitting on the literal":id"also split inside":id_2", so both placeholders became the same named group:generateRulesMatchercompiles rules inside a barecatch {}, so this was swallowed and the rule was dropped fromcompiledRulesentirely — no warning, no log, the rule just never matched.replacersubstituted the shorter placeholder first, corrupting the longer one:replacer("/dest/:id/:id_2", { id: "1", id_2: "2" })returned/dest/1/1_2rather than/dest/1/2.Placeholder names are already fully specified by
PLACEHOLDER_REGEX(/:([A-Za-z]\w*)/g), so:id_2is one token and not a:idfollowed by_2. Both functions now do a single regex-driven pass, matching each placeholder whole. This also fixes a third-order effect of the old sequential loop: a replacement value containing:namecould be re-substituted by a later iteration.I left the bare
catch {}ingenerateRulesMatcheralone — an invalid rule vanishing without a diagnostic is what makes this class of bug hard to find, but changing it is a separate behavioural decision and I didn't want to bundle it in. Happy to follow up if you'd like it.:id_2was always meant to be one placeholder. No syntax or interface change.Four tests added. Three of them fail without the source change, with exactly the symptoms above:
The fourth (
should leave placeholders with no replacement alone) passes either way and is there to pin the no-replacement behaviour against the rewrite. Fullrules-engine.test.tspasses (15 tests).Note:
asset-workerhas 3 pre-existing failures onmain(assets-manifest.test.ts, plus two 5s timeouts in the KV retry tests). I confirmed they fail identically with and without this change.