Skip to content

fix(worker-bundler): bound stacked import scans (#1718)#1961

Merged
mattzcarey merged 2 commits into
mainfrom
fix/issue-1718-1784302163
Jul 20, 2026
Merged

fix(worker-bundler): bound stacked import scans (#1718)#1961
mattzcarey merged 2 commits into
mainfrom
fix/issue-1718-1784302163

Conversation

@agent-think

@agent-think agent-think Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Requested by @mattzcarey

Closes #1718

What was wrong

The original change correctly removes the overlapping whitespace quantifiers in packages/worker-bundler/src/transformer.ts and the regex fallback in resolver.ts. During the requested final review, I found one remaining polynomial shape: because each regex is an unanchored global search, stacked near-matches such as ("import value ").repeat(n) + "X" retried the whole remaining suffix at every import. At 15,000 repetitions, the PR-head rewrite and fallback each took about 8 seconds locally.

What changed

This keeps the PR's small regex-based approach, but bounds each candidate at the next whole-word import/export keyword. That prevents repeated suffix scans while preserving the valid import/export forms already covered by the PR. A regression test exercises 15,000 stacked near-matches, and the changeset now describes both protections.

Testing

  • pnpm --filter @cloudflare/worker-bundler test: 186 passed (6 files)
  • New focused ReDoS suite: 5 passed, including the stacked-near-match regression
  • tsc --noEmit -p packages/worker-bundler/tsconfig.json: passed
  • oxfmt --check on changed files: passed
  • oxlint on changed TypeScript files: 0 warnings/errors
  • pnpm --filter @cloudflare/worker-bundler build: passed
  • Local stacked-input benchmark (15,000 repetitions): PR head ~8.1s rewrite / ~7.7s fallback; reviewed patterns ~2.4ms / ~1.2ms

Demo

Demo URL (expires after 60 mins): https://issue-1718-redos-fix-demo.alpine-larch.workers.dev

Open it and click Run 500k-space case and Run 100k stacked imports; both execute createWorker({ bundle: false }) in a live Worker and report successful completion. The demo installs and runs the packed @cloudflare/worker-bundler build from this branch (its unused, bundle-only esbuild WASM import is stubbed solely to fit the temporary free-preview upload limit).

This final review builds on the original PR #1718 and the original report in #1537.

🤖 generated by the pr-agent — please review carefully


Open in Devin Review

Matt Carey and others added 2 commits June 9, 2026 21:17
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-bot

changeset-bot Bot commented Jul 17, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 229b8b3

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

This PR includes changesets to release 1 package
Name Type
@cloudflare/worker-bundler Patch

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

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

@pkg-pr-new

pkg-pr-new Bot commented Jul 17, 2026

Copy link
Copy Markdown

Open in StackBlitz

agents

npm i https://pkg.pr.new/agents@1961

@cloudflare/ai-chat

npm i https://pkg.pr.new/@cloudflare/ai-chat@1961

@cloudflare/codemode

npm i https://pkg.pr.new/@cloudflare/codemode@1961

create-think

npm i https://pkg.pr.new/create-think@1961

hono-agents

npm i https://pkg.pr.new/hono-agents@1961

@cloudflare/shell

npm i https://pkg.pr.new/@cloudflare/shell@1961

@cloudflare/think

npm i https://pkg.pr.new/@cloudflare/think@1961

@cloudflare/voice

npm i https://pkg.pr.new/@cloudflare/voice@1961

@cloudflare/worker-bundler

npm i https://pkg.pr.new/@cloudflare/worker-bundler@1961

commit: 229b8b3

@mattzcarey
mattzcarey merged commit 365ced3 into main Jul 20, 2026
5 checks passed
@mattzcarey
mattzcarey deleted the fix/issue-1718-1784302163 branch July 20, 2026 16:01
@github-actions github-actions Bot mentioned this pull request Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant