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

fix: resist pollution #106

Merged
merged 12 commits into from
Apr 15, 2022
Merged

fix: resist pollution #106

merged 12 commits into from
Apr 15, 2022

Conversation

shadowspawn
Copy link
Collaborator

@shadowspawn shadowspawn commented Apr 13, 2022

Hopefully we do not cause prototype pollution, but we have two patterns which are affected by existing prototype pollution:

  • destructuring assignment in function arguments
  • accessing object properties, especially of option configuration, like optionConfig.type

Closes: #104

Refactor:

  • be rigorous about getting only own properties
  • use more ?? and null prototypes
  • test string type value is a string (idea from earlier PR)
  • split "check" and "store" into two functions (reduce max number of arguments, separation of concerns)
  • some minor changes for hopefully clearer code

Before:

% git switch main
Switched to branch 'main'
Your branch is up to date with 'upstream/main'.
% npx tape test/pollution.js
TAP version 13
# when prototype has multiple then ignored
not ok 1 should be deeply equivalent
  ...
# when prototype has type then ignored
not ok 2 should throw
  ...
# when prototype has short then ignored
not ok 3 should throw
  ...
# when prototype has strict then ignored
not ok 4 should throw
  ...
# when prototype has args then ignored
not ok 5 should be falsy
  ...

1..5
# tests 5
# pass  0
# fail  5

After:

% git switch -
Switched to branch 'feature/pollution-proof'
Your branch is up to date with 'origin/feature/pollution-proof'.
% npx tape test/pollution.js
TAP version 13
# when prototype has multiple then ignored
ok 1 should be deeply equivalent
# when prototype has type then ignored
ok 2 should throw
# when prototype has short then ignored
ok 3 should throw
# when prototype has strict then ignored
ok 4 should throw
# when prototype has args then ignored
ok 5 should be falsy

1..5
# tests 5
# pass  5

# ok

index.js Outdated Show resolved Hide resolved
@bcoe bcoe mentioned this pull request Apr 13, 2022
4 tasks
@bakkot

This comment was marked as duplicate.

@bakkot

This comment was marked as resolved.

@aaronccasanova

This comment was marked as resolved.

@shadowspawn shadowspawn changed the title fix: resist pollution protection fix: resist pollution Apr 14, 2022
Co-authored-by: Jordan Harband <ljharb@gmail.com>
@shadowspawn

This comment was marked as duplicate.

Copy link
Collaborator

@bcoe bcoe left a comment

Choose a reason for hiding this comment

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

Looking good to me so far.

I think a test for the specific case @bakkot points out would be valuable.

@shadowspawn
Copy link
Collaborator Author

Is it ok to set Object prototype properties in tests? (And delete them afterwards.)

@bcoe
Copy link
Collaborator

bcoe commented Apr 14, 2022

Is it ok to set Object prototype properties in tests? (And delete them afterwards).

@shadowspawn this seems reasonable to me in our own prototype-pollution.js test file. I think I'd avoid upstreaming these into the Node.js codebase (out of concern for side-effects).

@shadowspawn

This comment was marked as outdated.

utils.js Show resolved Hide resolved
index.js Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
utils.js Show resolved Hide resolved
@shadowspawn

This comment was marked as outdated.

Copy link
Collaborator

@aaronccasanova aaronccasanova left a comment

Choose a reason for hiding this comment

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

This is starting to look pretty robust 🙌

Realizing this is still in a draft state, the only thing I think would be helpful is having some brief JSDocs for the checkOptionUsage and storeOption utils. I found myself jumping around a bit to understand the shortOrLong param for checkOptionUsage was the raw option to use for the unknown option error and the values param for storeOption was a reference to the result.values object. These callouts a very non-blocking and think your WIP is looking great!

utils.js Show resolved Hide resolved
@shadowspawn
Copy link
Collaborator Author

some brief JSDocs for the checkOptionUsage and storeOption utils

Good suggestion. The long argument lists and few subtle arguments (like shortOrLong) have been irking me.

@shadowspawn shadowspawn marked this pull request as ready for review April 15, 2022 02:57
@shadowspawn
Copy link
Collaborator Author

Addressed feedback. Added tests. Moved out of Draft.

Thanks for the early comments reviewers, much appreciated commenting on the draft!

index.js Show resolved Hide resolved
test/prototype-pollution.js Outdated Show resolved Hide resolved
@shadowspawn shadowspawn requested a review from bcoe April 15, 2022 03:42
@bcoe bcoe merged commit ecf2dec into pkgjs:main Apr 15, 2022
@shadowspawn shadowspawn deleted the feature/pollution-proof branch April 15, 2022 21:40
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.

More pollution protection?
5 participants