Skip to content

fix: drop redundant repetition from star-led repeated extglobs - #257

Open
arturhvcpn wants to merge 1 commit into
micromatch:masterfrom
arturhvcpn:fix-redos-star-led-extglob
Open

arturhvcpn wants to merge 1 commit into
micromatch:masterfrom
arturhvcpn:fix-redos-star-led-extglob

Conversation

@arturhvcpn

@arturhvcpn arturhvcpn commented Nov 17, 2026 •

Copy link
Copy Markdown

Summary

A repeated extglob whose branch starts with a plain * (e.g. +(*a), *(*a)) compiles to a nested quantifier that backtracks exponentially on non-matching input. This is the issue reported in GHSA-fc5q-39xc-vjpf.

Reproduction

const picomatch = require('picomatch');

picomatch.makeRe('+(*a)').source;
// => '^(?:(?=.)(?:[^/]*?a)+)$'

picomatch.isMatch('a'.repeat(27) + '!', '+(*a)');
// => false, after ~4.6s

Every extra character roughly doubles the time.

Root cause

analyzeRepeatedExtglob only flags branches that are themselves *(…) sequences or nested extglobs. A branch such as *a is not flagged, so +(*a) is emitted as (?:[^/]*?a)+. Both the lazy [^/]*? and the outer + can consume the same run of as, and the engine tries every way of splitting it before failing.

Fix

A branch led by a plain * that cannot match / already absorbs any earlier repetition: *a*a matches exactly what *a matches within a segment. So for such branches the repetition is redundant, and:

  • +(…) closes like @(…)
  • *(…) closes like ?(…)
pattern before after
+(*a) ^(?:(?=.)(?:[^/]*?a)+)$ ^(?:(?=.)(?:[^/]*?a))$
*(*a) ^(?:(?=.)(?:[^/]*?a)*)$ ^(?:(?=.)(?:[^/]*?a)?)$
+(*a|*b) ^(?:(?=.)(?:[^/]*?a|[^/]*?b)+)$ ^(?:(?=.)(?:[^/]*?a|[^/]*?b))$

The rewrite only applies when every branch is star-led and contains no /, ** or brackets (a bracket such as [[:punct:]] could match /, and then the leading star could not absorb the previous repetition). +(*/a) keeps its +. Setting maxExtglobRecursion: false keeps the old output.

After the change, isMatch('a'.repeat(5000) + '!', '+(*a)') returns in 0ms.

Validation

  • Differential fuzz of the old regex against the new one: 26.8M comparisons per option set (default, windows, posix, dot, bash, capture, nocase), 0 divergences.
  • Full suite passes (2000 passing), plus npm run lint.
  • New tests in test/options.maxExtglobRecursion.js assert the generated source, match behaviour, the / case and the maxExtglobRecursion: false opt-out.

Known limitations

Refs GHSA-fc5q-39xc-vjpf

`+(*a)` and `*(*a)` compiled to `(?:[^/]*?a)+`, which backtracks
exponentially on non-matching input (GHSA-fc5q-39xc-vjpf). A branch that
starts with a `*` that cannot match `/` (no `/`, `**` or brackets) already
absorbs any earlier repetitions, so `+(...)` can close like `@(...)` and
`*(...)` like `?(...)` without changing what matches.
@arturhvcpn
arturhvcpn force-pushed the fix-redos-star-led-extglob branch from 953dcbd to 4e4892e Compare November 17, 2026 20:30
@DaleMckeown

Copy link
Copy Markdown

See #235 (comment).

@msalaheldincontractor-wsl

Copy link
Copy Markdown

Reviewed from the diff; I did not run this branch. For reference, on master (9fab7bf, Node 22) each of +(*), +(*a), *(*), *(*a), +(*)a and the two-branch +(*a|*b) takes more than 3 s for isMatch('a'.repeat(28) + '/', pattern).

  • The rewrite looks sound to me. When every branch starts with a plain * and can't match /, the leading star absorbs any earlier copy, so +(…) ≡ @(…) and *(…) ≡ ?(…) with no change in what matches. It covers all six patterns above, including the two-branch case.
  • One gap: isStarLedBranch rejects any branch containing [, so I'd expect +(*[ab]) to keep the nested quantifier ((?:[^/]*?[ab])+, the same shape as +(*a)). A bracket that can't match / (not negated, no POSIX class such as [[:punct:]]) seems safe to allow, and a timing test for +(*[ab]) would pin it.

@andrewkoski

Copy link
Copy Markdown

The fix misses wildcards in non-first branches: picomatch.isMatch('a'.repeat(26) + '!', '*(b|*a)') still takes ~1.5 s with this branch applied (0 ms with #259/#262/#264). Suggest checking every branch for a top-level *, not only the first.

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

5 participants