Skip to content

Commit 422eb4f

Browse files
authored
[security] Prevent overriding of build-in properties by default (#19)
[security] Prevent overriding of build-in properties by default
1 parent 0b65759 commit 422eb4f

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

.travis.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- "5"
54
- "4"
6-
- "0.12"
7-
- "0.10"
8-
- "0.8"
9-
before_install:
10-
- 'if [ "${TRAVIS_NODE_VERSION}" == "0.8" ]; then npm install -g npm@2.14.15; fi'
5+
- "6"
6+
- "8"
7+
- "9"
118
script:
129
- "npm run test-travis"
1310
after_script:

index.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@ function querystring(query) {
2525
, result = {}
2626
, part;
2727

28-
//
29-
// Little nifty parsing hack, leverage the fact that RegExp.exec increments
30-
// the lastIndex property so we can continue executing this loop until we've
31-
// parsed all results.
32-
//
33-
for (;
34-
part = parser.exec(query);
35-
result[decode(part[1])] = decode(part[2])
36-
);
28+
while (part = parser.exec(query)) {
29+
var key = decode(part[1])
30+
, value = decode(part[2]);
31+
32+
//
33+
// Prevent overriding of existing properties. This ensures that build-in
34+
// methods like `toString` or __proto__ are not overriden by malicious
35+
// querystrings.
36+
//
37+
if (key in result) continue;
38+
result[key] = value;
39+
}
3740

3841
return result;
3942
}

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
},
3232
"homepage": "https://github.com/unshiftio/querystringify",
3333
"devDependencies": {
34-
"assume": "~1.5.0",
35-
"istanbul": "0.4.x",
36-
"mocha": "~3.5.0",
37-
"pre-commit": "~1.2.0"
34+
"assume": "^2.0.1",
35+
"istanbul": "^0.4.5",
36+
"mocha": "^5.1.1",
37+
"pre-commit": "^1.2.2"
3838
}
3939
}

test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ describe('querystringify', function () {
6363
assume(obj.shizzle).equals('mynizzle');
6464
});
6565

66+
it('does not overide prototypes', function () {
67+
var obj = qs.parse('?toString&__proto__=lol');
68+
69+
assume(obj).is.a('object');
70+
assume(obj.toString).is.a('function');
71+
assume(obj.__proto__).does.not.equals('lol');
72+
});
73+
6674
it('works with querystring parameters without values', function () {
6775
var obj = qs.parse('?foo&bar=&shizzle=mynizzle');
6876

0 commit comments

Comments
 (0)