@interlace/eslint-devkit 1.11.0 → 1.13.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.
- package/package.json +1 -1
- package/src/rule-creation/index.d.ts +1 -0
- package/src/rule-creation/index.js +1 -1
- package/src/rule-creation/module-evidence.d.ts +68 -0
- package/src/rule-creation/module-evidence.js +1 -0
- package/src/rule-creation/sql-injection-rule.d.ts +20 -0
- package/src/rule-creation/sql-injection-rule.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@interlace/eslint-devkit",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.13.0",
|
|
4
4
|
"description": "Toolkit for building ESLint plugins in TypeScript — AST helpers, type utilities, rule-creation scaffolding, security benchmarks, and SARIF output.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?(function(o,m,k,k2){if(k2===void 0)k2=k;var desc=Object.getOwnPropertyDescriptor(m,k);if(!desc||("get"in desc?!m.__esModule:desc.writable||desc.configurable)){desc={enumerable:true,get:function(){return m[k]}}}Object.defineProperty(o,k2,desc)}):(function(o,m,k,k2){if(k2===void 0)k2=k;o[k2]=m[k]}));var __exportStar=this&&this.__exportStar||function(m,exports2){for(var p in m)if(p!=="default"&&!Object.prototype.hasOwnProperty.call(exports2,p))__createBinding(exports2,m,p)};Object.defineProperty(exports,"__esModule",{value:true});__exportStar(require("./rule-creator"),exports);__exportStar(require("./mock-context"),exports);__exportStar(require("./sql-injection-rule"),exports);__exportStar(require("./unscoped-mutation-rule"),exports);__exportStar(require("./require-tls-rule"),exports);__exportStar(require("./mass-assignment-rule"),exports);__exportStar(require("./hardcoded-credentials-rule"),exports);__exportStar(require("./sdk-api-key-rule"),exports);__exportStar(require("./browser-escape-hatch-rule"),exports);__exportStar(require("./system-prompt-injection-rule"),exports);__exportStar(require("./message-source"),exports);__exportStar(require("./raw-identifier-rule"),exports);
|
|
1
|
+
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?(function(o,m,k,k2){if(k2===void 0)k2=k;var desc=Object.getOwnPropertyDescriptor(m,k);if(!desc||("get"in desc?!m.__esModule:desc.writable||desc.configurable)){desc={enumerable:true,get:function(){return m[k]}}}Object.defineProperty(o,k2,desc)}):(function(o,m,k,k2){if(k2===void 0)k2=k;o[k2]=m[k]}));var __exportStar=this&&this.__exportStar||function(m,exports2){for(var p in m)if(p!=="default"&&!Object.prototype.hasOwnProperty.call(exports2,p))__createBinding(exports2,m,p)};Object.defineProperty(exports,"__esModule",{value:true});__exportStar(require("./rule-creator"),exports);__exportStar(require("./mock-context"),exports);__exportStar(require("./sql-injection-rule"),exports);__exportStar(require("./unscoped-mutation-rule"),exports);__exportStar(require("./require-tls-rule"),exports);__exportStar(require("./mass-assignment-rule"),exports);__exportStar(require("./hardcoded-credentials-rule"),exports);__exportStar(require("./sdk-api-key-rule"),exports);__exportStar(require("./browser-escape-hatch-rule"),exports);__exportStar(require("./system-prompt-injection-rule"),exports);__exportStar(require("./message-source"),exports);__exportStar(require("./raw-identifier-rule"),exports);__exportStar(require("./module-evidence"),exports);
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Ofri Peretz
|
|
3
|
+
* Licensed under the MIT License. Use of this source code is governed by the
|
|
4
|
+
* MIT license that can be found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
import type { TSESTree } from '@typescript-eslint/utils';
|
|
7
|
+
/**
|
|
8
|
+
* Extra evidence a plugin accepts beyond an import.
|
|
9
|
+
*
|
|
10
|
+
* Most SDKs need none: if the file does not import the package, it does not use
|
|
11
|
+
* the SDK. Two do, and both were established by measuring rather than guessing:
|
|
12
|
+
*
|
|
13
|
+
* - **Lambda** — 184 of 413 handler files (45%) import nothing AWS, because
|
|
14
|
+
* `aws-lambda` is a types package and a plain JS handler imports nothing.
|
|
15
|
+
* - **Express** — 68 of 114 files with a `(req, res)`-shaped function (60%)
|
|
16
|
+
* import no `express`, because route modules receive `app` from a caller.
|
|
17
|
+
*
|
|
18
|
+
* Anything supplied here is an *additional* way to open the gate, so a loose
|
|
19
|
+
* predicate re-introduces the false positives the gate exists to remove. It
|
|
20
|
+
* must identify the SDK's own contract — a handler export, a middleware
|
|
21
|
+
* signature — never a name that merely sounds related.
|
|
22
|
+
*/
|
|
23
|
+
export type ExtraEvidence = (node: TSESTree.Node) => boolean;
|
|
24
|
+
export interface ModuleEvidenceConfig {
|
|
25
|
+
/** Exact package roots, e.g. `['pg', 'postgres']`. */
|
|
26
|
+
readonly packages?: readonly string[];
|
|
27
|
+
/** Whole scopes, e.g. `['@ai-sdk']` — every package under them counts. */
|
|
28
|
+
readonly scopes?: readonly string[];
|
|
29
|
+
/** Package-root prefixes, e.g. `['serverless-']`. */
|
|
30
|
+
readonly prefixes?: readonly string[];
|
|
31
|
+
/** Non-import shapes that also mean "this file uses the SDK". */
|
|
32
|
+
readonly extraEvidence?: ExtraEvidence;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Build a per-file "does this file use my SDK?" probe.
|
|
36
|
+
*
|
|
37
|
+
* Every SDK-specific plugin gates every rule on one of these. Measured over
|
|
38
|
+
* 107,382 files across 107 repositories, **40% of all SDK-plugin findings were
|
|
39
|
+
* about an SDK the file never imports** — `lambda-security` reported on any
|
|
40
|
+
* `try/catch`, `postgresql-security` on any `.connect()`, and the seven SQL
|
|
41
|
+
* plugins on each other. A rule that matches a method name is detecting a word,
|
|
42
|
+
* not an interface.
|
|
43
|
+
*
|
|
44
|
+
* This lives in the devkit because it previously did not: five plugins each
|
|
45
|
+
* carried their own copy, so the two false-negative classes the audit found —
|
|
46
|
+
* TypeScript's `import =` and Deno's specifiers — had to be fixed five times,
|
|
47
|
+
* and the next such class would have been fixed ten. One implementation, one
|
|
48
|
+
* fix.
|
|
49
|
+
*
|
|
50
|
+
* The returned probe caches per `Program`, because `create` runs once per rule:
|
|
51
|
+
* an uncached probe walks the whole AST once for every rule in the plugin, and
|
|
52
|
+
* the files paying that cost are mostly the ones the gate exists to skip.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* export const fileUsesPostgres = createModuleEvidence({
|
|
57
|
+
* packages: ['pg', 'postgres', 'slonik'],
|
|
58
|
+
* scopes: ['@neondatabase'],
|
|
59
|
+
* });
|
|
60
|
+
*
|
|
61
|
+
* // in a rule:
|
|
62
|
+
* create(context) {
|
|
63
|
+
* if (!fileUsesPostgres(context.sourceCode.ast)) return {};
|
|
64
|
+
* …
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare function createModuleEvidence(config: ModuleEvidenceConfig): (ast: TSESTree.Program) => boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.createModuleEvidence=createModuleEvidence;const ast_node_types_1=require("../ast-node-types");function normalizeSpecifier(specifier){if(specifier.startsWith("npm:"))return specifier.slice(4);const deno=/^https?:\/\/deno\.land\/x\/([^@/]+)/.exec(specifier);return deno?deno[1]:specifier}function importEqualsSpecifier(node){if(node.type!==ast_node_types_1.AST_NODE_TYPES.TSImportEqualsDeclaration)return null;const ref=node.moduleReference;if(ref.type!==ast_node_types_1.AST_NODE_TYPES.TSExternalModuleReference)return null;return ref.expression.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof ref.expression.value==="string"?ref.expression.value:null}function bindsRequire(node){if(node.type===ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration||node.type===ast_node_types_1.AST_NODE_TYPES.FunctionExpression||node.type===ast_node_types_1.AST_NODE_TYPES.ArrowFunctionExpression){return node.params.some(p=>p.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&p.name==="require")}if(node.type===ast_node_types_1.AST_NODE_TYPES.Program||node.type===ast_node_types_1.AST_NODE_TYPES.BlockStatement){return node.body.some(stmt=>stmt.type===ast_node_types_1.AST_NODE_TYPES.VariableDeclaration&&stmt.declarations.some(d=>d.id.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&d.id.name==="require"))}return false}function createModuleEvidence(config){const packages=new Set(config.packages??[]);const scopes=new Set(config.scopes??[]);const prefixes=config.prefixes??[];const extra=config.extraEvidence;const owns=raw=>{const specifier=normalizeSpecifier(raw);if(specifier.startsWith(".")||specifier.startsWith("/"))return false;if(specifier.startsWith("@")){const[scope,name]=specifier.split("/");if(scopes.has(scope))return true;const root2=name===void 0?scope:`${scope}/${name}`;return packages.has(root2)||prefixes.some(p=>root2.startsWith(p))}const root=specifier.split("/")[0];return packages.has(root)||prefixes.some(p=>root.startsWith(p))};const dynamicLoadSpecifier=(node,requireIsShadowed)=>{const argument=node.type===ast_node_types_1.AST_NODE_TYPES.ImportExpression?node.source:!requireIsShadowed&&node.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&node.callee.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&node.callee.name==="require"?node.arguments[0]:void 0;return argument?.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof argument.value==="string"?argument.value:null};const cache=new WeakMap;const compute=ast=>{let found=false;const visit=(node,requireIsShadowed)=>{if((node.type===ast_node_types_1.AST_NODE_TYPES.ImportDeclaration||node.type===ast_node_types_1.AST_NODE_TYPES.ExportNamedDeclaration||node.type===ast_node_types_1.AST_NODE_TYPES.ExportAllDeclaration)&&node.source?.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof node.source.value==="string"&&owns(node.source.value)){found=true;return}const importEquals=importEqualsSpecifier(node);if(importEquals!==null&&owns(importEquals)){found=true;return}const dynamic=dynamicLoadSpecifier(node,requireIsShadowed);if(dynamic!==null&&owns(dynamic)){found=true;return}if(extra?.(node)){found=true;return}const shadowedHere=requireIsShadowed||bindsRequire(node);for(const key of Object.keys(node)){if(key==="parent")continue;const value=node[key];if(Array.isArray(value)){for(const child of value){if(child&&typeof child.type==="string"){visit(child,shadowedHere);if(found)return}}}else if(value&&typeof value==="object"&&typeof value.type==="string"){visit(value,shadowedHere);if(found)return}}};visit(ast,false);return found};return ast=>{const cached=cache.get(ast);if(cached!==void 0)return cached;const result=compute(ast);cache.set(ast,result);return result}}
|
|
@@ -28,6 +28,26 @@ export interface SqlInjectionRuleConfig {
|
|
|
28
28
|
};
|
|
29
29
|
/** Method names treated as raw-SQL sinks, e.g. `['query']` or `['query', 'raw', 'execute']`. */
|
|
30
30
|
readonly methods: readonly string[];
|
|
31
|
+
/**
|
|
32
|
+
* Modules whose API these sinks belong to. **The rule stays silent in a file
|
|
33
|
+
* that imports none of them.**
|
|
34
|
+
*
|
|
35
|
+
* Method names are not evidence of an SDK. `['raw']` is knex *and* drizzle;
|
|
36
|
+
* `['query']` is typeorm *and* pg *and* mysql2; `['get','all','run']` is
|
|
37
|
+
* better-sqlite3 *and* an Express router *and* `Promise.all`. Measured over
|
|
38
|
+
* 73,364 files, that produced 1,142 lines where two or more plugins reported
|
|
39
|
+
* the *same* CWE — 616 for postgres×typeorm, 503 for mysql×typeorm, 347 for
|
|
40
|
+
* drizzle×knex, all from this one factory.
|
|
41
|
+
*
|
|
42
|
+
* Requiring the import makes the collision impossible by construction rather
|
|
43
|
+
* than deduplicated after the fact, and it is *local* evidence: no project
|
|
44
|
+
* scan, nothing to go stale, and a file that does not import the driver is
|
|
45
|
+
* one this rule genuinely has nothing to say about.
|
|
46
|
+
*
|
|
47
|
+
* Matched against the import specifier's package root, so `'mysql2/promise'`
|
|
48
|
+
* matches `'mysql2'` and `'@prisma/client/edge'` matches `'@prisma/client'`.
|
|
49
|
+
*/
|
|
50
|
+
readonly modules: readonly string[];
|
|
31
51
|
/**
|
|
32
52
|
* Require SQL keywords in the *static* part of the string before reporting.
|
|
33
53
|
* Precision guard for broad sink lists (`.raw()`, `.execute()` are not
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.createSqlInjectionRule=createSqlInjectionRule;const ast_node_types_1=require("../ast-node-types");const messaging_1=require("../messaging");const SQL_KEYWORDS=/\b(?:select|insert|update|delete|drop|alter|truncate|union|from|where|values|set)\b/i;const FUNCTION_TYPES=new Set([ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration,ast_node_types_1.AST_NODE_TYPES.FunctionExpression,ast_node_types_1.AST_NODE_TYPES.ArrowFunctionExpression]);function enclosingFunction(node){for(let n=node.parent;n;n=n.parent){if(FUNCTION_TYPES.has(n.type))return n}return void 0}function callableName(fn){if(fn.type===ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration&&fn.id)return fn.id.name;const parent=fn.parent;if(parent.type===ast_node_types_1.AST_NODE_TYPES.VariableDeclarator&&parent.id.type===ast_node_types_1.AST_NODE_TYPES.Identifier){return parent.id.name}if((parent.type===ast_node_types_1.AST_NODE_TYPES.MethodDefinition||parent.type===ast_node_types_1.AST_NODE_TYPES.PropertyDefinition||parent.type===ast_node_types_1.AST_NODE_TYPES.Property)&&parent.key.type===ast_node_types_1.AST_NODE_TYPES.Identifier){return parent.key.name}return void 0}function staticText(node){if(node.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral){return node.quasis.map(q=>q.value.raw).join(" ")}if(node.type===ast_node_types_1.AST_NODE_TYPES.BinaryExpression){return`${staticText(node.left)} ${staticText(node.right)}`}if(node.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof node.value==="string"){return node.value}return""}function classify(node,requireSqlKeywords){let kind=false;if(node.type===ast_node_types_1.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"){kind="concat"}if(node.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral&&node.expressions.length>0){kind="template"}if(kind===false)return false;if(kind==="concat"&&staticText(node).trim()==="")return false;if(requireSqlKeywords&&!SQL_KEYWORDS.test(staticText(node)))return false;return kind}function createSqlInjectionRule(config){const sinks=new Set(config.methods);return{meta:{type:config.meta.type,docs:{...config.meta.docs},messages:{noUnsafeQuery:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"SQL Injection Risk",description:"Unsafe SQL query detected. Variable interpolation found.",severity:"CRITICAL",cwe:config.meta.docs.cwe,owasp:"A03:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"high",fix:config.fix,documentationLink:config.documentationLink}),unsafeTemplateLiteral:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"SQL Injection Risk",description:"Unsafe SQL query construction detected (template literal).",severity:"CRITICAL",cwe:config.meta.docs.cwe,owasp:"A03:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"high",fix:config.fix,documentationLink:config.documentationLink})},schema:[]},defaultOptions:[],create(context){const tainted=new Map;const sqlish=new Set;const wrappers=new Map;const definitions=new Map;const wrapperFns=new Map;const pending=[];const report=(node,kind)=>{context.report({node,messageId:kind==="template"?"unsafeTemplateLiteral":"noUnsafeQuery"})};return{"FunctionDeclaration, FunctionExpression, ArrowFunctionExpression"(node){const name=callableName(node);if(name!==void 0)definitions.set(name,(definitions.get(name)??0)+1)},VariableDeclarator(node){if(node.id.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&node.init){const kind=classify(node.init,config.requireSqlKeywords);if(kind)tainted.set(node.id.name,kind);if(SQL_KEYWORDS.test(staticText(node.init)))sqlish.add(node.id.name)}},AssignmentExpression(node){if(node.operator==="+="&&node.left.type===ast_node_types_1.AST_NODE_TYPES.Identifier){const gate=config.requireSqlKeywords&&!sqlish.has(node.left.name);const kind=classify(node.right,gate);if(kind)tainted.set(node.left.name,kind);if(SQL_KEYWORDS.test(staticText(node.right)))sqlish.add(node.left.name)}},CallExpression(node){const isMember=node.callee.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&node.callee.property.type===ast_node_types_1.AST_NODE_TYPES.Identifier;const calleeName=isMember?node.callee.property.name:node.callee.type===ast_node_types_1.AST_NODE_TYPES.Identifier?node.callee.name:void 0;if(calleeName===void 0)return;if(!isMember||!sinks.has(calleeName)){for(const[index,arg]of node.arguments.entries()){if(arg.type===ast_node_types_1.AST_NODE_TYPES.SpreadElement)continue;const kind=classify(arg,true);if(kind)pending.push({name:calleeName,index,arg,kind});else if(arg.type===ast_node_types_1.AST_NODE_TYPES.Identifier)pending.push({name:calleeName,index,arg,kind:void 0})}return}const queryArg=node.arguments[0];if(!queryArg)return;if(queryArg.type===ast_node_types_1.AST_NODE_TYPES.Identifier){const fn=enclosingFunction(node);const index=fn?.params.findIndex(p=>p.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&p.name===queryArg.name)??-1;const name=fn&&index>=0?callableName(fn):void 0;if(name!==void 0&&fn){wrappers.set(name,index);wrapperFns.set(fn,name)}}const direct=classify(queryArg,config.requireSqlKeywords);if(direct){report(queryArg,direct);return}if(queryArg.type===ast_node_types_1.AST_NODE_TYPES.Identifier){const taint=tainted.get(queryArg.name);if(taint)report(queryArg,taint)}},"Program:exit"(){const proven=new Map;for(const name of wrapperFns.values())proven.set(name,(proven.get(name)??0)+1);for(const{name,index,arg,kind}of pending){if(wrappers.get(name)!==index)continue;if(definitions.get(name)!==proven.get(name))continue;if(kind){report(arg,kind);continue}const varName=arg.name;const taint=tainted.get(varName);if(taint&&sqlish.has(varName))report(arg,taint)}}}}}}
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.createSqlInjectionRule=createSqlInjectionRule;const ast_node_types_1=require("../ast-node-types");const messaging_1=require("../messaging");const SQL_KEYWORDS=/\b(?:select|insert|update|delete|drop|alter|truncate|union|from|where|values|set)\b/i;const FUNCTION_TYPES=new Set([ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration,ast_node_types_1.AST_NODE_TYPES.FunctionExpression,ast_node_types_1.AST_NODE_TYPES.ArrowFunctionExpression]);function enclosingFunction(node){for(let n=node.parent;n;n=n.parent){if(FUNCTION_TYPES.has(n.type))return n}return void 0}function callableName(fn){if(fn.type===ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration&&fn.id)return fn.id.name;const parent=fn.parent;if(parent.type===ast_node_types_1.AST_NODE_TYPES.VariableDeclarator&&parent.id.type===ast_node_types_1.AST_NODE_TYPES.Identifier){return parent.id.name}if((parent.type===ast_node_types_1.AST_NODE_TYPES.MethodDefinition||parent.type===ast_node_types_1.AST_NODE_TYPES.PropertyDefinition||parent.type===ast_node_types_1.AST_NODE_TYPES.Property)&&parent.key.type===ast_node_types_1.AST_NODE_TYPES.Identifier){return parent.key.name}return void 0}function staticText(node){if(node.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral){return node.quasis.map(q=>q.value.raw).join(" ")}if(node.type===ast_node_types_1.AST_NODE_TYPES.BinaryExpression){return`${staticText(node.left)} ${staticText(node.right)}`}if(node.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof node.value==="string"){return node.value}return""}function classify(node,requireSqlKeywords){let kind=false;if(node.type===ast_node_types_1.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"){kind="concat"}if(node.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral&&node.expressions.length>0){kind="template"}if(kind===false)return false;if(kind==="concat"&&staticText(node).trim()==="")return false;if(requireSqlKeywords&&!SQL_KEYWORDS.test(staticText(node)))return false;return kind}function createSqlInjectionRule(config){const sinks=new Set(config.methods);const owned=new Set(config.modules);const owns=specifier=>{if(specifier.startsWith(".")||specifier.startsWith("/"))return false;const parts=specifier.split("/");const root=specifier.startsWith("@")?parts.slice(0,2).join("/"):parts[0];return owned.has(root)};return{meta:{type:config.meta.type,docs:{...config.meta.docs},messages:{noUnsafeQuery:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"SQL Injection Risk",description:"Unsafe SQL query detected. Variable interpolation found.",severity:"CRITICAL",cwe:config.meta.docs.cwe,owasp:"A03:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"high",fix:config.fix,documentationLink:config.documentationLink}),unsafeTemplateLiteral:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"SQL Injection Risk",description:"Unsafe SQL query construction detected (template literal).",severity:"CRITICAL",cwe:config.meta.docs.cwe,owasp:"A03:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"high",fix:config.fix,documentationLink:config.documentationLink})},schema:[]},defaultOptions:[],create(context){const tainted=new Map;const sqlish=new Set;const wrappers=new Map;const definitions=new Map;const wrapperFns=new Map;const pending=[];let ownsFile=false;const report=(node,kind)=>{if(!ownsFile)return;context.report({node,messageId:kind==="template"?"unsafeTemplateLiteral":"noUnsafeQuery"})};return{Program(program){ownsFile=program.body.some(stmt=>stmt.type===ast_node_types_1.AST_NODE_TYPES.ImportDeclaration&&owns(stmt.source.value))},"FunctionDeclaration, FunctionExpression, ArrowFunctionExpression"(node){const name=callableName(node);if(name!==void 0)definitions.set(name,(definitions.get(name)??0)+1)},VariableDeclarator(node){if(node.id.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&node.init){const kind=classify(node.init,config.requireSqlKeywords);if(kind)tainted.set(node.id.name,kind);if(SQL_KEYWORDS.test(staticText(node.init)))sqlish.add(node.id.name)}},AssignmentExpression(node){if(node.operator==="+="&&node.left.type===ast_node_types_1.AST_NODE_TYPES.Identifier){const gate=config.requireSqlKeywords&&!sqlish.has(node.left.name);const kind=classify(node.right,gate);if(kind)tainted.set(node.left.name,kind);if(SQL_KEYWORDS.test(staticText(node.right)))sqlish.add(node.left.name)}},CallExpression(node){const isMember=node.callee.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&node.callee.property.type===ast_node_types_1.AST_NODE_TYPES.Identifier;const calleeName=isMember?node.callee.property.name:node.callee.type===ast_node_types_1.AST_NODE_TYPES.Identifier?node.callee.name:void 0;if(calleeName===void 0)return;if(!isMember||!sinks.has(calleeName)){for(const[index,arg]of node.arguments.entries()){if(arg.type===ast_node_types_1.AST_NODE_TYPES.SpreadElement)continue;const kind=classify(arg,true);if(kind)pending.push({name:calleeName,index,arg,kind});else if(arg.type===ast_node_types_1.AST_NODE_TYPES.Identifier)pending.push({name:calleeName,index,arg,kind:void 0})}return}const queryArg=node.arguments[0];if(!queryArg)return;if(queryArg.type===ast_node_types_1.AST_NODE_TYPES.Identifier){const fn=enclosingFunction(node);const index=fn?.params.findIndex(p=>p.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&p.name===queryArg.name)??-1;const name=fn&&index>=0?callableName(fn):void 0;if(name!==void 0&&fn){wrappers.set(name,index);wrapperFns.set(fn,name)}}const direct=classify(queryArg,config.requireSqlKeywords);if(direct){report(queryArg,direct);return}if(queryArg.type===ast_node_types_1.AST_NODE_TYPES.Identifier){const taint=tainted.get(queryArg.name);if(taint)report(queryArg,taint)}},"Program:exit"(){const proven=new Map;for(const name of wrapperFns.values())proven.set(name,(proven.get(name)??0)+1);for(const{name,index,arg,kind}of pending){if(wrappers.get(name)!==index)continue;if(definitions.get(name)!==proven.get(name))continue;if(kind){report(arg,kind);continue}const varName=arg.name;const taint=tainted.get(varName);if(taint&&sqlish.has(varName))report(arg,taint)}}}}}}
|