Skip to content

Commit e06ce75

Browse files
committed
Fix nodejs#1707 hasOwnProperty usage
If hasOwnProperty is overridden, then calling `obj.hasOwnProperty(prop)` can fail. Any time a dictionary of user-generated items is built, we cannot rely on hasOwnProperty being safe, so must call it from the Object.prototype explicitly.
1 parent b3af074 commit e06ce75

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

lib/module.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ var runInNewContext = Script.runInNewContext;
2626
var assert = require('assert').ok;
2727

2828

29-
function hOP(obj, prop) {
29+
// If obj.hasOwnProperty has been overridden, then calling
30+
// obj.hasOwnProperty(prop) will break.
31+
// See: https://github.com/joyent/node/issues/1707
32+
function hasOwnProperty(obj, prop) {
3033
return Object.prototype.hasOwnProperty.call(obj, prop);
3134
}
3235

@@ -91,7 +94,7 @@ function statPath(path) {
9194
var packageCache = {};
9295

9396
function readPackage(requestPath) {
94-
if (hOP(packageCache, requestPath)) {
97+
if (hasOwnProperty(packageCache, requestPath)) {
9598
return packageCache[requestPath];
9699
}
97100

lib/querystring.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ var QueryString = exports;
2525
var urlDecode = process.binding('http_parser').urlDecode;
2626

2727

28-
function hOP(obj, prop) {
28+
// If obj.hasOwnProperty has been overridden, then calling
29+
// obj.hasOwnProperty(prop) will break.
30+
// See: https://github.com/joyent/node/issues/1707
31+
function hasOwnProperty(obj, prop) {
2932
return Object.prototype.hasOwnProperty.call(obj, prop);
3033
}
3134

@@ -171,7 +174,7 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq) {
171174
var k = QueryString.unescape(x[0], true);
172175
var v = QueryString.unescape(x.slice(1).join(eq), true);
173176

174-
if (!hOP(obj, k)) {
177+
if (!hasOwnProperty(obj, k)) {
175178
obj[k] = v;
176179
} else if (!Array.isArray(obj[k])) {
177180
obj[k] = [obj[k], v];

lib/repl.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ var path = require('path');
4646
var fs = require('fs');
4747
var rl = require('readline');
4848

49-
50-
function hOP(obj, prop) {
49+
// If obj.hasOwnProperty has been overridden, then calling
50+
// obj.hasOwnProperty(prop) will break.
51+
// See: https://github.com/joyent/node/issues/1707
52+
function hasOwnProperty(obj, prop) {
5153
return Object.prototype.hasOwnProperty.call(obj, prop);
5254
}
5355

@@ -452,7 +454,7 @@ REPLServer.prototype.complete = function(line) {
452454
group.sort();
453455
for (var j = 0; j < group.length; j++) {
454456
c = group[j];
455-
if (!hOP(uniq, c)) {
457+
if (!hasOwnProperty(uniq, c)) {
456458
completions.push(c);
457459
uniq[c] = true;
458460
}

test/simple/test-querystring.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var qsTestCases = [
4949
[' foo = bar ', '%20foo%20=%20bar%20', {' foo ': ' bar '}],
5050
['foo=%zx', 'foo=%25zx', {'foo': '%zx'}],
5151
['foo=%EF%BF%BD', 'foo=%EF%BF%BD', {'foo': '\ufffd' }],
52+
// See: https://github.com/joyent/node/issues/1707
5253
[ 'hasOwnProperty=x&toString=foo&valueOf=bar&__defineGetter__=baz',
5354
'hasOwnProperty=x&toString=foo&valueOf=bar&__defineGetter__=baz',
5455
{ hasOwnProperty: 'x',

0 commit comments

Comments
 (0)