GitHub advisory · npm
minimatch: minimatch.escape() output matches more or less than the literal input
Description
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…
Manual fix · npm
npm install minimatch@11.0.8Sources