eslint-plugin-secure-coding 3.4.3 → 3.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/index.js +1 -107
- package/src/oxlint.js +1 -3
- package/src/rules/detect-non-literal-regexp/index.js +1 -329
- package/src/rules/detect-object-injection/index.js +1 -691
- package/src/rules/detect-weak-password-validation/index.js +1 -54
- package/src/rules/no-directive-injection/index.js +1 -416
- package/src/rules/no-electron-security-issues/index.js +1 -385
- package/src/rules/no-format-string-injection/index.js +1 -565
- package/src/rules/no-graphql-injection/index.js +1 -612
- package/src/rules/no-hardcoded-credentials/index.js +1 -658
- package/src/rules/no-hardcoded-session-tokens/index.js +1 -56
- package/src/rules/no-improper-sanitization/index.js +1 -391
- package/src/rules/no-improper-type-validation/index.js +1 -422
- package/src/rules/no-insecure-comparison/index.js +1 -249
- package/src/rules/no-ldap-injection/index.js +1 -403
- package/src/rules/no-missing-authentication/index.js +1 -282
- package/src/rules/no-pii-in-logs/index.js +1 -63
- package/src/rules/no-privilege-escalation/index.js +1 -282
- package/src/rules/no-redos-vulnerable-regex/index.js +0 -0
- package/src/rules/no-sensitive-data-exposure/index.js +1 -249
- package/src/rules/no-template-injection/index.js +1 -84
- package/src/rules/no-unchecked-loop-condition/index.js +1 -541
- package/src/rules/no-unlimited-resource-allocation/index.js +1 -555
- package/src/rules/no-unsafe-deserialization/index.js +1 -497
- package/src/rules/no-unsafe-regex-construction/index.js +1 -275
- package/src/rules/no-weak-password-recovery/index.js +1 -382
- package/src/rules/no-xpath-injection/index.js +1 -431
- package/src/rules/no-xxe-injection/index.js +1 -230
- package/src/rules/require-backend-authorization/index.js +1 -58
- package/src/rules/require-secure-defaults/index.js +1 -41
- package/src/types/index.js +1 -2
|
@@ -1,612 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.noGraphqlInjection = 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.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
8
|
-
name: 'no-graphql-injection',
|
|
9
|
-
meta: {
|
|
10
|
-
type: 'problem',
|
|
11
|
-
docs: {
|
|
12
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-graphql-injection.md',
|
|
13
|
-
description: 'Detects GraphQL injection vulnerabilities and DoS attacks',
|
|
14
|
-
cwe: 'CWE-89',
|
|
15
|
-
},
|
|
16
|
-
hasSuggestions: true,
|
|
17
|
-
messages: {
|
|
18
|
-
graphqlInjection: (0, eslint_devkit_2.formatLLMMessage)({
|
|
19
|
-
icon: eslint_devkit_2.MessageIcons.SECURITY,
|
|
20
|
-
issueName: 'GraphQL Injection',
|
|
21
|
-
cwe: 'CWE-89',
|
|
22
|
-
description: 'GraphQL injection vulnerability detected',
|
|
23
|
-
severity: '{{severity}}',
|
|
24
|
-
fix: '{{safeAlternative}}',
|
|
25
|
-
documentationLink: 'https://owasp.org/Top10/2025/A05_2025-Injection/',
|
|
26
|
-
}),
|
|
27
|
-
introspectionQuery: (0, eslint_devkit_2.formatLLMMessage)({
|
|
28
|
-
icon: eslint_devkit_2.MessageIcons.SECURITY,
|
|
29
|
-
issueName: 'Dangerous Introspection Query',
|
|
30
|
-
cwe: 'CWE-200',
|
|
31
|
-
description: 'Introspection query may leak schema information',
|
|
32
|
-
severity: 'HIGH',
|
|
33
|
-
fix: 'Disable introspection in production',
|
|
34
|
-
documentationLink: 'https://graphql.org/learn/introspection/',
|
|
35
|
-
}),
|
|
36
|
-
complexQueryDos: (0, eslint_devkit_2.formatLLMMessage)({
|
|
37
|
-
icon: eslint_devkit_2.MessageIcons.SECURITY,
|
|
38
|
-
issueName: 'Complex Query DoS Risk',
|
|
39
|
-
cwe: 'CWE-400',
|
|
40
|
-
description: 'Complex query may cause DoS',
|
|
41
|
-
severity: 'MEDIUM',
|
|
42
|
-
fix: 'Limit query depth and complexity',
|
|
43
|
-
documentationLink: 'https://graphql.org/learn/queries/',
|
|
44
|
-
}),
|
|
45
|
-
unsafeVariableInterpolation: (0, eslint_devkit_2.formatLLMMessage)({
|
|
46
|
-
icon: eslint_devkit_2.MessageIcons.SECURITY,
|
|
47
|
-
issueName: 'Unsafe Variable Interpolation',
|
|
48
|
-
cwe: 'CWE-89',
|
|
49
|
-
description: 'Unsafe interpolation in GraphQL query',
|
|
50
|
-
severity: 'HIGH',
|
|
51
|
-
fix: 'Use GraphQL variables instead of string interpolation',
|
|
52
|
-
documentationLink: 'https://graphql.org/learn/queries/#variables',
|
|
53
|
-
}),
|
|
54
|
-
missingInputValidation: (0, eslint_devkit_2.formatLLMMessage)({
|
|
55
|
-
icon: eslint_devkit_2.MessageIcons.SECURITY,
|
|
56
|
-
issueName: 'Missing Input Validation',
|
|
57
|
-
cwe: 'CWE-20',
|
|
58
|
-
description: 'GraphQL input not validated',
|
|
59
|
-
severity: 'MEDIUM',
|
|
60
|
-
fix: 'Validate all user inputs before GraphQL execution',
|
|
61
|
-
documentationLink: 'https://graphql.org/learn/validation/',
|
|
62
|
-
}),
|
|
63
|
-
useQueryBuilder: (0, eslint_devkit_2.formatLLMMessage)({
|
|
64
|
-
icon: eslint_devkit_2.MessageIcons.INFO,
|
|
65
|
-
issueName: 'Use Query Builder',
|
|
66
|
-
description: 'Use GraphQL query builders for safe construction',
|
|
67
|
-
severity: 'LOW',
|
|
68
|
-
fix: 'Use graphql-tag or similar libraries',
|
|
69
|
-
documentationLink: 'https://www.npmjs.com/package/graphql-tag',
|
|
70
|
-
}),
|
|
71
|
-
disableIntrospection: (0, eslint_devkit_2.formatLLMMessage)({
|
|
72
|
-
icon: eslint_devkit_2.MessageIcons.INFO,
|
|
73
|
-
issueName: 'Disable Introspection',
|
|
74
|
-
description: 'Disable GraphQL introspection in production',
|
|
75
|
-
severity: 'LOW',
|
|
76
|
-
fix: 'Set introspection: false in GraphQL config',
|
|
77
|
-
documentationLink: 'https://www.apollographql.com/docs/apollo-server/security/introspection/',
|
|
78
|
-
}),
|
|
79
|
-
limitQueryDepth: (0, eslint_devkit_2.formatLLMMessage)({
|
|
80
|
-
icon: eslint_devkit_2.MessageIcons.INFO,
|
|
81
|
-
issueName: 'Limit Query Depth',
|
|
82
|
-
description: 'Limit maximum query depth',
|
|
83
|
-
severity: 'LOW',
|
|
84
|
-
fix: 'Use depth limiting plugins or custom validation',
|
|
85
|
-
documentationLink: 'https://www.npmjs.com/package/graphql-depth-limit',
|
|
86
|
-
}),
|
|
87
|
-
strategyQueryBuilder: (0, eslint_devkit_2.formatLLMMessage)({
|
|
88
|
-
icon: eslint_devkit_2.MessageIcons.STRATEGY,
|
|
89
|
-
issueName: 'Query Builder Strategy',
|
|
90
|
-
description: 'Use typed query builders for compile-time safety',
|
|
91
|
-
severity: 'LOW',
|
|
92
|
-
fix: 'Use GraphQL code generation tools',
|
|
93
|
-
documentationLink: 'https://graphql-code-generator.com/',
|
|
94
|
-
}),
|
|
95
|
-
strategyInputValidation: (0, eslint_devkit_2.formatLLMMessage)({
|
|
96
|
-
icon: eslint_devkit_2.MessageIcons.STRATEGY,
|
|
97
|
-
issueName: 'Input Validation Strategy',
|
|
98
|
-
description: 'Validate all inputs at GraphQL resolver level',
|
|
99
|
-
severity: 'LOW',
|
|
100
|
-
fix: 'Implement custom scalars and input validation',
|
|
101
|
-
documentationLink: 'https://graphql.org/learn/schema/#scalar-types',
|
|
102
|
-
}),
|
|
103
|
-
strategyIntrospection: (0, eslint_devkit_2.formatLLMMessage)({
|
|
104
|
-
icon: eslint_devkit_2.MessageIcons.STRATEGY,
|
|
105
|
-
issueName: 'Introspection Strategy',
|
|
106
|
-
description: 'Control introspection access based on environment',
|
|
107
|
-
severity: 'LOW',
|
|
108
|
-
fix: 'Enable introspection only for development/admin users',
|
|
109
|
-
documentationLink: 'https://www.apollographql.com/docs/apollo-server/security/introspection/',
|
|
110
|
-
})
|
|
111
|
-
},
|
|
112
|
-
schema: [
|
|
113
|
-
{
|
|
114
|
-
type: 'object',
|
|
115
|
-
properties: {
|
|
116
|
-
allowIntrospection: {
|
|
117
|
-
type: 'boolean',
|
|
118
|
-
default: false, description: 'Allow introspection queries'
|
|
119
|
-
},
|
|
120
|
-
maxQueryDepth: {
|
|
121
|
-
type: 'number',
|
|
122
|
-
minimum: 1,
|
|
123
|
-
default: 10, description: 'Maximum query nesting depth before reporting a DoS risk'
|
|
124
|
-
},
|
|
125
|
-
trustedGraphqlLibraries: {
|
|
126
|
-
type: 'array',
|
|
127
|
-
items: { type: 'string' },
|
|
128
|
-
default: ['graphql', 'apollo-server', 'graphql-tools', 'graphql-tag'], description: 'GraphQL libraries recognised as query builders'
|
|
129
|
-
},
|
|
130
|
-
validationFunctions: {
|
|
131
|
-
type: 'array',
|
|
132
|
-
items: { type: 'string' },
|
|
133
|
-
default: ['validate', 'sanitize', 'isValid', 'assertValid'], description: 'Function names that count as query validation'
|
|
134
|
-
},
|
|
135
|
-
safeTemplateLiteralCallers: {
|
|
136
|
-
type: 'array',
|
|
137
|
-
items: { type: 'string' },
|
|
138
|
-
default: [],
|
|
139
|
-
description: 'Additional callers where template literals are never GraphQL. Format: object.method or ClassName.',
|
|
140
|
-
},
|
|
141
|
-
trustedSanitizers: {
|
|
142
|
-
type: 'array',
|
|
143
|
-
items: { type: 'string' },
|
|
144
|
-
default: [],
|
|
145
|
-
description: 'Additional function names to consider as GraphQL sanitizers',
|
|
146
|
-
},
|
|
147
|
-
trustedAnnotations: {
|
|
148
|
-
type: 'array',
|
|
149
|
-
items: { type: 'string' },
|
|
150
|
-
default: [],
|
|
151
|
-
description: 'Additional JSDoc annotations to consider as safe markers',
|
|
152
|
-
},
|
|
153
|
-
strictMode: {
|
|
154
|
-
type: 'boolean',
|
|
155
|
-
default: false,
|
|
156
|
-
description: 'Disable all false positive detection (strict mode)',
|
|
157
|
-
},
|
|
158
|
-
},
|
|
159
|
-
additionalProperties: false,
|
|
160
|
-
},
|
|
161
|
-
],
|
|
162
|
-
},
|
|
163
|
-
defaultOptions: [
|
|
164
|
-
{
|
|
165
|
-
allowIntrospection: false,
|
|
166
|
-
maxQueryDepth: 10,
|
|
167
|
-
trustedGraphqlLibraries: ['graphql', 'apollo-server', 'graphql-tools', 'graphql-tag'],
|
|
168
|
-
validationFunctions: ['validate', 'sanitize', 'isValid', 'assertValid'],
|
|
169
|
-
safeTemplateLiteralCallers: [],
|
|
170
|
-
trustedSanitizers: [],
|
|
171
|
-
trustedAnnotations: [],
|
|
172
|
-
strictMode: false,
|
|
173
|
-
},
|
|
174
|
-
],
|
|
175
|
-
create(context) {
|
|
176
|
-
const options = context.options[0] || {};
|
|
177
|
-
const { allowIntrospection = false, maxQueryDepth = 10, trustedGraphqlLibraries = ['graphql', 'apollo-server', 'graphql-tools', 'graphql-tag'], validationFunctions = ['validate', 'sanitize', 'isValid', 'assertValid'], safeTemplateLiteralCallers = [], trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
|
|
178
|
-
const filename = context.filename;
|
|
179
|
-
const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
|
|
180
|
-
trustedSanitizers,
|
|
181
|
-
trustedAnnotations,
|
|
182
|
-
trustedOrmPatterns: [],
|
|
183
|
-
strictMode,
|
|
184
|
-
});
|
|
185
|
-
const isGraphqlRelated = (node) => {
|
|
186
|
-
const callee = node.callee;
|
|
187
|
-
if (callee.type === 'Identifier') {
|
|
188
|
-
return trustedGraphqlLibraries.some(lib => callee.name.toLowerCase().includes(lib.toLowerCase()));
|
|
189
|
-
}
|
|
190
|
-
if (callee.type === 'MemberExpression' && callee.object.type === 'Identifier') {
|
|
191
|
-
const objectName = callee.object.name;
|
|
192
|
-
return trustedGraphqlLibraries.some(lib => objectName.toLowerCase().includes(lib.toLowerCase()));
|
|
193
|
-
}
|
|
194
|
-
return false;
|
|
195
|
-
};
|
|
196
|
-
const BUILTIN_SAFE_MEMBER_CALLERS = new Set([
|
|
197
|
-
'console.log', 'console.warn', 'console.error', 'console.info', 'console.debug',
|
|
198
|
-
'console.trace', 'logger.log', 'logger.info', 'logger.warn', 'logger.error',
|
|
199
|
-
'logger.debug',
|
|
200
|
-
]);
|
|
201
|
-
const BUILTIN_SAFE_CONSTRUCTORS = new Set(['URL', 'Error', 'TypeError', 'RangeError']);
|
|
202
|
-
const safeMemberCallers = new Set(BUILTIN_SAFE_MEMBER_CALLERS);
|
|
203
|
-
const safeConstructors = new Set(BUILTIN_SAFE_CONSTRUCTORS);
|
|
204
|
-
for (const caller of safeTemplateLiteralCallers) {
|
|
205
|
-
if (caller.includes('.')) {
|
|
206
|
-
safeMemberCallers.add(caller);
|
|
207
|
-
}
|
|
208
|
-
else {
|
|
209
|
-
safeConstructors.add(caller);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
const isInSafeCallerContext = (node) => {
|
|
213
|
-
let current = node.parent;
|
|
214
|
-
while (current) {
|
|
215
|
-
if (current.type === 'CallExpression') {
|
|
216
|
-
const callee = current.callee;
|
|
217
|
-
if (callee.type === 'MemberExpression' &&
|
|
218
|
-
callee.object.type === 'Identifier' &&
|
|
219
|
-
callee.property.type === 'Identifier') {
|
|
220
|
-
const key = `${callee.object.name}.${callee.property.name}`;
|
|
221
|
-
if (safeMemberCallers.has(key))
|
|
222
|
-
return true;
|
|
223
|
-
}
|
|
224
|
-
break;
|
|
225
|
-
}
|
|
226
|
-
if (current.type === eslint_devkit_1.AST_NODE_TYPES.NewExpression) {
|
|
227
|
-
if (current.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
228
|
-
safeConstructors.has(current.callee.name)) {
|
|
229
|
-
return true;
|
|
230
|
-
}
|
|
231
|
-
break;
|
|
232
|
-
}
|
|
233
|
-
if (current.type === eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression ||
|
|
234
|
-
current.type === eslint_devkit_1.AST_NODE_TYPES.FunctionExpression ||
|
|
235
|
-
current.type === eslint_devkit_1.AST_NODE_TYPES.FunctionDeclaration) {
|
|
236
|
-
break;
|
|
237
|
-
}
|
|
238
|
-
current = current.parent;
|
|
239
|
-
}
|
|
240
|
-
return false;
|
|
241
|
-
};
|
|
242
|
-
const isWordChar = (ch) => {
|
|
243
|
-
const code = ch.charCodeAt(0);
|
|
244
|
-
return (code >= 65 && code <= 90) ||
|
|
245
|
-
(code >= 97 && code <= 122) ||
|
|
246
|
-
(code >= 48 && code <= 57) ||
|
|
247
|
-
code === 95;
|
|
248
|
-
};
|
|
249
|
-
const isWhitespace = (ch) => ch === ' ' || ch === '\n' || ch === '\t' || ch === '\r';
|
|
250
|
-
const GRAPHQL_OP_KEYWORDS = ['query', 'mutation', 'subscription'];
|
|
251
|
-
const GRAPHQL_SCHEMA_KEYWORDS = ['type', 'interface', 'enum', 'scalar', 'input'];
|
|
252
|
-
const isAtLineStart = (text, idx) => {
|
|
253
|
-
for (let i = idx - 1; i >= 0; i--) {
|
|
254
|
-
const ch = text[i];
|
|
255
|
-
if (ch === '\n')
|
|
256
|
-
return true;
|
|
257
|
-
if (!isWhitespace(ch))
|
|
258
|
-
return false;
|
|
259
|
-
}
|
|
260
|
-
return true;
|
|
261
|
-
};
|
|
262
|
-
const isStandaloneSelectionSet = (text) => {
|
|
263
|
-
const trimmed = text.trim();
|
|
264
|
-
if (!trimmed.startsWith('{') || !trimmed.endsWith('}'))
|
|
265
|
-
return false;
|
|
266
|
-
let braceDepth = 0;
|
|
267
|
-
for (const ch of trimmed) {
|
|
268
|
-
if (ch === '{') {
|
|
269
|
-
braceDepth++;
|
|
270
|
-
if (braceDepth >= 2)
|
|
271
|
-
return true;
|
|
272
|
-
}
|
|
273
|
-
else if (ch === '}') {
|
|
274
|
-
braceDepth = Math.max(0, braceDepth - 1);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
return false;
|
|
278
|
-
};
|
|
279
|
-
const findKeywordAtBoundary = (text, keyword, startFrom = 0) => {
|
|
280
|
-
let pos = startFrom;
|
|
281
|
-
while (true) {
|
|
282
|
-
const idx = text.indexOf(keyword, pos);
|
|
283
|
-
if (idx === -1)
|
|
284
|
-
return -1;
|
|
285
|
-
const beforeOk = idx === 0 || !isWordChar(text[idx - 1]);
|
|
286
|
-
const afterIdx = idx + keyword.length;
|
|
287
|
-
const afterOk = afterIdx >= text.length || !isWordChar(text[afterIdx]);
|
|
288
|
-
if (beforeOk && afterOk)
|
|
289
|
-
return idx;
|
|
290
|
-
pos = idx + 1;
|
|
291
|
-
}
|
|
292
|
-
};
|
|
293
|
-
const isGraphqlTemplate = (node) => {
|
|
294
|
-
const staticText = node.quasis
|
|
295
|
-
.map(q => (q.value.cooked ?? q.value.raw).toLowerCase())
|
|
296
|
-
.join('');
|
|
297
|
-
for (const keyword of GRAPHQL_OP_KEYWORDS) {
|
|
298
|
-
let pos = 0;
|
|
299
|
-
while (pos < staticText.length) {
|
|
300
|
-
const idx = findKeywordAtBoundary(staticText, keyword, pos);
|
|
301
|
-
if (idx === -1)
|
|
302
|
-
break;
|
|
303
|
-
pos = idx + 1;
|
|
304
|
-
if (!isAtLineStart(staticText, idx))
|
|
305
|
-
continue;
|
|
306
|
-
let scan = idx + keyword.length;
|
|
307
|
-
while (scan < staticText.length && isWhitespace(staticText[scan]))
|
|
308
|
-
scan++;
|
|
309
|
-
while (scan < staticText.length && isWordChar(staticText[scan]))
|
|
310
|
-
scan++;
|
|
311
|
-
while (scan < staticText.length && isWhitespace(staticText[scan]))
|
|
312
|
-
scan++;
|
|
313
|
-
if (scan < staticText.length && (staticText[scan] === '{' || staticText[scan] === '(')) {
|
|
314
|
-
return true;
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
{
|
|
319
|
-
let pos = 0;
|
|
320
|
-
while (pos < staticText.length) {
|
|
321
|
-
const idx = findKeywordAtBoundary(staticText, 'fragment', pos);
|
|
322
|
-
if (idx === -1)
|
|
323
|
-
break;
|
|
324
|
-
pos = idx + 1;
|
|
325
|
-
if (!isAtLineStart(staticText, idx))
|
|
326
|
-
continue;
|
|
327
|
-
let scan = idx + 8;
|
|
328
|
-
while (scan < staticText.length && isWhitespace(staticText[scan]))
|
|
329
|
-
scan++;
|
|
330
|
-
const nameStart = scan;
|
|
331
|
-
while (scan < staticText.length && isWordChar(staticText[scan]))
|
|
332
|
-
scan++;
|
|
333
|
-
if (scan === nameStart)
|
|
334
|
-
continue;
|
|
335
|
-
while (scan < staticText.length && isWhitespace(staticText[scan]))
|
|
336
|
-
scan++;
|
|
337
|
-
if (staticText.slice(scan, scan + 2) === 'on' && (scan + 2 >= staticText.length || !isWordChar(staticText[scan + 2]))) {
|
|
338
|
-
return true;
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
if (staticText.includes('{'))
|
|
343
|
-
for (const keyword of GRAPHQL_SCHEMA_KEYWORDS) {
|
|
344
|
-
let pos = 0;
|
|
345
|
-
while (pos < staticText.length) {
|
|
346
|
-
const idx = findKeywordAtBoundary(staticText, keyword, pos);
|
|
347
|
-
if (idx === -1)
|
|
348
|
-
break;
|
|
349
|
-
pos = idx + 1;
|
|
350
|
-
if (!isAtLineStart(staticText, idx))
|
|
351
|
-
continue;
|
|
352
|
-
let scan = idx + keyword.length;
|
|
353
|
-
if (scan >= staticText.length || !isWhitespace(staticText[scan]))
|
|
354
|
-
continue;
|
|
355
|
-
while (scan < staticText.length && isWhitespace(staticText[scan]))
|
|
356
|
-
scan++;
|
|
357
|
-
if (scan < staticText.length && isWordChar(staticText[scan])) {
|
|
358
|
-
return true;
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
return isStandaloneSelectionSet(staticText);
|
|
363
|
-
};
|
|
364
|
-
const templateHasIntrospection = (node) => {
|
|
365
|
-
return node.quasis.some(q => {
|
|
366
|
-
const text = (q.value.cooked ?? q.value.raw).toLowerCase();
|
|
367
|
-
return text.includes('__schema') || text.includes('__type');
|
|
368
|
-
});
|
|
369
|
-
};
|
|
370
|
-
const templateQueryDepth = (node) => {
|
|
371
|
-
let depth = 0;
|
|
372
|
-
let braceCount = 0;
|
|
373
|
-
for (const q of node.quasis) {
|
|
374
|
-
const text = q.value.cooked ?? q.value.raw;
|
|
375
|
-
for (const char of text) {
|
|
376
|
-
if (char === '{') {
|
|
377
|
-
braceCount++;
|
|
378
|
-
depth = Math.max(depth, braceCount);
|
|
379
|
-
}
|
|
380
|
-
else if (char === '}') {
|
|
381
|
-
braceCount--;
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
return depth;
|
|
386
|
-
};
|
|
387
|
-
const containsGraphqlText = (text) => {
|
|
388
|
-
const lower = text.toLowerCase();
|
|
389
|
-
for (const keyword of GRAPHQL_OP_KEYWORDS) {
|
|
390
|
-
const idx = findKeywordAtBoundary(lower, keyword);
|
|
391
|
-
if (idx === -1)
|
|
392
|
-
continue;
|
|
393
|
-
if (!isAtLineStart(lower, idx))
|
|
394
|
-
continue;
|
|
395
|
-
let scan = idx + keyword.length;
|
|
396
|
-
while (scan < lower.length && isWhitespace(lower[scan]))
|
|
397
|
-
scan++;
|
|
398
|
-
while (scan < lower.length && isWordChar(lower[scan]))
|
|
399
|
-
scan++;
|
|
400
|
-
while (scan < lower.length && isWhitespace(lower[scan]))
|
|
401
|
-
scan++;
|
|
402
|
-
if (scan < lower.length && (lower[scan] === '{' || lower[scan] === '('))
|
|
403
|
-
return true;
|
|
404
|
-
}
|
|
405
|
-
const fragIdx = findKeywordAtBoundary(lower, 'fragment');
|
|
406
|
-
if (fragIdx !== -1 && isAtLineStart(lower, fragIdx)) {
|
|
407
|
-
let scan = fragIdx + 8;
|
|
408
|
-
while (scan < lower.length && isWhitespace(lower[scan]))
|
|
409
|
-
scan++;
|
|
410
|
-
const nameStart = scan;
|
|
411
|
-
while (scan < lower.length && isWordChar(lower[scan]))
|
|
412
|
-
scan++;
|
|
413
|
-
if (scan > nameStart) {
|
|
414
|
-
while (scan < lower.length && isWhitespace(lower[scan]))
|
|
415
|
-
scan++;
|
|
416
|
-
if (lower.slice(scan, scan + 2) === 'on' && (scan + 2 >= lower.length || !isWordChar(lower[scan + 2])))
|
|
417
|
-
return true;
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
if (lower.includes('{'))
|
|
421
|
-
for (const keyword of GRAPHQL_SCHEMA_KEYWORDS) {
|
|
422
|
-
const idx = findKeywordAtBoundary(lower, keyword);
|
|
423
|
-
if (idx === -1)
|
|
424
|
-
continue;
|
|
425
|
-
if (!isAtLineStart(lower, idx))
|
|
426
|
-
continue;
|
|
427
|
-
let scan = idx + keyword.length;
|
|
428
|
-
if (scan >= lower.length || !isWhitespace(lower[scan]))
|
|
429
|
-
continue;
|
|
430
|
-
while (scan < lower.length && isWhitespace(lower[scan]))
|
|
431
|
-
scan++;
|
|
432
|
-
if (scan < lower.length && isWordChar(lower[scan]))
|
|
433
|
-
return true;
|
|
434
|
-
}
|
|
435
|
-
return isStandaloneSelectionSet(text);
|
|
436
|
-
};
|
|
437
|
-
const concatenatedStaticText = (node) => {
|
|
438
|
-
if (node.type === 'BinaryExpression' && node.operator === '+') {
|
|
439
|
-
return concatenatedStaticText(node.left) + concatenatedStaticText(node.right);
|
|
440
|
-
}
|
|
441
|
-
if (node.type === 'Literal' && typeof node.value === 'string')
|
|
442
|
-
return node.value;
|
|
443
|
-
if (node.type === 'TemplateLiteral') {
|
|
444
|
-
return node.quasis.map((q) => q.value.cooked ?? q.value.raw).join('');
|
|
445
|
-
}
|
|
446
|
-
return '';
|
|
447
|
-
};
|
|
448
|
-
const isInputValidated = (inputNode) => {
|
|
449
|
-
let current = inputNode;
|
|
450
|
-
while (current) {
|
|
451
|
-
if (current.type === 'CallExpression' &&
|
|
452
|
-
current.callee.type === 'Identifier' &&
|
|
453
|
-
validationFunctions.includes(current.callee.name)) {
|
|
454
|
-
return true;
|
|
455
|
-
}
|
|
456
|
-
current = current.parent;
|
|
457
|
-
}
|
|
458
|
-
return false;
|
|
459
|
-
};
|
|
460
|
-
return {
|
|
461
|
-
TemplateLiteral(node) {
|
|
462
|
-
if (isInSafeCallerContext(node)) {
|
|
463
|
-
return;
|
|
464
|
-
}
|
|
465
|
-
if (!isGraphqlTemplate(node)) {
|
|
466
|
-
return;
|
|
467
|
-
}
|
|
468
|
-
if (!allowIntrospection && templateHasIntrospection(node)) {
|
|
469
|
-
if (safetyChecker.isSafe(node, context)) {
|
|
470
|
-
return;
|
|
471
|
-
}
|
|
472
|
-
context.report({
|
|
473
|
-
node,
|
|
474
|
-
messageId: 'introspectionQuery',
|
|
475
|
-
data: {
|
|
476
|
-
filePath: filename,
|
|
477
|
-
line: String(node.loc?.start.line ?? 0),
|
|
478
|
-
},
|
|
479
|
-
});
|
|
480
|
-
return;
|
|
481
|
-
}
|
|
482
|
-
if (node.expressions.length > 0) {
|
|
483
|
-
const allExpressionsSafe = node.expressions.every((expr) => isInputValidated(expr) || safetyChecker.isSafe(expr, context));
|
|
484
|
-
if (!allExpressionsSafe) {
|
|
485
|
-
context.report({
|
|
486
|
-
node,
|
|
487
|
-
messageId: 'unsafeVariableInterpolation',
|
|
488
|
-
data: {
|
|
489
|
-
filePath: filename,
|
|
490
|
-
line: String(node.loc?.start.line ?? 0),
|
|
491
|
-
},
|
|
492
|
-
suggest: [
|
|
493
|
-
{
|
|
494
|
-
messageId: 'useQueryBuilder',
|
|
495
|
-
fix: () => null
|
|
496
|
-
},
|
|
497
|
-
],
|
|
498
|
-
});
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
const depth = templateQueryDepth(node);
|
|
502
|
-
if (depth > maxQueryDepth) {
|
|
503
|
-
context.report({
|
|
504
|
-
node,
|
|
505
|
-
messageId: 'complexQueryDos',
|
|
506
|
-
data: {
|
|
507
|
-
filePath: filename,
|
|
508
|
-
line: String(node.loc?.start.line ?? 0),
|
|
509
|
-
},
|
|
510
|
-
suggest: [
|
|
511
|
-
{
|
|
512
|
-
messageId: 'limitQueryDepth',
|
|
513
|
-
fix: () => null
|
|
514
|
-
},
|
|
515
|
-
],
|
|
516
|
-
});
|
|
517
|
-
}
|
|
518
|
-
},
|
|
519
|
-
Literal(node) {
|
|
520
|
-
if (typeof node.value !== 'string') {
|
|
521
|
-
return;
|
|
522
|
-
}
|
|
523
|
-
const queryText = node.value;
|
|
524
|
-
if (!containsGraphqlText(queryText)) {
|
|
525
|
-
return;
|
|
526
|
-
}
|
|
527
|
-
const lowerQuery = queryText.toLowerCase();
|
|
528
|
-
if (!allowIntrospection && (lowerQuery.includes('__schema') || lowerQuery.includes('__type'))) {
|
|
529
|
-
if (safetyChecker.isSafe(node, context)) {
|
|
530
|
-
return;
|
|
531
|
-
}
|
|
532
|
-
context.report({
|
|
533
|
-
node,
|
|
534
|
-
messageId: 'introspectionQuery',
|
|
535
|
-
data: {
|
|
536
|
-
filePath: filename,
|
|
537
|
-
line: String(node.loc?.start.line ?? 0),
|
|
538
|
-
},
|
|
539
|
-
});
|
|
540
|
-
}
|
|
541
|
-
let depth = 0;
|
|
542
|
-
let bc = 0;
|
|
543
|
-
for (const ch of queryText) {
|
|
544
|
-
if (ch === '{') {
|
|
545
|
-
bc++;
|
|
546
|
-
depth = Math.max(depth, bc);
|
|
547
|
-
}
|
|
548
|
-
else if (ch === '}') {
|
|
549
|
-
bc--;
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
if (depth > maxQueryDepth) {
|
|
553
|
-
context.report({
|
|
554
|
-
node,
|
|
555
|
-
messageId: 'complexQueryDos',
|
|
556
|
-
data: {
|
|
557
|
-
filePath: filename,
|
|
558
|
-
line: String(node.loc?.start.line ?? 0),
|
|
559
|
-
},
|
|
560
|
-
});
|
|
561
|
-
}
|
|
562
|
-
},
|
|
563
|
-
BinaryExpression(node) {
|
|
564
|
-
if (node.operator !== '+') {
|
|
565
|
-
return;
|
|
566
|
-
}
|
|
567
|
-
const fullText = concatenatedStaticText(node);
|
|
568
|
-
if (!containsGraphqlText(fullText)) {
|
|
569
|
-
return;
|
|
570
|
-
}
|
|
571
|
-
if (safetyChecker.isSafe(node, context)) {
|
|
572
|
-
return;
|
|
573
|
-
}
|
|
574
|
-
context.report({
|
|
575
|
-
node,
|
|
576
|
-
messageId: 'graphqlInjection',
|
|
577
|
-
data: {
|
|
578
|
-
filePath: filename,
|
|
579
|
-
line: String(node.loc?.start.line ?? 0),
|
|
580
|
-
severity: 'HIGH',
|
|
581
|
-
safeAlternative: 'Use GraphQL variables or query builders instead of string concatenation',
|
|
582
|
-
},
|
|
583
|
-
});
|
|
584
|
-
},
|
|
585
|
-
CallExpression(node) {
|
|
586
|
-
if (!isGraphqlRelated(node)) {
|
|
587
|
-
return;
|
|
588
|
-
}
|
|
589
|
-
const callee = node.callee;
|
|
590
|
-
if (callee.type === 'MemberExpression' &&
|
|
591
|
-
callee.property.type === 'Identifier' &&
|
|
592
|
-
['execute', 'executeQuery', 'query', 'mutate', 'subscribe'].includes(callee.property.name)) {
|
|
593
|
-
for (const arg of node.arguments) {
|
|
594
|
-
if (arg.type === 'Identifier' && !isInputValidated(arg)) {
|
|
595
|
-
if (safetyChecker.isSafe(arg, context)) {
|
|
596
|
-
continue;
|
|
597
|
-
}
|
|
598
|
-
context.report({
|
|
599
|
-
node: arg,
|
|
600
|
-
messageId: 'missingInputValidation',
|
|
601
|
-
data: {
|
|
602
|
-
filePath: filename,
|
|
603
|
-
line: String(node.loc?.start.line ?? 0),
|
|
604
|
-
},
|
|
605
|
-
});
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
};
|
|
611
|
-
},
|
|
612
|
-
});
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noGraphqlInjection=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");const eslint_devkit_3=require("@interlace/eslint-devkit");exports.noGraphqlInjection=(0,eslint_devkit_1.createRule)({name:"no-graphql-injection",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-secure-coding/docs/rules/no-graphql-injection.md",description:"Detects GraphQL injection vulnerabilities and DoS attacks",cwe:"CWE-89"},hasSuggestions:true,messages:{graphqlInjection:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"GraphQL Injection",cwe:"CWE-89",description:"GraphQL injection vulnerability detected",severity:"{{severity}}",fix:"{{safeAlternative}}",documentationLink:"https://owasp.org/Top10/2025/A05_2025-Injection/"}),introspectionQuery:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"Dangerous Introspection Query",cwe:"CWE-200",description:"Introspection query may leak schema information",severity:"HIGH",fix:"Disable introspection in production",documentationLink:"https://graphql.org/learn/introspection/"}),complexQueryDos:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"Complex Query DoS Risk",cwe:"CWE-400",description:"Complex query may cause DoS",severity:"MEDIUM",fix:"Limit query depth and complexity",documentationLink:"https://graphql.org/learn/queries/"}),unsafeVariableInterpolation:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"Unsafe Variable Interpolation",cwe:"CWE-89",description:"Unsafe interpolation in GraphQL query",severity:"HIGH",fix:"Use GraphQL variables instead of string interpolation",documentationLink:"https://graphql.org/learn/queries/#variables"}),missingInputValidation:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"Missing Input Validation",cwe:"CWE-20",description:"GraphQL input not validated",severity:"MEDIUM",fix:"Validate all user inputs before GraphQL execution",documentationLink:"https://graphql.org/learn/validation/"}),useQueryBuilder:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.INFO,issueName:"Use Query Builder",description:"Use GraphQL query builders for safe construction",severity:"LOW",fix:"Use graphql-tag or similar libraries",documentationLink:"https://www.npmjs.com/package/graphql-tag"}),disableIntrospection:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.INFO,issueName:"Disable Introspection",description:"Disable GraphQL introspection in production",severity:"LOW",fix:"Set introspection: false in GraphQL config",documentationLink:"https://www.apollographql.com/docs/apollo-server/security/introspection/"}),limitQueryDepth:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.INFO,issueName:"Limit Query Depth",description:"Limit maximum query depth",severity:"LOW",fix:"Use depth limiting plugins or custom validation",documentationLink:"https://www.npmjs.com/package/graphql-depth-limit"}),strategyQueryBuilder:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.STRATEGY,issueName:"Query Builder Strategy",description:"Use typed query builders for compile-time safety",severity:"LOW",fix:"Use GraphQL code generation tools",documentationLink:"https://graphql-code-generator.com/"}),strategyInputValidation:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.STRATEGY,issueName:"Input Validation Strategy",description:"Validate all inputs at GraphQL resolver level",severity:"LOW",fix:"Implement custom scalars and input validation",documentationLink:"https://graphql.org/learn/schema/#scalar-types"}),strategyIntrospection:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.STRATEGY,issueName:"Introspection Strategy",description:"Control introspection access based on environment",severity:"LOW",fix:"Enable introspection only for development/admin users",documentationLink:"https://www.apollographql.com/docs/apollo-server/security/introspection/"})},schema:[{type:"object",properties:{allowIntrospection:{type:"boolean",default:false,description:"Allow introspection queries"},maxQueryDepth:{type:"number",minimum:1,default:10,description:"Maximum query nesting depth before reporting a DoS risk"},trustedGraphqlLibraries:{type:"array",items:{type:"string"},default:["graphql","apollo-server","graphql-tools","graphql-tag"],description:"GraphQL libraries recognised as query builders"},validationFunctions:{type:"array",items:{type:"string"},default:["validate","sanitize","isValid","assertValid"],description:"Function names that count as query validation"},safeTemplateLiteralCallers:{type:"array",items:{type:"string"},default:[],description:"Additional callers where template literals are never GraphQL. Format: object.method or ClassName."},trustedSanitizers:{type:"array",items:{type:"string"},default:[],description:"Additional function names to consider as GraphQL sanitizers"},trustedAnnotations:{type:"array",items:{type:"string"},default:[],description:"Additional JSDoc annotations to consider as safe markers"},strictMode:{type:"boolean",default:false,description:"Disable all false positive detection (strict mode)"}},additionalProperties:false}]},defaultOptions:[{allowIntrospection:false,maxQueryDepth:10,trustedGraphqlLibraries:["graphql","apollo-server","graphql-tools","graphql-tag"],validationFunctions:["validate","sanitize","isValid","assertValid"],safeTemplateLiteralCallers:[],trustedSanitizers:[],trustedAnnotations:[],strictMode:false}],create(context){const options=context.options[0]||{};const{allowIntrospection=false,maxQueryDepth=10,trustedGraphqlLibraries=["graphql","apollo-server","graphql-tools","graphql-tag"],validationFunctions=["validate","sanitize","isValid","assertValid"],safeTemplateLiteralCallers=[],trustedSanitizers=[],trustedAnnotations=[],strictMode=false}=options;const filename=context.filename;const safetyChecker=(0,eslint_devkit_3.createSafetyChecker)({trustedSanitizers,trustedAnnotations,trustedOrmPatterns:[],strictMode});const isGraphqlRelated=node=>{const callee=node.callee;if(callee.type==="Identifier"){return trustedGraphqlLibraries.some(lib=>callee.name.toLowerCase().includes(lib.toLowerCase()))}if(callee.type==="MemberExpression"&&callee.object.type==="Identifier"){const objectName=callee.object.name;return trustedGraphqlLibraries.some(lib=>objectName.toLowerCase().includes(lib.toLowerCase()))}return false};const BUILTIN_SAFE_MEMBER_CALLERS=new Set(["console.log","console.warn","console.error","console.info","console.debug","console.trace","logger.log","logger.info","logger.warn","logger.error","logger.debug"]);const BUILTIN_SAFE_CONSTRUCTORS=new Set(["URL","Error","TypeError","RangeError"]);const safeMemberCallers=new Set(BUILTIN_SAFE_MEMBER_CALLERS);const safeConstructors=new Set(BUILTIN_SAFE_CONSTRUCTORS);for(const caller of safeTemplateLiteralCallers){if(caller.includes(".")){safeMemberCallers.add(caller)}else{safeConstructors.add(caller)}}const isInSafeCallerContext=node=>{let current=node.parent;while(current){if(current.type==="CallExpression"){const callee=current.callee;if(callee.type==="MemberExpression"&&callee.object.type==="Identifier"&&callee.property.type==="Identifier"){const key=`${callee.object.name}.${callee.property.name}`;if(safeMemberCallers.has(key))return true}break}if(current.type===eslint_devkit_1.AST_NODE_TYPES.NewExpression){if(current.callee.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&safeConstructors.has(current.callee.name)){return true}break}if(current.type===eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression||current.type===eslint_devkit_1.AST_NODE_TYPES.FunctionExpression||current.type===eslint_devkit_1.AST_NODE_TYPES.FunctionDeclaration){break}current=current.parent}return false};const isWordChar=ch=>{const code=ch.charCodeAt(0);return code>=65&&code<=90||code>=97&&code<=122||code>=48&&code<=57||code===95};const isWhitespace=ch=>ch===" "||ch==="\n"||ch===" "||ch==="\r";const GRAPHQL_OP_KEYWORDS=["query","mutation","subscription"];const GRAPHQL_SCHEMA_KEYWORDS=["type","interface","enum","scalar","input"];const isAtLineStart=(text,idx)=>{for(let i=idx-1;i>=0;i--){const ch=text[i];if(ch==="\n")return true;if(!isWhitespace(ch))return false}return true};const isStandaloneSelectionSet=text=>{const trimmed=text.trim();if(!trimmed.startsWith("{")||!trimmed.endsWith("}"))return false;let braceDepth=0;for(const ch of trimmed){if(ch==="{"){braceDepth++;if(braceDepth>=2)return true}else if(ch==="}"){braceDepth=Math.max(0,braceDepth-1)}}return false};const findKeywordAtBoundary=(text,keyword,startFrom=0)=>{let pos=startFrom;while(true){const idx=text.indexOf(keyword,pos);if(idx===-1)return-1;const beforeOk=idx===0||!isWordChar(text[idx-1]);const afterIdx=idx+keyword.length;const afterOk=afterIdx>=text.length||!isWordChar(text[afterIdx]);if(beforeOk&&afterOk)return idx;pos=idx+1}};const isGraphqlTemplate=node=>{const staticText=node.quasis.map(q=>(q.value.cooked??q.value.raw).toLowerCase()).join("");for(const keyword of GRAPHQL_OP_KEYWORDS){let pos=0;while(pos<staticText.length){const idx=findKeywordAtBoundary(staticText,keyword,pos);if(idx===-1)break;pos=idx+1;if(!isAtLineStart(staticText,idx))continue;let scan=idx+keyword.length;while(scan<staticText.length&&isWhitespace(staticText[scan]))scan++;while(scan<staticText.length&&isWordChar(staticText[scan]))scan++;while(scan<staticText.length&&isWhitespace(staticText[scan]))scan++;if(scan<staticText.length&&(staticText[scan]==="{"||staticText[scan]==="(")){return true}}}{let pos=0;while(pos<staticText.length){const idx=findKeywordAtBoundary(staticText,"fragment",pos);if(idx===-1)break;pos=idx+1;if(!isAtLineStart(staticText,idx))continue;let scan=idx+8;while(scan<staticText.length&&isWhitespace(staticText[scan]))scan++;const nameStart=scan;while(scan<staticText.length&&isWordChar(staticText[scan]))scan++;if(scan===nameStart)continue;while(scan<staticText.length&&isWhitespace(staticText[scan]))scan++;if(staticText.slice(scan,scan+2)==="on"&&(scan+2>=staticText.length||!isWordChar(staticText[scan+2]))){return true}}}if(staticText.includes("{"))for(const keyword of GRAPHQL_SCHEMA_KEYWORDS){let pos=0;while(pos<staticText.length){const idx=findKeywordAtBoundary(staticText,keyword,pos);if(idx===-1)break;pos=idx+1;if(!isAtLineStart(staticText,idx))continue;let scan=idx+keyword.length;if(scan>=staticText.length||!isWhitespace(staticText[scan]))continue;while(scan<staticText.length&&isWhitespace(staticText[scan]))scan++;if(scan<staticText.length&&isWordChar(staticText[scan])){return true}}}return isStandaloneSelectionSet(staticText)};const templateHasIntrospection=node=>{return node.quasis.some(q=>{const text=(q.value.cooked??q.value.raw).toLowerCase();return text.includes("__schema")||text.includes("__type")})};const templateQueryDepth=node=>{let depth=0;let braceCount=0;for(const q of node.quasis){const text=q.value.cooked??q.value.raw;for(const char of text){if(char==="{"){braceCount++;depth=Math.max(depth,braceCount)}else if(char==="}"){braceCount--}}}return depth};const containsGraphqlText=text=>{const lower=text.toLowerCase();for(const keyword of GRAPHQL_OP_KEYWORDS){const idx=findKeywordAtBoundary(lower,keyword);if(idx===-1)continue;if(!isAtLineStart(lower,idx))continue;let scan=idx+keyword.length;while(scan<lower.length&&isWhitespace(lower[scan]))scan++;while(scan<lower.length&&isWordChar(lower[scan]))scan++;while(scan<lower.length&&isWhitespace(lower[scan]))scan++;if(scan<lower.length&&(lower[scan]==="{"||lower[scan]==="("))return true}const fragIdx=findKeywordAtBoundary(lower,"fragment");if(fragIdx!==-1&&isAtLineStart(lower,fragIdx)){let scan=fragIdx+8;while(scan<lower.length&&isWhitespace(lower[scan]))scan++;const nameStart=scan;while(scan<lower.length&&isWordChar(lower[scan]))scan++;if(scan>nameStart){while(scan<lower.length&&isWhitespace(lower[scan]))scan++;if(lower.slice(scan,scan+2)==="on"&&(scan+2>=lower.length||!isWordChar(lower[scan+2])))return true}}if(lower.includes("{"))for(const keyword of GRAPHQL_SCHEMA_KEYWORDS){const idx=findKeywordAtBoundary(lower,keyword);if(idx===-1)continue;if(!isAtLineStart(lower,idx))continue;let scan=idx+keyword.length;if(scan>=lower.length||!isWhitespace(lower[scan]))continue;while(scan<lower.length&&isWhitespace(lower[scan]))scan++;if(scan<lower.length&&isWordChar(lower[scan]))return true}return isStandaloneSelectionSet(text)};const concatenatedStaticText=node=>{if(node.type==="BinaryExpression"&&node.operator==="+"){return concatenatedStaticText(node.left)+concatenatedStaticText(node.right)}if(node.type==="Literal"&&typeof node.value==="string")return node.value;if(node.type==="TemplateLiteral"){return node.quasis.map(q=>q.value.cooked??q.value.raw).join("")}return""};const isInputValidated=inputNode=>{let current=inputNode;while(current){if(current.type==="CallExpression"&¤t.callee.type==="Identifier"&&validationFunctions.includes(current.callee.name)){return true}current=current.parent}return false};return{TemplateLiteral(node){if(isInSafeCallerContext(node)){return}if(!isGraphqlTemplate(node)){return}if(!allowIntrospection&&templateHasIntrospection(node)){if(safetyChecker.isSafe(node,context)){return}context.report({node,messageId:"introspectionQuery",data:{filePath:filename,line:String(node.loc?.start.line??0)}});return}if(node.expressions.length>0){const allExpressionsSafe=node.expressions.every(expr=>isInputValidated(expr)||safetyChecker.isSafe(expr,context));if(!allExpressionsSafe){context.report({node,messageId:"unsafeVariableInterpolation",data:{filePath:filename,line:String(node.loc?.start.line??0)},suggest:[{messageId:"useQueryBuilder",fix:()=>null}]})}}const depth=templateQueryDepth(node);if(depth>maxQueryDepth){context.report({node,messageId:"complexQueryDos",data:{filePath:filename,line:String(node.loc?.start.line??0)},suggest:[{messageId:"limitQueryDepth",fix:()=>null}]})}},Literal(node){if(typeof node.value!=="string"){return}const queryText=node.value;if(!containsGraphqlText(queryText)){return}const lowerQuery=queryText.toLowerCase();if(!allowIntrospection&&(lowerQuery.includes("__schema")||lowerQuery.includes("__type"))){if(safetyChecker.isSafe(node,context)){return}context.report({node,messageId:"introspectionQuery",data:{filePath:filename,line:String(node.loc?.start.line??0)}})}let depth=0;let bc=0;for(const ch of queryText){if(ch==="{"){bc++;depth=Math.max(depth,bc)}else if(ch==="}"){bc--}}if(depth>maxQueryDepth){context.report({node,messageId:"complexQueryDos",data:{filePath:filename,line:String(node.loc?.start.line??0)}})}},BinaryExpression(node){if(node.operator!=="+"){return}const fullText=concatenatedStaticText(node);if(!containsGraphqlText(fullText)){return}if(safetyChecker.isSafe(node,context)){return}context.report({node,messageId:"graphqlInjection",data:{filePath:filename,line:String(node.loc?.start.line??0),severity:"HIGH",safeAlternative:"Use GraphQL variables or query builders instead of string concatenation"}})},CallExpression(node){if(!isGraphqlRelated(node)){return}const callee=node.callee;if(callee.type==="MemberExpression"&&callee.property.type==="Identifier"&&["execute","executeQuery","query","mutate","subscribe"].includes(callee.property.name)){for(const arg of node.arguments){if(arg.type==="Identifier"&&!isInputValidated(arg)){if(safetyChecker.isSafe(arg,context)){continue}context.report({node:arg,messageId:"missingInputValidation",data:{filePath:filename,line:String(node.loc?.start.line??0)}})}}}}}}});
|