Skip to content

[security] Prevent overriding of build-in properties by default #19

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

Merged
merged 3 commits into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
sudo: false
language: node_js
node_js:
- "5"
- "4"
- "0.12"
- "0.10"
- "0.8"
before_install:
- 'if [ "${TRAVIS_NODE_VERSION}" == "0.8" ]; then npm install -g npm@2.14.15; fi'
- "6"
- "8"
- "9"
script:
- "npm run test-travis"
after_script:
Expand Down
21 changes: 12 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ function querystring(query) {
, result = {}
, part;

//
// Little nifty parsing hack, leverage the fact that RegExp.exec increments
// the lastIndex property so we can continue executing this loop until we've
// parsed all results.
//
for (;
part = parser.exec(query);
result[decode(part[1])] = decode(part[2])
);
while (part = parser.exec(query)) {
var key = decode(part[1])
, value = decode(part[2]);

//
// Prevent overriding of existing properties. This ensures that build-in
// methods like `toString` or __proto__ are not overriden by malicious
// querystrings.
//
if (key in result) continue;
Copy link
Member

Choose a reason for hiding this comment

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

We should be safe even without this, as all of them can be safely overridden, with the exception of __proto__ but setting it to a string has no effect.

That said I'm also ok with this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Without the key in result check, the test will fail because toString() can be overridden.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that's my point, if your query string has a parameter called toString the expected behaviour is probably to find that parameter in the parsed object.

It overrides the toString inherited by Object.prototype but that another issue. I mean ideally the parsed object should not inherit from Object.prototype at all but we can't use Object.create(null) due to old browsers support.

result[key] = value;
}

return result;
}
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
},
"homepage": "https://github.com/unshiftio/querystringify",
"devDependencies": {
"assume": "~1.5.0",
"istanbul": "0.4.x",
"mocha": "~3.5.0",
"pre-commit": "~1.2.0"
"assume": "^2.0.1",
"istanbul": "^0.4.5",
"mocha": "^5.1.1",
"pre-commit": "^1.2.2"
}
}
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ describe('querystringify', function () {
assume(obj.shizzle).equals('mynizzle');
});

it('does not overide prototypes', function () {
var obj = qs.parse('?toString&__proto__=lol');

assume(obj).is.a('object');
assume(obj.toString).is.a('function');
assume(obj.__proto__).does.not.equals('lol');
});

it('works with querystring parameters without values', function () {
var obj = qs.parse('?foo&bar=&shizzle=mynizzle');

Expand Down