Skip to content

fix: guard conflicting and implied checks against Object.prototype keys (#2623) - #2625

Closed
EnesKartal wants to merge 1 commit into
yargs:mainfrom
EnesKartal:fix-prototype-conflict-crash
Closed

EnesKartal wants to merge 1 commit into
yargs:mainfrom
EnesKartal:fix-prototype-conflict-crash

Conversation

@EnesKartal

Copy link
Copy Markdown

Description

Fixes #2623.

When parsing CLI arguments whose names collide with Object.prototype properties (such as --constructor, --toString, --valueOf, or --hasOwnProperty), conflictingFn performed an un-guarded lookup if (conflicting[key]). Because conflicting is a standard object, conflicting['constructor'] resolved to Object rather than undefined, resulting in an unhandled TypeError: conflicting[key].forEach is not a function.

Similarly, calling .conflicts('constructor', 'foo') or .implies('constructor', 'foo') triggered if (!conflicting[key]) and if (!implied[key]), failing to initialize them to arrays and subsequently failing on .push().

This PR guards lookups on conflicting and implied using Object.prototype.hasOwnProperty.call(...), matching the existing defensive patterns used elsewhere in lib/validation.ts (e.g. for options.choices, demandedOptions, and newAliases).

Verification

  • Added test verifying that parsing flags named after Object.prototype properties (--constructor, --toString, --valueOf, --hasOwnProperty) succeeds without throwing.
  • Added tests verifying that .conflicts('constructor', ...) and .implies('constructor', ...) correctly register without throwing.
  • Full test suite passed: 846 passing (0 failures).
  • TypeScript compile and linter (gts lint && eslint) passed cleanly.

@shadowspawn

Copy link
Copy Markdown
Member

Unsolicited Pull Requests may be closed without discussion.

For more see Contributing and AI Policy.

@shadowspawn shadowspawn closed this Nov 8, 2026
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.

Passing --constructor, --toString or --valueOf crashes yargs with "conflicting[key].forEach is not a function"

2 participants