eslint-plugin-secure-coding 3.3.4 → 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.
Files changed (65) hide show
  1. package/README.md +4 -4
  2. package/package.json +30 -37
  3. package/src/index.d.ts +17 -23
  4. package/src/index.js +2 -113
  5. package/src/oxlint.d.ts +17 -20
  6. package/src/oxlint.js +0 -18
  7. package/src/rules/detect-non-literal-regexp/index.js +5 -79
  8. package/src/rules/detect-object-injection/index.js +2 -221
  9. package/src/rules/detect-weak-password-validation/index.js +0 -12
  10. package/src/rules/no-directive-injection/index.js +0 -33
  11. package/src/rules/no-electron-security-issues/index.js +0 -41
  12. package/src/rules/no-format-string-injection/index.js +0 -82
  13. package/src/rules/no-graphql-injection/index.js +86 -151
  14. package/src/rules/no-hardcoded-credentials/index.js +142 -184
  15. package/src/rules/no-hardcoded-session-tokens/index.js +0 -11
  16. package/src/rules/no-improper-sanitization/index.js +0 -44
  17. package/src/rules/no-improper-type-validation/index.js +1 -51
  18. package/src/rules/no-insecure-comparison/index.js +39 -35
  19. package/src/rules/no-ldap-injection/index.js +8 -61
  20. package/src/rules/no-missing-authentication/index.js +2 -56
  21. package/src/rules/no-pii-in-logs/index.js +0 -13
  22. package/src/rules/no-privilege-escalation/index.js +2 -49
  23. package/src/rules/no-redos-vulnerable-regex/index.js +3 -49
  24. package/src/rules/no-sensitive-data-exposure/index.js +5 -29
  25. package/src/rules/no-template-injection/index.js +0 -6
  26. package/src/rules/no-unchecked-loop-condition/index.js +3 -99
  27. package/src/rules/no-unlimited-resource-allocation/index.js +2 -75
  28. package/src/rules/no-unsafe-deserialization/index.js +55 -66
  29. package/src/rules/no-unsafe-regex-construction/index.js +22 -62
  30. package/src/rules/no-weak-password-recovery/index.js +0 -43
  31. package/src/rules/no-xpath-injection/index.js +13 -75
  32. package/src/rules/no-xxe-injection/index.js +1 -47
  33. package/src/rules/require-backend-authorization/index.js +0 -10
  34. package/src/rules/require-secure-defaults/index.js +0 -10
  35. package/src/types/index.js +0 -5
  36. package/AGENTS.md +0 -131
  37. package/CHANGELOG.md +0 -311
  38. package/src/rules/detect-non-literal-regexp/index.d.ts +0 -28
  39. package/src/rules/detect-object-injection/index.d.ts +0 -35
  40. package/src/rules/detect-weak-password-validation/index.d.ts +0 -12
  41. package/src/rules/no-directive-injection/index.d.ts +0 -38
  42. package/src/rules/no-electron-security-issues/index.d.ts +0 -36
  43. package/src/rules/no-format-string-injection/index.d.ts +0 -44
  44. package/src/rules/no-graphql-injection/index.d.ts +0 -47
  45. package/src/rules/no-hardcoded-credentials/index.d.ts +0 -44
  46. package/src/rules/no-hardcoded-session-tokens/index.d.ts +0 -12
  47. package/src/rules/no-improper-sanitization/index.d.ts +0 -38
  48. package/src/rules/no-improper-type-validation/index.d.ts +0 -36
  49. package/src/rules/no-insecure-comparison/index.d.ts +0 -26
  50. package/src/rules/no-ldap-injection/index.d.ts +0 -53
  51. package/src/rules/no-missing-authentication/index.d.ts +0 -32
  52. package/src/rules/no-pii-in-logs/index.d.ts +0 -12
  53. package/src/rules/no-privilege-escalation/index.d.ts +0 -32
  54. package/src/rules/no-redos-vulnerable-regex/index.d.ts +0 -28
  55. package/src/rules/no-sensitive-data-exposure/index.d.ts +0 -30
  56. package/src/rules/no-template-injection/index.d.ts +0 -23
  57. package/src/rules/no-unchecked-loop-condition/index.d.ts +0 -38
  58. package/src/rules/no-unlimited-resource-allocation/index.d.ts +0 -38
  59. package/src/rules/no-unsafe-deserialization/index.d.ts +0 -40
  60. package/src/rules/no-unsafe-regex-construction/index.d.ts +0 -30
  61. package/src/rules/no-weak-password-recovery/index.d.ts +0 -38
  62. package/src/rules/no-xpath-injection/index.d.ts +0 -39
  63. package/src/rules/no-xxe-injection/index.d.ts +0 -36
  64. package/src/rules/require-backend-authorization/index.d.ts +0 -12
  65. package/src/rules/require-secure-defaults/index.d.ts +0 -12
