fix(worker-bundler): eliminate polynomial ReDoS in import/export regexes#1718
Open
mattzcarey wants to merge 1 commit into
Open
fix(worker-bundler): eliminate polynomial ReDoS in import/export regexes#1718mattzcarey wants to merge 1 commit into
mattzcarey wants to merge 1 commit into
Conversation
The clause sub-pattern [\w*{}\s,]+ followed by \s+ let both quantifiers
consume the same whitespace, backtracking polynomially on near-match
inputs (import + 10k spaces took ~175s). Match clauses as non-whitespace
tokens separated by whitespace instead — linear and behaviorally
equivalent. Applies to rewriteImports (transformer.ts) and the
parseImports regex fallback (resolver.ts), which had the same shape.
Fixes #1537
🦋 Changeset detectedLatest commit: 9acd75d 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 |
agents
@cloudflare/ai-chat
@cloudflare/codemode
create-think
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
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.
Note
This is a Fable experiment (model-authored PR). Please close if it adds no value.
Fixes #1537
Problem
rewriteImports(packages/worker-bundler/src/transformer.ts) matched import/export clauses with:[\w*{}\s,]+includes\s, so it overlaps with the adjacent\s+— on near-match input the engine backtracks through every split of the whitespace between the two quantifiers. The issue's PoC reproduces on main:import+ 1k spaces +Ximport+ 5k spaces +Ximport+ 10k spaces +XThe same shape exists in two more places the issue didn't mention:
parseImportsRegexinresolver.ts(importRegexandexportFromRegex) — the fallback parser that runs whenever es-module-lexer throws (e.g. on JSX/TSX), so it's reachable with untrusted module source too. Fixed all three.Fix
Match the clause as non-whitespace tokens separated by whitespace:
No two adjacent quantifiers can consume the same characters, so matching is linear. I considered the bounded-quantifier mitigation (
{1,200}) but rejected it: it caps rather than removes the overlap (still ~2.7s at n=5000) and silently stops matching legitimately long clauses (a 200+ char multi-line named-import list).Verified equivalent on a 15-case corpus (default/named/namespace/side-effect/mixed/multi-line imports,
export {…} from/export * from, near-miss non-imports, dynamic import) and stress-tested the new pattern against other adversarial shapes (repeatedfromtokens, stacked near-matches, token tails) — all sub-20ms.Tests
packages/worker-bundler/src/tests/redos.test.ts(new):transformAndResolveon a 50k-space whitespace-bomb module completes in ms (bounded at 5s; old code takes tens of minutes at this size, so the bound is generous but unambiguous)parseImportsregex fallback (forced via JSX) on the same bomb completes in mstransformAndResolveexport … from, dynamic)Full package suite: 185 passed (6 files) via
pnpm test. Typecheck/oxfmt/oxlint clean.Notes for maintainers
dynamicImportRegex) has no overlapping quantifiers and is untouched.@cloudflare/worker-bundlerpatch).