Skip to content

[v2] fix: keep wildcards out of repeated extglobs (CVE-2026-112366) - #265

Open
jibsta210 wants to merge 1 commit into
micromatch:v2from
jibsta210:fix/v2-repeated-wildcard-extglob-redos
Open

jibsta210 wants to merge 1 commit into
micromatch:v2from
jibsta210:fix/v2-repeated-wildcard-extglob-redos

Conversation

@jibsta210

Copy link
Copy Markdown

Backport to v2 (2.3.2, used by micromatch 4 / chokidar 3 / anymatch) of #264; the lib/parse.js change is identical.

Problem

GHSA-fc5q-39xc-vjpf / CVE-2026-112366: a repeated extglob whose body contains a wildcard compiles to
one unbounded quantifier nested inside another, which backtracks exponentially on short input that
almost matches:

pm.makeRe('+(*a)').source                    //=> ^(?:(?=.)(?:[^/]*?a)+)$
pm.isMatch('a'.repeat(48) + '/', '+(*a)')     // does not return

analyzeRepeatedExtglob only examines multi-branch bodies, so single-branch forms (+(*), +(*a),
*(*a), +(a*b)) are never checked. The same nesting also comes from several branches (+(a*|b)),
a star inside a nested @(...), a negation (+(!(a)), which compiles to a lookahead plus a star),
and a regex + after a group (@(*a)+, (*a)+, {*a,b}+).

Fix

  1. Exact rewrite in extglobClose. When a +(...)/*(...) body holds a wildcard, the repeat is
    rewritten only where it provably matches the same strings, because a star can absorb neighbouring
    repeats:

    body rewrite why it is exact
    one branch with a star, +(*a) (?:[^/]*?a) the last star of one repeat absorbs the rest of it and the next repeat up to that repeat's trailing text
    starred branches all end with a star, +(a*|b) (?:b)*(?:a[^/]*?|b) a starred repeat absorbs every repeat after it
    starred branches all start with a star, +(*.js|*.ts) (?:[^/]*?\.js|[^/]*?\.ts) a starred repeat absorbs every repeat before it
    exactly one !(...), *(!(f)) one optional repeat a zero-width test plus a star

    Pieces may be literal text, ?, brackets and star-free groups ({js,ts}, @(js|ts)); every
    character a piece can match must be matched by every star (/ is excluded, so is \ with
    windows, and line terminators for bash's .*?). *(...) rewrites are made optional. The
    proof is about the compiled regex, so it holds under every option. Capture groups keep their
    count and order (the second copy of fixed branches is non-capturing); a group inside a rewritten
    repeat captures across what used to be several repeats.

    Anything else with a wildcard falls back to the existing maxExtglobRecursion literal safeguard,
    as +(a|aa) already does. Repeats without wildcards (*(f*(o)), +(foo|bar)) are untouched, so
    the bash extglob tests pass unchanged.

  2. A regex + after a group that holds a wildcard is read literally, as bash does (@(*a)+
    matches xa+). With regex: true, a * there is a wildcard instead of a quantifier.

  3. A final check in parse: if the finished regex still repeats a group that holds a repeated
    wildcard (a negated class, ., \S, \W, \D, or [\s\S], under *, +, {n,} or a count
    above 2), or a rewrite was followed by an unbalanced-paren repair or left an invalid regex, the
    pattern becomes a literal, with any leading ! kept. This covers malformed patterns whose
    parentheses are repaired after the extglob checks ran, for example (*|+(("")).

maxExtglobRecursion: false disables all of it. With it off, output is byte-identical to before.

Picomatch appends some text without updating the token (quoted text; on v2, text merged into
earlier text), so the analysis keeps that text in token fields of its own and refuses to rewrite
when it cannot tell what a token emits. Rebuild behaviour is unchanged.

Tests

test/redos-repeated-wildcard.js:

  • 54 patterns under 6 option sets compile with no wildcard inside a repeat, and match exactly
    what the unprotected regex matches on every input up to length 4 over a b . / \ \n x;
  • capture groups keep their count; quoted text, POSIX classes, literal braces, brace ranges, a **
    just before the extglob and a lazy ? after it are covered;
  • non-rewritable forms and malformed patterns become literals, and hostile input finishes within a
    child-process deadline;
  • 65 KB patterns (+(*a) repeated, deep @(/)+ nesting, {a,*b}+, )+ repeated) parse in linear
    time.

Full suite on v2: 1988 passing (npm run lint already fails on three untouched test files); the
same change on master passes 2013 with lint clean.

Outside the suite, three rounds of independent review and differential fuzzing (about 150,000
pattern and option cases per line, compared on every short input) found no wrong answer from a
rewrite. The only differences left are where the unprotected regex is itself invalid or drops quoted
or merged text and the rewrite keeps it. A static scan of about 200,000 random patterns found no
regex that still nests a wildcard in a repeat.

Not covered

Two other shapes are slow too, but they hold no wildcard, so they are not this advisory:

  • repeats of overlapping fixed-width alternatives, like +(a?|a), +([a]|a) or +(a|[ab]);
  • nested repeats of fixed text, like +(a*(a)) or raw regex (a+)+, which the existing tests require
    to keep working ((a+|b)+).

Both would need an ambiguity analysis of their own.

Related: #234, #239, #247, #246, #254.

🤖 Generated with Claude Code

A repeated extglob whose body holds a wildcard, such as +(*a), compiled to
an unbounded quantifier nested in another and backtracked exponentially on
short input that almost matched (GHSA-fc5q-39xc-vjpf).

Rewrite such repeats exactly when a star can absorb the neighbouring
repeats, and fall back to the existing literal safeguard otherwise. Also
read a regex + after a wildcard group literally, and check the final regex
so no repaired malformed pattern keeps a wildcard inside a repeat.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@casualuser

Copy link
Copy Markdown

This v2 port is now adopted and shipped in production by the @webomage fork: https://github.com/webomage/picomatch (branch fix/ghsa-fc5q-39xc-vjpf-v2, tag v2.3.3-wom.1, applied verbatim as commit 7b06253 with full credit to @jibsta210 in the commit trailer).

Verified against the GHSA-fc5q-39xc-vjpf PoC: +(*a) / *(*) go from 46-155s to <15ms on the same inputs, and the 2.x mocha suite passes (1988 tests, incl. the included regression tests). Thank you @jibsta210 — a v2 backport merge upstream would let us retire the fork.

@G-Rath

G-Rath commented Nov 18, 2026

Copy link
Copy Markdown

Both GitHub and VulnCheck have withdrawn this advisory, as it is not a security issue.

If a tool is still reporting you're "vulnerable", then you need to contact the tool author about getting their source updated.

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.

3 participants