Skip to content

Commit f27d65d

Browse files
authoredJan 9, 2024
fix: serialize URL string contents to prevent XSS (#173)
1 parent 02499c0 commit f27d65d

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed
 

‎index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ module.exports = function serialize(obj, options) {
258258
}
259259

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

264264
var fn = functions[valueIndex];

‎test/unit/serialize.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,8 @@ describe('serialize( obj )', function () {
461461
describe('URL', function () {
462462
it('should serialize URL', function () {
463463
var u = new URL('https://x.com/')
464-
expect(serialize(u)).to.equal('new URL("https://x.com/")');
465-
expect(serialize({t: [u]})).to.be.a('string').equal('{"t":[new URL("https://x.com/")]}');
464+
expect(serialize(u)).to.equal('new URL("https:\\u002F\\u002Fx.com\\u002F")');
465+
expect(serialize({t: [u]})).to.be.a('string').equal('{"t":[new URL("https:\\u002F\\u002Fx.com\\u002F")]}');
466466
});
467467

468468
it('should deserialize URL', function () {
@@ -477,6 +477,8 @@ describe('serialize( obj )', function () {
477477
expect(serialize('</script>')).to.equal('"\\u003C\\u002Fscript\\u003E"');
478478
expect(JSON.parse(serialize('</script>'))).to.equal('</script>');
479479
expect(eval(serialize('</script>'))).to.equal('</script>');
480+
expect(serialize(new URL('x:</script>'))).to.equal('new URL("x:\\u003C\\u002Fscript\\u003E")');
481+
expect(eval(serialize(new URL('x:</script>'))).href).to.equal('x:</script>');
480482
});
481483
});
482484

0 commit comments

Comments
 (0)
Please sign in to comment.