|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const suspectRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/ |
| 4 | + |
| 5 | +function parse (text, reviver, options) { |
| 6 | + // Normalize arguments |
| 7 | + if (options == null) { |
| 8 | + if (reviver != null && typeof reviver === 'object') { |
| 9 | + options = reviver |
| 10 | + reviver = undefined |
| 11 | + } else { |
| 12 | + options = {} |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + // Parse normally, allowing exceptions |
| 17 | + const obj = JSON.parse(text, reviver) |
| 18 | + |
| 19 | + // options.protoAction: 'error' (default) / 'remove' / 'ignore' |
| 20 | + if (options.protoAction === 'ignore') { |
| 21 | + return obj |
| 22 | + } |
| 23 | + |
| 24 | + // Ignore null and non-objects |
| 25 | + if (!obj || typeof obj !== 'object') { |
| 26 | + return obj |
| 27 | + } |
| 28 | + |
| 29 | + // Check original string for potential exploit |
| 30 | + if (!text.match(suspectRx)) { |
| 31 | + return obj |
| 32 | + } |
| 33 | + |
| 34 | + // Scan result for proto keys |
| 35 | + scan(obj, options) |
| 36 | + |
| 37 | + return obj |
| 38 | +} |
| 39 | + |
| 40 | +function scan (obj, options) { |
| 41 | + options = options || {} |
| 42 | + |
| 43 | + var next = [obj] |
| 44 | + |
| 45 | + while (next.length) { |
| 46 | + const nodes = next |
| 47 | + next = [] |
| 48 | + |
| 49 | + for (const node of nodes) { |
| 50 | + if (Object.prototype.hasOwnProperty.call(node, '__proto__')) { // Avoid calling node.hasOwnProperty directly |
| 51 | + if (options.protoAction !== 'remove') { |
| 52 | + throw new SyntaxError('Object contains forbidden prototype property') |
| 53 | + } |
| 54 | + |
| 55 | + delete node.__proto__ // eslint-disable-line |
| 56 | + } |
| 57 | + |
| 58 | + for (const key in node) { |
| 59 | + const value = node[key] |
| 60 | + if (value && typeof value === 'object') { |
| 61 | + next.push(node[key]) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +function safeParse (text, reviver) { |
| 69 | + try { |
| 70 | + return parse(text, reviver) |
| 71 | + } catch (ignoreError) { |
| 72 | + return null |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +module.exports = { |
| 77 | + parse, |
| 78 | + scan, |
| 79 | + safeParse |
| 80 | +} |
0 commit comments