eslint-plugin-secure-coding 3.4.0 → 3.4.1
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 +5 -18
- package/src/index.d.ts +17 -23
- package/src/index.js +2 -131
- package/src/oxlint.d.ts +17 -20
- package/src/oxlint.js +0 -18
- package/src/rules/detect-non-literal-regexp/index.js +5 -79
- package/src/rules/detect-object-injection/index.js +2 -221
- package/src/rules/detect-weak-password-validation/index.js +0 -12
- package/src/rules/no-directive-injection/index.js +0 -33
- package/src/rules/no-electron-security-issues/index.js +0 -41
- package/src/rules/no-format-string-injection/index.js +0 -82
- package/src/rules/no-graphql-injection/index.js +8 -133
- package/src/rules/no-hardcoded-credentials/index.js +3 -297
- package/src/rules/no-hardcoded-session-tokens/index.js +0 -11
- package/src/rules/no-improper-sanitization/index.js +0 -44
- package/src/rules/no-improper-type-validation/index.js +1 -51
- package/src/rules/no-insecure-comparison/index.js +2 -43
- package/src/rules/no-ldap-injection/index.js +8 -61
- package/src/rules/no-missing-authentication/index.js +2 -56
- package/src/rules/no-pii-in-logs/index.js +0 -13
- package/src/rules/no-privilege-escalation/index.js +2 -49
- package/src/rules/no-redos-vulnerable-regex/index.js +3 -49
- package/src/rules/no-sensitive-data-exposure/index.js +5 -29
- package/src/rules/no-template-injection/index.js +0 -6
- package/src/rules/no-unchecked-loop-condition/index.js +3 -99
- package/src/rules/no-unlimited-resource-allocation/index.js +2 -75
- package/src/rules/no-unsafe-deserialization/index.js +1 -111
- package/src/rules/no-unsafe-regex-construction/index.js +22 -62
- package/src/rules/no-weak-password-recovery/index.js +0 -43
- package/src/rules/no-xpath-injection/index.js +13 -75
- package/src/rules/no-xxe-injection/index.js +1 -47
- package/src/rules/require-backend-authorization/index.js +0 -10
- package/src/rules/require-secure-defaults/index.js +0 -10
- package/src/types/index.js +0 -5
- package/AGENTS.md +0 -131
- package/CHANGELOG.md +0 -379
- package/src/rules/detect-non-literal-regexp/index.d.ts +0 -28
- package/src/rules/detect-object-injection/index.d.ts +0 -35
- package/src/rules/detect-weak-password-validation/index.d.ts +0 -12
- package/src/rules/no-directive-injection/index.d.ts +0 -38
- package/src/rules/no-electron-security-issues/index.d.ts +0 -36
- package/src/rules/no-format-string-injection/index.d.ts +0 -44
- package/src/rules/no-graphql-injection/index.d.ts +0 -47
- package/src/rules/no-hardcoded-credentials/index.d.ts +0 -95
- package/src/rules/no-hardcoded-session-tokens/index.d.ts +0 -12
- package/src/rules/no-improper-sanitization/index.d.ts +0 -38
- package/src/rules/no-improper-type-validation/index.d.ts +0 -36
- package/src/rules/no-insecure-comparison/index.d.ts +0 -26
- package/src/rules/no-ldap-injection/index.d.ts +0 -53
- package/src/rules/no-missing-authentication/index.d.ts +0 -32
- package/src/rules/no-pii-in-logs/index.d.ts +0 -12
- package/src/rules/no-privilege-escalation/index.d.ts +0 -32
- package/src/rules/no-redos-vulnerable-regex/index.d.ts +0 -28
- package/src/rules/no-sensitive-data-exposure/index.d.ts +0 -30
- package/src/rules/no-template-injection/index.d.ts +0 -23
- package/src/rules/no-unchecked-loop-condition/index.d.ts +0 -38
- package/src/rules/no-unlimited-resource-allocation/index.d.ts +0 -38
- package/src/rules/no-unsafe-deserialization/index.d.ts +0 -40
- package/src/rules/no-unsafe-regex-construction/index.d.ts +0 -30
- package/src/rules/no-weak-password-recovery/index.d.ts +0 -38
- package/src/rules/no-xpath-injection/index.d.ts +0 -39
- package/src/rules/no-xxe-injection/index.d.ts +0 -36
- package/src/rules/require-backend-authorization/index.d.ts +0 -12
- package/src/rules/require-secure-defaults/index.d.ts +0 -12
|
@@ -1,14 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
-
* MIT license that can be found in the LICENSE file.
|
|
6
|
-
*/
|
|
7
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.noHardcodedSessionTokens = void 0;
|
|
9
|
-
/**
|
|
10
|
-
* @fileoverview Detect hardcoded session/JWT tokens
|
|
11
|
-
*/
|
|
12
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
13
5
|
exports.noHardcodedSessionTokens = (0, eslint_devkit_1.createRule)({
|
|
14
6
|
name: 'no-hardcoded-session-tokens',
|
|
@@ -42,16 +34,13 @@ exports.noHardcodedSessionTokens = (0, eslint_devkit_1.createRule)({
|
|
|
42
34
|
Literal(node) {
|
|
43
35
|
if (typeof node.value !== 'string')
|
|
44
36
|
return;
|
|
45
|
-
// Detect JWT tokens (start with eyJ and contain 2 dots)
|
|
46
37
|
if (node.value.startsWith('eyJ') && node.value.length > 50 &&
|
|
47
38
|
(node.value.match(/\./g) || []).length >= 2) {
|
|
48
39
|
report(node);
|
|
49
40
|
}
|
|
50
|
-
// Detect Bearer tokens
|
|
51
41
|
if (node.value.startsWith('Bearer ') && node.value.length > 20) {
|
|
52
42
|
report(node);
|
|
53
43
|
}
|
|
54
|
-
// Detect session_id patterns
|
|
55
44
|
const parent = node.parent;
|
|
56
45
|
if (parent?.type === 'VariableDeclarator' &&
|
|
57
46
|
parent.id.type === 'Identifier') {
|
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
-
* MIT license that can be found in the LICENSE file.
|
|
6
|
-
*/
|
|
7
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.noImproperSanitization = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
@@ -203,40 +198,27 @@ exports.noImproperSanitization = (0, eslint_devkit_1.createRule)({
|
|
|
203
198
|
const { safeSanitizers = ['DOMPurify.sanitize', 'he.encode', 'encodeURIComponent', 'encodeURI', 'escape'], dangerousChars = ['<', '>', '"', "'", '&', '`', '$', '{', '}', '|', ';', '(', ')'], trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
|
|
204
199
|
const sourceCode = context.sourceCode;
|
|
205
200
|
const filename = context.filename;
|
|
206
|
-
// Create safety checker for false positive detection
|
|
207
201
|
const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
|
|
208
202
|
trustedSanitizers,
|
|
209
203
|
trustedAnnotations,
|
|
210
204
|
trustedOrmPatterns: [],
|
|
211
205
|
strictMode,
|
|
212
206
|
});
|
|
213
|
-
/**
|
|
214
|
-
* Check if sanitization is safe
|
|
215
|
-
*/
|
|
216
207
|
const isSafeSanitizer = (callText) => {
|
|
217
208
|
return safeSanitizers.some(sanitizer => callText.includes(sanitizer));
|
|
218
209
|
};
|
|
219
|
-
/**
|
|
220
|
-
* Check if replace sanitization is incomplete
|
|
221
|
-
*/
|
|
222
210
|
const isIncompleteReplaceSanitization = (callExpression) => {
|
|
223
211
|
const callText = sourceCode.getText(callExpression);
|
|
224
|
-
// Check for incomplete HTML escaping (any quote style)
|
|
225
212
|
const escapesOnlyTags = /replace\(\s*\/[<>]/.test(callText);
|
|
226
213
|
if (escapesOnlyTags) {
|
|
227
|
-
// If only escaping < or > but not other dangerous chars, it's incomplete
|
|
228
214
|
const hasQuoteEscaping = /"|'|'/.test(callText);
|
|
229
215
|
const hasAmpersandEscaping = /&/.test(callText);
|
|
230
216
|
return !(hasQuoteEscaping && hasAmpersandEscaping);
|
|
231
217
|
}
|
|
232
218
|
return false;
|
|
233
219
|
};
|
|
234
|
-
/**
|
|
235
|
-
* Check if output context suggests needed encoding
|
|
236
|
-
*/
|
|
237
220
|
const needsContextEncoding = (outputNode) => {
|
|
238
221
|
let current = outputNode;
|
|
239
|
-
// Look for context clues in surrounding code
|
|
240
222
|
while (current) {
|
|
241
223
|
const text = sourceCode.getText(current).toLowerCase();
|
|
242
224
|
if (text.includes('innerhtml') || text.includes('outerhtml')) {
|
|
@@ -256,10 +238,8 @@ exports.noImproperSanitization = (0, eslint_devkit_1.createRule)({
|
|
|
256
238
|
return null;
|
|
257
239
|
};
|
|
258
240
|
return {
|
|
259
|
-
// Check call expressions for sanitization issues
|
|
260
241
|
CallExpression(node) {
|
|
261
242
|
const callee = node.callee;
|
|
262
|
-
// Check for replace() sanitization
|
|
263
243
|
if (callee.type === 'MemberExpression' &&
|
|
264
244
|
callee.property.type === 'Identifier' &&
|
|
265
245
|
callee.property.name === 'replace') {
|
|
@@ -277,16 +257,12 @@ exports.noImproperSanitization = (0, eslint_devkit_1.createRule)({
|
|
|
277
257
|
});
|
|
278
258
|
}
|
|
279
259
|
}
|
|
280
|
-
// Check for custom sanitizer functions
|
|
281
260
|
if (callee.type === 'Identifier') {
|
|
282
261
|
const functionName = callee.name;
|
|
283
|
-
// Check if it's a known dangerous sanitizer pattern
|
|
284
262
|
if (functionName.toLowerCase().includes('sanitize') ||
|
|
285
263
|
functionName.toLowerCase().includes('escape') ||
|
|
286
264
|
functionName.toLowerCase().includes('clean')) {
|
|
287
|
-
// If it's not in our safe list, flag it
|
|
288
265
|
if (!safeSanitizers.some(safe => functionName.includes(safe))) {
|
|
289
|
-
// Check if arguments contain user input
|
|
290
266
|
const args = node.arguments;
|
|
291
267
|
let hasUserInput = false;
|
|
292
268
|
for (const arg of args) {
|
|
@@ -315,21 +291,17 @@ exports.noImproperSanitization = (0, eslint_devkit_1.createRule)({
|
|
|
315
291
|
}
|
|
316
292
|
}
|
|
317
293
|
},
|
|
318
|
-
// Check assignments that might need sanitization
|
|
319
294
|
AssignmentExpression(node) {
|
|
320
295
|
const left = node.left;
|
|
321
296
|
const right = node.right;
|
|
322
|
-
// Check for assignments to potentially dangerous properties
|
|
323
297
|
if (left.type === 'MemberExpression' &&
|
|
324
298
|
left.property.type === 'Identifier') {
|
|
325
299
|
const propertyName = left.property.name.toLowerCase();
|
|
326
300
|
if (['innerhtml', 'outerhtml', 'innertext', 'textcontent'].includes(propertyName)) {
|
|
327
301
|
const encodingContext = needsContextEncoding(node);
|
|
328
302
|
if (encodingContext === 'html' && propertyName === 'innerhtml') {
|
|
329
|
-
// Check if right side is properly sanitized
|
|
330
303
|
const rightText = sourceCode.getText(right);
|
|
331
304
|
if (!isSafeSanitizer(rightText)) {
|
|
332
|
-
// Check if right side contains user input
|
|
333
305
|
const hasUserInput = rightText.includes('req.') ||
|
|
334
306
|
rightText.includes('body') ||
|
|
335
307
|
rightText.includes('query') ||
|
|
@@ -352,21 +324,10 @@ exports.noImproperSanitization = (0, eslint_devkit_1.createRule)({
|
|
|
352
324
|
}
|
|
353
325
|
}
|
|
354
326
|
},
|
|
355
|
-
// Check string literals that might contain dangerous characters
|
|
356
327
|
Literal(node) {
|
|
357
328
|
if (typeof node.value !== 'string') {
|
|
358
329
|
return;
|
|
359
330
|
}
|
|
360
|
-
// SAFE: literal assigned directly to innerHTML/outerHTML with no
|
|
361
|
-
// concatenation or interpolation AND containing no dangerous
|
|
362
|
-
// markup. Static developer-authored HTML normally has no taint
|
|
363
|
-
// source (`el.innerHTML = '<span class="x"></span>'` is the
|
|
364
|
-
// standard way to set static markup; flagging it would force
|
|
365
|
-
// every static UI template into the noise floor) — BUT a literal
|
|
366
|
-
// like `<script>alert(1)</script>` is unsafe even when
|
|
367
|
-
// developer-authored, because the markup itself is the vector.
|
|
368
|
-
// Only take the safe path when the literal is free of script
|
|
369
|
-
// tags, inline event handlers, and `javascript:` URIs.
|
|
370
331
|
const directParent = node.parent;
|
|
371
332
|
if (directParent?.type === 'AssignmentExpression' &&
|
|
372
333
|
directParent.right === node &&
|
|
@@ -382,11 +343,8 @@ exports.noImproperSanitization = (0, eslint_devkit_1.createRule)({
|
|
|
382
343
|
}
|
|
383
344
|
}
|
|
384
345
|
const text = node.value;
|
|
385
|
-
// Check if this string is used in a dangerous context
|
|
386
346
|
let current = node;
|
|
387
347
|
let isInDangerousContext = false;
|
|
388
|
-
// Every path that sets `isInDangerousContext = true` is followed by `break`,
|
|
389
|
-
// so the negation in the condition is dead (CodeQL: `js/useless-conditional`).
|
|
390
348
|
while (current) {
|
|
391
349
|
if (current.type === 'AssignmentExpression') {
|
|
392
350
|
const left = current.left;
|
|
@@ -402,7 +360,6 @@ exports.noImproperSanitization = (0, eslint_devkit_1.createRule)({
|
|
|
402
360
|
if (callee.type === 'MemberExpression' &&
|
|
403
361
|
callee.property.type === 'Identifier' &&
|
|
404
362
|
['write', 'send', 'json'].includes(callee.property.name)) {
|
|
405
|
-
// Could be response output
|
|
406
363
|
isInDangerousContext = true;
|
|
407
364
|
break;
|
|
408
365
|
}
|
|
@@ -410,7 +367,6 @@ exports.noImproperSanitization = (0, eslint_devkit_1.createRule)({
|
|
|
410
367
|
current = current.parent;
|
|
411
368
|
}
|
|
412
369
|
if (isInDangerousContext) {
|
|
413
|
-
// Check if string contains dangerous characters without proper escaping
|
|
414
370
|
const hasDangerousChars = dangerousChars.some(char => text.includes(char));
|
|
415
371
|
const hasEscaping = text.includes('<') || text.includes('>') ||
|
|
416
372
|
text.includes('"') || text.includes(''') ||
|
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
-
* MIT license that can be found in the LICENSE file.
|
|
6
|
-
*/
|
|
7
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.noImproperTypeValidation = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
@@ -188,34 +183,25 @@ exports.noImproperTypeValidation = (0, eslint_devkit_1.createRule)({
|
|
|
188
183
|
const { userInputVariables = ['req', 'request', 'body', 'query', 'params', 'input', 'data', 'userInput'], allowInstanceofSameRealm = true, trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
|
|
189
184
|
const sourceCode = context.sourceCode;
|
|
190
185
|
const filename = context.filename;
|
|
191
|
-
// Create safety checker for false positive detection
|
|
192
186
|
const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
|
|
193
187
|
trustedSanitizers,
|
|
194
188
|
trustedAnnotations,
|
|
195
189
|
trustedOrmPatterns: [],
|
|
196
190
|
strictMode,
|
|
197
191
|
});
|
|
198
|
-
/**
|
|
199
|
-
* Check if a variable is user input
|
|
200
|
-
*/
|
|
201
192
|
const isUserInput = (varName) => {
|
|
202
193
|
return userInputVariables.some(input => varName.includes(input));
|
|
203
194
|
};
|
|
204
|
-
/**
|
|
205
|
-
* Check if a typeof check is unsafe
|
|
206
|
-
*/
|
|
207
195
|
const isUnsafeTypeof = (node) => {
|
|
208
196
|
if (node.operator !== '===' && node.operator !== '!==') {
|
|
209
197
|
return false;
|
|
210
198
|
}
|
|
211
199
|
const left = node.left;
|
|
212
200
|
const right = node.right;
|
|
213
|
-
// Check for typeof x === 'object' (misses null)
|
|
214
201
|
if (left.type === 'UnaryExpression' &&
|
|
215
202
|
left.operator === 'typeof' &&
|
|
216
203
|
right.type === 'Literal' &&
|
|
217
204
|
right.value === 'object') {
|
|
218
|
-
// Check if this node is part of a larger expression that includes a null check
|
|
219
205
|
let varName = '';
|
|
220
206
|
if (left.argument.type === 'Identifier') {
|
|
221
207
|
varName = left.argument.name;
|
|
@@ -225,7 +211,6 @@ exports.noImproperTypeValidation = (0, eslint_devkit_1.createRule)({
|
|
|
225
211
|
let child = node;
|
|
226
212
|
while (current) {
|
|
227
213
|
if (current.type === 'LogicalExpression' && current.operator === '&&') {
|
|
228
|
-
// If we are on the right side, check the left side for null check
|
|
229
214
|
if (current.right === child) {
|
|
230
215
|
const leftText = sourceCode.getText(current.left);
|
|
231
216
|
if (leftText.includes(`${varName} !== null`) ||
|
|
@@ -245,9 +230,6 @@ exports.noImproperTypeValidation = (0, eslint_devkit_1.createRule)({
|
|
|
245
230
|
}
|
|
246
231
|
return false;
|
|
247
232
|
};
|
|
248
|
-
/**
|
|
249
|
-
* Check if instanceof usage is unsafe
|
|
250
|
-
*/
|
|
251
233
|
const isUnsafeInstanceof = (node) => {
|
|
252
234
|
if (node.operator !== 'instanceof') {
|
|
253
235
|
return false;
|
|
@@ -255,27 +237,17 @@ exports.noImproperTypeValidation = (0, eslint_devkit_1.createRule)({
|
|
|
255
237
|
if (!allowInstanceofSameRealm) {
|
|
256
238
|
return true;
|
|
257
239
|
}
|
|
258
|
-
// instanceof is generally safe within the same realm
|
|
259
|
-
// but can be problematic across different contexts/windows
|
|
260
240
|
return false;
|
|
261
241
|
};
|
|
262
|
-
/**
|
|
263
|
-
* Check if equality is loose and used for type checking
|
|
264
|
-
*/
|
|
265
242
|
const isLooseEqualityTypeCheck = (node) => {
|
|
266
243
|
if (node.operator !== '==' && node.operator !== '!=') {
|
|
267
244
|
return false;
|
|
268
245
|
}
|
|
269
246
|
const leftText = sourceCode.getText(node.left).toLowerCase();
|
|
270
247
|
const rightText = sourceCode.getText(node.right).toLowerCase();
|
|
271
|
-
// Check if comparing against null/undefined with loose equality
|
|
272
248
|
return (leftText.includes('null') || rightText.includes('null') ||
|
|
273
249
|
leftText.includes('undefined') || rightText.includes('undefined'));
|
|
274
250
|
};
|
|
275
|
-
/**
|
|
276
|
-
* Check for unreliable constructor checks
|
|
277
|
-
*/
|
|
278
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
279
251
|
const isUnreliableConstructorCheck = (node) => {
|
|
280
252
|
return node.property.type === 'Identifier' &&
|
|
281
253
|
node.property.name === 'name' &&
|
|
@@ -284,9 +256,7 @@ exports.noImproperTypeValidation = (0, eslint_devkit_1.createRule)({
|
|
|
284
256
|
node.object.property.name === 'constructor';
|
|
285
257
|
};
|
|
286
258
|
return {
|
|
287
|
-
// Check binary expressions for type validation issues
|
|
288
259
|
BinaryExpression(node) {
|
|
289
|
-
// Check for unsafe typeof usage
|
|
290
260
|
if (isUnsafeTypeof(node)) {
|
|
291
261
|
const left = node.left;
|
|
292
262
|
let matchesUserInput = false;
|
|
@@ -312,13 +282,12 @@ exports.noImproperTypeValidation = (0, eslint_devkit_1.createRule)({
|
|
|
312
282
|
suggest: [
|
|
313
283
|
{
|
|
314
284
|
messageId: 'useTypeofCorrectly',
|
|
315
|
-
fix: () => null
|
|
285
|
+
fix: () => null
|
|
316
286
|
},
|
|
317
287
|
],
|
|
318
288
|
});
|
|
319
289
|
}
|
|
320
290
|
}
|
|
321
|
-
// Check for unsafe instanceof usage
|
|
322
291
|
if (isUnsafeInstanceof(node)) {
|
|
323
292
|
const left = node.left;
|
|
324
293
|
if (left.type === 'Identifier' && isUserInput(left.name)) {
|
|
@@ -332,11 +301,9 @@ exports.noImproperTypeValidation = (0, eslint_devkit_1.createRule)({
|
|
|
332
301
|
});
|
|
333
302
|
}
|
|
334
303
|
}
|
|
335
|
-
// Check for loose equality in type checks
|
|
336
304
|
if (isLooseEqualityTypeCheck(node)) {
|
|
337
305
|
const left = node.left;
|
|
338
306
|
const right = node.right;
|
|
339
|
-
// Check if this involves user input variables
|
|
340
307
|
const leftText = sourceCode.getText(left);
|
|
341
308
|
const rightText = sourceCode.getText(right);
|
|
342
309
|
if ((left.type === 'Identifier' && isUserInput(left.name)) ||
|
|
@@ -356,24 +323,19 @@ exports.noImproperTypeValidation = (0, eslint_devkit_1.createRule)({
|
|
|
356
323
|
}
|
|
357
324
|
}
|
|
358
325
|
},
|
|
359
|
-
// Check member expressions for constructor.name usage
|
|
360
326
|
MemberExpression(node) {
|
|
361
327
|
if (isUnreliableConstructorCheck(node)) {
|
|
362
|
-
// Check if this involves user input
|
|
363
328
|
let current = node;
|
|
364
329
|
let involvesUserInput = false;
|
|
365
|
-
// Walk up to find if this is used with user input
|
|
366
330
|
while (current?.parent) {
|
|
367
331
|
current = current.parent;
|
|
368
332
|
if (current.type === 'VariableDeclarator' &&
|
|
369
333
|
current.init === node) {
|
|
370
|
-
// This is assignment - check if assigned to user input
|
|
371
334
|
involvesUserInput = true;
|
|
372
335
|
break;
|
|
373
336
|
}
|
|
374
337
|
if (current.type === 'BinaryExpression' &&
|
|
375
338
|
(current.left === node || current.right === node)) {
|
|
376
|
-
// Used in comparison - likely type checking
|
|
377
339
|
involvesUserInput = true;
|
|
378
340
|
break;
|
|
379
341
|
}
|
|
@@ -393,25 +355,18 @@ exports.noImproperTypeValidation = (0, eslint_devkit_1.createRule)({
|
|
|
393
355
|
}
|
|
394
356
|
}
|
|
395
357
|
},
|
|
396
|
-
// Check call expressions for type checking functions
|
|
397
358
|
CallExpression(node) {
|
|
398
359
|
const callee = node.callee;
|
|
399
|
-
// Check for safe type checking functions
|
|
400
360
|
if (callee.type === 'MemberExpression' &&
|
|
401
361
|
callee.property.type === 'Identifier') {
|
|
402
362
|
const methodName = callee.property.name;
|
|
403
363
|
const objectName = callee.object.type === 'Identifier' ? callee.object.name : '';
|
|
404
|
-
// Check for proper type guards
|
|
405
364
|
if (['isArray', 'isString', 'isNumber', 'isObject'].includes(methodName) &&
|
|
406
365
|
objectName && ['Array', 'Number', 'String', 'Object'].includes(objectName)) {
|
|
407
|
-
// This is good - using proper type guards
|
|
408
366
|
return;
|
|
409
367
|
}
|
|
410
368
|
}
|
|
411
|
-
// Check for typeof usage in function calls
|
|
412
369
|
if (callee.type === 'Identifier' && callee.name === 'typeof') {
|
|
413
|
-
// This is unusual - typeof is normally a unary operator
|
|
414
|
-
// But if used as a function call, it's likely wrong
|
|
415
370
|
context.report({
|
|
416
371
|
node,
|
|
417
372
|
messageId: 'improperTypeValidation',
|
|
@@ -424,10 +379,8 @@ exports.noImproperTypeValidation = (0, eslint_devkit_1.createRule)({
|
|
|
424
379
|
});
|
|
425
380
|
}
|
|
426
381
|
},
|
|
427
|
-
// Check if statements for incomplete type validation
|
|
428
382
|
IfStatement(node) {
|
|
429
383
|
const test = node.test;
|
|
430
|
-
// NEW: Check for implicit truthiness check on user input
|
|
431
384
|
if (test.type === 'Identifier' && isUserInput(test.name)) {
|
|
432
385
|
if (safetyChecker.isSafe(node, context)) {
|
|
433
386
|
return;
|
|
@@ -443,13 +396,10 @@ exports.noImproperTypeValidation = (0, eslint_devkit_1.createRule)({
|
|
|
443
396
|
}
|
|
444
397
|
});
|
|
445
398
|
}
|
|
446
|
-
// Look for if statements that only check one aspect of type
|
|
447
399
|
if (test.type === 'BinaryExpression') {
|
|
448
400
|
const testText = sourceCode.getText(test).toLowerCase();
|
|
449
|
-
// Check for incomplete null checks
|
|
450
401
|
if ((testText.includes('!= null') || testText.includes('== null')) &&
|
|
451
402
|
!testText.includes('!== undefined') && !testText.includes('=== undefined')) {
|
|
452
|
-
// Check if this involves user input
|
|
453
403
|
if ((test.left.type === 'Identifier' && isUserInput(test.left.name)) ||
|
|
454
404
|
(test.right.type === 'Identifier' && isUserInput(test.right.name))) {
|
|
455
405
|
if (safetyChecker.isSafe(node, context)) {
|
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
-
* MIT license that can be found in the LICENSE file.
|
|
6
|
-
*/
|
|
7
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.noInsecureComparison = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
@@ -81,10 +76,6 @@ exports.noInsecureComparison = (0, eslint_devkit_2.createRule)({
|
|
|
81
76
|
const filename = context.filename;
|
|
82
77
|
const isTestFile = allowInTests && /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
83
78
|
const sourceCode = context.sourceCode;
|
|
84
|
-
// Codemods and AST-walker tools legitimately compare AST identifiers
|
|
85
|
-
// (`node.key === 'foo'`, `node.name === 'bar'`) — those keys aren't
|
|
86
|
-
// secrets, they're AST property names that happen to share the word
|
|
87
|
-
// "key". Detect codemod context once per file.
|
|
88
79
|
const AST_TOOL_PACKAGES = [
|
|
89
80
|
'@babel/types', '@babel/traverse', '@babel/generator', '@babel/parser',
|
|
90
81
|
'recast', 'jscodeshift', 'estree-walker', 'unist-util-visit',
|
|
@@ -96,7 +87,6 @@ exports.noInsecureComparison = (0, eslint_devkit_2.createRule)({
|
|
|
96
87
|
return true;
|
|
97
88
|
if (/codemod\.[mc]?[jt]sx?$/i.test(filename))
|
|
98
89
|
return true;
|
|
99
|
-
// Look for AST-tool imports at the top of the file
|
|
100
90
|
const program = sourceCode.ast;
|
|
101
91
|
for (const stmt of program.body) {
|
|
102
92
|
if (stmt.type === 'ImportDeclaration') {
|
|
@@ -108,10 +98,6 @@ exports.noInsecureComparison = (0, eslint_devkit_2.createRule)({
|
|
|
108
98
|
}
|
|
109
99
|
return false;
|
|
110
100
|
})();
|
|
111
|
-
/**
|
|
112
|
-
* Check if a string matches any ignore pattern
|
|
113
|
-
*/
|
|
114
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
115
101
|
function matchesIgnorePattern(text, patterns) {
|
|
116
102
|
return patterns.some(pattern => {
|
|
117
103
|
try {
|
|
@@ -119,40 +105,23 @@ exports.noInsecureComparison = (0, eslint_devkit_2.createRule)({
|
|
|
119
105
|
return regex.test(text);
|
|
120
106
|
}
|
|
121
107
|
catch {
|
|
122
|
-
// Invalid regex - treat as literal string match
|
|
123
108
|
return text.toLowerCase().includes(pattern.toLowerCase());
|
|
124
109
|
}
|
|
125
110
|
});
|
|
126
111
|
}
|
|
127
|
-
/**
|
|
128
|
-
* Check BinaryExpression for insecure comparison operators
|
|
129
|
-
*/
|
|
130
112
|
function checkBinaryExpression(node) {
|
|
131
113
|
if (isTestFile) {
|
|
132
114
|
return;
|
|
133
115
|
}
|
|
134
|
-
// Skip codemod / AST-walker contexts — `node.key === '...'` style
|
|
135
|
-
// comparisons there are AST identifier checks, not secret comparisons.
|
|
136
116
|
if (isCodemodFile) {
|
|
137
117
|
return;
|
|
138
118
|
}
|
|
139
|
-
// Word-level, not substring-level. The bare keywords `key`, `auth` and
|
|
140
|
-
// `mac` used to be matched as substrings of the whole expression's source
|
|
141
|
-
// text, which made `if (key === "__non_webpack_require__")` a "timing
|
|
142
|
-
// attack" and swept in `monkey`, `keyword`, `machine`, `author`. A secret
|
|
143
|
-
// is named by a *word*, so match words.
|
|
144
119
|
const secretKeywords = new Set([
|
|
145
120
|
'secret', 'secrets', 'token', 'tokens', 'password', 'passwd', 'pwd',
|
|
146
121
|
'apikey', 'api_key', 'secretkey', 'secret_key', 'privatekey', 'private_key',
|
|
147
122
|
'signature', 'hmac', 'digest', 'checksum', 'nonce', 'otp',
|
|
148
123
|
'passwordhash', 'password_hash', 'hashedpassword', 'hashed_password',
|
|
149
124
|
]);
|
|
150
|
-
/**
|
|
151
|
-
* Split an identifier-ish name into lowercase word segments:
|
|
152
|
-
* `expectedPassword` → ['expected', 'password'], `api_key` → ['api','key'].
|
|
153
|
-
* The joined form is kept too so `apiKey` also matches the `apikey` entry.
|
|
154
|
-
*/
|
|
155
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
156
125
|
const nameSegments = (name) => {
|
|
157
126
|
const parts = name
|
|
158
127
|
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
|
|
@@ -161,8 +130,6 @@ exports.noInsecureComparison = (0, eslint_devkit_2.createRule)({
|
|
|
161
130
|
.map((p) => p.toLowerCase());
|
|
162
131
|
return [...parts, parts.join(''), parts.join('_'), name.toLowerCase()];
|
|
163
132
|
};
|
|
164
|
-
/** Every identifier / property name appearing inside an expression. */
|
|
165
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
166
133
|
const namesIn = (expr) => {
|
|
167
134
|
const out = [];
|
|
168
135
|
const walk = (n) => {
|
|
@@ -206,28 +173,24 @@ exports.noInsecureComparison = (0, eslint_devkit_2.createRule)({
|
|
|
206
173
|
const segments = namesIn(expr).flatMap(nameSegments);
|
|
207
174
|
if (segments.some(segment => secretKeywords.has(segment)))
|
|
208
175
|
return true;
|
|
209
|
-
// In security contexts, treat generic terms as potential secrets
|
|
210
176
|
if (isSecurityContext) {
|
|
211
177
|
const contextKeywords = new Set(['provided', 'expected', 'actual', 'input', 'value', 'data']);
|
|
212
178
|
return segments.some(segment => contextKeywords.has(segment));
|
|
213
179
|
}
|
|
214
180
|
return false;
|
|
215
181
|
};
|
|
216
|
-
// Timing-safe comparison for secrets even with strict equality
|
|
217
182
|
if ((node.operator === '===' || node.operator === '!==') &&
|
|
218
183
|
(isPotentialSecret(node.left) || isPotentialSecret(node.right))) {
|
|
219
|
-
// SKIP: Length comparisons are safe - they're actually required before timingSafeEqual
|
|
220
184
|
const isLengthComparison = (expr) => {
|
|
221
185
|
return expr.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
222
186
|
expr.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
223
187
|
expr.property.name === 'length';
|
|
224
188
|
};
|
|
225
189
|
if (isLengthComparison(node.left) || isLengthComparison(node.right)) {
|
|
226
|
-
return;
|
|
190
|
+
return;
|
|
227
191
|
}
|
|
228
192
|
const leftText = sourceCode.getText(node.left);
|
|
229
193
|
const rightText = sourceCode.getText(node.right);
|
|
230
|
-
// ... rest of logic uses example ...
|
|
231
194
|
const example = `crypto.timingSafeEqual(Buffer.from(${leftText}), Buffer.from(${rightText}))`;
|
|
232
195
|
context.report({
|
|
233
196
|
node,
|
|
@@ -239,19 +202,15 @@ exports.noInsecureComparison = (0, eslint_devkit_2.createRule)({
|
|
|
239
202
|
},
|
|
240
203
|
suggest: [
|
|
241
204
|
{
|
|
242
|
-
messageId: 'useStrictEquality',
|
|
243
|
-
// Wait, previous code used useStrictEquality as suggest?
|
|
244
|
-
// Ah, the previous code had a fix/suggest structure.
|
|
205
|
+
messageId: 'useStrictEquality',
|
|
245
206
|
fix: (fixer) => fixer.replaceText(node, example),
|
|
246
207
|
},
|
|
247
208
|
],
|
|
248
209
|
});
|
|
249
210
|
return;
|
|
250
211
|
}
|
|
251
|
-
// Check for insecure comparison operators
|
|
252
212
|
if (node.operator === '==' || node.operator === '!=') {
|
|
253
213
|
const text = sourceCode.getText(node);
|
|
254
|
-
// Check if it matches any ignore pattern
|
|
255
214
|
if (matchesIgnorePattern(text, ignorePatterns)) {
|
|
256
215
|
return;
|
|
257
216
|
}
|