Skip to content

Commit ab012e7

Browse files
committed
fix(parseKeyValue): ignore properties in prototype chain.
Previously, properties (typically functions) in the prototype chain (Object.prototype) would shadow query parameters, and cause them to be serialized incorrectly. This CL guards against this by using hasOwnProperty() to ensure that only own properties are a concern. Fixes angular#8068
1 parent 1a9cb0a commit ab012e7

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/Angular.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ function parseKeyValue(/**string*/keyValue) {
10911091
key = tryDecodeURIComponent(key_value[0]);
10921092
if ( isDefined(key) ) {
10931093
var val = isDefined(key_value[1]) ? tryDecodeURIComponent(key_value[1]) : true;
1094-
if (!obj[key]) {
1094+
if (!hasOwnProperty.call(obj, key)) {
10951095
obj[key] = val;
10961096
} else if(isArray(obj[key])) {
10971097
obj[key].push(val);

test/AngularSpec.js

+17
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,23 @@ describe('angular', function() {
480480
expect(parseKeyValue('flag1&flag1=value&flag1=value2&flag1')).
481481
toEqual({flag1: [true,'value','value2',true]});
482482
});
483+
484+
485+
it('should ignore properties higher in the prototype chain', function() {
486+
var hasOldQ = 'q' in Object.prototype;
487+
var oldQ = Object.prototype.q;
488+
Object.prototype.q = function(a,b,c,d,e,f,g) {};
489+
490+
expect(parseKeyValue('q=123')).toEqual({
491+
'q': '123'
492+
});
493+
494+
if (hasOldQ) {
495+
Object.prototype.q = oldQ;
496+
} else {
497+
delete Object.prototype.q;
498+
}
499+
});
483500
});
484501

485502
describe('toKeyValue', function() {

0 commit comments

Comments
 (0)