Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,11 @@ module.exports = function serialize(obj, options) {
}

if (type === 'L') {
return "new URL(" + serialize(urls[valueIndex].toString(), options) + ")";
var urlStr = urls[valueIndex].toString();
if (typeof urlStr !== 'string') {
throw new TypeError('URL.toString() must return a string');
}
return "new URL(" + serialize(urlStr, options) + ")";
}

var fn = functions[valueIndex];
Expand Down
12 changes: 12 additions & 0 deletions test/unit/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,18 @@ describe('serialize( obj )', function () {
strictEqual(d instanceof URL, true);
strictEqual(d.toString(), 'https://x.com/');
});

it('should throw when serializing a spoofed URL with non-string toString()', function () {
var fakeUrl = Object.create(URL.prototype);
fakeUrl.toString = function () {
return {
toString: function () {
return 'https://example.com/';
}
};
};
throws(function () { serialize({ url: fakeUrl }); }, TypeError);
});
});

describe('XSS', function () {
Expand Down
Loading