( reviver?: (key: any, value: any) => any, prohibitedKeys?: string[], )
| 22 | * @param prohibitedKeys - An array of keys to be rejected |
| 23 | */ |
| 24 | export function sanitizeJsonParse( |
| 25 | reviver?: (key: any, value: any) => any, |
| 26 | prohibitedKeys?: string[], |
| 27 | ) { |
| 28 | return (key: string, value: any) => { |
| 29 | if (key === '__proto__') { |
| 30 | // Reject `__proto__` |
| 31 | throw new Error(`JSON string cannot contain "${key}" key.`); |
| 32 | } |
| 33 | if ( |
| 34 | key === 'constructor' && |
| 35 | value != null && |
| 36 | Object.keys(value).some(k => isMatched(k, 'prototype')) |
| 37 | ) { |
| 38 | // Reject `constructor/prototype.*` |
| 39 | throw new Error( |
| 40 | `JSON string cannot contain "constructor.prototype" key.`, |
| 41 | ); |
| 42 | } |
| 43 | if (prohibitedKeys?.some(pattern => isMatched(key, pattern))) { |
| 44 | throw new Error(`JSON string cannot contain "${key}" key.`); |
| 45 | } |
| 46 | if (reviver) { |
| 47 | return reviver(key, value); |
| 48 | } else { |
| 49 | return value; |
| 50 | } |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Parse a json string that rejects prohibited keys |
no test coverage detected