eslint-plugin-secure-coding 1.0.0

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 (155) hide show
  1. package/AGENTS.md +196 -0
  2. package/CHANGELOG.md +105 -0
  3. package/LICENSE +23 -0
  4. package/README.md +377 -0
  5. package/package.json +80 -0
  6. package/src/index.d.ts +32 -0
  7. package/src/index.js +345 -0
  8. package/src/index.js.map +1 -0
  9. package/src/rules/security/database-injection.d.ts +13 -0
  10. package/src/rules/security/database-injection.js +407 -0
  11. package/src/rules/security/database-injection.js.map +1 -0
  12. package/src/rules/security/detect-child-process.d.ts +11 -0
  13. package/src/rules/security/detect-child-process.js +460 -0
  14. package/src/rules/security/detect-child-process.js.map +1 -0
  15. package/src/rules/security/detect-eval-with-expression.d.ts +9 -0
  16. package/src/rules/security/detect-eval-with-expression.js +393 -0
  17. package/src/rules/security/detect-eval-with-expression.js.map +1 -0
  18. package/src/rules/security/detect-non-literal-fs-filename.d.ts +7 -0
  19. package/src/rules/security/detect-non-literal-fs-filename.js +322 -0
  20. package/src/rules/security/detect-non-literal-fs-filename.js.map +1 -0
  21. package/src/rules/security/detect-non-literal-regexp.d.ts +9 -0
  22. package/src/rules/security/detect-non-literal-regexp.js +387 -0
  23. package/src/rules/security/detect-non-literal-regexp.js.map +1 -0
  24. package/src/rules/security/detect-object-injection.d.ts +11 -0
  25. package/src/rules/security/detect-object-injection.js +411 -0
  26. package/src/rules/security/detect-object-injection.js.map +1 -0
  27. package/src/rules/security/no-buffer-overread.d.ts +14 -0
  28. package/src/rules/security/no-buffer-overread.js +519 -0
  29. package/src/rules/security/no-buffer-overread.js.map +1 -0
  30. package/src/rules/security/no-clickjacking.d.ts +10 -0
  31. package/src/rules/security/no-clickjacking.js +381 -0
  32. package/src/rules/security/no-clickjacking.js.map +1 -0
  33. package/src/rules/security/no-directive-injection.d.ts +12 -0
  34. package/src/rules/security/no-directive-injection.js +446 -0
  35. package/src/rules/security/no-directive-injection.js.map +1 -0
  36. package/src/rules/security/no-document-cookie.d.ts +5 -0
  37. package/src/rules/security/no-document-cookie.js +90 -0
  38. package/src/rules/security/no-document-cookie.js.map +1 -0
  39. package/src/rules/security/no-electron-security-issues.d.ts +10 -0
  40. package/src/rules/security/no-electron-security-issues.js +421 -0
  41. package/src/rules/security/no-electron-security-issues.js.map +1 -0
  42. package/src/rules/security/no-exposed-sensitive-data.d.ts +11 -0
  43. package/src/rules/security/no-exposed-sensitive-data.js +341 -0
  44. package/src/rules/security/no-exposed-sensitive-data.js.map +1 -0
  45. package/src/rules/security/no-format-string-injection.d.ts +17 -0
  46. package/src/rules/security/no-format-string-injection.js +653 -0
  47. package/src/rules/security/no-format-string-injection.js.map +1 -0
  48. package/src/rules/security/no-graphql-injection.d.ts +12 -0
  49. package/src/rules/security/no-graphql-injection.js +410 -0
  50. package/src/rules/security/no-graphql-injection.js.map +1 -0
  51. package/src/rules/security/no-hardcoded-credentials.d.ts +26 -0
  52. package/src/rules/security/no-hardcoded-credentials.js +377 -0
  53. package/src/rules/security/no-hardcoded-credentials.js.map +1 -0
  54. package/src/rules/security/no-improper-sanitization.d.ts +12 -0
  55. package/src/rules/security/no-improper-sanitization.js +408 -0
  56. package/src/rules/security/no-improper-sanitization.js.map +1 -0
  57. package/src/rules/security/no-improper-type-validation.d.ts +10 -0
  58. package/src/rules/security/no-improper-type-validation.js +420 -0
  59. package/src/rules/security/no-improper-type-validation.js.map +1 -0
  60. package/src/rules/security/no-insecure-comparison.d.ts +7 -0
  61. package/src/rules/security/no-insecure-comparison.js +125 -0
  62. package/src/rules/security/no-insecure-comparison.js.map +1 -0
  63. package/src/rules/security/no-insecure-cookie-settings.d.ts +9 -0
  64. package/src/rules/security/no-insecure-cookie-settings.js +305 -0
  65. package/src/rules/security/no-insecure-cookie-settings.js.map +1 -0
  66. package/src/rules/security/no-insecure-jwt.d.ts +10 -0
  67. package/src/rules/security/no-insecure-jwt.js +338 -0
  68. package/src/rules/security/no-insecure-jwt.js.map +1 -0
  69. package/src/rules/security/no-insecure-redirects.d.ts +7 -0
  70. package/src/rules/security/no-insecure-redirects.js +215 -0
  71. package/src/rules/security/no-insecure-redirects.js.map +1 -0
  72. package/src/rules/security/no-insufficient-postmessage-validation.d.ts +14 -0
  73. package/src/rules/security/no-insufficient-postmessage-validation.js +390 -0
  74. package/src/rules/security/no-insufficient-postmessage-validation.js.map +1 -0
  75. package/src/rules/security/no-insufficient-random.d.ts +9 -0
  76. package/src/rules/security/no-insufficient-random.js +207 -0
  77. package/src/rules/security/no-insufficient-random.js.map +1 -0
  78. package/src/rules/security/no-ldap-injection.d.ts +10 -0
  79. package/src/rules/security/no-ldap-injection.js +449 -0
  80. package/src/rules/security/no-ldap-injection.js.map +1 -0
  81. package/src/rules/security/no-missing-authentication.d.ts +13 -0
  82. package/src/rules/security/no-missing-authentication.js +322 -0
  83. package/src/rules/security/no-missing-authentication.js.map +1 -0
  84. package/src/rules/security/no-missing-cors-check.d.ts +9 -0
  85. package/src/rules/security/no-missing-cors-check.js +449 -0
  86. package/src/rules/security/no-missing-cors-check.js.map +1 -0
  87. package/src/rules/security/no-missing-csrf-protection.d.ts +11 -0
  88. package/src/rules/security/no-missing-csrf-protection.js +183 -0
  89. package/src/rules/security/no-missing-csrf-protection.js.map +1 -0
  90. package/src/rules/security/no-missing-security-headers.d.ts +7 -0
  91. package/src/rules/security/no-missing-security-headers.js +217 -0
  92. package/src/rules/security/no-missing-security-headers.js.map +1 -0
  93. package/src/rules/security/no-privilege-escalation.d.ts +13 -0
  94. package/src/rules/security/no-privilege-escalation.js +321 -0
  95. package/src/rules/security/no-privilege-escalation.js.map +1 -0
  96. package/src/rules/security/no-redos-vulnerable-regex.d.ts +7 -0
  97. package/src/rules/security/no-redos-vulnerable-regex.js +307 -0
  98. package/src/rules/security/no-redos-vulnerable-regex.js.map +1 -0
  99. package/src/rules/security/no-sensitive-data-exposure.d.ts +11 -0
  100. package/src/rules/security/no-sensitive-data-exposure.js +251 -0
  101. package/src/rules/security/no-sensitive-data-exposure.js.map +1 -0
  102. package/src/rules/security/no-sql-injection.d.ts +10 -0
  103. package/src/rules/security/no-sql-injection.js +332 -0
  104. package/src/rules/security/no-sql-injection.js.map +1 -0
  105. package/src/rules/security/no-timing-attack.d.ts +10 -0
  106. package/src/rules/security/no-timing-attack.js +358 -0
  107. package/src/rules/security/no-timing-attack.js.map +1 -0
  108. package/src/rules/security/no-toctou-vulnerability.d.ts +7 -0
  109. package/src/rules/security/no-toctou-vulnerability.js +165 -0
  110. package/src/rules/security/no-toctou-vulnerability.js.map +1 -0
  111. package/src/rules/security/no-unchecked-loop-condition.d.ts +12 -0
  112. package/src/rules/security/no-unchecked-loop-condition.js +635 -0
  113. package/src/rules/security/no-unchecked-loop-condition.js.map +1 -0
  114. package/src/rules/security/no-unencrypted-transmission.d.ts +11 -0
  115. package/src/rules/security/no-unencrypted-transmission.js +237 -0
  116. package/src/rules/security/no-unencrypted-transmission.js.map +1 -0
  117. package/src/rules/security/no-unescaped-url-parameter.d.ts +9 -0
  118. package/src/rules/security/no-unescaped-url-parameter.js +266 -0
  119. package/src/rules/security/no-unescaped-url-parameter.js.map +1 -0
  120. package/src/rules/security/no-unlimited-resource-allocation.d.ts +12 -0
  121. package/src/rules/security/no-unlimited-resource-allocation.js +659 -0
  122. package/src/rules/security/no-unlimited-resource-allocation.js.map +1 -0
  123. package/src/rules/security/no-unsafe-deserialization.d.ts +10 -0
  124. package/src/rules/security/no-unsafe-deserialization.js +501 -0
  125. package/src/rules/security/no-unsafe-deserialization.js.map +1 -0
  126. package/src/rules/security/no-unsafe-dynamic-require.d.ts +5 -0
  127. package/src/rules/security/no-unsafe-dynamic-require.js +107 -0
  128. package/src/rules/security/no-unsafe-dynamic-require.js.map +1 -0
  129. package/src/rules/security/no-unsafe-regex-construction.d.ts +9 -0
  130. package/src/rules/security/no-unsafe-regex-construction.js +292 -0
  131. package/src/rules/security/no-unsafe-regex-construction.js.map +1 -0
  132. package/src/rules/security/no-unsanitized-html.d.ts +9 -0
  133. package/src/rules/security/no-unsanitized-html.js +347 -0
  134. package/src/rules/security/no-unsanitized-html.js.map +1 -0
  135. package/src/rules/security/no-unvalidated-user-input.d.ts +9 -0
  136. package/src/rules/security/no-unvalidated-user-input.js +418 -0
  137. package/src/rules/security/no-unvalidated-user-input.js.map +1 -0
  138. package/src/rules/security/no-weak-crypto.d.ts +11 -0
  139. package/src/rules/security/no-weak-crypto.js +350 -0
  140. package/src/rules/security/no-weak-crypto.js.map +1 -0
  141. package/src/rules/security/no-weak-password-recovery.d.ts +12 -0
  142. package/src/rules/security/no-weak-password-recovery.js +401 -0
  143. package/src/rules/security/no-weak-password-recovery.js.map +1 -0
  144. package/src/rules/security/no-xpath-injection.d.ts +10 -0
  145. package/src/rules/security/no-xpath-injection.js +487 -0
  146. package/src/rules/security/no-xpath-injection.js.map +1 -0
  147. package/src/rules/security/no-xxe-injection.d.ts +7 -0
  148. package/src/rules/security/no-xxe-injection.js +270 -0
  149. package/src/rules/security/no-xxe-injection.js.map +1 -0
  150. package/src/rules/security/no-zip-slip.d.ts +9 -0
  151. package/src/rules/security/no-zip-slip.js +446 -0
  152. package/src/rules/security/no-zip-slip.js.map +1 -0
  153. package/src/types/index.d.ts +131 -0
  154. package/src/types/index.js +18 -0
  155. package/src/types/index.js.map +1 -0
