Skip to content

[v2] fix: collapse single-branch wildcard extglobs (GHSA-fc5q-39xc-vjpf) - #268

Open
Arulraj-a2b wants to merge 1 commit into
micromatch:v2from
Arulraj-a2b:fix/v2-redos-single-branch-wildcard
Open

Arulraj-a2b wants to merge 1 commit into
micromatch:v2from
Arulraj-a2b:fix/v2-redos-single-branch-wildcard

Conversation

@Arulraj-a2b

Copy link
Copy Markdown

Back-port of #267 to the 2.x line (2.3.2 is still widely resolved, for example via micromatch 4).

Fixes GHSA-fc5q-39xc-vjpf (CVE-2026-112366).

Problem

A repeated extglob with a single branch that holds a wildcard, such as +(*a), *(*a), +(*)b or +(a*)b, compiles to a nested unbounded quantifier like (?:[^/]*?a)+. Matching it against a short non-matching string backtracks exponentially. On 4.0.7, +(*a) takes about 20 s on 28 characters. analyzeRepeatedExtglob skipped these bodies because it only looked at multi-branch alternations and nested *(...) sequences.

Fix

In analyzeRepeatedExtglob, a single branch with a wildcard (found from the parser's own tokens, so escaped \*, quoted "*", bracketed [*] and the * of a nested *(...) do not count) is handled in one of three ways:

  1. Collapse. If the branch P is built only from single-character matchers (literal characters, ?, plain bracket classes that cannot match /) and *, with no slash, then +(P) matches exactly what P matches. The first * absorbs every later repeat. So +(P) becomes (P) and *(P) becomes (P)?, and no nested quantifier is left. Matching is unchanged: +(*a) still matches aaa, +(a*b) still matches abab, +(*)c still matches abc.
  2. Leave as is. Outside bash mode, if the branch has a literal /, each repeat has to start after a slash that * cannot cross, so the repeat cannot backtrack. For example, +(src/*/) is unchanged.
  3. Risky. Any other shape, for example +(@(*a)), or bash mode where * crosses /, is literalized like the existing risky patterns +(a|aa) and +(+(a)). maxExtglobRecursion: false still opts out.

Tests

test/redos-single-branch-wildcard.js:

  • Runs 13 attack patterns × 6 option sets against 50,000-character inputs in a child process with a deadline, so a hang fails the test instead of freezing mocha.
  • Checks the collapsed regex output.
  • Checks that matching is unchanged across dot, windows, bash and nocase, that * still does not cross / outside bash mode, and that escaped, quoted and bracketed stars and slash-delimited repeats behave as before.

npm test passes: lint plus 2004 tests on master. The same change is in a separate PR against v2, where 1978 tests pass; v2's lint already fails on master in test/dotfiles.js and test/special-characters.js.

Before → after on 'a'.repeat(28) + '/': +(*a) 21.7 s → 1 ms, *(*a) 27.0 s → 0 ms, +(a*)b 11.9 s → 0 ms.

Related open PRs for the same advisory: #257–#266. This one keeps the change small (one function plus the close quantifier) and changes matching only for the risky shapes. Happy to fold it into whichever one you prefer.

A repeated extglob whose single branch holds a wildcard, such as +(*a) or +(a*)b, nested an unbounded quantifier and backtracked exponentially. A branch made of single-character matchers and * now collapses (+(P) -> (P), *(P) -> (P)?), slash-delimited repeats are left alone, and other wildcard shapes are literalized like the existing risky patterns.
@nl

nl commented Nov 18, 2026

Copy link
Copy Markdown

I tested this PR at e2e3342c62828b2e37a1cfbdc7e0e30355a30c9e on Node 24.9.0. The basic +(*a) case improves, but the slash exemption still leaves a repeated globstar branch unbounded in this test:

  • Pattern: +(**/a)b
  • Input: 'a/'.repeat(40) + 'a'
  • Expected: false promptly; observed: child terminated after the 2-second deadline (ETIMEDOUT).

The rationale that a slash fixes each repetition boundary does not hold when the branch contains a globstar that can cross slashes. Could this shape be covered before treating the single-branch wildcard cases as addressed?

Save as repro.cjs in a checkout at the revision above and run node repro.cjs ./index.js. Each match runs in a separate process so the reproducer itself stays bounded:

const cases = [['+(**/a)b', 'a/'.repeat(40) + 'a']];
const { spawnSync } = require('node:child_process');
const modulePath = require.resolve(process.argv[2]);
for (const [pattern, input] of cases) {
  const r = spawnSync(process.execPath, ['-e',
    'console.log(require(process.argv[1])(process.argv[2])(process.argv[3]))',
    modulePath, pattern, input], { timeout: 2000, encoding: 'utf8' });
  console.log({ pattern, result: r.error?.code ?? r.stdout.trim(), status: r.status });
}

@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