Skip to content

Commit e3aa524

Browse files
committed
Avoid errors caused by extension names or parameters having names that clash with things in Object.prototype.
1 parent 1e58c14 commit e3aa524

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/parser.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ var TOKEN = /([!#\$%&'\*\+\-\.\^_`\|~0-9A-Za-z]+)/,
88
EXT_LIST = new RegExp('^' + EXT.source + '(?: *, *' + EXT.source + ')*$'),
99
NUMBER = /^-?(0|[1-9][0-9]*)(\.[0-9]+)?$/;
1010

11+
var hasOwnProperty = Object.prototype.hasOwnProperty;
12+
1113
var Parser = {
1214
parseHeader: function(header) {
1315
var offers = new Offers();
@@ -35,7 +37,7 @@ var Parser = {
3537
}
3638
if (NUMBER.test(data)) data = parseFloat(data);
3739

38-
if (offer.hasOwnProperty(key)) {
40+
if (hasOwnProperty.call(offer, key)) {
3941
offer[key] = [].concat(offer[key]);
4042
offer[key].push(data);
4143
} else {
@@ -77,7 +79,9 @@ var Offers = function() {
7779
};
7880

7981
Offers.prototype.push = function(name, params) {
80-
this._byName[name] = this._byName[name] || [];
82+
if (!hasOwnProperty.call(this._byName, name))
83+
this._byName[name] = [];
84+
8185
this._byName[name].push(params);
8286
this._inOrder.push({name: name, params: params});
8387
};

spec/parser_spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ test.describe("Parser", function() { with(this) {
6767
{name: "a", params: {b: true}}],
6868
parse('a; b=1, c, b; d, c; e="hi, there"; e, a; b') )
6969
}})
70+
71+
it("parses an extension name that shadows an Object property", function() { with(this) {
72+
assertEqual( [{name: "hasOwnProperty", params: {}}],
73+
parse('hasOwnProperty') )
74+
}})
75+
76+
it("parses an extension param that shadows an Object property", function() { with(this) {
77+
var result = parse('foo; hasOwnProperty; x')[0]
78+
assertEqual( result.params.hasOwnProperty, true )
79+
}})
80+
7081
}})
7182

7283
describe("serializeParams", function() { with(this) {

0 commit comments

Comments
 (0)