proto.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict';
  2. /* eslint no-proto: 0 */
  3. var parse = require('../');
  4. var test = require('tape');
  5. test('proto pollution', function (t) {
  6. var argv = parse(['--__proto__.x', '123']);
  7. t.equal({}.x, undefined);
  8. t.equal(argv.__proto__.x, undefined);
  9. t.equal(argv.x, undefined);
  10. t.end();
  11. });
  12. test('proto pollution (array)', function (t) {
  13. var argv = parse(['--x', '4', '--x', '5', '--x.__proto__.z', '789']);
  14. t.equal({}.z, undefined);
  15. t.deepEqual(argv.x, [4, 5]);
  16. t.equal(argv.x.z, undefined);
  17. t.equal(argv.x.__proto__.z, undefined);
  18. t.end();
  19. });
  20. test('proto pollution (number)', function (t) {
  21. var argv = parse(['--x', '5', '--x.__proto__.z', '100']);
  22. t.equal({}.z, undefined);
  23. t.equal((4).z, undefined);
  24. t.equal(argv.x, 5);
  25. t.equal(argv.x.z, undefined);
  26. t.end();
  27. });
  28. test('proto pollution (string)', function (t) {
  29. var argv = parse(['--x', 'abc', '--x.__proto__.z', 'def']);
  30. t.equal({}.z, undefined);
  31. t.equal('...'.z, undefined);
  32. t.equal(argv.x, 'abc');
  33. t.equal(argv.x.z, undefined);
  34. t.end();
  35. });
  36. test('proto pollution (constructor)', function (t) {
  37. var argv = parse(['--constructor.prototype.y', '123']);
  38. t.equal({}.y, undefined);
  39. t.equal(argv.y, undefined);
  40. t.end();
  41. });
  42. test('proto pollution (constructor function)', function (t) {
  43. var argv = parse(['--_.concat.constructor.prototype.y', '123']);
  44. function fnToBeTested() {}
  45. t.equal(fnToBeTested.y, undefined);
  46. t.equal(argv.y, undefined);
  47. t.end();
  48. });
  49. // powered by snyk - https://github.com/backstage/backstage/issues/10343
  50. test('proto pollution (constructor function) snyk', function (t) {
  51. var argv = parse('--_.constructor.constructor.prototype.foo bar'.split(' '));
  52. t.equal(function () {}.foo, undefined);
  53. t.equal(argv.y, undefined);
  54. t.end();
  55. });