Skip to content

Commit

Permalink
Avoid errors caused by extension names or parameters having names tha…
Browse files Browse the repository at this point in the history
…t clash with things in Object.prototype.
  • Loading branch information
jcoglan committed Nov 11, 2017
1 parent 1e58c14 commit e3aa524
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var TOKEN = /([!#\$%&'\*\+\-\.\^_`\|~0-9A-Za-z]+)/,
EXT_LIST = new RegExp('^' + EXT.source + '(?: *, *' + EXT.source + ')*$'),
NUMBER = /^-?(0|[1-9][0-9]*)(\.[0-9]+)?$/;

var hasOwnProperty = Object.prototype.hasOwnProperty;

var Parser = {
parseHeader: function(header) {
var offers = new Offers();
Expand Down Expand Up @@ -35,7 +37,7 @@ var Parser = {
}
if (NUMBER.test(data)) data = parseFloat(data);

if (offer.hasOwnProperty(key)) {
if (hasOwnProperty.call(offer, key)) {
offer[key] = [].concat(offer[key]);
offer[key].push(data);
} else {
Expand Down Expand Up @@ -77,7 +79,9 @@ var Offers = function() {
};

Offers.prototype.push = function(name, params) {
this._byName[name] = this._byName[name] || [];
if (!hasOwnProperty.call(this._byName, name))
this._byName[name] = [];

this._byName[name].push(params);
this._inOrder.push({name: name, params: params});
};
Expand Down
11 changes: 11 additions & 0 deletions spec/parser_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ test.describe("Parser", function() { with(this) {
{name: "a", params: {b: true}}],
parse('a; b=1, c, b; d, c; e="hi, there"; e, a; b') )
}})

it("parses an extension name that shadows an Object property", function() { with(this) {
assertEqual( [{name: "hasOwnProperty", params: {}}],
parse('hasOwnProperty') )
}})

it("parses an extension param that shadows an Object property", function() { with(this) {
var result = parse('foo; hasOwnProperty; x')[0]
assertEqual( result.params.hasOwnProperty, true )
}})

}})

describe("serializeParams", function() { with(this) {
Expand Down

0 comments on commit e3aa524

Please sign in to comment.