eslint-plugin-secure-coding 3.4.3 → 3.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/package.json +2 -2
  2. package/src/index.js +1 -107
  3. package/src/oxlint.js +1 -3
  4. package/src/rules/detect-non-literal-regexp/index.js +1 -329
  5. package/src/rules/detect-object-injection/index.js +1 -691
  6. package/src/rules/detect-weak-password-validation/index.js +1 -54
  7. package/src/rules/no-directive-injection/index.js +1 -416
  8. package/src/rules/no-electron-security-issues/index.js +1 -385
  9. package/src/rules/no-format-string-injection/index.js +1 -565
  10. package/src/rules/no-graphql-injection/index.js +1 -612
  11. package/src/rules/no-hardcoded-credentials/index.js +1 -658
  12. package/src/rules/no-hardcoded-session-tokens/index.js +1 -56
  13. package/src/rules/no-improper-sanitization/index.js +1 -391
  14. package/src/rules/no-improper-type-validation/index.js +1 -422
  15. package/src/rules/no-insecure-comparison/index.js +1 -249
  16. package/src/rules/no-ldap-injection/index.js +1 -403
  17. package/src/rules/no-missing-authentication/index.js +1 -282
  18. package/src/rules/no-pii-in-logs/index.js +1 -63
  19. package/src/rules/no-privilege-escalation/index.js +1 -282
  20. package/src/rules/no-redos-vulnerable-regex/index.js +0 -0
  21. package/src/rules/no-sensitive-data-exposure/index.js +1 -249
  22. package/src/rules/no-template-injection/index.js +1 -84
  23. package/src/rules/no-unchecked-loop-condition/index.js +1 -541
  24. package/src/rules/no-unlimited-resource-allocation/index.js +1 -555
  25. package/src/rules/no-unsafe-deserialization/index.js +1 -497
  26. package/src/rules/no-unsafe-regex-construction/index.js +1 -275
  27. package/src/rules/no-weak-password-recovery/index.js +1 -382
  28. package/src/rules/no-xpath-injection/index.js +1 -431
  29. package/src/rules/no-xxe-injection/index.js +1 -230
  30. package/src/rules/require-backend-authorization/index.js +1 -58
  31. package/src/rules/require-secure-defaults/index.js +1 -41
  32. package/src/types/index.js +1 -2
