eslint-plugin-secure-coding 3.3.4 → 3.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/package.json +30 -37
- package/src/index.d.ts +17 -23
- package/src/index.js +2 -113
- package/src/oxlint.d.ts +17 -20
- package/src/oxlint.js +0 -18
- package/src/rules/detect-non-literal-regexp/index.js +5 -79
- package/src/rules/detect-object-injection/index.js +2 -221
- package/src/rules/detect-weak-password-validation/index.js +0 -12
- package/src/rules/no-directive-injection/index.js +0 -33
- package/src/rules/no-electron-security-issues/index.js +0 -41
- package/src/rules/no-format-string-injection/index.js +0 -82
- package/src/rules/no-graphql-injection/index.js +86 -151
- package/src/rules/no-hardcoded-credentials/index.js +142 -184
- package/src/rules/no-hardcoded-session-tokens/index.js +0 -11
- package/src/rules/no-improper-sanitization/index.js +0 -44
- package/src/rules/no-improper-type-validation/index.js +1 -51
- package/src/rules/no-insecure-comparison/index.js +39 -35
- package/src/rules/no-ldap-injection/index.js +8 -61
- package/src/rules/no-missing-authentication/index.js +2 -56
- package/src/rules/no-pii-in-logs/index.js +0 -13
- package/src/rules/no-privilege-escalation/index.js +2 -49
- package/src/rules/no-redos-vulnerable-regex/index.js +3 -49
- package/src/rules/no-sensitive-data-exposure/index.js +5 -29
- package/src/rules/no-template-injection/index.js +0 -6
- package/src/rules/no-unchecked-loop-condition/index.js +3 -99
- package/src/rules/no-unlimited-resource-allocation/index.js +2 -75
- package/src/rules/no-unsafe-deserialization/index.js +55 -66
- package/src/rules/no-unsafe-regex-construction/index.js +22 -62
- package/src/rules/no-weak-password-recovery/index.js +0 -43
- package/src/rules/no-xpath-injection/index.js +13 -75
- package/src/rules/no-xxe-injection/index.js +1 -47
- package/src/rules/require-backend-authorization/index.js +0 -10
- package/src/rules/require-secure-defaults/index.js +0 -10
- package/src/types/index.js +0 -5
- package/AGENTS.md +0 -131
- package/CHANGELOG.md +0 -311
- package/src/rules/detect-non-literal-regexp/index.d.ts +0 -28
- package/src/rules/detect-object-injection/index.d.ts +0 -35
- package/src/rules/detect-weak-password-validation/index.d.ts +0 -12
- package/src/rules/no-directive-injection/index.d.ts +0 -38
- package/src/rules/no-electron-security-issues/index.d.ts +0 -36
- package/src/rules/no-format-string-injection/index.d.ts +0 -44
- package/src/rules/no-graphql-injection/index.d.ts +0 -47
- package/src/rules/no-hardcoded-credentials/index.d.ts +0 -44
- package/src/rules/no-hardcoded-session-tokens/index.d.ts +0 -12
- package/src/rules/no-improper-sanitization/index.d.ts +0 -38
- package/src/rules/no-improper-type-validation/index.d.ts +0 -36
- package/src/rules/no-insecure-comparison/index.d.ts +0 -26
- package/src/rules/no-ldap-injection/index.d.ts +0 -53
- package/src/rules/no-missing-authentication/index.d.ts +0 -32
- package/src/rules/no-pii-in-logs/index.d.ts +0 -12
- package/src/rules/no-privilege-escalation/index.d.ts +0 -32
- package/src/rules/no-redos-vulnerable-regex/index.d.ts +0 -28
- package/src/rules/no-sensitive-data-exposure/index.d.ts +0 -30
- package/src/rules/no-template-injection/index.d.ts +0 -23
- package/src/rules/no-unchecked-loop-condition/index.d.ts +0 -38
- package/src/rules/no-unlimited-resource-allocation/index.d.ts +0 -38
- package/src/rules/no-unsafe-deserialization/index.d.ts +0 -40
- package/src/rules/no-unsafe-regex-construction/index.d.ts +0 -30
- package/src/rules/no-weak-password-recovery/index.d.ts +0 -38
- package/src/rules/no-xpath-injection/index.d.ts +0 -39
- package/src/rules/no-xxe-injection/index.d.ts +0 -36
- package/src/rules/require-backend-authorization/index.d.ts +0 -12
- package/src/rules/require-secure-defaults/index.d.ts +0 -12
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
-
* MIT license that can be found in the LICENSE file.
|
|
6
|
-
*/
|
|
7
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.noGraphqlInjection = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
@@ -180,41 +175,30 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
180
175
|
create(context) {
|
|
181
176
|
const options = context.options[0] || {};
|
|
182
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;
|
|
183
|
-
const sourceCode = context.sourceCode;
|
|
184
178
|
const filename = context.filename;
|
|
185
|
-
// Create safety checker for false positive detection
|
|
186
179
|
const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
|
|
187
180
|
trustedSanitizers,
|
|
188
181
|
trustedAnnotations,
|
|
189
182
|
trustedOrmPatterns: [],
|
|
190
183
|
strictMode,
|
|
191
184
|
});
|
|
192
|
-
/**
|
|
193
|
-
* Check if this is a GraphQL-related operation
|
|
194
|
-
*/
|
|
195
185
|
const isGraphqlRelated = (node) => {
|
|
196
186
|
const callee = node.callee;
|
|
197
|
-
// Check for GraphQL library calls
|
|
198
187
|
if (callee.type === 'Identifier') {
|
|
199
188
|
return trustedGraphqlLibraries.some(lib => callee.name.toLowerCase().includes(lib.toLowerCase()));
|
|
200
189
|
}
|
|
201
|
-
// Check for member expressions like graphql.parse, apollo.executeQuery
|
|
202
190
|
if (callee.type === 'MemberExpression' && callee.object.type === 'Identifier') {
|
|
203
191
|
const objectName = callee.object.name;
|
|
204
192
|
return trustedGraphqlLibraries.some(lib => objectName.toLowerCase().includes(lib.toLowerCase()));
|
|
205
193
|
}
|
|
206
194
|
return false;
|
|
207
195
|
};
|
|
208
|
-
// ─── AST-based safe caller detection ───────────────────────────────
|
|
209
|
-
// Hard-coded safe callers that should NEVER contain GraphQL queries.
|
|
210
|
-
// Users can extend via safeTemplateLiteralCallers option.
|
|
211
196
|
const BUILTIN_SAFE_MEMBER_CALLERS = new Set([
|
|
212
197
|
'console.log', 'console.warn', 'console.error', 'console.info', 'console.debug',
|
|
213
198
|
'console.trace', 'logger.log', 'logger.info', 'logger.warn', 'logger.error',
|
|
214
199
|
'logger.debug',
|
|
215
200
|
]);
|
|
216
201
|
const BUILTIN_SAFE_CONSTRUCTORS = new Set(['URL', 'Error', 'TypeError', 'RangeError']);
|
|
217
|
-
// Merge user-provided callers
|
|
218
202
|
const safeMemberCallers = new Set(BUILTIN_SAFE_MEMBER_CALLERS);
|
|
219
203
|
const safeConstructors = new Set(BUILTIN_SAFE_CONSTRUCTORS);
|
|
220
204
|
for (const caller of safeTemplateLiteralCallers) {
|
|
@@ -225,17 +209,11 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
225
209
|
safeConstructors.add(caller);
|
|
226
210
|
}
|
|
227
211
|
}
|
|
228
|
-
/**
|
|
229
|
-
* AST-based check: is this node inside a call that can't be GraphQL?
|
|
230
|
-
* Walks up from the TemplateLiteral to its nearest CallExpression/NewExpression parent.
|
|
231
|
-
*/
|
|
232
212
|
const isInSafeCallerContext = (node) => {
|
|
233
213
|
let current = node.parent;
|
|
234
214
|
while (current) {
|
|
235
|
-
// Direct arg to a call: console.log(`...`)
|
|
236
215
|
if (current.type === 'CallExpression') {
|
|
237
216
|
const callee = current.callee;
|
|
238
|
-
// object.method() pattern
|
|
239
217
|
if (callee.type === 'MemberExpression' &&
|
|
240
218
|
callee.object.type === 'Identifier' &&
|
|
241
219
|
callee.property.type === 'Identifier') {
|
|
@@ -243,9 +221,8 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
243
221
|
if (safeMemberCallers.has(key))
|
|
244
222
|
return true;
|
|
245
223
|
}
|
|
246
|
-
break;
|
|
224
|
+
break;
|
|
247
225
|
}
|
|
248
|
-
// new URL(`...`), new Error(`...`)
|
|
249
226
|
if (current.type === eslint_devkit_1.AST_NODE_TYPES.NewExpression) {
|
|
250
227
|
if (current.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
251
228
|
safeConstructors.has(current.callee.name)) {
|
|
@@ -253,7 +230,6 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
253
230
|
}
|
|
254
231
|
break;
|
|
255
232
|
}
|
|
256
|
-
// Don't walk past function boundaries
|
|
257
233
|
if (current.type === eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression ||
|
|
258
234
|
current.type === eslint_devkit_1.AST_NODE_TYPES.FunctionExpression ||
|
|
259
235
|
current.type === eslint_devkit_1.AST_NODE_TYPES.FunctionDeclaration) {
|
|
@@ -263,36 +239,45 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
263
239
|
}
|
|
264
240
|
return false;
|
|
265
241
|
};
|
|
266
|
-
// ─── AST-based GraphQL detection helpers ──────────────────────────
|
|
267
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
268
242
|
const isWordChar = (ch) => {
|
|
269
243
|
const code = ch.charCodeAt(0);
|
|
270
|
-
return (code >= 65 && code <= 90) ||
|
|
271
|
-
(code >= 97 && code <= 122) ||
|
|
272
|
-
(code >= 48 && code <= 57) ||
|
|
273
|
-
code === 95;
|
|
244
|
+
return (code >= 65 && code <= 90) ||
|
|
245
|
+
(code >= 97 && code <= 122) ||
|
|
246
|
+
(code >= 48 && code <= 57) ||
|
|
247
|
+
code === 95;
|
|
274
248
|
};
|
|
275
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
276
249
|
const isWhitespace = (ch) => ch === ' ' || ch === '\n' || ch === '\t' || ch === '\r';
|
|
277
|
-
/** Operation keywords that must be followed by optional name + { or ( */
|
|
278
250
|
const GRAPHQL_OP_KEYWORDS = ['query', 'mutation', 'subscription'];
|
|
279
|
-
/** Schema keywords that must be followed by a type name */
|
|
280
251
|
const GRAPHQL_SCHEMA_KEYWORDS = ['type', 'interface', 'enum', 'scalar', 'input'];
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
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
|
+
};
|
|
293
279
|
const findKeywordAtBoundary = (text, keyword, startFrom = 0) => {
|
|
294
280
|
let pos = startFrom;
|
|
295
|
-
// oxlint-disable-next-line no-constant-condition
|
|
296
281
|
while (true) {
|
|
297
282
|
const idx = text.indexOf(keyword, pos);
|
|
298
283
|
if (idx === -1)
|
|
@@ -305,17 +290,10 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
305
290
|
pos = idx + 1;
|
|
306
291
|
}
|
|
307
292
|
};
|
|
308
|
-
/**
|
|
309
|
-
* AST-based: Check if a TemplateLiteral contains GraphQL syntax.
|
|
310
|
-
* Examines quasis (static template parts) directly — no regex, no sourceCode.getText().
|
|
311
|
-
*/
|
|
312
293
|
const isGraphqlTemplate = (node) => {
|
|
313
|
-
// Build combined static text from quasis for keyword scanning
|
|
314
294
|
const staticText = node.quasis
|
|
315
295
|
.map(q => (q.value.cooked ?? q.value.raw).toLowerCase())
|
|
316
296
|
.join('');
|
|
317
|
-
// 1. Check for GraphQL operation keywords (query, mutation, subscription)
|
|
318
|
-
// Must be followed by optional name then { or (
|
|
319
297
|
for (const keyword of GRAPHQL_OP_KEYWORDS) {
|
|
320
298
|
let pos = 0;
|
|
321
299
|
while (pos < staticText.length) {
|
|
@@ -323,7 +301,8 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
323
301
|
if (idx === -1)
|
|
324
302
|
break;
|
|
325
303
|
pos = idx + 1;
|
|
326
|
-
|
|
304
|
+
if (!isAtLineStart(staticText, idx))
|
|
305
|
+
continue;
|
|
327
306
|
let scan = idx + keyword.length;
|
|
328
307
|
while (scan < staticText.length && isWhitespace(staticText[scan]))
|
|
329
308
|
scan++;
|
|
@@ -336,7 +315,6 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
336
315
|
}
|
|
337
316
|
}
|
|
338
317
|
}
|
|
339
|
-
// Check for fragment keyword: fragment Name on Type
|
|
340
318
|
{
|
|
341
319
|
let pos = 0;
|
|
342
320
|
while (pos < staticText.length) {
|
|
@@ -344,14 +322,16 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
344
322
|
if (idx === -1)
|
|
345
323
|
break;
|
|
346
324
|
pos = idx + 1;
|
|
347
|
-
|
|
325
|
+
if (!isAtLineStart(staticText, idx))
|
|
326
|
+
continue;
|
|
327
|
+
let scan = idx + 8;
|
|
348
328
|
while (scan < staticText.length && isWhitespace(staticText[scan]))
|
|
349
329
|
scan++;
|
|
350
330
|
const nameStart = scan;
|
|
351
331
|
while (scan < staticText.length && isWordChar(staticText[scan]))
|
|
352
332
|
scan++;
|
|
353
333
|
if (scan === nameStart)
|
|
354
|
-
continue;
|
|
334
|
+
continue;
|
|
355
335
|
while (scan < staticText.length && isWhitespace(staticText[scan]))
|
|
356
336
|
scan++;
|
|
357
337
|
if (staticText.slice(scan, scan + 2) === 'on' && (scan + 2 >= staticText.length || !isWordChar(staticText[scan + 2]))) {
|
|
@@ -359,54 +339,34 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
359
339
|
}
|
|
360
340
|
}
|
|
361
341
|
}
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
scan
|
|
376
|
-
|
|
377
|
-
|
|
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
|
+
}
|
|
378
360
|
}
|
|
379
361
|
}
|
|
380
|
-
|
|
381
|
-
// 3. Check for selection sets (nested braces): { users { name } }
|
|
382
|
-
let braceDepth = 0;
|
|
383
|
-
for (let i = 0; i < staticText.length; i++) {
|
|
384
|
-
if (staticText[i] === '{') {
|
|
385
|
-
braceDepth++;
|
|
386
|
-
if (braceDepth >= 2)
|
|
387
|
-
return true;
|
|
388
|
-
}
|
|
389
|
-
else if (staticText[i] === '}') {
|
|
390
|
-
braceDepth = Math.max(0, braceDepth - 1);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
return false;
|
|
362
|
+
return isStandaloneSelectionSet(staticText);
|
|
394
363
|
};
|
|
395
|
-
/**
|
|
396
|
-
* Check if a TemplateLiteral contains introspection patterns.
|
|
397
|
-
* AST-based: scans quasis directly.
|
|
398
|
-
*/
|
|
399
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
400
364
|
const templateHasIntrospection = (node) => {
|
|
401
365
|
return node.quasis.some(q => {
|
|
402
366
|
const text = (q.value.cooked ?? q.value.raw).toLowerCase();
|
|
403
367
|
return text.includes('__schema') || text.includes('__type');
|
|
404
368
|
});
|
|
405
369
|
};
|
|
406
|
-
/**
|
|
407
|
-
* Calculate query depth from template quasis (brace depth scan).
|
|
408
|
-
*/
|
|
409
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
410
370
|
const templateQueryDepth = (node) => {
|
|
411
371
|
let depth = 0;
|
|
412
372
|
let braceCount = 0;
|
|
@@ -424,17 +384,14 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
424
384
|
}
|
|
425
385
|
return depth;
|
|
426
386
|
};
|
|
427
|
-
/**
|
|
428
|
-
* Text-based GraphQL detection for string Literals and BinaryExpressions.
|
|
429
|
-
* Uses simple string methods — only regex is for fragment pattern.
|
|
430
|
-
*/
|
|
431
387
|
const containsGraphqlText = (text) => {
|
|
432
388
|
const lower = text.toLowerCase();
|
|
433
|
-
// Check operation keywords
|
|
434
389
|
for (const keyword of GRAPHQL_OP_KEYWORDS) {
|
|
435
390
|
const idx = findKeywordAtBoundary(lower, keyword);
|
|
436
391
|
if (idx === -1)
|
|
437
392
|
continue;
|
|
393
|
+
if (!isAtLineStart(lower, idx))
|
|
394
|
+
continue;
|
|
438
395
|
let scan = idx + keyword.length;
|
|
439
396
|
while (scan < lower.length && isWhitespace(lower[scan]))
|
|
440
397
|
scan++;
|
|
@@ -445,9 +402,8 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
445
402
|
if (scan < lower.length && (lower[scan] === '{' || lower[scan] === '('))
|
|
446
403
|
return true;
|
|
447
404
|
}
|
|
448
|
-
// Check fragment
|
|
449
405
|
const fragIdx = findKeywordAtBoundary(lower, 'fragment');
|
|
450
|
-
if (fragIdx !== -1) {
|
|
406
|
+
if (fragIdx !== -1 && isAtLineStart(lower, fragIdx)) {
|
|
451
407
|
let scan = fragIdx + 8;
|
|
452
408
|
while (scan < lower.length && isWhitespace(lower[scan]))
|
|
453
409
|
scan++;
|
|
@@ -461,40 +417,36 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
461
417
|
return true;
|
|
462
418
|
}
|
|
463
419
|
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
// Check nested braces
|
|
478
|
-
let braceDepth = 0;
|
|
479
|
-
for (let i = 0; i < text.length; i++) {
|
|
480
|
-
if (text[i] === '{') {
|
|
481
|
-
braceDepth++;
|
|
482
|
-
if (braceDepth >= 2)
|
|
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]))
|
|
483
433
|
return true;
|
|
484
434
|
}
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
435
|
+
return isStandaloneSelectionSet(text);
|
|
436
|
+
};
|
|
437
|
+
const concatenatedStaticText = (node) => {
|
|
438
|
+
if (node.type === 'BinaryExpression' && node.operator === '+') {
|
|
439
|
+
return concatenatedStaticText(node.left) + concatenatedStaticText(node.right);
|
|
488
440
|
}
|
|
489
|
-
|
|
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 '';
|
|
490
447
|
};
|
|
491
|
-
/**
|
|
492
|
-
* Check if input is validated before use
|
|
493
|
-
*/
|
|
494
448
|
const isInputValidated = (inputNode) => {
|
|
495
|
-
// Check if input comes from a validation function
|
|
496
449
|
let current = inputNode;
|
|
497
|
-
// Walk up the AST to find validation calls
|
|
498
450
|
while (current) {
|
|
499
451
|
if (current.type === 'CallExpression' &&
|
|
500
452
|
current.callee.type === 'Identifier' &&
|
|
@@ -506,19 +458,14 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
506
458
|
return false;
|
|
507
459
|
};
|
|
508
460
|
return {
|
|
509
|
-
// Check template literals for GraphQL queries
|
|
510
461
|
TemplateLiteral(node) {
|
|
511
|
-
// AST-based context check: skip templates inside safe callers
|
|
512
462
|
if (isInSafeCallerContext(node)) {
|
|
513
463
|
return;
|
|
514
464
|
}
|
|
515
|
-
// AST-based GraphQL detection: scan quasis, no regex
|
|
516
465
|
if (!isGraphqlTemplate(node)) {
|
|
517
466
|
return;
|
|
518
467
|
}
|
|
519
|
-
// Check for introspection queries (AST-based)
|
|
520
468
|
if (!allowIntrospection && templateHasIntrospection(node)) {
|
|
521
|
-
// FALSE POSITIVE REDUCTION: Skip if annotated as safe
|
|
522
469
|
if (safetyChecker.isSafe(node, context)) {
|
|
523
470
|
return;
|
|
524
471
|
}
|
|
@@ -532,9 +479,7 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
532
479
|
});
|
|
533
480
|
return;
|
|
534
481
|
}
|
|
535
|
-
// Check for unsafe interpolation (AST-based: just check expressions array)
|
|
536
482
|
if (node.expressions.length > 0) {
|
|
537
|
-
// FALSE POSITIVE REDUCTION: Skip if all expressions are validated
|
|
538
483
|
const allExpressionsSafe = node.expressions.every((expr) => isInputValidated(expr) || safetyChecker.isSafe(expr, context));
|
|
539
484
|
if (!allExpressionsSafe) {
|
|
540
485
|
context.report({
|
|
@@ -547,13 +492,12 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
547
492
|
suggest: [
|
|
548
493
|
{
|
|
549
494
|
messageId: 'useQueryBuilder',
|
|
550
|
-
fix: () => null
|
|
495
|
+
fix: () => null
|
|
551
496
|
},
|
|
552
497
|
],
|
|
553
498
|
});
|
|
554
499
|
}
|
|
555
500
|
}
|
|
556
|
-
// Check query depth for DoS protection (AST-based)
|
|
557
501
|
const depth = templateQueryDepth(node);
|
|
558
502
|
if (depth > maxQueryDepth) {
|
|
559
503
|
context.report({
|
|
@@ -572,7 +516,6 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
572
516
|
});
|
|
573
517
|
}
|
|
574
518
|
},
|
|
575
|
-
// Check string literals for GraphQL queries
|
|
576
519
|
Literal(node) {
|
|
577
520
|
if (typeof node.value !== 'string') {
|
|
578
521
|
return;
|
|
@@ -581,7 +524,6 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
581
524
|
if (!containsGraphqlText(queryText)) {
|
|
582
525
|
return;
|
|
583
526
|
}
|
|
584
|
-
// Check for introspection queries
|
|
585
527
|
const lowerQuery = queryText.toLowerCase();
|
|
586
528
|
if (!allowIntrospection && (lowerQuery.includes('__schema') || lowerQuery.includes('__type'))) {
|
|
587
529
|
if (safetyChecker.isSafe(node, context)) {
|
|
@@ -596,7 +538,6 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
596
538
|
},
|
|
597
539
|
});
|
|
598
540
|
}
|
|
599
|
-
// Check query depth via brace scanner
|
|
600
541
|
let depth = 0;
|
|
601
542
|
let bc = 0;
|
|
602
543
|
for (const ch of queryText) {
|
|
@@ -619,16 +560,14 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
619
560
|
});
|
|
620
561
|
}
|
|
621
562
|
},
|
|
622
|
-
// Check binary expressions (string concatenation)
|
|
623
563
|
BinaryExpression(node) {
|
|
624
564
|
if (node.operator !== '+') {
|
|
625
565
|
return;
|
|
626
566
|
}
|
|
627
|
-
const fullText =
|
|
567
|
+
const fullText = concatenatedStaticText(node);
|
|
628
568
|
if (!containsGraphqlText(fullText)) {
|
|
629
569
|
return;
|
|
630
570
|
}
|
|
631
|
-
// String concatenation in GraphQL queries is dangerous
|
|
632
571
|
if (safetyChecker.isSafe(node, context)) {
|
|
633
572
|
return;
|
|
634
573
|
}
|
|
@@ -643,20 +582,16 @@ exports.noGraphqlInjection = (0, eslint_devkit_1.createRule)({
|
|
|
643
582
|
},
|
|
644
583
|
});
|
|
645
584
|
},
|
|
646
|
-
// Check GraphQL execution calls
|
|
647
585
|
CallExpression(node) {
|
|
648
586
|
if (!isGraphqlRelated(node)) {
|
|
649
587
|
return;
|
|
650
588
|
}
|
|
651
589
|
const callee = node.callee;
|
|
652
|
-
// Check for execute/query methods
|
|
653
590
|
if (callee.type === 'MemberExpression' &&
|
|
654
591
|
callee.property.type === 'Identifier' &&
|
|
655
592
|
['execute', 'executeQuery', 'query', 'mutate', 'subscribe'].includes(callee.property.name)) {
|
|
656
|
-
// Check arguments for unvalidated inputs
|
|
657
593
|
for (const arg of node.arguments) {
|
|
658
594
|
if (arg.type === 'Identifier' && !isInputValidated(arg)) {
|
|
659
|
-
// FALSE POSITIVE REDUCTION
|
|
660
595
|
if (safetyChecker.isSafe(arg, context)) {
|
|
661
596
|
continue;
|
|
662
597
|
}
|