|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const test = require('tap').test |
| 4 | +const j = require('./index') |
| 5 | + |
| 6 | +test('parse', t => { |
| 7 | + t.test('parses object string', t => { |
| 8 | + t.deepEqual( |
| 9 | + j.parse('{"a": 5, "b": 6}'), |
| 10 | + JSON.parse('{"a": 5, "b": 6}') |
| 11 | + ) |
| 12 | + t.end() |
| 13 | + }) |
| 14 | + |
| 15 | + t.test('parses null string', t => { |
| 16 | + t.strictEqual( |
| 17 | + j.parse('null'), |
| 18 | + JSON.parse('null') |
| 19 | + ) |
| 20 | + t.end() |
| 21 | + }) |
| 22 | + |
| 23 | + t.test('parses 0 string', t => { |
| 24 | + t.strictEqual( |
| 25 | + j.parse('0'), |
| 26 | + JSON.parse('0') |
| 27 | + ) |
| 28 | + t.end() |
| 29 | + }) |
| 30 | + |
| 31 | + t.test('parses string string', t => { |
| 32 | + t.strictEqual( |
| 33 | + j.parse('"X"'), |
| 34 | + JSON.parse('"X"') |
| 35 | + ) |
| 36 | + t.end() |
| 37 | + }) |
| 38 | + |
| 39 | + t.test('parses object string (reviver)', t => { |
| 40 | + const reviver = (key, value) => { |
| 41 | + return typeof value === 'number' ? value + 1 : value |
| 42 | + } |
| 43 | + |
| 44 | + t.deepEqual( |
| 45 | + j.parse('{"a": 5, "b": 6}', reviver), |
| 46 | + JSON.parse('{"a": 5, "b": 6}', reviver) |
| 47 | + ) |
| 48 | + t.end() |
| 49 | + }) |
| 50 | + |
| 51 | + t.test('sanitizes object string (reviver, options)', t => { |
| 52 | + const reviver = (key, value) => { |
| 53 | + return typeof value === 'number' ? value + 1 : value |
| 54 | + } |
| 55 | + |
| 56 | + t.deepEqual( |
| 57 | + j.parse('{"a": 5, "b": 6,"__proto__": { "x": 7 }}', reviver, { protoAction: 'remove' }), |
| 58 | + { a: 6, b: 7 } |
| 59 | + ) |
| 60 | + t.end() |
| 61 | + }) |
| 62 | + |
| 63 | + t.test('sanitizes object string (options)', t => { |
| 64 | + t.deepEqual( |
| 65 | + j.parse('{"a": 5, "b": 6,"__proto__": { "x": 7 }}', { protoAction: 'remove' }), |
| 66 | + { a: 5, b: 6 } |
| 67 | + ) |
| 68 | + t.end() |
| 69 | + }) |
| 70 | + |
| 71 | + t.test('sanitizes object string (null, options)', t => { |
| 72 | + t.deepEqual( |
| 73 | + j.parse('{"a": 5, "b": 6,"__proto__": { "x": 7 }}', null, { protoAction: 'remove' }), |
| 74 | + { a: 5, b: 6 } |
| 75 | + ) |
| 76 | + t.end() |
| 77 | + }) |
| 78 | + |
| 79 | + t.test('sanitizes object string (null, options)', t => { |
| 80 | + t.deepEqual( |
| 81 | + j.parse('{"a": 5, "b": 6,"__proto__": { "x": 7 }}', { protoAction: 'remove' }), |
| 82 | + { a: 5, b: 6 } |
| 83 | + ) |
| 84 | + t.end() |
| 85 | + }) |
| 86 | + |
| 87 | + t.test('sanitizes nested object string', t => { |
| 88 | + t.deepEqual( |
| 89 | + j.parse('{ "a": 5, "b": 6, "__proto__": { "x": 7 }, "c": { "d": 0, "e": "text", "__proto__": { "y": 8 }, "f": { "g": 2 } } }', { protoAction: 'remove' }), |
| 90 | + { a: 5, b: 6, c: { d: 0, e: 'text', f: { g: 2 } } } |
| 91 | + ) |
| 92 | + t.end() |
| 93 | + }) |
| 94 | + |
| 95 | + t.test('ignores proto property', t => { |
| 96 | + t.deepEqual( |
| 97 | + j.parse('{ "a": 5, "b": 6, "__proto__": { "x": 7 } }', { protoAction: 'ignore' }), |
| 98 | + JSON.parse('{ "a": 5, "b": 6, "__proto__": { "x": 7 } }') |
| 99 | + ) |
| 100 | + t.end() |
| 101 | + }) |
| 102 | + |
| 103 | + t.test('ignores proto value', t => { |
| 104 | + t.deepEqual( |
| 105 | + j.parse('{"a": 5, "b": "__proto__"}'), |
| 106 | + { a: 5, b: '__proto__' } |
| 107 | + ) |
| 108 | + t.end() |
| 109 | + }) |
| 110 | + |
| 111 | + t.test('errors on proto property', t => { |
| 112 | + t.throws(() => j.parse('{ "a": 5, "b": 6, "__proto__": { "x": 7 } }'), SyntaxError) |
| 113 | + t.throws(() => j.parse('{ "a": 5, "b": 6, "__proto__" : { "x": 7 } }'), SyntaxError) |
| 114 | + t.throws(() => j.parse('{ "a": 5, "b": 6, "__proto__" \n\r\t : { "x": 7 } }'), SyntaxError) |
| 115 | + t.throws(() => j.parse('{ "a": 5, "b": 6, "__proto__" \n \r \t : { "x": 7 } }'), SyntaxError) |
| 116 | + t.end() |
| 117 | + }) |
| 118 | + |
| 119 | + t.test('errors on proto property (null, null)', t => { |
| 120 | + t.throws(() => j.parse('{ "a": 5, "b": 6, "__proto__": { "x": 7 } }', null, null), SyntaxError) |
| 121 | + t.end() |
| 122 | + }) |
| 123 | + |
| 124 | + t.test('errors on proto property (explicit options)', t => { |
| 125 | + t.throws(() => j.parse('{ "a": 5, "b": 6, "__proto__": { "x": 7 } }', { protoAction: 'error' }), SyntaxError) |
| 126 | + t.end() |
| 127 | + }) |
| 128 | + |
| 129 | + t.test('errors on proto property (unicode)', t => { |
| 130 | + t.throws(() => j.parse('{ "a": 5, "b": 6, "\\u005f_proto__": { "x": 7 } }'), SyntaxError) |
| 131 | + t.throws(() => j.parse('{ "a": 5, "b": 6, "_\\u005fp\\u0072oto__": { "x": 7 } }'), SyntaxError) |
| 132 | + t.throws(() => j.parse('{ "a": 5, "b": 6, "\\u005f\\u005f\\u0070\\u0072\\u006f\\u0074\\u006f\\u005f\\u005f": { "x": 7 } }'), SyntaxError) |
| 133 | + t.throws(() => j.parse('{ "a": 5, "b": 6, "\\u005F_proto__": { "x": 7 } }'), SyntaxError) |
| 134 | + t.throws(() => j.parse('{ "a": 5, "b": 6, "_\\u005Fp\\u0072oto__": { "x": 7 } }'), SyntaxError) |
| 135 | + t.throws(() => j.parse('{ "a": 5, "b": 6, "\\u005F\\u005F\\u0070\\u0072\\u006F\\u0074\\u006F\\u005F\\u005F": { "x": 7 } }'), SyntaxError) |
| 136 | + t.end() |
| 137 | + }) |
| 138 | + |
| 139 | + t.end() |
| 140 | +}) |
| 141 | + |
| 142 | +test('scan', t => { |
| 143 | + t.test('sanitizes nested object string', t => { |
| 144 | + const text = '{ "a": 5, "b": 6, "__proto__": { "x": 7 }, "c": { "d": 0, "e": "text", "__proto__": { "y": 8 }, "f": { "g": 2 } } }' |
| 145 | + const obj = JSON.parse(text) |
| 146 | + |
| 147 | + j.scan(obj, { protoAction: 'remove' }) |
| 148 | + t.deepEqual(obj, { a: 5, b: 6, c: { d: 0, e: 'text', f: { g: 2 } } }) |
| 149 | + t.end() |
| 150 | + }) |
| 151 | + |
| 152 | + t.test('errors on proto property', t => { |
| 153 | + const text = '{ "a": 5, "b": 6, "__proto__": { "x": 7 }, "c": { "d": 0, "e": "text", "__proto__": { "y": 8 }, "f": { "g": 2 } } }' |
| 154 | + const obj = JSON.parse(text) |
| 155 | + |
| 156 | + t.throws(() => j.scan(obj), SyntaxError) |
| 157 | + t.end() |
| 158 | + }) |
| 159 | + |
| 160 | + t.test('does not break when hasOwnProperty is overwritten', t => { |
| 161 | + const text = '{ "a": 5, "b": 6, "hasOwnProperty": "text", "__proto__": { "x": 7 } }' |
| 162 | + const obj = JSON.parse(text) |
| 163 | + |
| 164 | + j.scan(obj, { protoAction: 'remove' }) |
| 165 | + t.deepEqual(obj, { a: 5, b: 6, hasOwnProperty: 'text' }) |
| 166 | + t.end() |
| 167 | + }) |
| 168 | + |
| 169 | + t.end() |
| 170 | +}) |
| 171 | + |
| 172 | +test('safeParse', t => { |
| 173 | + t.test('parses object string', t => { |
| 174 | + t.deepEqual( |
| 175 | + j.safeParse('{"a": 5, "b": 6}'), |
| 176 | + { a: 5, b: 6 } |
| 177 | + ) |
| 178 | + t.end() |
| 179 | + }) |
| 180 | + |
| 181 | + t.test('returns null on proto object string', t => { |
| 182 | + t.strictEqual( |
| 183 | + j.safeParse('{ "a": 5, "b": 6, "__proto__": { "x": 7 } }'), |
| 184 | + null |
| 185 | + ) |
| 186 | + t.end() |
| 187 | + }) |
| 188 | + |
| 189 | + t.test('returns null on invalid object string', t => { |
| 190 | + t.strictEqual( |
| 191 | + j.safeParse('{"a": 5, "b": 6'), |
| 192 | + null |
| 193 | + ) |
| 194 | + t.end() |
| 195 | + }) |
| 196 | + |
| 197 | + t.end() |
| 198 | +}) |
0 commit comments