Skip to content

Commit

Permalink
fix: serialize URL string contents to prevent XSS (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
rrdelaney authored Jan 9, 2024
1 parent 02499c0 commit f27d65d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ module.exports = function serialize(obj, options) {
}

if (type === 'L') {
return "new URL(\"" + urls[valueIndex].toString() + "\")";
return "new URL(" + serialize(urls[valueIndex].toString(), options) + ")";
}

var fn = functions[valueIndex];
Expand Down
6 changes: 4 additions & 2 deletions test/unit/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,8 @@ describe('serialize( obj )', function () {
describe('URL', function () {
it('should serialize URL', function () {
var u = new URL('https://x.com/')
expect(serialize(u)).to.equal('new URL("https://x.com/")');
expect(serialize({t: [u]})).to.be.a('string').equal('{"t":[new URL("https://x.com/")]}');
expect(serialize(u)).to.equal('new URL("https:\\u002F\\u002Fx.com\\u002F")');
expect(serialize({t: [u]})).to.be.a('string').equal('{"t":[new URL("https:\\u002F\\u002Fx.com\\u002F")]}');
});

it('should deserialize URL', function () {
Expand All @@ -477,6 +477,8 @@ describe('serialize( obj )', function () {
expect(serialize('</script>')).to.equal('"\\u003C\\u002Fscript\\u003E"');
expect(JSON.parse(serialize('</script>'))).to.equal('</script>');
expect(eval(serialize('</script>'))).to.equal('</script>');
expect(serialize(new URL('x:</script>'))).to.equal('new URL("x:\\u003C\\u002Fscript\\u003E")');
expect(eval(serialize(new URL('x:</script>'))).href).to.equal('x:</script>');
});
});

Expand Down

0 comments on commit f27d65d

Please sign in to comment.