@interlace/eslint-devkit 1.18.2 → 1.19.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/package.json +1 -1
- package/src/ast/module-binding.js +1 -1
- package/src/ast/spellings.d.ts +41 -0
- package/src/ast/spellings.js +1 -1
- package/src/rule-creation/hardcoded-credentials-rule.js +1 -1
- package/src/rule-creation/message-source.js +1 -1
- 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.19.1",
|
|
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";Object.defineProperty(exports,"__esModule",{value:true});exports.resolveModuleBinding=resolveModuleBinding;exports.isModuleBinding=isModuleBinding;const ast_node_types_1=require("../ast-node-types");function normalize(specifier,options){const bare=specifier.startsWith("node:")?specifier.slice("node:".length):specifier;return options.equivalents?.[bare]??bare}function requireTarget(node,options){if(node.type!==ast_node_types_1.AST_NODE_TYPES.CallExpression)return void 0;if(node.callee.type!==ast_node_types_1.AST_NODE_TYPES.Identifier||node.callee.name!=="require")return void 0;const[arg]=node.arguments;if(arg?.type!==ast_node_types_1.AST_NODE_TYPES.Literal)return void 0;return typeof arg.value==="string"?normalize(arg.value,options):void 0}function destructuredKey(pattern,boundName){if(pattern.type!==ast_node_types_1.AST_NODE_TYPES.ObjectPattern)return void 0;for(const property of pattern.properties){if(property.type!==ast_node_types_1.AST_NODE_TYPES.Property)continue;if(property.computed)continue;if(property.value.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)continue;if(property.value.name!==boundName)continue;const{key}=property;return key.type===ast_node_types_1.AST_NODE_TYPES.Identifier?key.name:String(key.value)}return void 0}function lookup(name,scope){for(let current=scope;current;current=current.upper){const variable=current.variables.find(v=>v.name===name);if(variable)return variable}return void 0}function resolveModuleBinding(node,scope,options={}){return resolve(node,scope,options,new Set)}function resolve(node,scope,options,seen){if(seen.has(node))return void 0;seen.add(node);const required=requireTarget(node,options);if(required!==void 0)return{module:required,path:[]};if(node.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.resolveModuleBinding=resolveModuleBinding;exports.isModuleBinding=isModuleBinding;const spellings_1=require("./spellings");const ast_node_types_1=require("../ast-node-types");function normalize(specifier,options){const bare=specifier.startsWith("node:")?specifier.slice("node:".length):specifier;return options.equivalents?.[bare]??bare}function requireTarget(node,options){if(node.type!==ast_node_types_1.AST_NODE_TYPES.CallExpression)return void 0;if(node.callee.type!==ast_node_types_1.AST_NODE_TYPES.Identifier||node.callee.name!=="require")return void 0;const[arg]=node.arguments;if(arg?.type!==ast_node_types_1.AST_NODE_TYPES.Literal)return void 0;return typeof arg.value==="string"?normalize(arg.value,options):void 0}function destructuredKey(pattern,boundName){if(pattern.type!==ast_node_types_1.AST_NODE_TYPES.ObjectPattern)return void 0;for(const property of pattern.properties){if(property.type!==ast_node_types_1.AST_NODE_TYPES.Property)continue;if(property.computed)continue;if(property.value.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)continue;if(property.value.name!==boundName)continue;const{key}=property;return key.type===ast_node_types_1.AST_NODE_TYPES.Identifier?key.name:String(key.value)}return void 0}function lookup(name,scope){for(let current=scope;current;current=current.upper){const variable=current.variables.find(v=>v.name===name);if(variable)return variable}return void 0}function resolveModuleBinding(node,scope,options={}){return resolve(node,scope,options,new Set)}function resolve(node,scope,options,seen){if(seen.has(node))return void 0;seen.add(node);const required=requireTarget(node,options);if(required!==void 0)return{module:required,path:[]};if(node.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression){const property=(0,spellings_1.propertyName)(node);if(property===null)return void 0;const base2=resolve(node.object,scope,options,seen);return base2&&{module:base2.module,path:[...base2.path,property]}}if(node.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)return void 0;const variable=lookup(node.name,scope);const def=variable?.defs[0];if(!def)return void 0;if(def.type==="ImportBinding"){const declaration=def.parent;if(declaration?.type!==ast_node_types_1.AST_NODE_TYPES.ImportDeclaration)return void 0;const module=normalize(String(declaration.source.value),options);if(def.node.type===ast_node_types_1.AST_NODE_TYPES.ImportSpecifier){const imported=def.node.imported;const name=imported.type===ast_node_types_1.AST_NODE_TYPES.Identifier?imported.name:String(imported.value);return{module,path:[name]}}return{module,path:[]}}if(def.type!=="Variable"||!def.node.init)return void 0;if(variable&&variable.references.filter(ref=>ref.isWrite()).length!==1)return void 0;const base=resolve(def.node.init,scope,options,seen);if(!base)return void 0;if(def.node.id.type===ast_node_types_1.AST_NODE_TYPES.Identifier)return base;const key=destructuredKey(def.node.id,node.name);return key===void 0?void 0:{module:base.module,path:[...base.path,key]}}function isModuleBinding(node,scope,module,exportPath,options={}){const binding=resolveModuleBinding(node,scope,options);if(!binding||binding.module!==module)return false;if(!exportPath)return true;return binding.path.length===exportPath.length&&exportPath.every((part,i)=>binding.path[i]===part)}
|
package/src/ast/spellings.d.ts
CHANGED
|
@@ -111,3 +111,44 @@ export interface RequestShapeOptions {
|
|
|
111
111
|
export declare function readsRequestShape(node: TSESTree.Node, sourceCode: {
|
|
112
112
|
getScope: (node: TSESTree.Node) => TSESLint.Scope.Scope;
|
|
113
113
|
}, options?: RequestShapeOptions): boolean;
|
|
114
|
+
/**
|
|
115
|
+
* Whether a name that may not have resolved is one of a known set.
|
|
116
|
+
*
|
|
117
|
+
* `propertyName` returns `string | null` so that "this property is not
|
|
118
|
+
* statically nameable" — `o[k]`, `` o[`a${b}`] `` — stays visible. Casting
|
|
119
|
+
* that away with `namesOneOf(propertyName(node), SET)` works, because
|
|
120
|
+
* `Set.prototype.has(null)` is false, but it spells "the rule abstained" and
|
|
121
|
+
* "the name is absent from the set" identically, and it lies to the type
|
|
122
|
+
* system about a value the AST genuinely may not supply.
|
|
123
|
+
*
|
|
124
|
+
* Use this where the membership test reads better as one expression, and an
|
|
125
|
+
* explicit `name !== null && …` where the resolved name is needed afterwards.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
129
|
+
* if (namesOneOf(propertyName(callee), HTTP_METHODS)) { … }
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
export declare const namesOneOf: (name: string | null, names: ReadonlySet<string> | readonly string[]) => boolean;
|
|
133
|
+
/**
|
|
134
|
+
* The property a node reads, or `null` if it does not read one at all.
|
|
135
|
+
*
|
|
136
|
+
* `propertyName` takes a `MemberExpression`, so a caller holding an arbitrary
|
|
137
|
+
* node has to test the type first — and that test is usually already sitting
|
|
138
|
+
* in a boolean chain the caller cannot restructure:
|
|
139
|
+
*
|
|
140
|
+
* a chain the caller cannot split — istanbul counts an `&&` operand as
|
|
141
|
+
* covered once it is EVALUATED, so a type test that is never false there
|
|
142
|
+
* looks covered, and promoting it to its own statement exposes an arm no
|
|
143
|
+
* test reaches. Resolving once, before the chain, leaves the chain intact:
|
|
144
|
+
* it gets a binding to test, and the body gets a narrowed `string` — no
|
|
145
|
+
* second call, and no cast standing in for a question already answered.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* const method = memberPropertyName(callee);
|
|
150
|
+
* if (callee.type !== 'MemberExpression' || method === null) return;
|
|
151
|
+
* method.toLowerCase(); // string
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
export declare const memberPropertyName: (node: TSESTree.Node | null | undefined) => string | null;
|
package/src/ast/spellings.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.isStaticString=void 0;exports.staticString=staticString;exports.propertyName=propertyName;exports.objectKeyName=objectKeyName;exports.memberPath=memberPath;exports.readsRequestShape=readsRequestShape;const ast_node_types_1=require("../ast-node-types");function staticString(node){if(!node)return null;if(node.type===ast_node_types_1.AST_NODE_TYPES.Literal){return typeof node.value==="string"?node.value:null}if(node.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral&&node.expressions.length===0){const[only]=node.quasis;return only.value.cooked}return null}const isStaticString=(node,value)=>staticString(node)===value;exports.isStaticString=isStaticString;function propertyName(node){if(!node.computed){return node.property.type===ast_node_types_1.AST_NODE_TYPES.Identifier?node.property.name:null}return staticString(node.property)}function objectKeyName(node){const key=node.key;if(!node.computed&&key.type===ast_node_types_1.AST_NODE_TYPES.Identifier)return key.name;if(key.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof key.value==="number")return String(key.value);return staticString(key)}function memberPath(node){if(node.type===ast_node_types_1.AST_NODE_TYPES.Identifier)return[node.name];if(node.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression)return null;const head=memberPath(node.object);if(head===null)return null;const tail=propertyName(node);return tail===null?null:[...head,tail]}const REQUEST_SHAPE=new Set(["query","params","headers","cookies","queryStringParameters","pathParameters","multiValueHeaders"]);const REQUEST_SHAPE_NEEDS_DEPTH=new Set(["body"]);function readsRequestShape(node,sourceCode,options={}){let current=node;let firstProperty=null;let secondProperty=null;let depth=0;while(current.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression){const name=propertyName(current);if(name!==null){secondProperty=firstProperty;firstProperty=name}depth+=1;current=current.object}if(current.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)return false;if(firstProperty===null)return false;if((firstProperty==="request"||firstProperty==="req")&&secondProperty!==null){firstProperty=secondProperty;depth-=1}const known=REQUEST_SHAPE.has(firstProperty);const needsDepth=REQUEST_SHAPE_NEEDS_DEPTH.has(firstProperty);if(!known&&!needsDepth)return false;if(needsDepth&&(options.bodyNeedsDepth??true)&&depth<2)return false;const scope=sourceCode.getScope(current);for(let s=scope;s!==null;s=s.upper){const variable=s.set.get(current.name);if(variable===void 0)continue;return variable.defs.some(d=>d.type==="Parameter")}return false}
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.memberPropertyName=exports.namesOneOf=exports.isStaticString=void 0;exports.staticString=staticString;exports.propertyName=propertyName;exports.objectKeyName=objectKeyName;exports.memberPath=memberPath;exports.readsRequestShape=readsRequestShape;const ast_node_types_1=require("../ast-node-types");function staticString(node){if(!node)return null;if(node.type===ast_node_types_1.AST_NODE_TYPES.Literal){return typeof node.value==="string"?node.value:null}if(node.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral&&node.expressions.length===0){const[only]=node.quasis;return only.value.cooked??only.value.raw}return null}const isStaticString=(node,value)=>staticString(node)===value;exports.isStaticString=isStaticString;function propertyName(node){if(!node.computed){return node.property.type===ast_node_types_1.AST_NODE_TYPES.Identifier?node.property.name:null}return staticString(node.property)}function objectKeyName(node){const key=node.key;if(!node.computed&&key.type===ast_node_types_1.AST_NODE_TYPES.Identifier)return key.name;if(key.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof key.value==="number")return String(key.value);return staticString(key)}function memberPath(node){if(node.type===ast_node_types_1.AST_NODE_TYPES.Identifier)return[node.name];if(node.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression)return null;const head=memberPath(node.object);if(head===null)return null;const tail=propertyName(node);return tail===null?null:[...head,tail]}const REQUEST_SHAPE=new Set(["query","params","headers","cookies","queryStringParameters","pathParameters","multiValueHeaders"]);const REQUEST_SHAPE_NEEDS_DEPTH=new Set(["body"]);function readsRequestShape(node,sourceCode,options={}){let current=node;let firstProperty=null;let secondProperty=null;let depth=0;while(current.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression){const name=propertyName(current);if(name!==null){secondProperty=firstProperty;firstProperty=name}depth+=1;current=current.object}if(current.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)return false;if(firstProperty===null)return false;if((firstProperty==="request"||firstProperty==="req")&&secondProperty!==null){firstProperty=secondProperty;depth-=1}const known=REQUEST_SHAPE.has(firstProperty);const needsDepth=REQUEST_SHAPE_NEEDS_DEPTH.has(firstProperty);if(!known&&!needsDepth)return false;if(needsDepth&&(options.bodyNeedsDepth??true)&&depth<2)return false;const scope=sourceCode.getScope(current);for(let s=scope;s!==null;s=s.upper){const variable=s.set.get(current.name);if(variable===void 0)continue;return variable.defs.some(d=>d.type==="Parameter")}return false}const namesOneOf=(name,names)=>name!==null&&(Array.isArray(names)?names.includes(name):names.has(name));exports.namesOneOf=namesOneOf;const memberPropertyName=node=>node!=null&&node.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression?propertyName(node):null;exports.memberPropertyName=memberPropertyName;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.urlEmbedsCredentials=urlEmbedsCredentials;exports.staticStringValue=staticStringValue;exports.isLiteralSecret=isLiteralSecret;exports.createHardcodedCredentialsRule=createHardcodedCredentialsRule;const ast_node_types_1=require("../ast-node-types");const messaging_1=require("../messaging");const unscoped_mutation_rule_1=require("./unscoped-mutation-rule");const require_tls_rule_1=require("./require-tls-rule");const CREDENTIAL_KEYS=["password","pass","pwd","secret","passwd","auth"];const DEFAULT_CONNECTION_KEYS=["host","hostname","port","database","connectionString","socketPath","uri","url","connection","pool","schema"];function urlEmbedsCredentials(value,schemes){const match=/^([a-z][a-z0-9+.-]*):\/\/([^/@\s:]*:[^/@\s]+)@/i.exec(value);if(match===null)return false;return schemes.some(scheme=>scheme.toLowerCase()===match[1].toLowerCase())}function staticStringValue(node){if(node.type===ast_node_types_1.AST_NODE_TYPES.Literal){return typeof node.value==="string"?node.value:void 0}if(node.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral&&node.expressions.length===0){
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.urlEmbedsCredentials=urlEmbedsCredentials;exports.staticStringValue=staticStringValue;exports.isLiteralSecret=isLiteralSecret;exports.createHardcodedCredentialsRule=createHardcodedCredentialsRule;const ast_node_types_1=require("../ast-node-types");const messaging_1=require("../messaging");const unscoped_mutation_rule_1=require("./unscoped-mutation-rule");const require_tls_rule_1=require("./require-tls-rule");const CREDENTIAL_KEYS=["password","pass","pwd","secret","passwd","auth"];const DEFAULT_CONNECTION_KEYS=["host","hostname","port","database","connectionString","socketPath","uri","url","connection","pool","schema"];function urlEmbedsCredentials(value,schemes){const match=/^([a-z][a-z0-9+.-]*):\/\/([^/@\s:]*:[^/@\s]+)@/i.exec(value);if(match===null)return false;return schemes.some(scheme=>scheme.toLowerCase()===match[1].toLowerCase())}function staticStringValue(node){if(node.type===ast_node_types_1.AST_NODE_TYPES.Literal){return typeof node.value==="string"?node.value:void 0}if(node.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral&&node.expressions.length===0){const[{value}]=node.quasis;return value.cooked??value.raw}return void 0}function isLiteralSecret(node){const value=staticStringValue(node);return value!==void 0&&value.length>0}function createHardcodedCredentialsRule(config){const connectionKeys=[...DEFAULT_CONNECTION_KEYS,...config.connectionKeys??[]];return{meta:{type:config.meta.type,docs:{...config.meta.docs},messages:{hardcodedPassword:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Hardcoded Credentials",description:"A database password is written as a literal. It is in git history, every fork, and the built image \u2014 rotating it means rewriting history, so it never gets rotated.",severity:"CRITICAL",cwe:config.meta.docs.cwe,owasp:"A07:2021",compliance:["SOC2","PCI-DSS","ISO27001","NIST-CSF"],effort:"low",fix:config.fix,documentationLink:config.documentationLink}),credentialsInUrl:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Hardcoded Credentials",description:"A connection URL embeds a password in its userinfo section, so the secret is committed with the string.",severity:"CRITICAL",cwe:config.meta.docs.cwe,owasp:"A07:2021",compliance:["SOC2","PCI-DSS","ISO27001","NIST-CSF"],effort:"low",fix:config.urlFix,documentationLink:config.documentationLink})},schema:[]},defaultOptions:[],create(context){let bindings=new Set;let reported=new WeakSet;let scanned=new WeakSet;const report=(node,messageId)=>{if(reported.has(node))return;reported.add(node);context.report({node,messageId})};const scanConfig=obj=>{if(scanned.has(obj))return;scanned.add(obj);for(const prop of obj.properties){if(prop.type!==ast_node_types_1.AST_NODE_TYPES.Property)continue;const name=(0,unscoped_mutation_rule_1.propertyKeyName)(prop);if(name===void 0)continue;if(CREDENTIAL_KEYS.includes(name.toLowerCase())){if(isLiteralSecret(prop.value))report(prop.value,"hardcodedPassword");continue}const url=staticStringValue(prop.value);if(url!==void 0&&urlEmbedsCredentials(url,config.urlSchemes)){report(prop.value,"credentialsInUrl");continue}if(prop.value.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression&&connectionKeys.includes(name)){scanConfig(prop.value)}}};return{Program(program){bindings=(0,unscoped_mutation_rule_1.driverBindings)(program,config.modules);reported=new WeakSet;scanned=new WeakSet},ObjectExpression(node){if(bindings.size===0)return;if(!(0,require_tls_rule_1.looksLikeConnectionConfig)(node,connectionKeys))return;scanConfig(node)},"Literal, TemplateLiteral"(node){if(bindings.size===0)return;const value=staticStringValue(node);if(value===void 0)return;if(!urlEmbedsCredentials(value,config.urlSchemes))return;if(!(0,require_tls_rule_1.inConnectionPosition)(node,connectionKeys,bindings))return;report(node,"credentialsInUrl")}}}}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.SHADOWED=void 0;exports.createReceiverResolver=createReceiverResolver;exports.constructedSource=constructedSource;exports.receiverSource=receiverSource;exports.handlerSource=handlerSource;exports.readsEventPayload=readsEventPayload;exports.payloadRoot=payloadRoot;exports.createPayloadResolver=createPayloadResolver;const ast_node_types_1=require("../ast-node-types");const CONSTRUCTORS=new Map([["WebSocket","websocket"],["Worker","worker"],["SharedWorker","worker"],["FileReader","filereader"]]);const POSTMESSAGE_RECEIVERS=new Set(["window","self","globalThis","parent","top"]);exports.SHADOWED=Symbol("shadowed");function findVariable(scope,name){for(let current=scope;current!==null;current=current.upper){const found=current.variables.find(variable=>variable.name===name);if(found!==void 0)return found}return void 0}function createReceiverResolver(sourceCode){return node=>{const scope=sourceCode.getScope(node);const variable=findVariable(scope,node.name);if(variable===void 0||variable.defs.length===0)return void 0;if(variable.defs.length!==1)return exports.SHADOWED;if(variable.references.filter(reference=>reference.isWrite()).length>1){return exports.SHADOWED}const declarator=variable.defs.find(definition=>definition.node.type===ast_node_types_1.AST_NODE_TYPES.VariableDeclarator);if(declarator===void 0)return exports.SHADOWED;return constructedSource(declarator.node.init)??exports.SHADOWED}}function constructedSource(node){if(node==null||node.type!==ast_node_types_1.AST_NODE_TYPES.NewExpression)return void 0;if(node.callee.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)return void 0;return CONSTRUCTORS.get(node.callee.name)}function receiverSource(receiver,resolve){if(receiver.type===ast_node_types_1.AST_NODE_TYPES.Identifier){const resolved=resolve(receiver);if(resolved===void 0){return POSTMESSAGE_RECEIVERS.has(receiver.name)?"postmessage":void 0}return resolved===exports.SHADOWED?void 0:resolved}return constructedSource(receiver)}function handlerSource(node,resolve){if(node.type===ast_node_types_1.AST_NODE_TYPES.AssignmentExpression){if(node.left.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression)return void 0;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.SHADOWED=void 0;exports.createReceiverResolver=createReceiverResolver;exports.constructedSource=constructedSource;exports.receiverSource=receiverSource;exports.handlerSource=handlerSource;exports.readsEventPayload=readsEventPayload;exports.payloadRoot=payloadRoot;exports.createPayloadResolver=createPayloadResolver;const ast_node_types_1=require("../ast-node-types");const spellings_1=require("../ast/spellings");const CONSTRUCTORS=new Map([["WebSocket","websocket"],["Worker","worker"],["SharedWorker","worker"],["FileReader","filereader"]]);const POSTMESSAGE_RECEIVERS=new Set(["window","self","globalThis","parent","top"]);exports.SHADOWED=Symbol("shadowed");function findVariable(scope,name){for(let current=scope;current!==null;current=current.upper){const found=current.variables.find(variable=>variable.name===name);if(found!==void 0)return found}return void 0}function createReceiverResolver(sourceCode){return node=>{const scope=sourceCode.getScope(node);const variable=findVariable(scope,node.name);if(variable===void 0||variable.defs.length===0)return void 0;if(variable.defs.length!==1)return exports.SHADOWED;if(variable.references.filter(reference=>reference.isWrite()).length>1){return exports.SHADOWED}const declarator=variable.defs.find(definition=>definition.node.type===ast_node_types_1.AST_NODE_TYPES.VariableDeclarator);if(declarator===void 0)return exports.SHADOWED;return constructedSource(declarator.node.init)??exports.SHADOWED}}function constructedSource(node){if(node==null||node.type!==ast_node_types_1.AST_NODE_TYPES.NewExpression)return void 0;if(node.callee.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)return void 0;return CONSTRUCTORS.get(node.callee.name)}function receiverSource(receiver,resolve){if(receiver.type===ast_node_types_1.AST_NODE_TYPES.Identifier){const resolved=resolve(receiver);if(resolved===void 0){return POSTMESSAGE_RECEIVERS.has(receiver.name)?"postmessage":void 0}return resolved===exports.SHADOWED?void 0:resolved}return constructedSource(receiver)}function handlerSource(node,resolve){if(node.type===ast_node_types_1.AST_NODE_TYPES.AssignmentExpression){if(node.left.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression)return void 0;const handlerProp=(0,spellings_1.propertyName)(node.left);if(handlerProp===null)return void 0;const source=receiverSource(node.left.object,resolve);if(source===void 0)return void 0;if(!HANDLER_PROPS[source].has(handlerProp))return void 0;return withHandler(source,node.right)}if(node.type===ast_node_types_1.AST_NODE_TYPES.CallExpression){if(node.callee.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression)return void 0;if((0,spellings_1.propertyName)(node.callee)!=="addEventListener")return void 0;const[eventType,handler]=node.arguments;if(eventType?.type!==ast_node_types_1.AST_NODE_TYPES.Literal)return void 0;if(typeof eventType.value!=="string")return void 0;const source=receiverSource(node.callee.object,resolve);if(source===void 0)return void 0;if(!HANDLER_EVENTS[source].has(eventType.value))return void 0;return withHandler(source,handler)}return void 0}function withHandler(source,handler){if(handler===void 0)return void 0;if(handler.type!==ast_node_types_1.AST_NODE_TYPES.ArrowFunctionExpression&&handler.type!==ast_node_types_1.AST_NODE_TYPES.FunctionExpression){return void 0}const first=handler.params[0];if(first?.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)return void 0;return{source,eventParam:first.name,handler}}const HANDLER_PROPS={websocket:new Set(["onmessage"]),worker:new Set(["onmessage"]),filereader:new Set(["onload","onloadend"]),postmessage:new Set(["onmessage"])};const HANDLER_EVENTS={websocket:new Set(["message"]),worker:new Set(["message"]),filereader:new Set(["load","loadend"]),postmessage:new Set(["message"])};function readsEventPayload(node,eventParam){const root=payloadRoot(node);return root!==void 0&&root.name===eventParam}function payloadRoot(node){let current=node;while(current.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression){current=current.object}return current.type===ast_node_types_1.AST_NODE_TYPES.Identifier?current:void 0}function createPayloadResolver(sourceCode){const resolve=createReceiverResolver(sourceCode);const program=sourceCode.ast;const scopes=[];walk(program,node=>{const handler=handlerSource(node,resolve);if(handler===void 0)return;scopes.push({source:handler.source,handler:handler.handler,range:handler.handler.range})});scopes.sort((a,b)=>a.range[1]-a.range[0]-(b.range[1]-b.range[0]));return node=>scopes.find(scope=>node.range[0]>=scope.range[0]&&node.range[1]<=scope.range[1]&&readsPayloadOf(node,scope.handler,sourceCode))?.source}function readsPayloadOf(node,handler,sourceCode){const root=payloadRoot(node);if(root===void 0)return false;const variable=findVariable(sourceCode.getScope(root),root.name);if(variable===void 0)return false;return variable.defs.some(definition=>definition.type==="Parameter"&&definition.node===handler)}function walk(node,visit){visit(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(isNode(child))walk(child,visit)}}else if(isNode(value)){walk(value,visit)}}}function isNode(value){return typeof value==="object"&&value!==null&&typeof value.type==="string"}
|
|
@@ -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 module_evidence_1=require("./module-evidence");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 usesModule=(0,module_evidence_1.createModuleEvidence)({packages:config.modules});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=usesModule(program)},"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
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.createSqlInjectionRule=createSqlInjectionRule;const ast_node_types_1=require("../ast-node-types");const spellings_1=require("../ast/spellings");const messaging_1=require("../messaging");const module_evidence_1=require("./module-evidence");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 usesModule=(0,module_evidence_1.createModuleEvidence)({packages:config.modules});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=usesModule(program)},"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;const calleeName=isMember?(0,spellings_1.propertyName)(node.callee)??void 0: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)}}}}}}
|