|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +if ((!common.hasCrypto) || (!common.hasIntl)) { |
| 5 | + common.skip('ESLint tests require crypto and Intl'); |
| 6 | +} |
| 7 | + |
| 8 | +common.skipIfEslintMissing(); |
| 9 | + |
| 10 | +const RuleTester = require('../../tools/node_modules/eslint').RuleTester; |
| 11 | +const rule = require('../../tools/eslint-rules/avoid-prototype-pollution'); |
| 12 | + |
| 13 | +new RuleTester({ |
| 14 | + parserOptions: { ecmaVersion: 2022 }, |
| 15 | +}) |
| 16 | + .run('property-descriptor-no-prototype-pollution', rule, { |
| 17 | + valid: [ |
| 18 | + 'ObjectDefineProperties({}, {})', |
| 19 | + 'ObjectCreate(null, {})', |
| 20 | + 'ObjectDefineProperties({}, { key })', |
| 21 | + 'ObjectCreate(null, { key })', |
| 22 | + 'ObjectDefineProperties({}, { ...spread })', |
| 23 | + 'ObjectCreate(null, { ...spread })', |
| 24 | + 'ObjectDefineProperties({}, { key: valueDescriptor })', |
| 25 | + 'ObjectCreate(null, { key: valueDescriptor })', |
| 26 | + 'ObjectDefineProperties({}, { key: { ...{}, __proto__: null } })', |
| 27 | + 'ObjectCreate(null, { key: { ...{}, __proto__: null } })', |
| 28 | + 'ObjectDefineProperties({}, { key: { __proto__: null } })', |
| 29 | + 'ObjectCreate(null, { key: { __proto__: null } })', |
| 30 | + 'ObjectDefineProperties({}, { key: { __proto__: null, enumerable: true } })', |
| 31 | + 'ObjectCreate(null, { key: { __proto__: null, enumerable: true } })', |
| 32 | + 'ObjectDefineProperties({}, { key: { "__proto__": null } })', |
| 33 | + 'ObjectCreate(null, { key: { "__proto__": null } })', |
| 34 | + 'ObjectDefineProperties({}, { key: { \'__proto__\': null } })', |
| 35 | + 'ObjectCreate(null, { key: { \'__proto__\': null } })', |
| 36 | + 'ObjectDefineProperty({}, "key", ObjectCreate(null))', |
| 37 | + 'ReflectDefineProperty({}, "key", ObjectCreate(null))', |
| 38 | + 'ObjectDefineProperty({}, "key", valueDescriptor)', |
| 39 | + 'ReflectDefineProperty({}, "key", valueDescriptor)', |
| 40 | + 'ObjectDefineProperty({}, "key", { __proto__: null })', |
| 41 | + 'ReflectDefineProperty({}, "key", { __proto__: null })', |
| 42 | + 'ObjectDefineProperty({}, "key", { __proto__: null, enumerable: true })', |
| 43 | + 'ReflectDefineProperty({}, "key", { __proto__: null, enumerable: true })', |
| 44 | + 'ObjectDefineProperty({}, "key", { "__proto__": null })', |
| 45 | + 'ReflectDefineProperty({}, "key", { "__proto__": null })', |
| 46 | + 'ObjectDefineProperty({}, "key", { \'__proto__\': null })', |
| 47 | + 'ReflectDefineProperty({}, "key", { \'__proto__\': null })', |
| 48 | + ], |
| 49 | + invalid: [ |
| 50 | + { |
| 51 | + code: 'ObjectDefineProperties({}, ObjectGetOwnPropertyDescriptors({}))', |
| 52 | + errors: [{ message: /prototype pollution/ }], |
| 53 | + }, |
| 54 | + { |
| 55 | + code: 'ObjectCreate(null, ObjectGetOwnPropertyDescriptors({}))', |
| 56 | + errors: [{ message: /prototype pollution/ }], |
| 57 | + }, |
| 58 | + { |
| 59 | + code: 'ObjectDefineProperties({}, { key: {} })', |
| 60 | + errors: [{ message: /null-prototype/ }], |
| 61 | + }, |
| 62 | + { |
| 63 | + code: 'ObjectCreate(null, { key: {} })', |
| 64 | + errors: [{ message: /null-prototype/ }], |
| 65 | + }, |
| 66 | + { |
| 67 | + code: 'ObjectDefineProperties({}, { key: { [void 0]: { ...{ __proto__: null } } } })', |
| 68 | + errors: [{ message: /null-prototype/ }], |
| 69 | + }, |
| 70 | + { |
| 71 | + code: 'ObjectCreate(null, { key: { [void 0]: { ...{ __proto__: null } } } })', |
| 72 | + errors: [{ message: /null-prototype/ }], |
| 73 | + }, |
| 74 | + { |
| 75 | + code: 'ObjectDefineProperties({}, { key: { __proto__: Object.prototype } })', |
| 76 | + errors: [{ message: /null-prototype/ }], |
| 77 | + }, |
| 78 | + { |
| 79 | + code: 'ObjectCreate(null, { key: { __proto__: Object.prototype } })', |
| 80 | + errors: [{ message: /null-prototype/ }], |
| 81 | + }, |
| 82 | + { |
| 83 | + code: 'ObjectDefineProperties({}, { key: { [`__proto__`]: null } })', |
| 84 | + errors: [{ message: /null-prototype/ }], |
| 85 | + }, |
| 86 | + { |
| 87 | + code: 'ObjectCreate(null, { key: { [`__proto__`]: null } })', |
| 88 | + errors: [{ message: /null-prototype/ }], |
| 89 | + }, |
| 90 | + { |
| 91 | + code: 'ObjectDefineProperties({}, { key: { enumerable: true } })', |
| 92 | + errors: [{ message: /null-prototype/ }], |
| 93 | + }, |
| 94 | + { |
| 95 | + code: 'ObjectCreate(null, { key: { enumerable: true } })', |
| 96 | + errors: [{ message: /null-prototype/ }], |
| 97 | + }, |
| 98 | + { |
| 99 | + code: 'ObjectDefineProperty({}, "key", {})', |
| 100 | + errors: [{ message: /null-prototype/ }], |
| 101 | + }, |
| 102 | + { |
| 103 | + code: 'ReflectDefineProperty({}, "key", {})', |
| 104 | + errors: [{ message: /null-prototype/ }], |
| 105 | + }, |
| 106 | + { |
| 107 | + code: 'ObjectDefineProperty({}, "key", ObjectGetOwnPropertyDescriptor({}, "key"))', |
| 108 | + errors: [{ message: /prototype pollution/ }], |
| 109 | + }, |
| 110 | + { |
| 111 | + code: 'ReflectDefineProperty({}, "key", ObjectGetOwnPropertyDescriptor({}, "key"))', |
| 112 | + errors: [{ message: /prototype pollution/ }], |
| 113 | + }, |
| 114 | + { |
| 115 | + code: 'ObjectDefineProperty({}, "key", ReflectGetOwnPropertyDescriptor({}, "key"))', |
| 116 | + errors: [{ message: /prototype pollution/ }], |
| 117 | + }, |
| 118 | + { |
| 119 | + code: 'ReflectDefineProperty({}, "key", ReflectGetOwnPropertyDescriptor({}, "key"))', |
| 120 | + errors: [{ message: /prototype pollution/ }], |
| 121 | + }, |
| 122 | + { |
| 123 | + code: 'ObjectDefineProperty({}, "key", { __proto__: Object.prototype })', |
| 124 | + errors: [{ message: /null-prototype/ }], |
| 125 | + }, |
| 126 | + { |
| 127 | + code: 'ReflectDefineProperty({}, "key", { __proto__: Object.prototype })', |
| 128 | + errors: [{ message: /null-prototype/ }], |
| 129 | + }, |
| 130 | + { |
| 131 | + code: 'ObjectDefineProperty({}, "key", { [`__proto__`]: null })', |
| 132 | + errors: [{ message: /null-prototype/ }], |
| 133 | + }, |
| 134 | + { |
| 135 | + code: 'ReflectDefineProperty({}, "key", { [`__proto__`]: null })', |
| 136 | + errors: [{ message: /null-prototype/ }], |
| 137 | + }, |
| 138 | + { |
| 139 | + code: 'ObjectDefineProperty({}, "key", { enumerable: true })', |
| 140 | + errors: [{ message: /null-prototype/ }], |
| 141 | + }, |
| 142 | + { |
| 143 | + code: 'ReflectDefineProperty({}, "key", { enumerable: true })', |
| 144 | + errors: [{ message: /null-prototype/ }], |
| 145 | + }, |
| 146 | + ] |
| 147 | + }); |
0 commit comments