@@ -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.noDirectiveInjection = void 0;
9
4
  const eslint_devkit_1 = require("@interlace/eslint-devkit");
@@ -202,30 +197,23 @@ exports.noDirectiveInjection = (0, eslint_devkit_1.createRule)({
202
197
  const { userInputVariables = ['req', 'request', 'body', 'query', 'params', 'input', 'data', 'userInput'], trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
203
198
  const sourceCode = context.sourceCode;
204
199
  const filename = context.filename;
205
- // Create safety checker for false positive detection
206
200
  const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
207
201
  trustedSanitizers,
208
202
  trustedAnnotations,
209
203
  trustedOrmPatterns: [],
210
204
  strictMode,
211
205
  });
212
- /**
213
- * Check if a variable contains user input
214
- */
215
206
  const isUserInput = (varName) => {
216
207
  return userInputVariables.some(input => varName.includes(input)) ||
217
208
  varName.startsWith('user');
218
209
  };
219
210
  return {
220
- // Check JSX attributes for directive injection
221
211
  JSXAttribute(node) {
222
212
  const attrName = node.name;
223
213
  const attrValue = node.value;
224
- // Check for dangerouslySetInnerHTML
225
214
  if (attrName.type === 'JSXIdentifier' && attrName.name === 'dangerouslySetInnerHTML') {
226
215
  if (attrValue && attrValue.type === 'JSXExpressionContainer') {
227
216
  const expression = attrValue.expression;
228
- // Check if the expression contains user input
229
217
  const expressionText = sourceCode.getText(expression);
230
218
  if (userInputVariables.some(input => expressionText.includes(input))) {
231
219
  if (safetyChecker.isSafe(node, context)) {
@@ -242,14 +230,11 @@ exports.noDirectiveInjection = (0, eslint_devkit_1.createRule)({
242
230
  }
243
231
  }
244
232
  }
245
- // Check for dynamic directive names (Angular/Vue style)
246
- // Check for dynamic directive names (Angular/Vue style)
247
233
  const isNamespaceDirective = ((attrName.type === 'JSXNamespacedName' && (attrName.namespace.name === 'v' || attrName.namespace.name === 'ng')) ||
248
234
  (attrName.type === 'JSXIdentifier' && (attrName.name.startsWith('ng:') || attrName.name.startsWith('v:'))));
249
235
  if (isNamespaceDirective && attrValue) {
250
236
  if (attrValue.type === 'JSXExpressionContainer') {
251
237
  const expression = attrValue.expression;
252
- // Check if directive value comes from user input
253
238
  if (expression.type === 'Identifier' && isUserInput(expression.name)) {
254
239
  if (safetyChecker.isSafe(node, context)) {
255
240
  return;
@@ -267,10 +252,8 @@ exports.noDirectiveInjection = (0, eslint_devkit_1.createRule)({
267
252
  }
268
253
  }
269
254
  }
270
- // Check for dynamic component binding (React/Angular)
271
255
  if (attrName.type === 'JSXIdentifier') {
272
256
  const attrNameStr = attrName.name;
273
- // Check for is="" (dynamic component in Vue/Angular)
274
257
  if (attrNameStr === 'is' && attrValue) {
275
258
  if (attrValue.type === 'JSXExpressionContainer') {
276
259
  const expression = attrValue.expression;
@@ -291,15 +274,12 @@ exports.noDirectiveInjection = (0, eslint_devkit_1.createRule)({
291
274
  }
292
275
  }
293
276
  },
294
- // Check for innerHTML assignments
295
277
  AssignmentExpression(node) {
296
278
  const left = node.left;
297
279
  const right = node.right;
298
- // Check for element.innerHTML = userInput
299
280
  if (left.type === 'MemberExpression' &&
300
281
  left.property.type === 'Identifier' &&
301
282
  left.property.name === 'innerHTML') {
302
- // Check if right side contains user input
303
283
  const rightText = sourceCode.getText(right);
304
284
  if (userInputVariables.some(input => rightText.includes(input))) {
305
285
  if (safetyChecker.isSafe(node, context)) {
@@ -316,15 +296,12 @@ exports.noDirectiveInjection = (0, eslint_devkit_1.createRule)({
316
296
  }
317
297
  }
318
298
  },
319
- // Check for template compilation with user input
320
299
  CallExpression(node) {
321
300
  const callee = node.callee;
322
- // Check for template compilation functions
323
301
  if (callee.type === 'MemberExpression' &&
324
302
  callee.property.type === 'Identifier') {
325
303
  const methodName = callee.property.name;
326
304
  const objectName = callee.object.type === 'Identifier' ? callee.object.name : '';
327
- // Template compilation functions
328
305
  if (['compile', 'template', '$compile', '$interpolate'].includes(methodName) ||
329
306
  (objectName === 'Handlebars' && methodName === 'compile') ||
330
307
  (objectName === '_' && methodName === 'template') ||
@@ -334,7 +311,6 @@ exports.noDirectiveInjection = (0, eslint_devkit_1.createRule)({
334
311
  const args = node.arguments;
335
312
  if (args.length > 0) {
336
313
  const templateArg = args[0];
337
- // Check if template comes from user input
338
314
  if (templateArg.type === 'Identifier' && isUserInput(templateArg.name)) {
339
315
  if (safetyChecker.isSafe(node, context)) {
340
316
  return;
@@ -350,7 +326,6 @@ exports.noDirectiveInjection = (0, eslint_devkit_1.createRule)({
350
326
  }
351
327
  }
352
328
  }
353
- // Check for Vue.js v-html directive
354
329
  if (objectName === 'Vue' && methodName === 'directive') {
355
330
  const args = node.arguments;
356
331
  if (args.length >= 2) {
@@ -371,7 +346,6 @@ exports.noDirectiveInjection = (0, eslint_devkit_1.createRule)({
371
346
  }
372
347
  }
373
348
  }
374
- // Check for Angular directive creation
375
349
  if (callee.type === 'Identifier' && callee.name === 'directive') {
376
350
  const args = node.arguments;
377
351
  if (args.length >= 2) {
@@ -392,16 +366,11 @@ exports.noDirectiveInjection = (0, eslint_devkit_1.createRule)({
392
366
  }
393
367
  }
394
368
  },
395
- // Check template literals for injection
396
369
  TemplateLiteral(node) {
397
- // Check if template literal is used dangerously
398
370
  let current = node;
399
371
  let isInDangerousContext = false;
400
- // Every assignment of `isInDangerousContext = true` is followed by `break`,
401
- // so the negation is dead (CodeQL: `js/useless-conditional`).
402
372
  while (current) {
403
373
  if (current.type === 'JSXExpressionContainer') {
404
- // Check if we are inside dangerouslySetInnerHTML attribute
405
374
  if (current.parent?.type === 'JSXAttribute' &&
406
375
  current.parent.name.type === 'JSXIdentifier' &&
407
376
  current.parent.name.name === 'dangerouslySetInnerHTML') {
@@ -410,7 +379,6 @@ exports.noDirectiveInjection = (0, eslint_devkit_1.createRule)({
410
379
  }
411
380
  }
412
381
  else if (current.type === 'AssignmentExpression') {
413
- // Check for innerHTML assignment
414
382
  const left = current.left;
415
383
  if (left.type === 'MemberExpression' &&
416
384
  left.property.type === 'Identifier' &&
@@ -422,7 +390,6 @@ exports.noDirectiveInjection = (0, eslint_devkit_1.createRule)({
422
390
  current = current.parent;
423
391
  }
424
392
  if (isInDangerousContext) {
425
- // Check if template contains user input
426
393
  const hasUserInput = node.expressions.some((expr) => (expr.type === 'Identifier' && isUserInput(expr.name)) ||
427
394
  (expr.type === 'MemberExpression' &&
428
395
  expr.object.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.noElectronSecurityIssues = void 0;
9
4
  const eslint_devkit_1 = require("@interlace/eslint-devkit");
@@ -205,31 +200,22 @@ exports.noElectronSecurityIssues = (0, eslint_devkit_1.createRule)({
205
200
  const options = context.options[0] || {};
206
201
  const { allowedIpcChannels = [], trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
207
202
  const filename = context.filename;
208
- // Create safety checker for false positive detection
209
203
  const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
210
204
  trustedSanitizers,
211
205
  trustedAnnotations,
212
206
  trustedOrmPatterns: [],
213
207
  strictMode,
214
208
  });
215
- /**
216
- * Check if this is an Electron BrowserWindow creation
217
- */
218
- // oxlint-disable-next-line consistent-function-scoping
219
209
  const isBrowserWindowCreation = (node) => {
220
210
  return node.callee.type === 'Identifier' &&
221
211
  node.callee.name === 'BrowserWindow';
222
212
  };
223
- /**
224
- * Check if BrowserWindow options contain insecure settings
225
- */
226
213
  const checkBrowserWindowOptions = (optionsNode) => {
227
214
  for (const prop of optionsNode.properties) {
228
215
  if (prop.type === 'Property' &&
229
216
  prop.key.type === 'Identifier') {
230
217
  const key = prop.key.name;
231
218
  const value = prop.value;
232
- // Check for insecure boolean options
233
219
  if (['nodeIntegration', 'contextIsolation', 'webSecurity', 'allowRunningInsecureContent', 'sandbox'].includes(key)) {
234
220
  if (value.type === 'Literal') {
235
221
  const isInsecure = ((key === 'nodeIntegration' && value.value === true) ||
@@ -238,7 +224,6 @@ exports.noElectronSecurityIssues = (0, eslint_devkit_1.createRule)({
238
224
  (key === 'allowRunningInsecureContent' && value.value === true) ||
239
225
  (key === 'sandbox' && value.value === false));
240
226
  if (isInsecure) {
241
- // FALSE POSITIVE REDUCTION
242
227
  if (safetyChecker.isSafe(prop, context)) {
243
228
  continue;
244
229
  }
@@ -261,10 +246,6 @@ exports.noElectronSecurityIssues = (0, eslint_devkit_1.createRule)({
261
246
  }
262
247
  }
263
248
  };
264
- /**
265
- * Check if this is an IPC call
266
- */
267
- // oxlint-disable-next-line consistent-function-scoping
268
249
  const isIpcCall = (node) => {
269
250
  const callee = node.callee;
270
251
  if (callee.type === 'MemberExpression' &&
@@ -275,19 +256,14 @@ exports.noElectronSecurityIssues = (0, eslint_devkit_1.createRule)({
275
256
  }
276
257
  return false;
277
258
  };
278
- /**
279
- * Check for unsafe IPC patterns
280
- */
281
259
  const checkIpcCall = (node) => {
282
260
  const args = node.arguments;
283
261
  if (args.length === 0) {
284
262
  return;
285
263
  }
286
- // Check channel name (first argument)
287
264
  const channelArg = args[0];
288
265
  if (channelArg.type === 'Literal' && typeof channelArg.value === 'string') {
289
266
  const channel = channelArg.value;
290
- // Check if channel is allowed
291
267
  if (allowedIpcChannels.length > 0 && !allowedIpcChannels.includes(channel)) {
292
268
  if (safetyChecker.isSafe(node, context)) {
293
269
  return;
@@ -303,9 +279,6 @@ exports.noElectronSecurityIssues = (0, eslint_devkit_1.createRule)({
303
279
  }
304
280
  }
305
281
  };
306
- /**
307
- * Check for direct Node.js API access in renderer-like files
308
- */
309
282
  const isRendererFile = () => {
310
283
  const fileName = filename.toLowerCase();
311
284
  return fileName.includes('renderer') ||
@@ -313,13 +286,8 @@ exports.noElectronSecurityIssues = (0, eslint_devkit_1.createRule)({
313
286
  fileName.includes('ui') ||
314
287
  fileName.includes('view');
315
288
  };
316
- /**
317
- * Check for Node.js API usage
318
- */
319
- // oxlint-disable-next-line consistent-function-scoping
320
289
  const isNodeApiCall = (node) => {
321
290
  const callee = node.callee;
322
- // Check for require('fs'), require('child_process'), etc.
323
291
  if (callee.type === 'Identifier' && callee.name === 'require') {
324
292
  const arg = node.arguments[0];
325
293
  if (arg?.type === 'Literal' && typeof arg.value === 'string') {
@@ -327,7 +295,6 @@ exports.noElectronSecurityIssues = (0, eslint_devkit_1.createRule)({
327
295
  return ['fs', 'child_process', 'os', 'path', 'crypto', 'http', 'https'].includes(moduleName);
328
296
  }
329
297
  }
330
- // Check for global Node.js objects
331
298
  if (callee.type === 'MemberExpression' &&
332
299
  callee.object.type === 'Identifier' &&
333
300
  ['process', 'global', '__dirname', '__filename'].includes(callee.object.name)) {
@@ -336,7 +303,6 @@ exports.noElectronSecurityIssues = (0, eslint_devkit_1.createRule)({
336
303
  return false;
337
304
  };
338
305
  return {
339
- // Check BrowserWindow creation
340
306
  NewExpression(node) {
341
307
  try {
342
308
  if (isBrowserWindowCreation(node)) {
@@ -350,13 +316,11 @@ exports.noElectronSecurityIssues = (0, eslint_devkit_1.createRule)({
350
316
  return;
351
317
  }
352
318
  },
353
- // Check IPC calls
354
319
  CallExpression(node) {
355
320
  try {
356
321
  if (isIpcCall(node)) {
357
322
  checkIpcCall(node);
358
323
  }
359
- // Check for Node.js API usage in renderer files
360
324
  if (isRendererFile() && isNodeApiCall(node)) {
361
325
  if (safetyChecker.isSafe(node, context)) {
362
326
  return;
@@ -372,20 +336,16 @@ exports.noElectronSecurityIssues = (0, eslint_devkit_1.createRule)({
372
336
  }
373
337
  }
374
338
  catch {
375
- // Skip problematic nodes to avoid rule crashes
376
339
  return;
377
340
  }
378
341
  },
379
- // Check for preload script issues
380
342
  AssignmentExpression(node) {
381
343
  try {
382
- // Look for preload script assignments
383
344
  if (node.left.type === 'MemberExpression' &&
384
345
  node.left.property.type === 'Identifier' &&
385
346
  node.left.property.name === 'preload') {
386
347
  if (node.right.type === 'Literal' && typeof node.right.value === 'string') {
387
348
  const preloadPath = node.right.value;
388
- // Check for potentially unsafe preload patterns
389
349
  if (preloadPath.includes('node_modules') ||
390
350
  preloadPath.includes('http') ||
391
351
  preloadPath.includes('remote')) {
@@ -408,7 +368,6 @@ exports.noElectronSecurityIssues = (0, eslint_devkit_1.createRule)({
408
368
  return;
409
369
  }
410
370
  },
411
- // Check for insecure webPreferences patterns
412
371
  Property(node) {
413
372
  try {
414
373
  if (node.key.type === 'Identifier' && node.key.name === 'webPreferences') {
@@ -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.noFormatStringInjection = void 0;
9
4
  const eslint_devkit_1 = require("@interlace/eslint-devkit");
@@ -143,17 +138,14 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
143
138
  const options = { ...defaultOptions, ...context.options[0] };
144
139
  const { formatSpecifiers, userInputVariables, trustedSanitizers, } = options;
145
140
  const filename = context.filename;
146
- // Create safety checker for false positive detection (simplified implementation)
147
141
  const safetyChecker = {
148
142
  isSafe: (safeNode, ruleCtx) => {
149
- // Check for JSDoc @safe-format annotation
150
143
  const comments = ruleCtx.sourceCode.getCommentsBefore(safeNode);
151
144
  for (const comment of comments) {
152
145
  if (comment.type === 'Block' && comment.value.includes('@safe-format')) {
153
146
  return true;
154
147
  }
155
148
  }
156
- // For CallExpression nodes, check if first argument is safe
157
149
  if (safeNode.type === 'CallExpression' && safeNode.arguments.length > 0) {
158
150
  const firstArg = safeNode.arguments[0];
159
151
  if (firstArg.type === 'Identifier' && validatedVariables.has(firstArg.name)) {
@@ -163,9 +155,6 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
163
155
  return false;
164
156
  }
165
157
  };
166
- /**
167
- * Check if a variable contains user input
168
- */
169
158
  const isUserInput = (varName) => {
170
159
  const lowerName = varName.toLowerCase();
171
160
  return userInputVariables.some(input => lowerName.includes(input.toLowerCase())) ||
@@ -178,15 +167,11 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
178
167
  lowerName.includes('userformat') ||
179
168
  lowerName.includes('uservar');
180
169
  };
181
- /**
182
- * Check if a node represents user input (including member expressions)
183
- */
184
170
  const isUserInputNode = (node) => {
185
171
  if (node.type === 'Identifier') {
186
172
  return isUserInput(node.name) || dangerousVariables.has(node.name);
187
173
  }
188
174
  if (node.type === 'MemberExpression') {
189
- // Check patterns like req.query.*, req.body.*, req.params.*, etc.
190
175
  if (node.object.type === 'MemberExpression' &&
191
176
  node.object.object.type === 'Identifier' &&
192
177
  node.object.object.name === 'req' &&
@@ -194,20 +179,14 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
194
179
  ['query', 'body', 'params', 'param'].includes(node.object.property.name)) {
195
180
  return true;
196
181
  }
197
- // Check patterns like req.*
198
182
  if (node.object.type === 'Identifier' && node.object.name === 'req') {
199
183
  return true;
200
184
  }
201
- // Check other user input patterns
202
185
  const fullName = getMemberExpressionName(node);
203
186
  return isUserInput(fullName);
204
187
  }
205
188
  return false;
206
189
  };
207
- /**
208
- * Get the full name of a member expression (e.g., req.query.format)
209
- */
210
- // oxlint-disable-next-line consistent-function-scoping
211
190
  const getMemberExpressionName = (node) => {
212
191
  if (node.object.type === 'Identifier') {
213
192
  if (node.property.type === 'Identifier') {
@@ -222,16 +201,9 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
222
201
  }
223
202
  return '';
224
203
  };
225
- /**
226
- * Check if a string contains format specifiers
227
- */
228
204
  const containsFormatSpecifiers = (text) => {
229
205
  return formatSpecifiers.some(specifier => text.includes(specifier));
230
206
  };
231
- /**
232
- * Check if a call expression uses format functions
233
- */
234
- // oxlint-disable-next-line consistent-function-scoping
235
207
  const isConsoleMethod = (node) => {
236
208
  const callee = node.callee;
237
209
  return callee.type === 'MemberExpression' &&
@@ -240,10 +212,8 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
240
212
  callee.property.type === 'Identifier' &&
241
213
  ['log', 'error', 'warn', 'info', 'debug'].includes(callee.property.name);
242
214
  };
243
- // oxlint-disable-next-line consistent-function-scoping
244
215
  const isFormatFunctionCall = (node) => {
245
216
  const callee = node.callee;
246
- // Check for util.format
247
217
  if (callee.type === 'MemberExpression' &&
248
218
  callee.object.type === 'Identifier' &&
249
219
  callee.object.name === 'util' &&
@@ -251,7 +221,6 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
251
221
  callee.property.name === 'format') {
252
222
  return true;
253
223
  }
254
- // Check for console methods
255
224
  if (callee.type === 'MemberExpression' &&
256
225
  callee.object.type === 'Identifier' &&
257
226
  callee.object.name === 'console' &&
@@ -259,21 +228,15 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
259
228
  ['log', 'error', 'warn', 'info', 'debug'].includes(callee.property.name)) {
260
229
  return true;
261
230
  }
262
- // Check for sprintf/printf functions
263
231
  if (callee.type === 'Identifier' &&
264
232
  ['sprintf', 'printf', 'vsprintf'].includes(callee.name)) {
265
233
  return true;
266
234
  }
267
235
  return false;
268
236
  };
269
- // Track variables that have been validated/sanitized
270
237
  const validatedVariables = new Set();
271
238
  const dangerousVariables = new Set();
272
- /**
273
- * Check if input has been validated/sanitized
274
- */
275
239
  const isInputValidated = (inputNode) => {
276
- // Check if this is a validated variable
277
240
  if (inputNode.type === 'Identifier' && validatedVariables.has(inputNode.name)) {
278
241
  return true;
279
242
  }
@@ -288,16 +251,12 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
288
251
  }
289
252
  return false;
290
253
  };
291
- /**
292
- * Check if string literal contains format specifiers
293
- */
294
254
  const hasFormatSpecifiers = (node) => {
295
255
  if (typeof node.value !== 'string') {
296
256
  return false;
297
257
  }
298
258
  return containsFormatSpecifiers(node.value);
299
259
  };
300
- // Helper functions for expression analysis
301
260
  function containsFormatSpecifiersInExpression(expr) {
302
261
  if (expr.left.type === 'Literal' && typeof expr.left.value === 'string' && containsFormatSpecifiers(expr.left.value)) {
303
262
  return true;
@@ -329,7 +288,6 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
329
288
  return false;
330
289
  }
331
290
  return {
332
- // Check call expressions for format function usage
333
291
  CallExpression: function (node) {
334
292
  if (!isFormatFunctionCall(node)) {
335
293
  return;
@@ -338,10 +296,7 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
338
296
  if (args.length === 0) {
339
297
  return;
340
298
  }
341
- // For util.format and sprintf, first argument is the format string
342
299
  const formatArg = args[0];
343
- // Check if format string comes from user input
344
- // But skip console methods since they don't use the first arg as a format template
345
300
  const isFormatFromUserInput = isUserInputNode(formatArg) ||
346
301
  (formatArg.type === 'Identifier' && dangerousVariables.has(formatArg.name)) ||
347
302
  (formatArg.type === 'BinaryExpression' && hasUserInputInExpression(formatArg));
@@ -359,7 +314,6 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
359
314
  });
360
315
  return;
361
316
  }
362
- // Check if format string is a template literal or binary expression with user input
363
317
  if (formatArg.type === 'TemplateLiteral') {
364
318
  const hasUserInput = formatArg.expressions.some((expr) => isUserInputNode(expr));
365
319
  if (hasUserInput) {
@@ -398,15 +352,12 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
398
352
  return;
399
353
  }
400
354
  }
401
- // Check for format specifiers in subsequent arguments (could indicate user input in format position)
402
- // Only check if the format string itself is not validated/safe
403
355
  const fmtArg = args[0];
404
356
  const isFormatSafe = isInputValidated(fmtArg) ||
405
357
  (fmtArg.type === 'Identifier' && validatedVariables.has(fmtArg.name));
406
358
  if (!isFormatSafe) {
407
359
  let hasUserInputInArgs = false;
408
360
  let hasSpecifiersInFormat = false;
409
- // Check if any argument contains user input (skip first arg which is format string)
410
361
  for (let i = 1; i < args.length; i++) {
411
362
  const arg = args[i];
412
363
  if (isUserInputNode(arg) && !isInputValidated(arg)) {
@@ -414,7 +365,6 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
414
365
  break;
415
366
  }
416
367
  }
417
- // Check if any argument (potential format string) contains specifiers
418
368
  const firstArg = args[0];
419
369
  if (firstArg.type === 'Literal' && typeof firstArg.value === 'string') {
420
370
  if (containsFormatSpecifiers(firstArg.value)) {
@@ -422,22 +372,17 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
422
372
  }
423
373
  }
424
374
  else if (firstArg.type === 'Identifier') {
425
- // Check if the identifier name suggests it contains format specifiers
426
375
  const varName = firstArg.name.toLowerCase();
427
376
  if (varName.includes('format') || varName.includes('template') || varName.includes('pattern')) {
428
377
  hasSpecifiersInFormat = true;
429
378
  }
430
379
  }
431
- // Special case: For console.log/console.error with single argument, don't flag
432
- // console.log(userMessage) is equivalent to console.log("%s", userMessage) but is generally safe
433
380
  if (!hasSpecifiersInFormat && args.length === 2 && isConsoleMethod(node)) {
434
- // Don't report
435
381
  }
436
382
  else if (hasSpecifiersInFormat && hasUserInputInArgs) {
437
383
  if (safetyChecker.isSafe(node, context)) {
438
384
  return;
439
385
  }
440
- // Choose message ID based on format string type
441
386
  const messageId = firstArg.type === 'Literal' ? 'unsafeFormatSpecifier' : 'missingFormatValidation';
442
387
  context.report({
443
388
  node: node,
@@ -459,25 +404,17 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
459
404
  }
460
405
  }
461
406
  },
462
- // Check string literals for format specifiers with user input context
463
407
  Literal: function (node) {
464
408
  if (!hasFormatSpecifiers(node)) {
465
409
  return;
466
410
  }
467
- // Only check literals that are dynamically constructed or come from user input
468
- // Hardcoded string literals with format specifiers are safe when used properly
469
411
  const text = node.value;
470
- // Check if this literal is constructed from user input (e.g., concatenation)
471
412
  let current = node;
472
413
  let isFromUserInput = false;
473
- // Walk up to find if this literal is part of a concatenation or template with user input.
474
- // Every path that sets `isFromUserInput = true` is immediately followed by `break`,
475
- // so the negation in the loop condition is dead (CodeQL: `js/useless-conditional`).
476
414
  while (current) {
477
415
  if (current.type === 'BinaryExpression' &&
478
416
  current.operator === '+' &&
479
417
  (current.left === node || current.right === node)) {
480
- // Check if the other side contains user input
481
418
  const otherSide = current.left === node ? current.right : current.left;
482
419
  if (otherSide.type === 'Identifier' && isUserInput(otherSide.name)) {
483
420
  isFromUserInput = true;
@@ -487,8 +424,6 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
487
424
  if (current.type === 'VariableDeclarator' &&
488
425
  current.init === node.parent &&
489
426
  current.id.type === 'Identifier') {
490
- // console.log('DEBUG: Checking variable declarator', current.id.name);
491
- // Check if variable name suggests user input
492
427
  if (isUserInput(current.id.name)) {
493
428
  isFromUserInput = true;
494
429
  break;
@@ -496,19 +431,13 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
496
431
  }
497
432
  current = current.parent;
498
433
  }
499
- // Only flag if the literal is constructed from user input
500
434
  if (!isFromUserInput) {
501
435
  return;
502
436
  }
503
- // Check if this string is used in a context where it could be dangerous
504
437
  current = node;
505
438
  let isInDangerousContext = false;
506
- // Walk up to find if this is passed to a format function. Every path that sets
507
- // `isInDangerousContext = true` is followed by `break`, so the negation in the
508
- // condition is dead (CodeQL: `js/useless-conditional`).
509
439
  while (current) {
510
440
  if (current.type === 'CallExpression' && isFormatFunctionCall(current)) {
511
- // Check if this is the first argument (format string position)
512
441
  const args = current.arguments;
513
442
  if (args.length > 0 && args[0] === node) {
514
443
  isInDangerousContext = true;
@@ -531,9 +460,7 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
531
460
  });
532
461
  }
533
462
  },
534
- // Check template literals for format string injection
535
463
  TemplateLiteral: function (node) {
536
- // Check if template literal is used as format string
537
464
  let templateCurrent = node;
538
465
  let isFormatString = false;
539
466
  while (templateCurrent) {
@@ -547,14 +474,10 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
547
474
  templateCurrent = templateCurrent.parent;
548
475
  }
549
476
  if (isFormatString) {
550
- // Template literal used as format string - let CallExpression visitor handle this
551
- // to avoid duplicate reporting
552
477
  return;
553
478
  }
554
- // Check if template literal contains user input and is used dangerously
555
479
  const hasUserInput = node.expressions.some((expr) => isUserInputNode(expr));
556
480
  if (hasUserInput) {
557
- // Check if this template is assigned to a variable that could be used as format string
558
481
  let assignCurrent = node;
559
482
  let isAssignedToVariable = false;
560
483
  while (assignCurrent) {
@@ -581,19 +504,16 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
581
504
  }
582
505
  }
583
506
  },
584
- // Check variable assignments that might create format strings
585
507
  VariableDeclarator: function (node) {
586
508
  if (!node.init || node.id.type !== 'Identifier') {
587
509
  return;
588
510
  }
589
511
  const varName = node.id.name;
590
- // Track variables that are assigned the result of sanitization functions
591
512
  if (node.init.type === 'CallExpression' &&
592
513
  node.init.callee.type === 'Identifier' &&
593
514
  trustedSanitizers.includes(node.init.callee.name)) {
594
515
  validatedVariables.add(varName);
595
516
  }
596
- // Track variables that are assigned user input (dangerous)
597
517
  if (isUserInputNode(node.init) || (node.init.type === 'BinaryExpression' && hasUserInputInExpression(node.init))) {
598
518
  dangerousVariables.add(varName);
599
519
  }
@@ -601,7 +521,6 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
601
521
  if (!varNameLower.includes('format') && !varNameLower.includes('template') && !varNameLower.includes('fmt') && !varNameLower.includes('str')) {
602
522
  return;
603
523
  }
604
- // Check if assigned value contains format specifiers and user input
605
524
  if (node.init.type === 'TemplateLiteral') {
606
525
  const hasSpecifiers = node.init.quasis.some((quasi) => containsFormatSpecifiers(quasi.value.raw));
607
526
  const hasUserInput = node.init.expressions.some((expr) => isUserInputNode(expr));
@@ -621,7 +540,6 @@ exports.noFormatStringInjection = (0, eslint_devkit_1.createRule)({
621
540
  });
622
541
  }
623
542
  }
624
- // Check if assigned value is a string concatenation with user input
625
543
  if (node.init.type === 'BinaryExpression' && node.init.operator === '+') {
626
544
  const hasSpecifiers = containsFormatSpecifiersInExpression(node.init);
627
545
  const hasUserInput = hasUserInputInExpression(node.init);