65bdc9e9fdbdd12024d1b3769b0da3a7fc5b148dd529066e13237b905531466cf0b97d895ea5d0aea3d903b48eace8798983a793f9b2ff2955b8764c50d79a-exec 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <a href="http://hapijs.com"><img src="https://raw.githubusercontent.com/hapijs/assets/master/images/family.png" width="180px" align="right" /></a>
  2. # Bourne. JSON Bourne.
  3. `JSON.parse()` drop-in replacement with prototype poisoning protection
  4. [![Build Status](https://travis-ci.org/hapijs/bourne.svg)](https://travis-ci.org/hapijs/bourne)
  5. ## Introduction
  6. Consider this:
  7. ```
  8. > const a = '{"__proto__":{ "b":5}}';
  9. '{"__proto__":{ "b":5}}'
  10. > const b = JSON.parse(a);
  11. { __proto__: { b: 5 } }
  12. > b.b;
  13. undefined
  14. > const c = Object.assign({}, b);
  15. {}
  16. > c.b
  17. 5
  18. ```
  19. The problem is that `JSON.parse()` retains the `__proto__` property as a plain object key. By
  20. itself, this is not a security issue. However, as soon as that object is assigned to another or
  21. iterated on and values copied, the `__proto__` property leaks and becomes the object's prototype.
  22. ## API
  23. ### `Bourne.parse(text, [reviver], [options])`
  24. Parses a given JSON-formatted text into an object where:
  25. - `text` - the JSON text string.
  26. - `reviver` - the `JSON.parse()` optional `reviver` argument.
  27. - `options` - optional configuration object where:
  28. - `protoAction` - optional string with one of:
  29. - `'error'` - throw a `SyntaxError` when a `__proto__` key is found. This is the default value.
  30. - `'remove'` - deletes any `__proto__` keys from the result object.
  31. - `'ignore'` - skips all validation (same as calling `JSON.parse()` directly).
  32. ### `Bourne.scan(obj, [options])`
  33. Scans a given object for prototype properties where:
  34. - `obj` - the object being scanned.
  35. - `options` - optional configuration object where:
  36. - `protoAction` - optional string with one of:
  37. - `'error'` - throw a `SyntaxError` when a `__proto__` key is found. This is the default value.
  38. - `'remove'` - deletes any `__proto__` keys from the input `obj`.