@@ -0,0 +1,420 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.noImproperTypeValidation = 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
+ exports.noImproperTypeValidation = (0, eslint_devkit_1.createRule)({
8
+ name: 'no-improper-type-validation',
9
+ meta: {
10
+ type: 'problem',
11
+ docs: {
12
+ description: 'Detects improper type validation in user input handling',
13
+ },
14
+ fixable: 'code',
15
+ hasSuggestions: true,
16
+ messages: {
17
+ improperTypeValidation: (0, eslint_devkit_2.formatLLMMessage)({
18
+ icon: eslint_devkit_2.MessageIcons.SECURITY,
19
+ issueName: 'Improper Type Validation',
20
+ cwe: 'CWE-1287',
21
+ description: 'Type validation may be bypassed or incomplete',
22
+ severity: '{{severity}}',
23
+ fix: '{{safeAlternative}}',
24
+ documentationLink: 'https://cwe.mitre.org/data/definitions/1287.html',
25
+ }),
26
+ unsafeTypeofCheck: (0, eslint_devkit_2.formatLLMMessage)({
27
+ icon: eslint_devkit_2.MessageIcons.WARNING,
28
+ issueName: 'Unsafe typeof Check',
29
+ cwe: 'CWE-1287',
30
+ description: 'typeof check misses null values',
31
+ severity: 'MEDIUM',
32
+ fix: 'Use value != null && typeof value === "object"',
33
+ documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof',
34
+ }),
35
+ unsafeInstanceofUsage: (0, eslint_devkit_2.formatLLMMessage)({
36
+ icon: eslint_devkit_2.MessageIcons.WARNING,
37
+ issueName: 'Unsafe instanceof Usage',
38
+ cwe: 'CWE-1287',
39
+ description: 'instanceof may fail across contexts',
40
+ severity: 'LOW',
41
+ fix: 'Use Array.isArray() or typeof checks',
42
+ documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof',
43
+ }),
44
+ looseEqualityTypeCheck: (0, eslint_devkit_2.formatLLMMessage)({
45
+ icon: eslint_devkit_2.MessageIcons.WARNING,
46
+ issueName: 'Loose Equality Type Check',
47
+ cwe: 'CWE-1287',
48
+ description: 'Loose equality may cause type confusion',
49
+ severity: 'LOW',
50
+ fix: 'Use strict equality (===) for type checking',
51
+ documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality',
52
+ }),
53
+ missingNullCheck: (0, eslint_devkit_2.formatLLMMessage)({
54
+ icon: eslint_devkit_2.MessageIcons.WARNING,
55
+ issueName: 'Missing Null Check',
56
+ cwe: 'CWE-1287',
57
+ description: 'Type check doesn\'t handle null values',
58
+ severity: 'MEDIUM',
59
+ fix: 'Check for both null and undefined',
60
+ documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof',
61
+ }),
62
+ unreliableConstructorCheck: (0, eslint_devkit_2.formatLLMMessage)({
63
+ icon: eslint_devkit_2.MessageIcons.WARNING,
64
+ issueName: 'Unreliable Constructor Check',
65
+ cwe: 'CWE-1287',
66
+ description: 'constructor.name can be spoofed',
67
+ severity: 'MEDIUM',
68
+ fix: 'Use Object.prototype.toString.call() or duck typing',
69
+ documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor',
70
+ }),
71
+ incompleteTypeValidation: (0, eslint_devkit_2.formatLLMMessage)({
72
+ icon: eslint_devkit_2.MessageIcons.WARNING,
73
+ issueName: 'Incomplete Type Validation',
74
+ cwe: 'CWE-1287',
75
+ description: 'Type validation is incomplete',
76
+ severity: 'LOW',
77
+ fix: 'Use comprehensive type checking',
78
+ documentationLink: 'https://cwe.mitre.org/data/definitions/1287.html',
79
+ }),
80
+ useTypeofCorrectly: (0, eslint_devkit_2.formatLLMMessage)({
81
+ icon: eslint_devkit_2.MessageIcons.INFO,
82
+ issueName: 'Use typeof Correctly',
83
+ description: 'Use typeof with null checks for objects',
84
+ severity: 'LOW',
85
+ fix: 'if (value != null && typeof value === "object")',
86
+ documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof',
87
+ }),
88
+ useProperTypeGuards: (0, eslint_devkit_2.formatLLMMessage)({
89
+ icon: eslint_devkit_2.MessageIcons.INFO,
90
+ issueName: 'Use Proper Type Guards',
91
+ description: 'Use proper type guard functions',
92
+ severity: 'LOW',
93
+ fix: 'Array.isArray(), Number.isNaN(), etc.',
94
+ documentationLink: 'https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types',
95
+ }),
96
+ validateUserInput: (0, eslint_devkit_2.formatLLMMessage)({
97
+ icon: eslint_devkit_2.MessageIcons.INFO,
98
+ issueName: 'Validate User Input',
99
+ description: 'Validate user input before processing',
100
+ severity: 'LOW',
101
+ fix: 'Use schema validation libraries',
102
+ documentationLink: 'https://joi.dev/',
103
+ }),
104
+ strategyTypeGuards: (0, eslint_devkit_2.formatLLMMessage)({
105
+ icon: eslint_devkit_2.MessageIcons.STRATEGY,
106
+ issueName: 'Type Guards Strategy',
107
+ description: 'Use type guards for runtime type checking',
108
+ severity: 'LOW',
109
+ fix: 'Implement custom type guard functions',
110
+ documentationLink: 'https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types',
111
+ }),
112
+ strategySchemaValidation: (0, eslint_devkit_2.formatLLMMessage)({
113
+ icon: eslint_devkit_2.MessageIcons.STRATEGY,
114
+ issueName: 'Schema Validation Strategy',
115
+ description: 'Use schema validation for complex inputs',
116
+ severity: 'LOW',
117
+ fix: 'Use Joi, Yup, or Zod for input validation',
118
+ documentationLink: 'https://joi.dev/',
119
+ }),
120
+ strategyDefensiveProgramming: (0, eslint_devkit_2.formatLLMMessage)({
121
+ icon: eslint_devkit_2.MessageIcons.STRATEGY,
122
+ issueName: 'Defensive Programming Strategy',
123
+ description: 'Use defensive programming techniques',
124
+ severity: 'LOW',
125
+ fix: 'Check types and handle edge cases explicitly',
126
+ documentationLink: 'https://en.wikipedia.org/wiki/Defensive_programming',
127
+ })
128
+ },
129
+ schema: [
130
+ {
131
+ type: 'object',
132
+ properties: {
133
+ userInputVariables: {
134
+ type: 'array',
135
+ items: { type: 'string' },
136
+ default: ['req', 'request', 'body', 'query', 'params', 'input', 'data', 'userInput'],
137
+ },
138
+ safeTypeCheckFunctions: {
139
+ type: 'array',
140
+ items: { type: 'string' },
141
+ default: ['isArray', 'isString', 'isNumber', 'isObject', 'validateType', 'checkType'],
142
+ },
143
+ allowInstanceofSameRealm: {
144
+ type: 'boolean',
145
+ default: true,
146
+ description: 'Allow instanceof for same-realm objects'
147
+ },
148
+ trustedSanitizers: {
149
+ type: 'array',
150
+ items: { type: 'string' },
151
+ default: [],
152
+ description: 'Additional function names to consider as type validators',
153
+ },
154
+ trustedAnnotations: {
155
+ type: 'array',
156
+ items: { type: 'string' },
157
+ default: [],
158
+ description: 'Additional JSDoc annotations to consider as safe markers',
159
+ },
160
+ strictMode: {
161
+ type: 'boolean',
162
+ default: false,
163
+ description: 'Disable all false positive detection (strict mode)',
164
+ },
165
+ },
166
+ additionalProperties: false,
167
+ },
168
+ ],
169
+ },
170
+ defaultOptions: [
171
+ {
172
+ userInputVariables: ['req', 'request', 'body', 'query', 'params', 'input', 'data', 'userInput'],
173
+ safeTypeCheckFunctions: ['isArray', 'isString', 'isNumber', 'isObject', 'validateType', 'checkType'],
174
+ allowInstanceofSameRealm: true,
175
+ trustedSanitizers: [],
176
+ trustedAnnotations: [],
177
+ strictMode: false,
178
+ },
179
+ ],
180
+ create(context) {
181
+ const options = context.options[0] || {};
182
+ const { userInputVariables = ['req', 'request', 'body', 'query', 'params', 'input', 'data', 'userInput'], allowInstanceofSameRealm = true, trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
183
+ const sourceCode = context.sourceCode || context.sourceCode;
184
+ const filename = context.filename || context.getFilename();
185
+ // Create safety checker for false positive detection
186
+ const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
187
+ trustedSanitizers,
188
+ trustedAnnotations,
189
+ trustedOrmPatterns: [],
190
+ strictMode,
191
+ });
192
+ /**
193
+ * Check if a variable is user input
194
+ */
195
+ const isUserInput = (varName) => {
196
+ return userInputVariables.some(input => varName.includes(input));
197
+ };
198
+ /**
199
+ * Check if a typeof check is unsafe
200
+ */
201
+ const isUnsafeTypeof = (node) => {
202
+ if (node.operator !== '===' && node.operator !== '!==') {
203
+ return false;
204
+ }
205
+ const left = node.left;
206
+ const right = node.right;
207
+ // Check for typeof x === 'object' (misses null)
208
+ if (left.type === 'UnaryExpression' &&
209
+ left.operator === 'typeof' &&
210
+ right.type === 'Literal' &&
211
+ right.value === 'object') {
212
+ return true;
213
+ }
214
+ return false;
215
+ };
216
+ /**
217
+ * Check if instanceof usage is unsafe
218
+ */
219
+ const isUnsafeInstanceof = (node) => {
220
+ if (node.operator !== 'instanceof') {
221
+ return false;
222
+ }
223
+ if (!allowInstanceofSameRealm) {
224
+ return true;
225
+ }
226
+ // instanceof is generally safe within the same realm
227
+ // but can be problematic across different contexts/windows
228
+ return false;
229
+ };
230
+ /**
231
+ * Check if equality is loose and used for type checking
232
+ */
233
+ const isLooseEqualityTypeCheck = (node) => {
234
+ if (node.operator !== '==' && node.operator !== '!=') {
235
+ return false;
236
+ }
237
+ const leftText = sourceCode.getText(node.left).toLowerCase();
238
+ const rightText = sourceCode.getText(node.right).toLowerCase();
239
+ // Check if comparing against null/undefined with loose equality
240
+ return (leftText.includes('null') || rightText.includes('null') ||
241
+ leftText.includes('undefined') || rightText.includes('undefined'));
242
+ };
243
+ /**
244
+ * Check for unreliable constructor checks
245
+ */
246
+ const isUnreliableConstructorCheck = (node) => {
247
+ return node.property.type === 'Identifier' &&
248
+ node.property.name === 'name' &&
249
+ node.object.type === 'MemberExpression' &&
250
+ node.object.property.type === 'Identifier' &&
251
+ node.object.property.name === 'constructor';
252
+ };
253
+ return {
254
+ // Check binary expressions for type validation issues
255
+ BinaryExpression(node) {
256
+ // Check for unsafe typeof usage
257
+ if (isUnsafeTypeof(node)) {
258
+ // Check if this involves user input
259
+ const left = node.left;
260
+ if (left.argument.type === 'Identifier' && isUserInput(left.argument.name)) {
261
+ // FALSE POSITIVE REDUCTION
262
+ if (safetyChecker.isSafe(node, context)) {
263
+ return;
264
+ }
265
+ context.report({
266
+ node,
267
+ messageId: 'unsafeTypeofCheck',
268
+ data: {
269
+ filePath: filename,
270
+ line: String(node.loc?.start.line ?? 0),
271
+ },
272
+ suggest: [
273
+ {
274
+ messageId: 'useTypeofCorrectly',
275
+ fix: () => null // Would need complex AST manipulation
276
+ },
277
+ ],
278
+ });
279
+ }
280
+ }
281
+ // Check for unsafe instanceof usage
282
+ if (isUnsafeInstanceof(node)) {
283
+ const left = node.left;
284
+ if (left.type === 'Identifier' && isUserInput(left.name)) {
285
+ context.report({
286
+ node,
287
+ messageId: 'unsafeInstanceofUsage',
288
+ data: {
289
+ filePath: filename,
290
+ line: String(node.loc?.start.line ?? 0),
291
+ },
292
+ });
293
+ }
294
+ }
295
+ // Check for loose equality in type checks
296
+ if (isLooseEqualityTypeCheck(node)) {
297
+ const left = node.left;
298
+ const right = node.right;
299
+ // Check if this involves user input variables
300
+ const leftText = sourceCode.getText(left);
301
+ const rightText = sourceCode.getText(right);
302
+ if ((left.type === 'Identifier' && isUserInput(left.name)) ||
303
+ (right.type === 'Identifier' && isUserInput(right.name)) ||
304
+ leftText.includes('null') || rightText.includes('null')) {
305
+ // FALSE POSITIVE REDUCTION
306
+ if (safetyChecker.isSafe(node, context)) {
307
+ return;
308
+ }
309
+ context.report({
310
+ node,
311
+ messageId: 'looseEqualityTypeCheck',
312
+ data: {
313
+ filePath: filename,
314
+ line: String(node.loc?.start.line ?? 0),
315
+ },
316
+ });
317
+ }
318
+ }
319
+ },
320
+ // Check member expressions for constructor.name usage
321
+ MemberExpression(node) {
322
+ if (isUnreliableConstructorCheck(node)) {
323
+ // Check if this involves user input
324
+ let current = node;
325
+ let involvesUserInput = false;
326
+ // Walk up to find if this is used with user input
327
+ while (current.parent) {
328
+ current = current.parent;
329
+ if (current.type === 'VariableDeclarator' &&
330
+ current.init === node) {
331
+ // This is assignment - check if assigned to user input
332
+ involvesUserInput = true;
333
+ break;
334
+ }
335
+ if (current.type === 'BinaryExpression' &&
336
+ (current.left === node || current.right === node)) {
337
+ // Used in comparison - likely type checking
338
+ involvesUserInput = true;
339
+ break;
340
+ }
341
+ }
342
+ if (involvesUserInput) {
343
+ // FALSE POSITIVE REDUCTION
344
+ if (safetyChecker.isSafe(node, context)) {
345
+ return;
346
+ }
347
+ context.report({
348
+ node,
349
+ messageId: 'unreliableConstructorCheck',
350
+ data: {
351
+ filePath: filename,
352
+ line: String(node.loc?.start.line ?? 0),
353
+ },
354
+ });
355
+ }
356
+ }
357
+ },
358
+ // Check call expressions for type checking functions
359
+ CallExpression(node) {
360
+ const callee = node.callee;
361
+ // Check for safe type checking functions
362
+ if (callee.type === 'MemberExpression' &&
363
+ callee.property.type === 'Identifier') {
364
+ const methodName = callee.property.name;
365
+ const objectName = callee.object.type === 'Identifier' ? callee.object.name : '';
366
+ // Check for proper type guards
367
+ if (['isArray', 'isString', 'isNumber', 'isObject'].includes(methodName) &&
368
+ objectName && ['Array', 'Number', 'String', 'Object'].includes(objectName)) {
369
+ // This is good - using proper type guards
370
+ return;
371
+ }
372
+ }
373
+ // Check for typeof usage in function calls
374
+ if (callee.type === 'Identifier' && callee.name === 'typeof') {
375
+ // This is unusual - typeof is normally a unary operator
376
+ // But if used as a function call, it's likely wrong
377
+ context.report({
378
+ node,
379
+ messageId: 'improperTypeValidation',
380
+ data: {
381
+ filePath: filename,
382
+ line: String(node.loc?.start.line ?? 0),
383
+ severity: 'LOW',
384
+ safeAlternative: 'Use typeof as unary operator: typeof value',
385
+ },
386
+ });
387
+ }
388
+ },
389
+ // Check if statements for incomplete type validation
390
+ IfStatement(node) {
391
+ const test = node.test;
392
+ // Look for if statements that only check one aspect of type
393
+ if (test.type === 'BinaryExpression') {
394
+ const testText = sourceCode.getText(test).toLowerCase();
395
+ // Check for incomplete null checks
396
+ if ((testText.includes('!= null') || testText.includes('== null')) &&
397
+ !testText.includes('!== undefined') && !testText.includes('=== undefined')) {
398
+ // Check if this involves user input
399
+ if ((test.left.type === 'Identifier' && isUserInput(test.left.name)) ||
400
+ (test.right.type === 'Identifier' && isUserInput(test.right.name))) {
401
+ // FALSE POSITIVE REDUCTION
402
+ if (safetyChecker.isSafe(node, context)) {
403
+ return;
404
+ }
405
+ context.report({
406
+ node: test,
407
+ messageId: 'missingNullCheck',
408
+ data: {
409
+ filePath: filename,
410
+ line: String(node.loc?.start.line ?? 0),
411
+ },
412
+ });
413
+ }
414
+ }
415
+ }
416
+ }
417
+ };
418
+ },
419
+ });
420
+ //# sourceMappingURL=no-improper-type-validation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"no-improper-type-validation.js","sourceRoot":"","sources":["../../../../../../packages/eslint-plugin-secure-coding/src/rules/security/no-improper-type-validation.ts"],"names":[],"mappings":";;;AAgBA,4DAAsD;AACtD,4DAA0E;AAC1E,4DAGkC;AA8BrB,QAAA,wBAAwB,GAAG,IAAA,0BAAU,EAA0B;IAC1E,IAAI,EAAE,6BAA6B;IACnC,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,IAAI,EAAE;YACJ,WAAW,EAAE,yDAAyD;SACvE;QACD,OAAO,EAAE,MAAM;QACf,cAAc,EAAE,IAAI;QACpB,QAAQ,EAAE;YACR,sBAAsB,EAAE,IAAA,gCAAgB,EAAC;gBACvC,IAAI,EAAE,4BAAY,CAAC,QAAQ;gBAC3B,SAAS,EAAE,0BAA0B;gBACrC,GAAG,EAAE,UAAU;gBACf,WAAW,EAAE,+CAA+C;gBAC5D,QAAQ,EAAE,cAAc;gBACxB,GAAG,EAAE,qBAAqB;gBAC1B,iBAAiB,EAAE,kDAAkD;aACtE,CAAC;YACF,iBAAiB,EAAE,IAAA,gCAAgB,EAAC;gBAClC,IAAI,EAAE,4BAAY,CAAC,OAAO;gBAC1B,SAAS,EAAE,qBAAqB;gBAChC,GAAG,EAAE,UAAU;gBACf,WAAW,EAAE,iCAAiC;gBAC9C,QAAQ,EAAE,QAAQ;gBAClB,GAAG,EAAE,gDAAgD;gBACrD,iBAAiB,EAAE,oFAAoF;aACxG,CAAC;YACF,qBAAqB,EAAE,IAAA,gCAAgB,EAAC;gBACtC,IAAI,EAAE,4BAAY,CAAC,OAAO;gBAC1B,SAAS,EAAE,yBAAyB;gBACpC,GAAG,EAAE,UAAU;gBACf,WAAW,EAAE,qCAAqC;gBAClD,QAAQ,EAAE,KAAK;gBACf,GAAG,EAAE,sCAAsC;gBAC3C,iBAAiB,EAAE,wFAAwF;a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,7 @@
1
+ export interface Options {
2
+ /** Allow insecure comparison in test files. Default: false */
3
+ allowInTests?: boolean;
4
+ /** Additional patterns to ignore. Default: [] */
5
+ ignorePatterns?: string[];
6
+ }
7
+ export declare const noInsecureComparison: ESLintUtils.RuleModule<MessageIds, Options, unknown, ESLintUtils.RuleListener>;
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.noInsecureComparison = void 0;
4
+ const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
+ const eslint_devkit_2 = require("@interlace/eslint-devkit");
6
+ exports.noInsecureComparison = (0, eslint_devkit_2.createRule)({
7
+ name: 'no-insecure-comparison',
8
+ meta: {
9
+ type: 'problem',
10
+ docs: {
11
+ description: 'Detects insecure comparison operators (==, !=) that can lead to type coercion vulnerabilities',
12
+ },
13
+ fixable: 'code',
14
+ hasSuggestions: true,
15
+ messages: {
16
+ insecureComparison: (0, eslint_devkit_1.formatLLMMessage)({
17
+ icon: eslint_devkit_1.MessageIcons.SECURITY,
18
+ issueName: 'Insecure Comparison',
19
+ cwe: 'CWE-697',
20
+ description: 'Insecure comparison operator ({{operator}}) detected - can lead to type coercion vulnerabilities',
21
+ severity: 'HIGH',
22
+ fix: 'Use strict equality ({{strictOperator}}) instead: {{example}}',
23
+ documentationLink: 'https://cwe.mitre.org/data/definitions/697.html',
24
+ }),
25
+ useStrictEquality: (0, eslint_devkit_1.formatLLMMessage)({
26
+ icon: eslint_devkit_1.MessageIcons.INFO,
27
+ issueName: 'Use Strict Equality',
28
+ description: 'Use strict equality operator',
29
+ severity: 'LOW',
30
+ fix: 'Replace == with === and != with !==',
31
+ documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality',
32
+ }),
33
+ },
34
+ schema: [
35
+ {
36
+ type: 'object',
37
+ properties: {
38
+ allowInTests: {
39
+ type: 'boolean',
40
+ default: false,
41
+ description: 'Allow insecure comparison in test files',
42
+ },
43
+ ignorePatterns: {
44
+ type: 'array',
45
+ items: { type: 'string' },
46
+ default: [],
47
+ description: 'Additional patterns to ignore',
48
+ },
49
+ },
50
+ additionalProperties: false,
51
+ },
52
+ ],
53
+ },
54
+ defaultOptions: [
55
+ {
56
+ allowInTests: false,
57
+ ignorePatterns: [],
58
+ },
59
+ ],
60
+ create(context, [options = {}]) {
61
+ const { allowInTests = false, ignorePatterns = [], } = options;
62
+ const filename = context.getFilename();
63
+ const isTestFile = allowInTests && /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
64
+ const sourceCode = context.sourceCode || context.sourceCode;
65
+ /**
66
+ * Check if a string matches any ignore pattern
67
+ */
68
+ function matchesIgnorePattern(text, patterns) {
69
+ return patterns.some(pattern => {
70
+ try {
71
+ const regex = new RegExp(pattern, 'i');
72
+ return regex.test(text);
73
+ }
74
+ catch {
75
+ // Invalid regex - treat as literal string match
76
+ return text.toLowerCase().includes(pattern.toLowerCase());
77
+ }
78
+ });
79
+ }
80
+ /**
81
+ * Check BinaryExpression for insecure comparison operators
82
+ */
83
+ function checkBinaryExpression(node) {
84
+ if (isTestFile) {
85
+ return;
86
+ }
87
+ // Check for insecure comparison operators
88
+ if (node.operator === '==' || node.operator === '!=') {
89
+ const text = sourceCode.getText(node);
90
+ // Check if it matches any ignore pattern
91
+ if (matchesIgnorePattern(text, ignorePatterns)) {
92
+ return;
93
+ }
94
+ const strictOperator = node.operator === '==' ? '===' : '!==';
95
+ const leftText = sourceCode.getText(node.left);
96
+ const rightText = sourceCode.getText(node.right);
97
+ const example = `${leftText} ${strictOperator} ${rightText}`;
98
+ context.report({
99
+ node: node,
100
+ messageId: 'insecureComparison',
101
+ data: {
102
+ operator: node.operator,
103
+ strictOperator,
104
+ example,
105
+ },
106
+ fix: (fixer) => {
107
+ return fixer.replaceText(node, example);
108
+ },
109
+ suggest: [
110
+ {
111
+ messageId: 'useStrictEquality',
112
+ fix: (fixer) => {
113
+ return fixer.replaceText(node, example);
114
+ },
115
+ },
116
+ ],
117
+ });
118
+ }
119
+ }
120
+ return {
121
+ BinaryExpression: checkBinaryExpression,
122
+ };
123
+ },
124
+ });
125
+ //# sourceMappingURL=no-insecure-comparison.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"no-insecure-comparison.js","sourceRoot":"","sources":["../../../../../../packages/eslint-plugin-secure-coding/src/rules/security/no-insecure-comparison.ts"],"names":[],"mappings":";;;AASA,4DAA0E;AAC1E,4DAAsD;AAczC,QAAA,oBAAoB,GAAG,IAAA,0BAAU,EAA0B;IACtE,IAAI,EAAE,wBAAwB;IAC9B,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,IAAI,EAAE;YACJ,WAAW,EAAE,+FAA+F;SAC7G;QACD,OAAO,EAAE,MAAM;QACf,cAAc,EAAE,IAAI;QACpB,QAAQ,EAAE;YACR,kBAAkB,EAAE,IAAA,gCAAgB,EAAC;gBACnC,IAAI,EAAE,4BAAY,CAAC,QAAQ;gBAC3B,SAAS,EAAE,qBAAqB;gBAChC,GAAG,EAAE,SAAS;gBACd,WAAW,EAAE,kGAAkG;gBAC/G,QAAQ,EAAE,MAAM;gBAChB,GAAG,EAAE,+DAA+D;gBACpE,iBAAiB,EAAE,iDAAiD;aACrE,CAAC;YACF,iBAAiB,EAAE,IAAA,gCAAgB,EAAC;gBAClC,IAAI,EAAE,4BAAY,CAAC,IAAI;gBACvB,SAAS,EAAE,qBAAqB;gBAChC,WAAW,EAAE,8BAA8B;gBAC3C,QAAQ,EAAE,KAAK;gBACf,GAAG,EAAE,qCAAqC;gBAC1C,iBAAiB,EAAE,6FAA6F;aACjH,CAAC;SACH;QACD,MAAM,EAAE;YACN;gBACE,IAAI,EAAE,QAAQ;gBACd,UAAU,EAAE;oBACV,YAAY,EAAE;wBACZ,IAAI,EAAE,SAAS;wBACf,OAAO,EAAE,KAAK;wBACd,WAAW,EAAE,yCAAyC;qBACvD;oBACD,cAAc,EAAE;wBACd,IAAI,EAAE,OAAO;wBACb,KAAK,EA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library patterns to recognize. Default: ['cookie', 'js-cookie', 'universal-cookie'] */
5
+ cookieLibraries?: string[];
6
+ /** Additional safe patterns to ignore. Default: [] */
7
+ ignorePatterns?: string[];
8
+ }
9
+ export declare const noInsecureCookieSettings: ESLintUtils.RuleModule<MessageIds, Options, unknown, ESLintUtils.RuleListener>;