Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Robustness: rework isConstructorOrProto #24

Merged
merged 2 commits into from
Feb 25, 2023

Conversation

shadowspawn
Copy link
Collaborator

The isConstructorOrProto fix does not look right for the v0.2.x branch. I looked at various commits and suspect the back port of the fix itself went somewhat awry. This PR makes the code match the mainline.

Here are the relevant lines of code from the main branch and from the original commit and adapted commit adding isConstructorOrProto.

Main line

Original change

Adapted change (suspect)

  • minimist/index.js

    Lines 9 to 11 in ef9153f

    function isConstructorOrProto(obj, key) {
    return key === 'constructor' && (typeof obj[key] === 'function' || key === '__proto__');
    }
  • minimist/index.js

    Lines 28 to 30 in ef9153f

    if (key === '__proto__' || isConstructorOrProto(o, key)) {
    return;
    }
  • if (key === '__proto__') { return; }

- modify implementation of isConstructorOrProto to match main branch
- call isConstructorOrProto on last key too
@codecov-commenter
Copy link

codecov-commenter commented Feb 17, 2023

Codecov Report

Merging #24 (3dbebff) into v0.2.x (c0b2661) will not change coverage.
The diff coverage is 100.00%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@           Coverage Diff           @@
##           v0.2.x      #24   +/-   ##
=======================================
  Coverage   98.55%   98.55%           
=======================================
  Files           1        1           
  Lines         138      138           
  Branches       60       60           
=======================================
  Hits          136      136           
  Misses          2        2           
Impacted Files Coverage Δ
index.js 98.55% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this fails to handle constructor properly, could we add a failing test that would have caught this? Presumably the default branch lacks the same test (it'll get pulled in when i merge in the backport).

@@ -7,7 +7,7 @@ function isNumber(x) {
}

function isConstructorOrProto(obj, key) {
return key === 'constructor' && (typeof obj[key] === 'function' || key === '__proto__');
return (key === 'constructor' && typeof obj[key] === 'function') || key === '__proto__';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woof, good catch

if (key === '__proto__' || isConstructorOrProto(o, key)) {
if (isConstructorOrProto(o, key)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at least this wasn't wrong for "proto", but now it's better :-)

@shadowspawn
Copy link
Collaborator Author

I'll look at adding tests for constructor, and the last key. (It took me a while to untangle the code, but I hopefully learnt enough on the way to write the tests now!)

@shadowspawn
Copy link
Collaborator Author

There are separate checks against the leading keys in a dotted option name than against the last key. If the checks for __proto__ or constructor are wrong in the checks of the last key then it won't lead to prototype pollution as such, but will lead to less consistent behaviour between --a.constructor.prototype.b and --a.constructor.

The pollution checks on the v0.2.x branch missed the constructor check on the last key in the dotted option name. So again this does not allow prototype pollution, but does mean there is different behaviour between the main branch and the v0.2.x branch.

This PR adds a test which fails if the constructor check is missing for the last key. (And a matching test for __proto__.)

The tests were run against the previous code in #25 to show the failure.

@shadowspawn
Copy link
Collaborator Author

To be clear, after testing I do not think this PR is a security fix. Just a tidy-up to make the dotted option key handling more consistent.

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.

None yet

3 participants