From 18e2c56cea52d8e50d53ce93bc2c580086d3cb1e Mon Sep 17 00:00:00 2001 From: = Date: Wed, 12 Feb 2020 11:15:59 +0100 Subject: [PATCH 1/3] Introduce support for Infinity --- index.js | 13 +++++++++++-- test/unit/serialize.js | 32 ++++++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 3594167..b0f6abe 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ See the accompanying LICENSE file for terms. // Generate an internal UID to make the regexp pattern harder to guess. var UID = Math.floor(Math.random() * 0x10000000000).toString(16); -var PLACE_HOLDER_REGEXP = new RegExp('"@__(F|R|D|M|S|U)-' + UID + '-(\\d+)__@"', 'g'); +var PLACE_HOLDER_REGEXP = new RegExp('"@__(F|R|D|M|S|U|I)-' + UID + '-(\\d+)__@"', 'g'); var IS_NATIVE_CODE_REGEXP = /\{\s*\[native code\]\s*\}/g; var IS_PURE_FUNCTION = /function.*?\(/; @@ -57,6 +57,7 @@ module.exports = function serialize(obj, options) { var maps = []; var sets = []; var undefs = []; + var infinities= []; // Returns placeholders for functions and regexps (identified by index) // which are later replaced by their string representation. @@ -102,6 +103,10 @@ module.exports = function serialize(obj, options) { return '@__U-' + UID + '-' + (undefs.push(origValue) - 1) + '__@'; } + if (origValue === Infinity) { + return '@__I-' + UID + '-' + (infinities.push(origValue) - 1) + '__@'; + } + return value; } @@ -175,7 +180,7 @@ module.exports = function serialize(obj, options) { str = str.replace(UNSAFE_CHARS_REGEXP, escapeUnsafeChars); } - if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0 && undefs.length === 0) { + if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0 && undefs.length === 0 && infinities.length === 0) { return str; } @@ -203,6 +208,10 @@ module.exports = function serialize(obj, options) { return 'undefined' } + if (type === 'I') { + return 'Infinity'; + } + var fn = functions[valueIndex]; return serializeFunc(fn); diff --git a/test/unit/serialize.js b/test/unit/serialize.js index 0c0df3d..b160cdc 100644 --- a/test/unit/serialize.js +++ b/test/unit/serialize.js @@ -335,16 +335,18 @@ describe('serialize( obj )', function () { var regexKey = /.*/; var m = new Map([ ['a', 123], - [regexKey, 456] + [regexKey, 456], + [Infinity,322] ]); - expect(serialize(m)).to.be.a('string').equal('new Map([["a",123],[new RegExp(".*", ""),456]])'); - expect(serialize({t: [m]})).to.be.a('string').equal('{"t":[new Map([["a",123],[new RegExp(".*", ""),456]])]}'); + expect(serialize(m)).to.be.a('string').equal('new Map([["a",123],[new RegExp(".*", ""),456],[Infinity,322]])'); + expect(serialize({t: [m]})).to.be.a('string').equal('{"t":[new Map([["a",123],[new RegExp(".*", ""),456],[Infinity,322]])]}'); }); it('should deserialize a map', function () { var m = eval(serialize(new Map([ ['a', 123], - [null, 456] + [null, 456], + [Infinity,322] ]))); expect(m).to.be.a('Map'); expect(m.get(null)).to.equal(456); @@ -357,23 +359,37 @@ describe('serialize( obj )', function () { var m = new Set([ 'a', 123, - regex + regex, + Infinity ]); - expect(serialize(m)).to.be.a('string').equal('new Set(["a",123,new RegExp(".*", "")])'); - expect(serialize({t: [m]})).to.be.a('string').equal('{"t":[new Set(["a",123,new RegExp(".*", "")])]}'); + expect(serialize(m)).to.be.a('string').equal('new Set(["a",123,new RegExp(".*", ""),Infinity])'); + expect(serialize({t: [m]})).to.be.a('string').equal('{"t":[new Set(["a",123,new RegExp(".*", ""),Infinity])]}'); }); it('should deserialize a set', function () { var m = eval(serialize(new Set([ 'a', 123, - null + null, + Infinity ]))); expect(m).to.be.a('Set'); expect(m.has(null)).to.equal(true); }); }); + describe('Infinity', function () { + it('should serialize Infinity', function () { + expect(serialize(Infinity)).to.equal('Infinity'); + expect(serialize({t: [Infinity]})).to.be.a('string').equal('{"t":[Infinity]}'); + }); + + it('should deserialize Infinity', function () { + var d = eval(serialize(Infinity)); + expect(d).to.equal(Infinity); + }); + }); + describe('XSS', function () { it('should encode unsafe HTML chars to Unicode', function () { expect(serialize('')).to.equal('"\\u003C\\u002Fscript\\u003E"'); From 4599ec6355db25dafd24668943c04f4430fee6b3 Mon Sep 17 00:00:00 2001 From: = Date: Wed, 12 Feb 2020 13:00:02 +0100 Subject: [PATCH 2/3] Add missing -Infinity support --- index.js | 4 ++-- test/unit/serialize.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index b0f6abe..841bb7e 100644 --- a/index.js +++ b/index.js @@ -103,7 +103,7 @@ module.exports = function serialize(obj, options) { return '@__U-' + UID + '-' + (undefs.push(origValue) - 1) + '__@'; } - if (origValue === Infinity) { + if (type === 'number' && !isNaN(origValue) && !isFinite(origValue)) { return '@__I-' + UID + '-' + (infinities.push(origValue) - 1) + '__@'; } @@ -209,7 +209,7 @@ module.exports = function serialize(obj, options) { } if (type === 'I') { - return 'Infinity'; + return infinities[valueIndex]; } var fn = functions[valueIndex]; diff --git a/test/unit/serialize.js b/test/unit/serialize.js index b160cdc..52e98d4 100644 --- a/test/unit/serialize.js +++ b/test/unit/serialize.js @@ -388,6 +388,16 @@ describe('serialize( obj )', function () { var d = eval(serialize(Infinity)); expect(d).to.equal(Infinity); }); + + it('should serialize -Infinity', function () { + expect(serialize(-Infinity)).to.equal('-Infinity'); + expect(serialize({t: [-Infinity]})).to.be.a('string').equal('{"t":[-Infinity]}'); + }); + + it('should deserialize -Infinity', function () { + var d = eval(serialize(-Infinity)); + expect(d).to.equal(-Infinity); + }); }); describe('XSS', function () { From 6ac6afe5d66ca3a9b72d61c2708c9a876116ac34 Mon Sep 17 00:00:00 2001 From: = Date: Wed, 12 Feb 2020 16:06:48 +0100 Subject: [PATCH 3/3] Update magic numbers to match sequence --- test/unit/serialize.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/unit/serialize.js b/test/unit/serialize.js index 52e98d4..3944344 100644 --- a/test/unit/serialize.js +++ b/test/unit/serialize.js @@ -336,17 +336,17 @@ describe('serialize( obj )', function () { var m = new Map([ ['a', 123], [regexKey, 456], - [Infinity,322] + [Infinity, 789] ]); - expect(serialize(m)).to.be.a('string').equal('new Map([["a",123],[new RegExp(".*", ""),456],[Infinity,322]])'); - expect(serialize({t: [m]})).to.be.a('string').equal('{"t":[new Map([["a",123],[new RegExp(".*", ""),456],[Infinity,322]])]}'); + expect(serialize(m)).to.be.a('string').equal('new Map([["a",123],[new RegExp(".*", ""),456],[Infinity,789]])'); + expect(serialize({t: [m]})).to.be.a('string').equal('{"t":[new Map([["a",123],[new RegExp(".*", ""),456],[Infinity,789]])]}'); }); it('should deserialize a map', function () { var m = eval(serialize(new Map([ ['a', 123], [null, 456], - [Infinity,322] + [Infinity, 789] ]))); expect(m).to.be.a('Map'); expect(m.get(null)).to.equal(456);