Skip to content

fix(security): remove ReDoS-prone regex in extractCidPath (CodeQL high)#315

Merged
QSchlegel merged 1 commit into
preprodfrom
claude/fix-ipfs-redos
Jun 15, 2026
Merged

fix(security): remove ReDoS-prone regex in extractCidPath (CodeQL high)#315
QSchlegel merged 1 commit into
preprodfrom
claude/fix-ipfs-redos

Conversation

@QSchlegel

Copy link
Copy Markdown
Collaborator

Fixes the high-severity CodeQL alert ("Polynomial regular expression used on uncontrolled data") flagged on the release PR #303, in src/lib/ipfs.ts.

The issue

extractCidPath() parses caller-supplied strings (IPFS anchor URLs, CIDs) and ran:

const m = s.match(/\/ipfs\/(.+)$/i);

As a search-anywhere pattern, the engine retries the /ipfs/ literal at many start positions, each followed by a backtracking .+$. On hostile input like /ipfs/a/ipfs/a/ipfs/a… this is O(n²) polynomial work — a denial-of-service vector, since extractCidPath runs on user-controlled anchor values (e.g. in /api/ipfs/resolve).

The fix

Replace the regex with a linear indexOf("/ipfs/") + slice, preserving the exact behavior: take everything after the first /ipfs/ segment that has content, case-insensitive. No backtracking → linear time.

Added src/__tests__/ipfs.test.ts covering the equivalence cases (bare CID, ipfs://, gateway URLs with path, case-insensitive marker, query/hash stripping, traversal/short rejection) and a hostile repeated-/ipfs/ input that must terminate in <1s.

Verify

  • tsc clean · jest 369 passed (+7 new) · lint clean.

Merging into preprod clears the alert on the release PR #303.

🤖 Generated with Claude Code

extractCidPath ran `s.match(/\/ipfs\/(.+)$/i)` on caller-supplied strings
(anchor URLs, CIDs). As a search-anywhere pattern it retries `/ipfs/` at many
start positions with a backtracking `.+$`, which CodeQL flags as polynomial
ReDoS on hostile inputs like "/ipfs/a/ipfs/a/ipfs/a…".

Replace it with a linear `indexOf("/ipfs/")` + `slice`, preserving the exact
behavior (everything after the first "/ipfs/" segment that has content,
case-insensitive). Add an extractCidPath test covering the equivalence cases
plus a hostile-input case that must terminate quickly.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@vercel

vercel Bot commented Jun 15, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
multisig Ready Ready Preview, Comment Jun 15, 2026 10:53am

Request Review

@QSchlegel QSchlegel merged commit dbc9356 into preprod Jun 15, 2026
7 checks passed
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