Skip to content

fix: prevent prototype pollution via inherited parse results - #184

Merged
sxzz merged 1 commit into
mainfrom
fix/cac-prototype-pollution-argv
Nov 20, 2026
Merged

sxzz merged 1 commit into
mainfrom
fix/cac-prototype-pollution-argv

Conversation

@sxzzbot

@sxzzbot sxzzbot commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Summary

mri read previously parsed flags from a plain object without an own-property check. A --__proto__ flag replaced the prototype of its result, so later flags could pass Object.prototype back to cac as an option value. Walking a dotted option name into that value then wrote onto Object.prototype. The existing UNSAFE_KEYS check only looks at key names, so it did not catch this.

Changes

  • Fork mri into src/mri.ts. mri has not had a release since 2021, so it is now part of cac, trimmed to the options cac uses (alias, boolean). Its result object has no prototype, so flags such as --__proto__ or --toString are plain own keys. The mri devDependency, the inlineDeps setup and the inlinedDependencies field are removed.
  • Harden setDotProp(). It no longer descends into inherited members, so --valueOf.x=1 does not write onto the built-in Object.prototype.valueOf.
  • License. mri's MIT notice is added to LICENSE.

Verification

  • New regression test: ignore inherited members while parsing options. It fails without this change.
  • src/mri.ts was compared with mri 1.2.0 on 300k randomly generated argv and alias/boolean configs. Outputs and thrown errors were identical.
  • pnpm test, pnpm lint, pnpm typecheck and pnpm build pass.

Reported by João Pereira.

mri read previously parsed flags from a plain object without an own-property
check, so a `--__proto__` flag replaced the prototype of its result and later
flags could hand `Object.prototype` back to cac as an option value. Walking a
dotted option name into that value then wrote onto `Object.prototype`, which
the existing `UNSAFE_KEYS` check on key names does not catch.

mri is no longer maintained, so it is now forked into `src/mri.ts`, trimmed to
the options cac uses. Its result object has no prototype, so flags such as
`--__proto__` or `--toString` are plain own keys. Parsing is otherwise
unchanged, checked against mri 1.2.0 on randomly generated argv. mri's MIT
notice is added to LICENSE.

`setDotProp()` also stops descending into inherited members, so
`--valueOf.x=1` no longer writes onto the built-in `Object.prototype.valueOf`.

Reported-by: João Pereira <joaomhmpereira@gmail.com>
@pkg-pr-new

pkg-pr-new Bot commented Nov 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/cac@184

commit: dbede6d

@codecov

codecov Bot commented Nov 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.60656% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.12%. Comparing base (12a5e82) to head (dbede6d).

Files with missing lines Patch % Lines
src/mri.ts 83.05% 7 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #184      +/-   ##
==========================================
+ Coverage   67.01%   69.12%   +2.11%     
==========================================
  Files           6        7       +1     
  Lines         388      447      +59     
  Branches      106      127      +21     
==========================================
+ Hits          260      309      +49     
- Misses        111      118       +7     
- Partials       17       20       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sxzz
sxzz merged commit c31a68a into main Nov 20, 2026
15 checks passed
@sxzz
sxzz deleted the fix/cac-prototype-pollution-argv branch November 20, 2026 19:16
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.

2 participants