eslint-plugin-secure-coding 3.4.3 → 3.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/index.js +1 -107
- package/src/oxlint.js +1 -3
- package/src/rules/detect-non-literal-regexp/index.js +1 -329
- package/src/rules/detect-object-injection/index.js +1 -691
- package/src/rules/detect-weak-password-validation/index.js +1 -54
- package/src/rules/no-directive-injection/index.js +1 -416
- package/src/rules/no-electron-security-issues/index.js +1 -385
- package/src/rules/no-format-string-injection/index.js +1 -565
- package/src/rules/no-graphql-injection/index.js +1 -612
- package/src/rules/no-hardcoded-credentials/index.js +1 -658
- package/src/rules/no-hardcoded-session-tokens/index.js +1 -56
- package/src/rules/no-improper-sanitization/index.js +1 -391
- package/src/rules/no-improper-type-validation/index.js +1 -422
- package/src/rules/no-insecure-comparison/index.js +1 -249
- package/src/rules/no-ldap-injection/index.js +1 -403
- package/src/rules/no-missing-authentication/index.js +1 -282
- package/src/rules/no-pii-in-logs/index.js +1 -63
- package/src/rules/no-privilege-escalation/index.js +1 -282
- package/src/rules/no-redos-vulnerable-regex/index.js +0 -0
- package/src/rules/no-sensitive-data-exposure/index.js +1 -249
- package/src/rules/no-template-injection/index.js +1 -84
- package/src/rules/no-unchecked-loop-condition/index.js +1 -541
- package/src/rules/no-unlimited-resource-allocation/index.js +1 -555
- package/src/rules/no-unsafe-deserialization/index.js +1 -497
- package/src/rules/no-unsafe-regex-construction/index.js +1 -275
- package/src/rules/no-weak-password-recovery/index.js +1 -382
- package/src/rules/no-xpath-injection/index.js +1 -431
- package/src/rules/no-xxe-injection/index.js +1 -230
- package/src/rules/require-backend-authorization/index.js +1 -58
- package/src/rules/require-secure-defaults/index.js +1 -41
- package/src/types/index.js +1 -2
|
@@ -1,691 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.detectObjectInjection = void 0;
|
|
4
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
-
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
6
|
-
const eslint_devkit_3 = require("@interlace/eslint-devkit");
|
|
7
|
-
const TYPED_ARRAY_CTORS = new Set([
|
|
8
|
-
'Int8Array', 'Uint8Array', 'Uint8ClampedArray',
|
|
9
|
-
'Int16Array', 'Uint16Array',
|
|
10
|
-
'Int32Array', 'Uint32Array',
|
|
11
|
-
'Float32Array', 'Float64Array',
|
|
12
|
-
'BigInt64Array', 'BigUint64Array',
|
|
13
|
-
]);
|
|
14
|
-
const OBJECT_INJECTION_PATTERNS = [
|
|
15
|
-
{
|
|
16
|
-
pattern: '__proto__',
|
|
17
|
-
dangerous: true,
|
|
18
|
-
vulnerability: 'prototype-pollution',
|
|
19
|
-
safeAlternative: 'Object.create(null) or Map',
|
|
20
|
-
example: {
|
|
21
|
-
bad: 'obj[userInput] = value; // if userInput is "__proto__"',
|
|
22
|
-
good: 'const map = new Map(); map.set(userInput, value);'
|
|
23
|
-
},
|
|
24
|
-
effort: '15-20 minutes',
|
|
25
|
-
riskLevel: 'critical'
|
|
26
|
-
},
|
|
27
|
-
{
|
|
28
|
-
pattern: 'prototype',
|
|
29
|
-
dangerous: true,
|
|
30
|
-
vulnerability: 'prototype-pollution',
|
|
31
|
-
safeAlternative: 'Avoid prototype manipulation',
|
|
32
|
-
example: {
|
|
33
|
-
bad: 'obj[userInput] = value; // if userInput is "prototype"',
|
|
34
|
-
good: 'if (!obj.hasOwnProperty(userInput)) obj[userInput] = value;'
|
|
35
|
-
},
|
|
36
|
-
effort: '10-15 minutes',
|
|
37
|
-
riskLevel: 'high'
|
|
38
|
-
},
|
|
39
|
-
{
|
|
40
|
-
pattern: 'constructor',
|
|
41
|
-
dangerous: true,
|
|
42
|
-
vulnerability: 'method-injection',
|
|
43
|
-
safeAlternative: 'Validate property names against whitelist',
|
|
44
|
-
example: {
|
|
45
|
-
bad: 'obj[userInput] = value; // if userInput is "constructor"',
|
|
46
|
-
good: 'const ALLOWED_KEYS = [\'name\', \'age\', \'email\']; if (ALLOWED_KEYS.includes(userInput)) obj[userInput] = value;'
|
|
47
|
-
},
|
|
48
|
-
effort: '10-15 minutes',
|
|
49
|
-
riskLevel: 'medium'
|
|
50
|
-
}
|
|
51
|
-
];
|
|
52
|
-
exports.detectObjectInjection = (0, eslint_devkit_3.createRule)({
|
|
53
|
-
name: 'detect-object-injection',
|
|
54
|
-
meta: {
|
|
55
|
-
type: 'problem',
|
|
56
|
-
docs: {
|
|
57
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/detect-object-injection.md',
|
|
58
|
-
description: 'Detects variable[key] as a left- or right-hand assignment operand',
|
|
59
|
-
cwe: 'CWE-915',
|
|
60
|
-
confidence: 'low',
|
|
61
|
-
},
|
|
62
|
-
messages: {
|
|
63
|
-
objectInjection: (0, eslint_devkit_2.formatLLMMessage)({
|
|
64
|
-
icon: eslint_devkit_2.MessageIcons.WARNING,
|
|
65
|
-
issueName: 'Object injection',
|
|
66
|
-
cwe: 'CWE-915',
|
|
67
|
-
description: 'Object injection/Prototype pollution (incl. model/tool outputs)',
|
|
68
|
-
severity: '{{riskLevel}}',
|
|
69
|
-
fix: '{{safeAlternative}}',
|
|
70
|
-
documentationLink: 'https://portswigger.net/web-security/prototype-pollution',
|
|
71
|
-
}),
|
|
72
|
-
useMapInstead: (0, eslint_devkit_2.formatLLMMessage)({
|
|
73
|
-
icon: eslint_devkit_2.MessageIcons.INFO,
|
|
74
|
-
issueName: 'Use Map',
|
|
75
|
-
description: 'Use Map instead of plain objects',
|
|
76
|
-
severity: 'LOW',
|
|
77
|
-
fix: 'const map = new Map(); map.set(key, value);',
|
|
78
|
-
documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map',
|
|
79
|
-
}),
|
|
80
|
-
useHasOwnProperty: (0, eslint_devkit_2.formatLLMMessage)({
|
|
81
|
-
icon: eslint_devkit_2.MessageIcons.INFO,
|
|
82
|
-
issueName: 'Use hasOwnProperty',
|
|
83
|
-
description: 'Check hasOwnProperty to avoid prototype properties',
|
|
84
|
-
severity: 'LOW',
|
|
85
|
-
fix: 'if (obj.hasOwnProperty(key)) { obj[key] = value; }',
|
|
86
|
-
documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty',
|
|
87
|
-
}),
|
|
88
|
-
whitelistKeys: (0, eslint_devkit_2.formatLLMMessage)({
|
|
89
|
-
icon: eslint_devkit_2.MessageIcons.INFO,
|
|
90
|
-
issueName: 'Whitelist Keys',
|
|
91
|
-
description: 'Whitelist allowed property names',
|
|
92
|
-
severity: 'LOW',
|
|
93
|
-
fix: 'const ALLOWED = ["name", "email"]; if (ALLOWED.includes(key)) obj[key] = value; // reject model/tool-supplied unknown keys',
|
|
94
|
-
documentationLink: 'https://portswigger.net/web-security/prototype-pollution',
|
|
95
|
-
}),
|
|
96
|
-
useObjectCreate: (0, eslint_devkit_2.formatLLMMessage)({
|
|
97
|
-
icon: eslint_devkit_2.MessageIcons.INFO,
|
|
98
|
-
issueName: 'Use Object.create(null)',
|
|
99
|
-
description: 'Create clean objects without prototypes',
|
|
100
|
-
severity: 'LOW',
|
|
101
|
-
fix: 'const obj = Object.create(null);',
|
|
102
|
-
documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create',
|
|
103
|
-
}),
|
|
104
|
-
freezePrototypes: (0, eslint_devkit_2.formatLLMMessage)({
|
|
105
|
-
icon: eslint_devkit_2.MessageIcons.INFO,
|
|
106
|
-
issueName: 'Freeze Prototypes',
|
|
107
|
-
description: 'Freeze Object.prototype to prevent pollution',
|
|
108
|
-
severity: 'LOW',
|
|
109
|
-
fix: 'Object.freeze(Object.prototype);',
|
|
110
|
-
documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze',
|
|
111
|
-
}),
|
|
112
|
-
strategyValidate: (0, eslint_devkit_2.formatLLMMessage)({
|
|
113
|
-
icon: eslint_devkit_2.MessageIcons.STRATEGY,
|
|
114
|
-
issueName: 'Validate Input',
|
|
115
|
-
description: 'Add input validation before property access',
|
|
116
|
-
severity: 'LOW',
|
|
117
|
-
fix: 'Validate key against allowed values before access',
|
|
118
|
-
documentationLink: 'https://portswigger.net/web-security/prototype-pollution',
|
|
119
|
-
}),
|
|
120
|
-
strategyWhitelist: (0, eslint_devkit_2.formatLLMMessage)({
|
|
121
|
-
icon: eslint_devkit_2.MessageIcons.STRATEGY,
|
|
122
|
-
issueName: 'Whitelist Properties',
|
|
123
|
-
description: 'Whitelist allowed property names only',
|
|
124
|
-
severity: 'LOW',
|
|
125
|
-
fix: 'Define allowed keys and validate against them',
|
|
126
|
-
documentationLink: 'https://portswigger.net/web-security/prototype-pollution',
|
|
127
|
-
}),
|
|
128
|
-
strategyFreeze: (0, eslint_devkit_2.formatLLMMessage)({
|
|
129
|
-
icon: eslint_devkit_2.MessageIcons.STRATEGY,
|
|
130
|
-
issueName: 'Freeze Prototypes',
|
|
131
|
-
description: 'Freeze prototypes to prevent pollution',
|
|
132
|
-
severity: 'LOW',
|
|
133
|
-
fix: 'Object.freeze(Object.prototype) at app startup',
|
|
134
|
-
documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze',
|
|
135
|
-
})
|
|
136
|
-
},
|
|
137
|
-
schema: [
|
|
138
|
-
{
|
|
139
|
-
type: 'object',
|
|
140
|
-
properties: {
|
|
141
|
-
allowLiterals: {
|
|
142
|
-
type: 'boolean',
|
|
143
|
-
default: false,
|
|
144
|
-
description: 'Allow bracket notation with literal strings'
|
|
145
|
-
},
|
|
146
|
-
additionalMethods: {
|
|
147
|
-
type: 'array',
|
|
148
|
-
items: { type: 'string' },
|
|
149
|
-
default: [],
|
|
150
|
-
description: 'Additional object methods to check for injection'
|
|
151
|
-
},
|
|
152
|
-
dangerousProperties: {
|
|
153
|
-
type: 'array',
|
|
154
|
-
items: { type: 'string' },
|
|
155
|
-
default: ['__proto__', 'prototype', 'constructor'],
|
|
156
|
-
description: 'Properties to consider dangerous'
|
|
157
|
-
},
|
|
158
|
-
strategy: {
|
|
159
|
-
type: 'string',
|
|
160
|
-
enum: ['validate', 'whitelist', 'freeze', 'auto'],
|
|
161
|
-
default: 'auto',
|
|
162
|
-
description: 'Strategy for fixing object injection (auto = smart detection)'
|
|
163
|
-
}
|
|
164
|
-
},
|
|
165
|
-
additionalProperties: false,
|
|
166
|
-
},
|
|
167
|
-
],
|
|
168
|
-
},
|
|
169
|
-
defaultOptions: [
|
|
170
|
-
{
|
|
171
|
-
allowLiterals: false,
|
|
172
|
-
additionalMethods: [],
|
|
173
|
-
dangerousProperties: ['__proto__', 'prototype', 'constructor'],
|
|
174
|
-
strategy: 'auto'
|
|
175
|
-
},
|
|
176
|
-
],
|
|
177
|
-
create(context) {
|
|
178
|
-
const options = context.options[0] || {};
|
|
179
|
-
const { allowLiterals: _allowLiterals = false, dangerousProperties = ['__proto__', 'prototype', 'constructor'], } = options;
|
|
180
|
-
const handledMemberExpressions = new WeakSet();
|
|
181
|
-
const sourceCode = context.sourceCode;
|
|
182
|
-
const isInCodemodContext = (() => {
|
|
183
|
-
const filename = context.filename;
|
|
184
|
-
if (/\/codemod[s]?\//i.test(filename))
|
|
185
|
-
return true;
|
|
186
|
-
if (/codemod\.[mc]?[jt]sx?$/i.test(filename))
|
|
187
|
-
return true;
|
|
188
|
-
const imports = sourceCode.ast.body.filter((n) => n.type === eslint_devkit_1.AST_NODE_TYPES.ImportDeclaration);
|
|
189
|
-
return imports.some((i) => {
|
|
190
|
-
const src = String(i.source.value);
|
|
191
|
-
return (src === '@babel/types' ||
|
|
192
|
-
src === '@babel/traverse' ||
|
|
193
|
-
src === 'recast' ||
|
|
194
|
-
src === 'jscodeshift' ||
|
|
195
|
-
src === 'eslint' ||
|
|
196
|
-
src === 'estree-walker' ||
|
|
197
|
-
src === 'ast-types' ||
|
|
198
|
-
src === 'esrap' ||
|
|
199
|
-
src === 'unist-util-visit' ||
|
|
200
|
-
src.startsWith('@typescript-eslint/'));
|
|
201
|
-
});
|
|
202
|
-
})();
|
|
203
|
-
const isTestFile = (() => {
|
|
204
|
-
const f = context.filename;
|
|
205
|
-
return (/\.test\.[mc]?[jt]sx?$/.test(f) ||
|
|
206
|
-
/\.spec\.[mc]?[jt]sx?$/.test(f) ||
|
|
207
|
-
/\/__tests__\//.test(f) ||
|
|
208
|
-
/\/test\//.test(f) ||
|
|
209
|
-
/\.fixture\.[mc]?[jt]sx?$/.test(f));
|
|
210
|
-
})();
|
|
211
|
-
const isLiteralString = (node) => {
|
|
212
|
-
return node.type === eslint_devkit_1.AST_NODE_TYPES.Literal && typeof node.value === 'string';
|
|
213
|
-
};
|
|
214
|
-
const hasPrecedingValidation = (propertyNode, node) => {
|
|
215
|
-
if (propertyNode.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
216
|
-
return false;
|
|
217
|
-
}
|
|
218
|
-
const keyName = propertyNode.name;
|
|
219
|
-
const isIncludesCall = (testNode) => {
|
|
220
|
-
if (testNode.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression &&
|
|
221
|
-
testNode.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
222
|
-
testNode.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
223
|
-
testNode.callee.property.name === 'includes' &&
|
|
224
|
-
testNode.arguments.length > 0 &&
|
|
225
|
-
testNode.arguments[0].type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
226
|
-
testNode.arguments[0].name === keyName) {
|
|
227
|
-
return true;
|
|
228
|
-
}
|
|
229
|
-
if (testNode.type === eslint_devkit_1.AST_NODE_TYPES.UnaryExpression &&
|
|
230
|
-
testNode.operator === '!' &&
|
|
231
|
-
testNode.argument.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
|
|
232
|
-
return isIncludesCall(testNode.argument);
|
|
233
|
-
}
|
|
234
|
-
return false;
|
|
235
|
-
};
|
|
236
|
-
const isHasOwnPropertyCall = (testNode) => {
|
|
237
|
-
if (testNode.type !== eslint_devkit_1.AST_NODE_TYPES.CallExpression)
|
|
238
|
-
return false;
|
|
239
|
-
const callee = testNode.callee;
|
|
240
|
-
const args = testNode.arguments;
|
|
241
|
-
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
242
|
-
callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
243
|
-
callee.property.name === 'call' &&
|
|
244
|
-
args.length >= 2 &&
|
|
245
|
-
args[1].type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
246
|
-
args[1].name === keyName) {
|
|
247
|
-
return true;
|
|
248
|
-
}
|
|
249
|
-
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
250
|
-
callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
251
|
-
(callee.property.name === 'hasOwnProperty' || callee.property.name === 'hasOwn')) {
|
|
252
|
-
const keyArg = callee.property.name === 'hasOwn' ? args[1] : args[0];
|
|
253
|
-
if (keyArg?.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && keyArg.name === keyName) {
|
|
254
|
-
return true;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
return false;
|
|
258
|
-
};
|
|
259
|
-
const isInOperator = (testNode) => {
|
|
260
|
-
return testNode.type === eslint_devkit_1.AST_NODE_TYPES.BinaryExpression &&
|
|
261
|
-
testNode.operator === 'in' &&
|
|
262
|
-
testNode.left.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
263
|
-
testNode.left.name === keyName;
|
|
264
|
-
};
|
|
265
|
-
const hasValidation = (testNode) => {
|
|
266
|
-
return isIncludesCall(testNode) || isHasOwnPropertyCall(testNode) || isInOperator(testNode);
|
|
267
|
-
};
|
|
268
|
-
const hasEarlyExit = (consequent) => {
|
|
269
|
-
if (consequent.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement) {
|
|
270
|
-
return consequent.body.some(stmt => stmt.type === eslint_devkit_1.AST_NODE_TYPES.ThrowStatement ||
|
|
271
|
-
stmt.type === eslint_devkit_1.AST_NODE_TYPES.ReturnStatement);
|
|
272
|
-
}
|
|
273
|
-
return consequent.type === eslint_devkit_1.AST_NODE_TYPES.ThrowStatement ||
|
|
274
|
-
consequent.type === eslint_devkit_1.AST_NODE_TYPES.ReturnStatement;
|
|
275
|
-
};
|
|
276
|
-
let current = node.parent;
|
|
277
|
-
let foundFunctionBody = false;
|
|
278
|
-
while (current && !foundFunctionBody) {
|
|
279
|
-
if (current.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement) {
|
|
280
|
-
if (hasValidation(current.test)) {
|
|
281
|
-
return true;
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
if (current.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement && current.parent && (current.parent.type === eslint_devkit_1.AST_NODE_TYPES.FunctionDeclaration ||
|
|
285
|
-
current.parent.type === eslint_devkit_1.AST_NODE_TYPES.FunctionExpression ||
|
|
286
|
-
current.parent.type === eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression)) {
|
|
287
|
-
foundFunctionBody = true;
|
|
288
|
-
const blockBody = current.body;
|
|
289
|
-
const nodeIndex = blockBody.findIndex((stmt) => {
|
|
290
|
-
let check = node;
|
|
291
|
-
while (check) {
|
|
292
|
-
if (check === stmt)
|
|
293
|
-
return true;
|
|
294
|
-
check = check.parent;
|
|
295
|
-
}
|
|
296
|
-
return false;
|
|
297
|
-
});
|
|
298
|
-
for (let i = 0; i < nodeIndex; i++) {
|
|
299
|
-
const stmt = blockBody[i];
|
|
300
|
-
if (stmt.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement &&
|
|
301
|
-
hasValidation(stmt.test) &&
|
|
302
|
-
hasEarlyExit(stmt.consequent)) {
|
|
303
|
-
return true;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
current = current.parent;
|
|
308
|
-
}
|
|
309
|
-
return false;
|
|
310
|
-
};
|
|
311
|
-
const isDangerousPropertyAccess = (propertyNode) => {
|
|
312
|
-
if (propertyNode.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
313
|
-
const name = propertyNode.name;
|
|
314
|
-
const numericIndexNames = new Set(['i', 'j', 'k', 'index', 'idx', 'n', 'len']);
|
|
315
|
-
if (numericIndexNames.has(name)) {
|
|
316
|
-
return false;
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
if (propertyNode.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
320
|
-
const name = propertyNode.name;
|
|
321
|
-
if (/^[A-Z][A-Z0-9_]{2,}$/.test(name)) {
|
|
322
|
-
return false;
|
|
323
|
-
}
|
|
324
|
-
if (/^[a-z]/.test(name) &&
|
|
325
|
-
/(?:Code|Status|Version|Kind|Mode|Type|Stage|Level|Phase|Step|Flag|Num|Count)$/.test(name)) {
|
|
326
|
-
return false;
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
if (isLiteralString(propertyNode)) {
|
|
330
|
-
const propName = String(propertyNode.value);
|
|
331
|
-
if (dangerousProperties.includes(propName)) {
|
|
332
|
-
return true;
|
|
333
|
-
}
|
|
334
|
-
return false;
|
|
335
|
-
}
|
|
336
|
-
return true;
|
|
337
|
-
};
|
|
338
|
-
const isPrototypelessObject = (objectNode) => {
|
|
339
|
-
if (objectNode.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
340
|
-
return false;
|
|
341
|
-
}
|
|
342
|
-
const varName = objectNode.name;
|
|
343
|
-
let current = objectNode;
|
|
344
|
-
while (current) {
|
|
345
|
-
if (current.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement ||
|
|
346
|
-
current.type === eslint_devkit_1.AST_NODE_TYPES.Program) {
|
|
347
|
-
const statements = current.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement
|
|
348
|
-
? current.body
|
|
349
|
-
: current.body;
|
|
350
|
-
for (const stmt of statements) {
|
|
351
|
-
if (stmt.type === eslint_devkit_1.AST_NODE_TYPES.VariableDeclaration) {
|
|
352
|
-
for (const decl of stmt.declarations) {
|
|
353
|
-
if (decl.id.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
354
|
-
decl.id.name === varName &&
|
|
355
|
-
decl.init) {
|
|
356
|
-
if (decl.init.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression &&
|
|
357
|
-
decl.init.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
358
|
-
decl.init.callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
359
|
-
decl.init.callee.object.name === 'Object' &&
|
|
360
|
-
decl.init.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
361
|
-
decl.init.callee.property.name === 'create' &&
|
|
362
|
-
decl.init.arguments.length > 0 &&
|
|
363
|
-
decl.init.arguments[0].type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
364
|
-
decl.init.arguments[0].value === null) {
|
|
365
|
-
return true;
|
|
366
|
-
}
|
|
367
|
-
if (decl.init.type === eslint_devkit_1.AST_NODE_TYPES.ArrayExpression &&
|
|
368
|
-
decl.init.elements.length > 0 &&
|
|
369
|
-
decl.init.elements[0]?.type === eslint_devkit_1.AST_NODE_TYPES.SpreadElement) {
|
|
370
|
-
return true;
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
current = current.parent;
|
|
378
|
-
}
|
|
379
|
-
return false;
|
|
380
|
-
};
|
|
381
|
-
const extractPropertyAccess = (node) => {
|
|
382
|
-
let object;
|
|
383
|
-
let property;
|
|
384
|
-
let propertyNode;
|
|
385
|
-
let isAssignment = false;
|
|
386
|
-
if (node.type === eslint_devkit_1.AST_NODE_TYPES.AssignmentExpression && node.left.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
|
|
387
|
-
object = sourceCode.getText(node.left.object);
|
|
388
|
-
property = sourceCode.getText(node.left.property);
|
|
389
|
-
propertyNode = node.left.property;
|
|
390
|
-
isAssignment = true;
|
|
391
|
-
}
|
|
392
|
-
else {
|
|
393
|
-
const memberNode = node;
|
|
394
|
-
object = sourceCode.getText(memberNode.object);
|
|
395
|
-
property = sourceCode.getText(memberNode.property);
|
|
396
|
-
propertyNode = memberNode.property;
|
|
397
|
-
isAssignment = false;
|
|
398
|
-
}
|
|
399
|
-
const pattern = OBJECT_INJECTION_PATTERNS.find(p => new RegExp(p.pattern, 'i').test(property) ||
|
|
400
|
-
dangerousProperties.includes(property.replace(/['"]/g, ''))) || null;
|
|
401
|
-
return { object, property, propertyNode, isAssignment, pattern };
|
|
402
|
-
};
|
|
403
|
-
const isHighRiskAssignment = (node) => {
|
|
404
|
-
if (node.left.type !== 'MemberExpression') {
|
|
405
|
-
return false;
|
|
406
|
-
}
|
|
407
|
-
if (!node.left.computed) {
|
|
408
|
-
return false;
|
|
409
|
-
}
|
|
410
|
-
if (isPrototypelessObject(node.left.object)) {
|
|
411
|
-
return false;
|
|
412
|
-
}
|
|
413
|
-
if (isTypedArrayObject(node.left.object)) {
|
|
414
|
-
return false;
|
|
415
|
-
}
|
|
416
|
-
const { propertyNode } = extractPropertyAccess(node);
|
|
417
|
-
if (isNumericKey(propertyNode)) {
|
|
418
|
-
return false;
|
|
419
|
-
}
|
|
420
|
-
if (isForInOrObjectKeysKey(propertyNode)) {
|
|
421
|
-
return false;
|
|
422
|
-
}
|
|
423
|
-
if (hasPrecedingValidation(propertyNode, node)) {
|
|
424
|
-
return false;
|
|
425
|
-
}
|
|
426
|
-
return isDangerousPropertyAccess(propertyNode);
|
|
427
|
-
};
|
|
428
|
-
const isReflectResultAccess = (objectNode) => {
|
|
429
|
-
if (objectNode.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
|
|
430
|
-
const callee = objectNode.callee;
|
|
431
|
-
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
432
|
-
callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
433
|
-
callee.object.name === 'Reflect') {
|
|
434
|
-
return true;
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
if (objectNode.type === eslint_devkit_1.AST_NODE_TYPES.ChainExpression) {
|
|
438
|
-
return isReflectResultAccess(objectNode.expression);
|
|
439
|
-
}
|
|
440
|
-
return false;
|
|
441
|
-
};
|
|
442
|
-
const isHighRiskMemberAccess = (node) => {
|
|
443
|
-
if (!node.computed) {
|
|
444
|
-
return false;
|
|
445
|
-
}
|
|
446
|
-
if (isReflectResultAccess(node.object)) {
|
|
447
|
-
return false;
|
|
448
|
-
}
|
|
449
|
-
if (isTypedArrayObject(node.object)) {
|
|
450
|
-
return false;
|
|
451
|
-
}
|
|
452
|
-
const { propertyNode } = extractPropertyAccess(node);
|
|
453
|
-
if (isNumericKey(propertyNode)) {
|
|
454
|
-
return false;
|
|
455
|
-
}
|
|
456
|
-
if (isForInOrObjectKeysKey(propertyNode)) {
|
|
457
|
-
return false;
|
|
458
|
-
}
|
|
459
|
-
if (hasPrecedingValidation(propertyNode, node)) {
|
|
460
|
-
return false;
|
|
461
|
-
}
|
|
462
|
-
return isDangerousPropertyAccess(propertyNode);
|
|
463
|
-
};
|
|
464
|
-
const isNumericKey = (node) => {
|
|
465
|
-
if (node.type === eslint_devkit_1.AST_NODE_TYPES.Literal && typeof node.value === 'number') {
|
|
466
|
-
return true;
|
|
467
|
-
}
|
|
468
|
-
if (node.type === eslint_devkit_1.AST_NODE_TYPES.UnaryExpression && node.operator === '+') {
|
|
469
|
-
return true;
|
|
470
|
-
}
|
|
471
|
-
if (node.type === eslint_devkit_1.AST_NODE_TYPES.BinaryExpression) {
|
|
472
|
-
const op = node.operator;
|
|
473
|
-
if (op === '|' || op === '&' || op === '^' || op === '<<' || op === '>>' || op === '>>>' || op === '*' || op === '/' || op === '%' || op === '-') {
|
|
474
|
-
return true;
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
if (node.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
|
|
478
|
-
const callee = node.callee;
|
|
479
|
-
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
480
|
-
const name = callee.name;
|
|
481
|
-
if (name === 'Number' || name === 'parseInt' || name === 'parseFloat')
|
|
482
|
-
return true;
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
if (node.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
486
|
-
return isLoopCounterIdentifier(node);
|
|
487
|
-
}
|
|
488
|
-
return false;
|
|
489
|
-
};
|
|
490
|
-
const isLoopCounterIdentifier = (node) => {
|
|
491
|
-
const scope = context.sourceCode.getScope(node);
|
|
492
|
-
const variable = scope.references.find((r) => r.identifier === node)?.resolved;
|
|
493
|
-
if (!variable || variable.defs.length === 0)
|
|
494
|
-
return false;
|
|
495
|
-
const def = variable.defs[0];
|
|
496
|
-
const parent = def.node?.parent;
|
|
497
|
-
const grand = parent?.parent;
|
|
498
|
-
if (parent?.type === eslint_devkit_1.AST_NODE_TYPES.VariableDeclaration &&
|
|
499
|
-
grand?.type === eslint_devkit_1.AST_NODE_TYPES.ForStatement &&
|
|
500
|
-
grand.init === parent) {
|
|
501
|
-
const init = def.node.init;
|
|
502
|
-
if (!init)
|
|
503
|
-
return false;
|
|
504
|
-
if (init.type === eslint_devkit_1.AST_NODE_TYPES.Literal && typeof init.value === 'number') {
|
|
505
|
-
return true;
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
return false;
|
|
509
|
-
};
|
|
510
|
-
const isForInOrObjectKeysKey = (node) => {
|
|
511
|
-
if (node.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
512
|
-
return false;
|
|
513
|
-
const scope = context.sourceCode.getScope(node);
|
|
514
|
-
const variable = scope.references.find((r) => r.identifier === node)?.resolved;
|
|
515
|
-
if (!variable || variable.defs.length === 0)
|
|
516
|
-
return false;
|
|
517
|
-
const def = variable.defs[0];
|
|
518
|
-
const varDecl = def.node?.parent;
|
|
519
|
-
const loopStmt = varDecl?.parent;
|
|
520
|
-
if (!varDecl || varDecl.type !== eslint_devkit_1.AST_NODE_TYPES.VariableDeclaration)
|
|
521
|
-
return false;
|
|
522
|
-
if (loopStmt?.type === eslint_devkit_1.AST_NODE_TYPES.ForInStatement && loopStmt.left === varDecl) {
|
|
523
|
-
return true;
|
|
524
|
-
}
|
|
525
|
-
if (loopStmt?.type === eslint_devkit_1.AST_NODE_TYPES.ForOfStatement && loopStmt.left === varDecl) {
|
|
526
|
-
const right = loopStmt.right;
|
|
527
|
-
return (right.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression &&
|
|
528
|
-
right.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
529
|
-
!right.callee.computed &&
|
|
530
|
-
right.callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
531
|
-
right.callee.object.name === 'Object' &&
|
|
532
|
-
right.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
533
|
-
(right.callee.property.name === 'keys' || right.callee.property.name === 'entries'));
|
|
534
|
-
}
|
|
535
|
-
return false;
|
|
536
|
-
};
|
|
537
|
-
const isTypedArrayObject = (objectNode) => {
|
|
538
|
-
if (objectNode.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier)
|
|
539
|
-
return false;
|
|
540
|
-
const varName = objectNode.name;
|
|
541
|
-
let scope = context.sourceCode.getScope(objectNode);
|
|
542
|
-
while (scope) {
|
|
543
|
-
const variable = scope.variables.find((v) => v.name === varName);
|
|
544
|
-
if (variable) {
|
|
545
|
-
for (const def of variable.defs) {
|
|
546
|
-
const init = def.node.init;
|
|
547
|
-
if (init?.type === eslint_devkit_1.AST_NODE_TYPES.NewExpression &&
|
|
548
|
-
init.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
549
|
-
TYPED_ARRAY_CTORS.has(init.callee.name)) {
|
|
550
|
-
return true;
|
|
551
|
-
}
|
|
552
|
-
}
|
|
553
|
-
break;
|
|
554
|
-
}
|
|
555
|
-
if (!scope.upper)
|
|
556
|
-
break;
|
|
557
|
-
scope = scope.upper;
|
|
558
|
-
}
|
|
559
|
-
return false;
|
|
560
|
-
};
|
|
561
|
-
const determineRiskLevel = (pattern, isAssignment) => {
|
|
562
|
-
if (pattern?.riskLevel === 'critical' || (pattern && isAssignment)) {
|
|
563
|
-
return 'CRITICAL';
|
|
564
|
-
}
|
|
565
|
-
if (pattern?.riskLevel === 'high' || isAssignment) {
|
|
566
|
-
return 'HIGH';
|
|
567
|
-
}
|
|
568
|
-
return 'MEDIUM';
|
|
569
|
-
};
|
|
570
|
-
const checkAssignmentExpression = (node) => {
|
|
571
|
-
if (!isHighRiskAssignment(node)) {
|
|
572
|
-
return;
|
|
573
|
-
}
|
|
574
|
-
let me = node.left;
|
|
575
|
-
handledMemberExpressions.add(me);
|
|
576
|
-
while (me.object.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression && me.object.computed) {
|
|
577
|
-
me = me.object;
|
|
578
|
-
handledMemberExpressions.add(me);
|
|
579
|
-
}
|
|
580
|
-
const { object, property, isAssignment, pattern } = extractPropertyAccess(node);
|
|
581
|
-
const riskLevel = determineRiskLevel(pattern, isAssignment);
|
|
582
|
-
context.report({
|
|
583
|
-
node,
|
|
584
|
-
messageId: 'objectInjection',
|
|
585
|
-
data: {
|
|
586
|
-
pattern: `${object}[${property}]`,
|
|
587
|
-
riskLevel,
|
|
588
|
-
vulnerability: pattern?.vulnerability || 'object injection',
|
|
589
|
-
safeAlternative: pattern?.safeAlternative || 'Use Map or property whitelisting',
|
|
590
|
-
},
|
|
591
|
-
suggest: [
|
|
592
|
-
{
|
|
593
|
-
messageId: 'useMapInstead',
|
|
594
|
-
fix: () => null
|
|
595
|
-
},
|
|
596
|
-
{
|
|
597
|
-
messageId: 'useHasOwnProperty',
|
|
598
|
-
fix: () => null
|
|
599
|
-
},
|
|
600
|
-
{
|
|
601
|
-
messageId: 'whitelistKeys',
|
|
602
|
-
fix: () => null
|
|
603
|
-
},
|
|
604
|
-
{
|
|
605
|
-
messageId: 'useObjectCreate',
|
|
606
|
-
fix: () => null
|
|
607
|
-
},
|
|
608
|
-
{
|
|
609
|
-
messageId: 'freezePrototypes',
|
|
610
|
-
fix: () => null
|
|
611
|
-
}
|
|
612
|
-
]
|
|
613
|
-
});
|
|
614
|
-
};
|
|
615
|
-
const checkMemberExpression = (node) => {
|
|
616
|
-
if (!isHighRiskMemberAccess(node)) {
|
|
617
|
-
return;
|
|
618
|
-
}
|
|
619
|
-
if (handledMemberExpressions.has(node)) {
|
|
620
|
-
return;
|
|
621
|
-
}
|
|
622
|
-
const parent = node.parent;
|
|
623
|
-
if (parent?.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
624
|
-
parent.computed &&
|
|
625
|
-
parent.object === node) {
|
|
626
|
-
return;
|
|
627
|
-
}
|
|
628
|
-
if (parent && parent.type === eslint_devkit_1.AST_NODE_TYPES.AssignmentExpression && parent.left === node) {
|
|
629
|
-
return;
|
|
630
|
-
}
|
|
631
|
-
const { object, property, isAssignment, pattern } = extractPropertyAccess(node);
|
|
632
|
-
const riskLevel = determineRiskLevel(pattern, isAssignment);
|
|
633
|
-
context.report({
|
|
634
|
-
node,
|
|
635
|
-
messageId: 'objectInjection',
|
|
636
|
-
data: {
|
|
637
|
-
pattern: `${object}[${property}]`,
|
|
638
|
-
riskLevel,
|
|
639
|
-
vulnerability: pattern?.vulnerability || 'object injection',
|
|
640
|
-
safeAlternative: pattern?.safeAlternative || 'Use Map or property whitelisting',
|
|
641
|
-
}
|
|
642
|
-
});
|
|
643
|
-
};
|
|
644
|
-
const checkObjectAssignSpread = (node) => {
|
|
645
|
-
if (node.callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression)
|
|
646
|
-
return;
|
|
647
|
-
const callee = node.callee;
|
|
648
|
-
const objectIsObject = callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
649
|
-
callee.object.name === 'Object';
|
|
650
|
-
const propIsAssign = !callee.computed &&
|
|
651
|
-
callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
652
|
-
callee.property.name === 'assign';
|
|
653
|
-
if (!objectIsObject || !propIsAssign)
|
|
654
|
-
return;
|
|
655
|
-
if (node.arguments[0]?.type === eslint_devkit_1.AST_NODE_TYPES.ObjectExpression)
|
|
656
|
-
return;
|
|
657
|
-
const sources = node.arguments.slice(1);
|
|
658
|
-
const anyTaintedSource = sources.some((s) => s.type !== eslint_devkit_1.AST_NODE_TYPES.ObjectExpression &&
|
|
659
|
-
s.type !== eslint_devkit_1.AST_NODE_TYPES.Literal);
|
|
660
|
-
if (!anyTaintedSource)
|
|
661
|
-
return;
|
|
662
|
-
context.report({
|
|
663
|
-
node,
|
|
664
|
-
messageId: 'objectInjection',
|
|
665
|
-
data: {
|
|
666
|
-
pattern: 'Object.assign(target, untrustedSource)',
|
|
667
|
-
riskLevel: 'HIGH',
|
|
668
|
-
vulnerability: 'object injection via Object.assign spread',
|
|
669
|
-
safeAlternative: 'Validate or whitelist keys before merging: `for (const k of Object.keys(src)) if (!ALLOWED.has(k)) continue;`',
|
|
670
|
-
},
|
|
671
|
-
});
|
|
672
|
-
};
|
|
673
|
-
return {
|
|
674
|
-
AssignmentExpression: (node) => {
|
|
675
|
-
if (isInCodemodContext || isTestFile)
|
|
676
|
-
return;
|
|
677
|
-
return checkAssignmentExpression(node);
|
|
678
|
-
},
|
|
679
|
-
MemberExpression: (node) => {
|
|
680
|
-
if (isInCodemodContext || isTestFile)
|
|
681
|
-
return;
|
|
682
|
-
return checkMemberExpression(node);
|
|
683
|
-
},
|
|
684
|
-
CallExpression: (node) => {
|
|
685
|
-
if (isInCodemodContext || isTestFile)
|
|
686
|
-
return;
|
|
687
|
-
return checkObjectAssignSpread(node);
|
|
688
|
-
},
|
|
689
|
-
};
|
|
690
|
-
},
|
|
691
|
-
});
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.detectObjectInjection=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");const eslint_devkit_3=require("@interlace/eslint-devkit");const TYPED_ARRAY_CTORS=new Set(["Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array","BigInt64Array","BigUint64Array"]);const OBJECT_INJECTION_PATTERNS=[{pattern:"__proto__",dangerous:true,vulnerability:"prototype-pollution",safeAlternative:"Object.create(null) or Map",example:{bad:'obj[userInput] = value; // if userInput is "__proto__"',good:"const map = new Map(); map.set(userInput, value);"},effort:"15-20 minutes",riskLevel:"critical"},{pattern:"prototype",dangerous:true,vulnerability:"prototype-pollution",safeAlternative:"Avoid prototype manipulation",example:{bad:'obj[userInput] = value; // if userInput is "prototype"',good:"if (!obj.hasOwnProperty(userInput)) obj[userInput] = value;"},effort:"10-15 minutes",riskLevel:"high"},{pattern:"constructor",dangerous:true,vulnerability:"method-injection",safeAlternative:"Validate property names against whitelist",example:{bad:'obj[userInput] = value; // if userInput is "constructor"',good:"const ALLOWED_KEYS = ['name', 'age', 'email']; if (ALLOWED_KEYS.includes(userInput)) obj[userInput] = value;"},effort:"10-15 minutes",riskLevel:"medium"}];exports.detectObjectInjection=(0,eslint_devkit_3.createRule)({name:"detect-object-injection",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/detect-object-injection.md",description:"Detects variable[key] as a left- or right-hand assignment operand",cwe:"CWE-915",confidence:"low"},messages:{objectInjection:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.WARNING,issueName:"Object injection",cwe:"CWE-915",description:"Object injection/Prototype pollution (incl. model/tool outputs)",severity:"{{riskLevel}}",fix:"{{safeAlternative}}",documentationLink:"https://portswigger.net/web-security/prototype-pollution"}),useMapInstead:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.INFO,issueName:"Use Map",description:"Use Map instead of plain objects",severity:"LOW",fix:"const map = new Map(); map.set(key, value);",documentationLink:"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"}),useHasOwnProperty:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.INFO,issueName:"Use hasOwnProperty",description:"Check hasOwnProperty to avoid prototype properties",severity:"LOW",fix:"if (obj.hasOwnProperty(key)) { obj[key] = value; }",documentationLink:"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty"}),whitelistKeys:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.INFO,issueName:"Whitelist Keys",description:"Whitelist allowed property names",severity:"LOW",fix:'const ALLOWED = ["name", "email"]; if (ALLOWED.includes(key)) obj[key] = value; // reject model/tool-supplied unknown keys',documentationLink:"https://portswigger.net/web-security/prototype-pollution"}),useObjectCreate:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.INFO,issueName:"Use Object.create(null)",description:"Create clean objects without prototypes",severity:"LOW",fix:"const obj = Object.create(null);",documentationLink:"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create"}),freezePrototypes:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.INFO,issueName:"Freeze Prototypes",description:"Freeze Object.prototype to prevent pollution",severity:"LOW",fix:"Object.freeze(Object.prototype);",documentationLink:"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze"}),strategyValidate:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.STRATEGY,issueName:"Validate Input",description:"Add input validation before property access",severity:"LOW",fix:"Validate key against allowed values before access",documentationLink:"https://portswigger.net/web-security/prototype-pollution"}),strategyWhitelist:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.STRATEGY,issueName:"Whitelist Properties",description:"Whitelist allowed property names only",severity:"LOW",fix:"Define allowed keys and validate against them",documentationLink:"https://portswigger.net/web-security/prototype-pollution"}),strategyFreeze:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.STRATEGY,issueName:"Freeze Prototypes",description:"Freeze prototypes to prevent pollution",severity:"LOW",fix:"Object.freeze(Object.prototype) at app startup",documentationLink:"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze"})},schema:[{type:"object",properties:{allowLiterals:{type:"boolean",default:false,description:"Allow bracket notation with literal strings"},additionalMethods:{type:"array",items:{type:"string"},default:[],description:"Additional object methods to check for injection"},dangerousProperties:{type:"array",items:{type:"string"},default:["__proto__","prototype","constructor"],description:"Properties to consider dangerous"},strategy:{type:"string",enum:["validate","whitelist","freeze","auto"],default:"auto",description:"Strategy for fixing object injection (auto = smart detection)"}},additionalProperties:false}]},defaultOptions:[{allowLiterals:false,additionalMethods:[],dangerousProperties:["__proto__","prototype","constructor"],strategy:"auto"}],create(context){const options=context.options[0]||{};const{allowLiterals:_allowLiterals=false,dangerousProperties=["__proto__","prototype","constructor"]}=options;const handledMemberExpressions=new WeakSet;const sourceCode=context.sourceCode;const isInCodemodContext=(()=>{const filename=context.filename;if(/\/codemod[s]?\//i.test(filename))return true;if(/codemod\.[mc]?[jt]sx?$/i.test(filename))return true;const imports=sourceCode.ast.body.filter(n=>n.type===eslint_devkit_1.AST_NODE_TYPES.ImportDeclaration);return imports.some(i=>{const src=String(i.source.value);return src==="@babel/types"||src==="@babel/traverse"||src==="recast"||src==="jscodeshift"||src==="eslint"||src==="estree-walker"||src==="ast-types"||src==="esrap"||src==="unist-util-visit"||src.startsWith("@typescript-eslint/")})})();const isTestFile=(()=>{const f=context.filename;return/\.test\.[mc]?[jt]sx?$/.test(f)||/\.spec\.[mc]?[jt]sx?$/.test(f)||/\/__tests__\//.test(f)||/\/test\//.test(f)||/\.fixture\.[mc]?[jt]sx?$/.test(f)})();const isLiteralString=node=>{return node.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&typeof node.value==="string"};const hasPrecedingValidation=(propertyNode,node)=>{if(propertyNode.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier){return false}const keyName=propertyNode.name;const isIncludesCall=testNode=>{if(testNode.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression&&testNode.callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&testNode.callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&testNode.callee.property.name==="includes"&&testNode.arguments.length>0&&testNode.arguments[0].type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&testNode.arguments[0].name===keyName){return true}if(testNode.type===eslint_devkit_1.AST_NODE_TYPES.UnaryExpression&&testNode.operator==="!"&&testNode.argument.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression){return isIncludesCall(testNode.argument)}return false};const isHasOwnPropertyCall=testNode=>{if(testNode.type!==eslint_devkit_1.AST_NODE_TYPES.CallExpression)return false;const callee=testNode.callee;const args=testNode.arguments;if(callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&callee.property.name==="call"&&args.length>=2&&args[1].type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&args[1].name===keyName){return true}if(callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&(callee.property.name==="hasOwnProperty"||callee.property.name==="hasOwn")){const keyArg=callee.property.name==="hasOwn"?args[1]:args[0];if(keyArg?.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&keyArg.name===keyName){return true}}return false};const isInOperator=testNode=>{return testNode.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression&&testNode.operator==="in"&&testNode.left.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&testNode.left.name===keyName};const hasValidation=testNode=>{return isIncludesCall(testNode)||isHasOwnPropertyCall(testNode)||isInOperator(testNode)};const hasEarlyExit=consequent=>{if(consequent.type===eslint_devkit_1.AST_NODE_TYPES.BlockStatement){return consequent.body.some(stmt=>stmt.type===eslint_devkit_1.AST_NODE_TYPES.ThrowStatement||stmt.type===eslint_devkit_1.AST_NODE_TYPES.ReturnStatement)}return consequent.type===eslint_devkit_1.AST_NODE_TYPES.ThrowStatement||consequent.type===eslint_devkit_1.AST_NODE_TYPES.ReturnStatement};let current=node.parent;let foundFunctionBody=false;while(current&&!foundFunctionBody){if(current.type===eslint_devkit_1.AST_NODE_TYPES.IfStatement){if(hasValidation(current.test)){return true}}if(current.type===eslint_devkit_1.AST_NODE_TYPES.BlockStatement&¤t.parent&&(current.parent.type===eslint_devkit_1.AST_NODE_TYPES.FunctionDeclaration||current.parent.type===eslint_devkit_1.AST_NODE_TYPES.FunctionExpression||current.parent.type===eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression)){foundFunctionBody=true;const blockBody=current.body;const nodeIndex=blockBody.findIndex(stmt=>{let check=node;while(check){if(check===stmt)return true;check=check.parent}return false});for(let i=0;i<nodeIndex;i++){const stmt=blockBody[i];if(stmt.type===eslint_devkit_1.AST_NODE_TYPES.IfStatement&&hasValidation(stmt.test)&&hasEarlyExit(stmt.consequent)){return true}}}current=current.parent}return false};const isDangerousPropertyAccess=propertyNode=>{if(propertyNode.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const name=propertyNode.name;const numericIndexNames=new Set(["i","j","k","index","idx","n","len"]);if(numericIndexNames.has(name)){return false}}if(propertyNode.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const name=propertyNode.name;if(/^[A-Z][A-Z0-9_]{2,}$/.test(name)){return false}if(/^[a-z]/.test(name)&&/(?:Code|Status|Version|Kind|Mode|Type|Stage|Level|Phase|Step|Flag|Num|Count)$/.test(name)){return false}}if(isLiteralString(propertyNode)){const propName=String(propertyNode.value);if(dangerousProperties.includes(propName)){return true}return false}return true};const isPrototypelessObject=objectNode=>{if(objectNode.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier){return false}const varName=objectNode.name;let current=objectNode;while(current){if(current.type===eslint_devkit_1.AST_NODE_TYPES.BlockStatement||current.type===eslint_devkit_1.AST_NODE_TYPES.Program){const statements=current.type===eslint_devkit_1.AST_NODE_TYPES.BlockStatement?current.body:current.body;for(const stmt of statements){if(stmt.type===eslint_devkit_1.AST_NODE_TYPES.VariableDeclaration){for(const decl of stmt.declarations){if(decl.id.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&decl.id.name===varName&&decl.init){if(decl.init.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression&&decl.init.callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&decl.init.callee.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&decl.init.callee.object.name==="Object"&&decl.init.callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&decl.init.callee.property.name==="create"&&decl.init.arguments.length>0&&decl.init.arguments[0].type===eslint_devkit_1.AST_NODE_TYPES.Literal&&decl.init.arguments[0].value===null){return true}if(decl.init.type===eslint_devkit_1.AST_NODE_TYPES.ArrayExpression&&decl.init.elements.length>0&&decl.init.elements[0]?.type===eslint_devkit_1.AST_NODE_TYPES.SpreadElement){return true}}}}}}current=current.parent}return false};const extractPropertyAccess=node=>{let object;let property;let propertyNode;let isAssignment=false;if(node.type===eslint_devkit_1.AST_NODE_TYPES.AssignmentExpression&&node.left.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression){object=sourceCode.getText(node.left.object);property=sourceCode.getText(node.left.property);propertyNode=node.left.property;isAssignment=true}else{const memberNode=node;object=sourceCode.getText(memberNode.object);property=sourceCode.getText(memberNode.property);propertyNode=memberNode.property;isAssignment=false}const pattern=OBJECT_INJECTION_PATTERNS.find(p=>new RegExp(p.pattern,"i").test(property)||dangerousProperties.includes(property.replace(/['"]/g,"")))||null;return{object,property,propertyNode,isAssignment,pattern}};const isHighRiskAssignment=node=>{if(node.left.type!=="MemberExpression"){return false}if(!node.left.computed){return false}if(isPrototypelessObject(node.left.object)){return false}if(isTypedArrayObject(node.left.object)){return false}const{propertyNode}=extractPropertyAccess(node);if(isNumericKey(propertyNode)){return false}if(isForInOrObjectKeysKey(propertyNode)){return false}if(hasPrecedingValidation(propertyNode,node)){return false}return isDangerousPropertyAccess(propertyNode)};const isReflectResultAccess=objectNode=>{if(objectNode.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression){const callee=objectNode.callee;if(callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&callee.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&callee.object.name==="Reflect"){return true}}if(objectNode.type===eslint_devkit_1.AST_NODE_TYPES.ChainExpression){return isReflectResultAccess(objectNode.expression)}return false};const isHighRiskMemberAccess=node=>{if(!node.computed){return false}if(isReflectResultAccess(node.object)){return false}if(isTypedArrayObject(node.object)){return false}const{propertyNode}=extractPropertyAccess(node);if(isNumericKey(propertyNode)){return false}if(isForInOrObjectKeysKey(propertyNode)){return false}if(hasPrecedingValidation(propertyNode,node)){return false}return isDangerousPropertyAccess(propertyNode)};const isNumericKey=node=>{if(node.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&typeof node.value==="number"){return true}if(node.type===eslint_devkit_1.AST_NODE_TYPES.UnaryExpression&&node.operator==="+"){return true}if(node.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression){const op=node.operator;if(op==="|"||op==="&"||op==="^"||op==="<<"||op===">>"||op===">>>"||op==="*"||op==="/"||op==="%"||op==="-"){return true}}if(node.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression){const callee=node.callee;if(callee.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const name=callee.name;if(name==="Number"||name==="parseInt"||name==="parseFloat")return true}}if(node.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){return isLoopCounterIdentifier(node)}return false};const isLoopCounterIdentifier=node=>{const scope=context.sourceCode.getScope(node);const variable=scope.references.find(r=>r.identifier===node)?.resolved;if(!variable||variable.defs.length===0)return false;const def=variable.defs[0];const parent=def.node?.parent;const grand=parent?.parent;if(parent?.type===eslint_devkit_1.AST_NODE_TYPES.VariableDeclaration&&grand?.type===eslint_devkit_1.AST_NODE_TYPES.ForStatement&&grand.init===parent){const init=def.node.init;if(!init)return false;if(init.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&typeof init.value==="number"){return true}}return false};const isForInOrObjectKeysKey=node=>{if(node.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return false;const scope=context.sourceCode.getScope(node);const variable=scope.references.find(r=>r.identifier===node)?.resolved;if(!variable||variable.defs.length===0)return false;const def=variable.defs[0];const varDecl=def.node?.parent;const loopStmt=varDecl?.parent;if(!varDecl||varDecl.type!==eslint_devkit_1.AST_NODE_TYPES.VariableDeclaration)return false;if(loopStmt?.type===eslint_devkit_1.AST_NODE_TYPES.ForInStatement&&loopStmt.left===varDecl){return true}if(loopStmt?.type===eslint_devkit_1.AST_NODE_TYPES.ForOfStatement&&loopStmt.left===varDecl){const right=loopStmt.right;return right.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression&&right.callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&!right.callee.computed&&right.callee.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&right.callee.object.name==="Object"&&right.callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&(right.callee.property.name==="keys"||right.callee.property.name==="entries")}return false};const isTypedArrayObject=objectNode=>{if(objectNode.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return false;const varName=objectNode.name;let scope=context.sourceCode.getScope(objectNode);while(scope){const variable=scope.variables.find(v=>v.name===varName);if(variable){for(const def of variable.defs){const init=def.node.init;if(init?.type===eslint_devkit_1.AST_NODE_TYPES.NewExpression&&init.callee.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&TYPED_ARRAY_CTORS.has(init.callee.name)){return true}}break}if(!scope.upper)break;scope=scope.upper}return false};const determineRiskLevel=(pattern,isAssignment)=>{if(pattern?.riskLevel==="critical"||pattern&&isAssignment){return"CRITICAL"}if(pattern?.riskLevel==="high"||isAssignment){return"HIGH"}return"MEDIUM"};const checkAssignmentExpression=node=>{if(!isHighRiskAssignment(node)){return}let me=node.left;handledMemberExpressions.add(me);while(me.object.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&me.object.computed){me=me.object;handledMemberExpressions.add(me)}const{object,property,isAssignment,pattern}=extractPropertyAccess(node);const riskLevel=determineRiskLevel(pattern,isAssignment);context.report({node,messageId:"objectInjection",data:{pattern:`${object}[${property}]`,riskLevel,vulnerability:pattern?.vulnerability||"object injection",safeAlternative:pattern?.safeAlternative||"Use Map or property whitelisting"},suggest:[{messageId:"useMapInstead",fix:()=>null},{messageId:"useHasOwnProperty",fix:()=>null},{messageId:"whitelistKeys",fix:()=>null},{messageId:"useObjectCreate",fix:()=>null},{messageId:"freezePrototypes",fix:()=>null}]})};const checkMemberExpression=node=>{if(!isHighRiskMemberAccess(node)){return}if(handledMemberExpressions.has(node)){return}const parent=node.parent;if(parent?.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&parent.computed&&parent.object===node){return}if(parent&&parent.type===eslint_devkit_1.AST_NODE_TYPES.AssignmentExpression&&parent.left===node){return}const{object,property,isAssignment,pattern}=extractPropertyAccess(node);const riskLevel=determineRiskLevel(pattern,isAssignment);context.report({node,messageId:"objectInjection",data:{pattern:`${object}[${property}]`,riskLevel,vulnerability:pattern?.vulnerability||"object injection",safeAlternative:pattern?.safeAlternative||"Use Map or property whitelisting"}})};const checkObjectAssignSpread=node=>{if(node.callee.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression)return;const callee=node.callee;const objectIsObject=callee.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&callee.object.name==="Object";const propIsAssign=!callee.computed&&callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&callee.property.name==="assign";if(!objectIsObject||!propIsAssign)return;if(node.arguments[0]?.type===eslint_devkit_1.AST_NODE_TYPES.ObjectExpression)return;const sources=node.arguments.slice(1);const anyTaintedSource=sources.some(s=>s.type!==eslint_devkit_1.AST_NODE_TYPES.ObjectExpression&&s.type!==eslint_devkit_1.AST_NODE_TYPES.Literal);if(!anyTaintedSource)return;context.report({node,messageId:"objectInjection",data:{pattern:"Object.assign(target, untrustedSource)",riskLevel:"HIGH",vulnerability:"object injection via Object.assign spread",safeAlternative:"Validate or whitelist keys before merging: `for (const k of Object.keys(src)) if (!ALLOWED.has(k)) continue;`"}})};return{AssignmentExpression:node=>{if(isInCodemodContext||isTestFile)return;return checkAssignmentExpression(node)},MemberExpression:node=>{if(isInCodemodContext||isTestFile)return;return checkMemberExpression(node)},CallExpression:node=>{if(isInCodemodContext||isTestFile)return;return checkObjectAssignSpread(node)}}}});
|