Skip to content

fix: match a single-branch repeated extglob with a wildcard star in one pass - #266

Closed
aoikisan wants to merge 1 commit into
micromatch:masterfrom
aoikisan:repeated-extglob-wildcard-star
Closed

aoikisan wants to merge 1 commit into
micromatch:masterfrom
aoikisan:repeated-extglob-wildcard-star

Conversation

@aoikisan

Copy link
Copy Markdown

For GHSA-fc5q-39xc-vjpf (CVE-2026-112366). I know there are other pull requests open for it. This one keeps the matching: no existing test changes, and a pattern such as +(*)c or +(*.js) matches the same strings as before.

The problem

A * wildcard inside +(...) or *(...) nests one unbounded quantifier in another. +(*a) compiles to (?:[^/]*?a)+, which backtracks exponentially on a short string that does not match. analyzeRepeatedExtglob only looks at bodies with more than one branch, so a single branch is never flagged.

What changes

  • A single branch of literals, ? and at least one * is compiled once. +(X) becomes (?:X) and *(X) becomes (?:X)?. For such a body the repetition adds no strings: two matches side by side are again one match, because the star of the first takes everything up to the last literal of the second. Nothing in the body can match a slash, so that holds in any position.
  • A single branch that mixes a wildcard star with a slash, a bracket, a brace or a nested group cannot be collapsed that way. It is matched as literal text, as +(a|aa) and +(*|?) already are. This is the one change in behavior: +(a/*) was a working glob and is now literal.
  • A * that opens a nested *(...) is not a wildcard and stays with the star sequence rewrite: +(*(a)) is still a*.
  • maxExtglobRecursion: false keeps the old output.

Both changes are in lib/parse.js: a classifier next to analyzeRepeatedExtglob, and three lines in extglobClose that pick the closing quantifier.

Proof

One isMatch of n letters a and a b, in ms, Node 24 on Windows:

Pattern n=16 n=20 n=24 n=28
+(*a) 4.0.7 3 32.6 506.6 8115.8
+(*a) this branch 1.1 1 1 1.1
*(*a) 4.0.7 3.1 33.4 520 8173.7
*(*a) this branch 1.1 1.1 1.1 1.1
Check Result
npm run mocha on master 1998 passing
npm run mocha on this branch 2003 passing: the 1998, unchanged, and 5 new
The new tests against master's lib/parse.js 2 of the 5 fail
Mutation: *(X) closed with ) where )? belongs 3 tests fail
npm run lint exit 0
Master against this branch: 17 bodies in 16 positions under 6 option sets (1632 patterns, 1504 of which compile differently), each asked about every string of up to 6 characters over a b . / x 31,874,592 comparisons, 0 differ
The same comparison with the ) mutation 234 differ

What it does not do

  • More than one branch with a star is not touched. +(a|*a) still takes 179 ms for 16 letters and 8370 ms for 20 on this branch, as on master. The advisory is about the single branch, and a rewrite that keeps the meaning of several branches is a larger change than this one.
  • No v2 backport here. The same two edits apply to 2.3.2; I can open that one if this direction is wanted.

…ne pass

A `*` wildcard inside `+(...)` or `*(...)` nests one unbounded quantifier
in another: `+(*a)` compiled to `(?:[^/]*?a)+`, which backtracks
exponentially on a short string that does not match (GHSA-fc5q-39xc-vjpf,
CVE-2026-112366). analyzeRepeatedExtglob only looked at bodies with more
than one branch, so these were never flagged.

When the single branch holds nothing but literal characters, `?` and at
least one `*`, the repetition adds no strings: two matches side by side
are again one match, because the star of the first takes everything up to
the last literal of the second. Such a body is now compiled once, `+(X)`
as `(?:X)` and `*(X)` as `(?:X)?`, so every pattern that matched before
still matches the same strings.

A single branch that mixes a wildcard star with a slash, a bracket, a
brace or a nested group cannot be collapsed that way and is matched as
literal text, like the other risky repeated extglobs.

`maxExtglobRecursion: false` keeps the old output.
@casualuser

Copy link
Copy Markdown

This fix is now adopted and shipped in production by the @webomage fork: https://github.com/webomage/picomatch (branch fix/ghsa-fc5q-39xc-vjpf-v4, tag v4.0.8-wom.1, applied verbatim as commit 6786b48 with full credit to @aoikisan in the commit trailer).

Verified here against the ReDoS PoC from GHSA-fc5q-39xc-vjpf (CVSS 8.7): +(*a) / *(a*)-style patterns go from 48s of backtracking on 29-char inputs to single-digit milliseconds, and the full upstream mocha suite passes (2003 tests, incl. the new regression tests). Thank you @aoikisan — we would also very much like to see this merged upstream so the fork can be retired.

@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.

@aoikisan

Copy link
Copy Markdown
Author

A correction on scope, since this branch is now shipped: the webomage fork's tag v4.0.8-wom.1 has a lib/ that is byte-identical to this PR's head (cad55d0), compared by git blob hash.

This PR fixes 4 of the 13 extglob forms I found that backtrack without bound, and matches the same strings for those 4. A fifth, +(@(*a)), is fast only because this PR compiles it as the literal text +(@(*a)): a matches it on 4.0.7 and does not with this PR. The same happens to +(a/*), which was never slow: a/x matches on 4.0.7 and does not with this PR. The other 8 forms still backtrack with it applied:

Glob Input 4.0.7 this PR
+(*a), *(*a), +(*), +(a*) aa + aa x k + / over 2 s by k=16 or 32 flat
+(a|*a) aa + aaa x k + / over 2 s at k=8 over 2 s at k=8
+(a*|b), +(*a|b) aa + aa x k + / over 2 s at k=16 over 2 s at k=16
+(!(a)), *(!(f)) a + a x k + / over 2 s at k=32 over 2 s at k=32
@(*a)+, (*a)+, {*a,b}+ aa + aa x k + / over 2 s at k=16 over 2 s at k=16
*a*a*a*a*b a + aaaaaaaa x k + / 1.08 s at k=16 1.02 s at k=16

I am not extending this PR one shape at a time: each change of this kind rewrites one regex shape, and the table shows the shapes outnumber the rewrites. #278 takes a different route, bounding test() and exec() on the regex itself without changing the parser or what any glob matches; it has the full table and the script that produced these numbers. I would close this PR in its favor if that approach is of interest.

@aoikisan

Copy link
Copy Markdown
Author

Closing in favor of #278, which handles these forms and more without changing what any glob matches in the tests. This PR changes +(a/*) and +(@(*a)), as noted above.

@aoikisan aoikisan closed this Nov 30, 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.

3 participants