@@ -1,275 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noUnsafeRegexConstruction = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- const eslint_devkit_2 = require("@interlace/eslint-devkit");
6
- const INLINE_ESCAPE_SUFFIX = '.replace(/[.*+?^${}()|[\\]\\\\]/g, "\\\\$&")';
7
- function isUserInput(node) {
8
- if (node.type === 'CallExpression') {
9
- return true;
10
- }
11
- if (node.type === 'TemplateLiteral') {
12
- return node.expressions.length > 0;
13
- }
14
- if (node.type === 'MemberExpression') {
15
- return true;
16
- }
17
- if (node.type === 'Identifier') {
18
- const safeIdentifiers = ['pattern', 'regex', 'regExp', 'regexp'];
19
- if (safeIdentifiers.includes(node.name.toLowerCase())) {
20
- return true;
21
- }
22
- return true;
23
- }
24
- return false;
25
- }
26
- function isEscaped(node, trustedFunctions, sourceCode) {
27
- if (node.type === 'CallExpression' && node.callee.type === 'Identifier') {
28
- const functionName = node.callee.name;
29
- if (trustedFunctions.includes(functionName)) {
30
- return true;
31
- }
32
- }
33
- let current = node;
34
- let depth = 0;
35
- const maxDepth = 5;
36
- while (depth < maxDepth) {
37
- const parent = sourceCode.getNodeByRangeIndex?.(current.range[0] - 1) ||
38
- current.parent;
39
- if (!parent)
40
- break;
41
- if (parent.type === 'CallExpression' &&
42
- parent.callee.type === 'Identifier') {
43
- const functionName = parent.callee.name;
44
- if (trustedFunctions.includes(functionName)) {
45
- return true;
46
- }
47
- }
48
- current = parent;
49
- depth++;
50
- }
51
- return false;
52
- }
53
- function hasDynamicFlags(node) {
54
- if (node.arguments.length > 1) {
55
- const flagsNode = node.arguments[1];
56
- return isUserInput(flagsNode);
57
- }
58
- return false;
59
- }
60
- function extractPattern(node, sourceCode, trustedFunctions) {
61
- const patternNode = node.arguments.length > 0 ? node.arguments[0] : null;
62
- if (!patternNode) {
63
- return { patternNode: null, isUserInput: false, isEscaped: false };
64
- }
65
- const isUserInputValue = isUserInput(patternNode);
66
- const allTrustedFunctions = [
67
- ...new Set([
68
- 'escapeRegex',
69
- 'escape',
70
- 'sanitize',
71
- 'RegExp.escape',
72
- ...trustedFunctions,
73
- ]),
74
- ];
75
- const isEscapedValue = isEscaped(patternNode, allTrustedFunctions, sourceCode);
76
- return {
77
- patternNode,
78
- isUserInput: isUserInputValue,
79
- isEscaped: isEscapedValue,
80
- };
81
- }
82
- exports.noUnsafeRegexConstruction = (0, eslint_devkit_2.createRule)({
83
- name: 'no-unsafe-regex-construction',
84
- meta: {
85
- type: 'problem',
86
- docs: {
87
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-unsafe-regex-construction.md',
88
- description: 'Detects unsafe regex construction patterns (user input without escaping, dynamic flags)',
89
- cwe: 'CWE-400',
90
- cvss: 7.5,
91
- },
92
- hasSuggestions: true,
93
- messages: {
94
- unsafeRegexConstruction: (0, eslint_devkit_1.formatLLMMessage)({
95
- icon: eslint_devkit_1.MessageIcons.SECURITY,
96
- issueName: 'Unsafe regex construction',
97
- cwe: 'CWE-400',
98
- description: '{{issue}}: {{details}}',
99
- severity: 'HIGH',
100
- fix: '{{fix}}',
101
- documentationLink: 'https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS',
102
- }),
103
- escapeUserInput: (0, eslint_devkit_1.formatLLMMessage)({
104
- icon: eslint_devkit_1.MessageIcons.INFO,
105
- issueName: 'Escape User Input',
106
- description: 'Escape user input for regex',
107
- severity: 'LOW',
108
- fix: 'input.replace(/[.*+?^${}()|[\\]\\\\]/g, "\\\\$&")',
109
- documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping',
110
- }),
111
- validatePattern: (0, eslint_devkit_1.formatLLMMessage)({
112
- icon: eslint_devkit_1.MessageIcons.INFO,
113
- issueName: 'Validate Pattern',
114
- description: 'Validate pattern against whitelist',
115
- severity: 'LOW',
116
- fix: 'Validate pattern before creating RegExp',
117
- documentationLink: 'https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS',
118
- }),
119
- useSafeLibrary: (0, eslint_devkit_1.formatLLMMessage)({
120
- icon: eslint_devkit_1.MessageIcons.INFO,
121
- issueName: 'Use safe-regex',
122
- description: 'Use safe-regex library for validation',
123
- severity: 'LOW',
124
- fix: 'if (safeRegex(pattern)) { new RegExp(pattern) }',
125
- documentationLink: 'https://github.com/substack/safe-regex',
126
- }),
127
- avoidDynamicFlags: (0, eslint_devkit_1.formatLLMMessage)({
128
- icon: eslint_devkit_1.MessageIcons.INFO,
129
- issueName: 'Use Static Flags',
130
- description: 'Use static flags instead of dynamic',
131
- severity: 'LOW',
132
- fix: 'new RegExp(pattern, "gi") with static flags',
133
- documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp',
134
- }),
135
- },
136
- schema: [
137
- {
138
- type: 'object',
139
- properties: {
140
- allowLiterals: {
141
- type: 'boolean',
142
- default: true,
143
- description: 'Allow literal string patterns',
144
- },
145
- trustedEscapingFunctions: {
146
- type: 'array',
147
- items: { type: 'string' },
148
- default: ['escapeRegex', 'escape', 'sanitize'],
149
- description: 'Trusted functions that escape input',
150
- },
151
- maxPatternLength: {
152
- type: 'number',
153
- default: 100,
154
- minimum: 1,
155
- description: 'Maximum pattern length for dynamic regex',
156
- },
157
- },
158
- additionalProperties: false,
159
- },
160
- ],
161
- },
162
- defaultOptions: [
163
- {
164
- allowLiterals: true,
165
- trustedEscapingFunctions: ['escapeRegex', 'escape', 'sanitize'],
166
- maxPatternLength: 100,
167
- },
168
- ],
169
- create(context, [options = {}]) {
170
- const { allowLiterals = true, maxPatternLength = 100, trustedEscapingFunctions = ['escapeRegex', 'escape', 'sanitize'], } = options;
171
- const sourceCode = context.sourceCode;
172
- function checkRegExpCall(node) {
173
- const isRegExpCall = (node.type === 'CallExpression' &&
174
- node.callee.type === 'Identifier' &&
175
- node.callee.name === 'RegExp') ||
176
- (node.type === 'NewExpression' &&
177
- node.callee.type === 'Identifier' &&
178
- node.callee.name === 'RegExp');
179
- if (!isRegExpCall) {
180
- return;
181
- }
182
- const { patternNode, isUserInput: isUserInputValue, isEscaped: isEscapedValue, } = extractPattern(node, sourceCode, trustedEscapingFunctions);
183
- if (!patternNode) {
184
- return;
185
- }
186
- if (patternNode.type === 'Literal' &&
187
- typeof patternNode.value === 'string') {
188
- const patternLength = patternNode.value.length;
189
- if (patternLength > maxPatternLength) {
190
- context.report({
191
- node: patternNode,
192
- messageId: 'unsafeRegexConstruction',
193
- data: {
194
- issue: 'Pattern too long',
195
- details: `Pattern length (${patternLength}) exceeds maximum (${maxPatternLength})`,
196
- fix: 'Split into smaller patterns or validate length',
197
- },
198
- suggest: [
199
- {
200
- messageId: 'validatePattern',
201
- fix: () => null,
202
- },
203
- ],
204
- });
205
- return;
206
- }
207
- if (!allowLiterals) {
208
- context.report({
209
- node: patternNode,
210
- messageId: 'unsafeRegexConstruction',
211
- data: {
212
- issue: 'Literal regex pattern',
213
- details: 'Literal regex patterns should be avoided for security. Use variables instead.',
214
- fix: 'Use a variable or RegExp constructor with a string variable',
215
- },
216
- suggest: [
217
- {
218
- messageId: 'validatePattern',
219
- fix: () => null,
220
- },
221
- ],
222
- });
223
- return;
224
- }
225
- }
226
- if (isUserInputValue && !isEscapedValue) {
227
- const patternText = context.sourceCode.getText(patternNode);
228
- context.report({
229
- node: patternNode,
230
- messageId: 'unsafeRegexConstruction',
231
- data: {
232
- issue: 'User input in regex without escaping',
233
- details: 'User input in regex pattern can lead to ReDoS or injection attacks',
234
- fix: 'Escape special characters before using in regex',
235
- },
236
- suggest: [
237
- {
238
- messageId: 'escapeUserInput',
239
- fix: (fixer) => fixer.replaceText(patternNode, `(${patternText})${INLINE_ESCAPE_SUFFIX}`),
240
- },
241
- {
242
- messageId: 'validatePattern',
243
- fix: () => null,
244
- },
245
- {
246
- messageId: 'useSafeLibrary',
247
- fix: () => null,
248
- },
249
- ],
250
- });
251
- }
252
- if (hasDynamicFlags(node)) {
253
- context.report({
254
- node,
255
- messageId: 'unsafeRegexConstruction',
256
- data: {
257
- issue: 'Dynamic regex flags',
258
- details: 'Dynamic flags can lead to unexpected behavior or security issues',
259
- fix: 'Use static flags instead of dynamic flags',
260
- },
261
- suggest: [
262
- {
263
- messageId: 'avoidDynamicFlags',
264
- fix: () => null,
265
- },
266
- ],
267
- });
268
- }
269
- }
270
- return {
271
- CallExpression: checkRegExpCall,
272
- NewExpression: checkRegExpCall,
273
- };
274
- },
275
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noUnsafeRegexConstruction=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");const INLINE_ESCAPE_SUFFIX='.replace(/[.*+?^${}()|[\\]\\\\]/g, "\\\\$&")';function isUserInput(node){if(node.type==="CallExpression"){return true}if(node.type==="TemplateLiteral"){return node.expressions.length>0}if(node.type==="MemberExpression"){return true}if(node.type==="Identifier"){const safeIdentifiers=["pattern","regex","regExp","regexp"];if(safeIdentifiers.includes(node.name.toLowerCase())){return true}return true}return false}function isEscaped(node,trustedFunctions,sourceCode){if(node.type==="CallExpression"&&node.callee.type==="Identifier"){const functionName=node.callee.name;if(trustedFunctions.includes(functionName)){return true}}let current=node;let depth=0;const maxDepth=5;while(depth<maxDepth){const parent=sourceCode.getNodeByRangeIndex?.(current.range[0]-1)||current.parent;if(!parent)break;if(parent.type==="CallExpression"&&parent.callee.type==="Identifier"){const functionName=parent.callee.name;if(trustedFunctions.includes(functionName)){return true}}current=parent;depth++}return false}function hasDynamicFlags(node){if(node.arguments.length>1){const flagsNode=node.arguments[1];return isUserInput(flagsNode)}return false}function extractPattern(node,sourceCode,trustedFunctions){const patternNode=node.arguments.length>0?node.arguments[0]:null;if(!patternNode){return{patternNode:null,isUserInput:false,isEscaped:false}}const isUserInputValue=isUserInput(patternNode);const allTrustedFunctions=[...new Set(["escapeRegex","escape","sanitize","RegExp.escape",...trustedFunctions])];const isEscapedValue=isEscaped(patternNode,allTrustedFunctions,sourceCode);return{patternNode,isUserInput:isUserInputValue,isEscaped:isEscapedValue}}exports.noUnsafeRegexConstruction=(0,eslint_devkit_2.createRule)({name:"no-unsafe-regex-construction",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-unsafe-regex-construction.md",description:"Detects unsafe regex construction patterns (user input without escaping, dynamic flags)",cwe:"CWE-400",cvss:7.5},hasSuggestions:true,messages:{unsafeRegexConstruction:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Unsafe regex construction",cwe:"CWE-400",description:"{{issue}}: {{details}}",severity:"HIGH",fix:"{{fix}}",documentationLink:"https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS"}),escapeUserInput:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Escape User Input",description:"Escape user input for regex",severity:"LOW",fix:'input.replace(/[.*+?^${}()|[\\]\\\\]/g, "\\\\$&")',documentationLink:"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping"}),validatePattern:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Validate Pattern",description:"Validate pattern against whitelist",severity:"LOW",fix:"Validate pattern before creating RegExp",documentationLink:"https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS"}),useSafeLibrary:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Use safe-regex",description:"Use safe-regex library for validation",severity:"LOW",fix:"if (safeRegex(pattern)) { new RegExp(pattern) }",documentationLink:"https://github.com/substack/safe-regex"}),avoidDynamicFlags:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Use Static Flags",description:"Use static flags instead of dynamic",severity:"LOW",fix:'new RegExp(pattern, "gi") with static flags',documentationLink:"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp"})},schema:[{type:"object",properties:{allowLiterals:{type:"boolean",default:true,description:"Allow literal string patterns"},trustedEscapingFunctions:{type:"array",items:{type:"string"},default:["escapeRegex","escape","sanitize"],description:"Trusted functions that escape input"},maxPatternLength:{type:"number",default:100,minimum:1,description:"Maximum pattern length for dynamic regex"}},additionalProperties:false}]},defaultOptions:[{allowLiterals:true,trustedEscapingFunctions:["escapeRegex","escape","sanitize"],maxPatternLength:100}],create(context,[options={}]){const{allowLiterals=true,maxPatternLength=100,trustedEscapingFunctions=["escapeRegex","escape","sanitize"]}=options;const sourceCode=context.sourceCode;function checkRegExpCall(node){const isRegExpCall=node.type==="CallExpression"&&node.callee.type==="Identifier"&&node.callee.name==="RegExp"||node.type==="NewExpression"&&node.callee.type==="Identifier"&&node.callee.name==="RegExp";if(!isRegExpCall){return}const{patternNode,isUserInput:isUserInputValue,isEscaped:isEscapedValue}=extractPattern(node,sourceCode,trustedEscapingFunctions);if(!patternNode){return}if(patternNode.type==="Literal"&&typeof patternNode.value==="string"){const patternLength=patternNode.value.length;if(patternLength>maxPatternLength){context.report({node:patternNode,messageId:"unsafeRegexConstruction",data:{issue:"Pattern too long",details:`Pattern length (${patternLength}) exceeds maximum (${maxPatternLength})`,fix:"Split into smaller patterns or validate length"},suggest:[{messageId:"validatePattern",fix:()=>null}]});return}if(!allowLiterals){context.report({node:patternNode,messageId:"unsafeRegexConstruction",data:{issue:"Literal regex pattern",details:"Literal regex patterns should be avoided for security. Use variables instead.",fix:"Use a variable or RegExp constructor with a string variable"},suggest:[{messageId:"validatePattern",fix:()=>null}]});return}}if(isUserInputValue&&!isEscapedValue){const patternText=context.sourceCode.getText(patternNode);context.report({node:patternNode,messageId:"unsafeRegexConstruction",data:{issue:"User input in regex without escaping",details:"User input in regex pattern can lead to ReDoS or injection attacks",fix:"Escape special characters before using in regex"},suggest:[{messageId:"escapeUserInput",fix:fixer=>fixer.replaceText(patternNode,`(${patternText})${INLINE_ESCAPE_SUFFIX}`)},{messageId:"validatePattern",fix:()=>null},{messageId:"useSafeLibrary",fix:()=>null}]})}if(hasDynamicFlags(node)){context.report({node,messageId:"unsafeRegexConstruction",data:{issue:"Dynamic regex flags",details:"Dynamic flags can lead to unexpected behavior or security issues",fix:"Use static flags instead of dynamic flags"},suggest:[{messageId:"avoidDynamicFlags",fix:()=>null}]})}}return{CallExpression:checkRegExpCall,NewExpression:checkRegExpCall}}});