npm · minimatch
minimatch: minimatch.escape() output matches more or less than the literal input
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:
! 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.# 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.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\\*{,}')).{ 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.
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 !.
src/escape.ts:22-24: the comment states that ! needs no escaping because the parens that make extglobs magic are escaped. That is true for !(...). It ignores pattern-level negation.src/escape.ts:25-32: none of the four branches escapes !. They escape ?*()[], plus \ in POSIX mode and {} with magicalBraces. A leading ! passes through.src/index.ts:1360-1374 (parseNegate, called from make() at src/index.ts:730): every leading ! toggles this.negate and is stripped. match() then inverts the result.\! is already a supported way to write a literal !. test/patterns.js:260 has the entry ['!\\!a*', ['a!b', 'd', 'e', '\\!a']], which excludes !ab and !abc. The POSIX part of the fix uses that existing syntax, and unescape() already reverses it.test/escape-has-magic.js checks the unescape() round trip and hasMagic() === false for every entry in test/patterns.js. That list includes '!a*' (line 251), '!\\!a*' (line 260) and '# ignore this' (line 327). The test never checks negate or comment. A negated literal pattern reports hasMagic() === false, so the suite passes. Measured on 11.0.5 with the port described under Suggested fix: 811 of 811 upstream assertions pass, and 6 of 784 added negate/comment assertions fail ('!a*', '!\\!a*' and '# ignore this', each in POSIX and Windows mode).Behavior 2: leading #.
src/escape.ts:25-32: no branch handles #.src/index.ts:118-120 (minimatch() shortcut) and src/index.ts:719-722 (make()): a pattern whose first character is # is a comment. minimatch() returns false and Minimatch builds an empty set.Behavior 3: escaped backslash collapsed by brace expansion.
src/escape.ts:28 and src/escape.ts:32: a literal \ is escaped as \\ and a literal * as \*. The user text x\* becomes x\\\*.src/index.ts:733 calls #expandBraces(), which reaches braceExpand() at src/index.ts:243-257. When the pattern contains any {...} group, src/index.ts:256 passes the whole pattern to brace-expansion expand().index.js:16 (slashPattern = /\\\\/g) and index.js:54-61 (escapeBraces) replace each \\ pair with a sentinel. index.js:62-69 (unescapeBraces, applied to every result at index.js:125) turns that sentinel into one \ (line 64). x\\\*.{js,ts} therefore expands to x\\*.js and x\\*.ts. In minimatch syntax that is an escaped backslash followed by a live *.escape() leaves { and } as they are (see the related note below). The user text can then supply the brace group, and the caller appends nothing: escape('x\\*{,}') returns x\\\*{,}, which expands to x\\* twice and matches x\anything. Commit 0d27f1c, which added magicalBraces to escape(), states that it resolves #234. Neither that commit nor its tests cover braces that turn an escaped * into a wildcard. Its tests in test/escape-has-magic.js compare escape() and unescape() strings only, and none of their inputs contains *. With magicalBraces: true in POSIX mode the user braces are escaped, so a brace group from the caller is required.escape('\\{', { magicalBraces: true }) + '.{js,ts}' gives \\\{.{js,ts}, which does not match \{.js on 11.0.5.{1..5000} (100 or more values, src/index.ts:263) is matched without brace-expansion in 11.0.5, so that suffix alone does not trigger behavior 3.Behavior 4: windowsPathsNoEscape + magicalBraces.
src/escape.ts:26-27: in this mode every magic character, including { and }, is wrapped in a one-character class: x{a,b} becomes x[{]a,b[}]. The README and the JSDoc at src/escape.ts:6-10 justify this by saying a character class "can only be satisfied by that exact character".src/index.ts:599-601: in this mode backslashes are converted to / before parsing, so brace-expansion's \{ escape is not available to escape().src/index.ts:251-256: the brace check regex and brace-expansion both see {]a,b[} as a brace set. brace-expansion splits it at the comma into x[]a] and x[b[]. hasMagic() returns true and the literal does not match itself.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.
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
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 repoSources: 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.