medium

CVE-2026-140006

npm · minimatch

Summary

minimatch: minimatch.escape() output matches more or less than the literal input

Severity
medium
CVSS
5.4
EPSS
0.4% (p39)
CWE
CWE-116
Also known as
GHSA-c859-jr93-m5j4
Published
2026-12-01
Updated
2026-12-01

Advisory details

Summary

minimatch.escape() is documented to "Escape all magic characters in a glob pattern, so that it will only ever match literal strings." (README, section minimatch.escape(pattern, options = {})). In minimatch 11.0.5 (latest release; main is at the v11.0.5 tag, commit ea0a4a6) four classes of input break that contract:

  1. A leading ! is left unescaped. escape('!secret.txt') returns '!secret.txt', which is a negated pattern. It matches every path except secret.txt, for example passwd. This holds in POSIX mode and in windowsPathsNoEscape mode.
  2. A leading # is left unescaped. escape('#notes.md') returns '#notes.md', which is a comment pattern. It matches nothing, including #notes.md. This holds in both modes.
  3. An escaped backslash does not survive brace expansion. For the user text x\* (x, backslash, star), escape() returns x\\\*. Alone, that pattern matches only the literal. When the pattern also contains a brace group, brace-expansion turns \\ back into a single \, so the * becomes a live wildcard. The brace group can come from the caller (escape(u) + '.{js,ts}') or, with default options, from the user text itself (escape('x\\*{,}')).
  4. With { windowsPathsNoEscape: true, magicalBraces: true }, escape('x{a,b}') returns x[{]a,b[}]. Brace-expansion does not know [...] classes and expands it to x[]a] and x[b[]. The pattern matches xa and does not match x{a,b}.

The README documents negation and comments for patterns in general (README lines 484-493). The escape() section does not mention them, and its contract ("only ever match literal strings") is what callers rely on. The README warns that building a matcher from untrusted user patterns is unsafe. escape() is the documented way to make text match only as a literal (README.md:213-214), which is how callers embed untrusted text. These cases are inside that boundary. Behaviors 1 and 3 widen a match (fail open for an allowlist). Behavior 2 empties a match (fail open for a denylist or ignore list). Behavior 4 does both.

Details

All line numbers refer to tag v11.0.5. brace-expansion line numbers refer to brace-expansion 5.0.12, the version installed under ^5.0.12 at test time, file dist/commonjs/index.js.

Behavior 1: leading !.

Behavior 2: leading #.

Behavior 3: escaped backslash collapsed by brace expansion.

Behavior 4: windowsPathsNoEscape + magicalBraces.

Related note (not a numbered behavior): default options leave braces live. With default options, and with windowsPathsNoEscape alone, escape() does not escape { or }. On 11.0.5, escape('x{a,b}') returns x{a,b}, which matches xa and does not match x{a,b}. The same holds with { windowsPathsNoEscape: true }. The JSDoc at src/escape.ts:12-13 says braces are escaped only with magicalBraces. The README sentence has no such qualifier. Commit 0d27f1c ("Support magicalBraces option in escape/unescape") states that it resolves #234. It made brace escaping opt-in and kept the default. The text of issue #234 itself was not read from this environment. This report treats the gap as a documentation issue, and the fix below does not change it. Either the README should state the magicalBraces requirement, or the default should escape braces. The bisection shows the gap in every release from 7.4.0 to 11.0.5.

Suggested fix

Two parts. Behaviors 1 and 2 are fixed in src/escape.ts. Behaviors 3 and 4 cannot be fixed in escape.ts alone: every encoding of a literal backslash (\\, [\\]) and every class encoding of a brace ([{]) is rewritten by brace-expansion. The fix for those belongs in braceExpand() in src/index.ts, which is the single path to br

References

Related advisories

Is your project exposed to this? Stateward checks every dependency on every pull request and flags it only if your code actually reaches it.

Check my repo

Summarize with AI

ChatGPTClaudePerplexity

Sources: CISA KEV (public domain), OSV.dev & GitHub Advisory Database (CC-BY-4.0), FIRST EPSS, NVD/CWE (public domain). Served live from the Stateward advisory database.