@interlace/eslint-devkit 1.21.2 → 1.21.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 +1 -1
- package/src/rule-creation/driver-origin.js +1 -1
- package/src/rule-creation/mass-assignment-rule.js +1 -1
- package/src/rule-creation/require-tls-rule.d.ts +6 -0
- package/src/rule-creation/require-tls-rule.js +1 -1
- package/src/rule-creation/sql-injection-rule.js +1 -1
- package/src/rule-creation/unscoped-mutation-rule.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@interlace/eslint-devkit",
|
|
3
|
-
"version": "1.21.
|
|
3
|
+
"version": "1.21.4",
|
|
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.isPathAlias=isPathAlias;exports.sourceMatches=sourceMatches;exports.eitherOrigin=eitherOrigin;exports.isFunctionNode=isFunctionNode;exports.writtenAnnotation=writtenAnnotation;exports.createOriginClassifier=createOriginClassifier;const ast_node_types_1=require("../ast-node-types");const spellings_1=require("../ast/spellings");const type_syntax_1=require("../ast/type-syntax");const module_evidence_1=require("./module-evidence");const value_follow_1=require("./value-follow");const PLATFORM_GLOBALS=new Set(["Array","Map","Set","WeakMap","WeakSet","Object","JSON","Math","Promise","Reflect","URL","URLSearchParams","Headers","FormData","Date","globalThis","window","document","console","process","Buffer"]);function isPathAlias(specifier){return/^(?:[@~]\/|~|#)/.test(specifier)}function sourceMatches(source,modules){return modules.some(m=>source===m||source.startsWith(`${m}/`))}function eitherOrigin(a,b){if(a==="ours"||b==="ours")return"ours";return a==="foreign"&&b==="foreign"?"foreign":"unknown"}function isFunctionNode(node){return 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}function writtenAnnotation(identifier){const holder=identifier;return holder.typeAnnotation?.typeAnnotation}function enclosingClass(node){for(let n=node.parent;n;n=n.parent){if(n.type===ast_node_types_1.AST_NODE_TYPES.ClassDeclaration||n.type===ast_node_types_1.AST_NODE_TYPES.ClassExpression){return n}}return void 0}function typeRoot(name){let cursor=name;while(cursor.type===ast_node_types_1.AST_NODE_TYPES.TSQualifiedName)cursor=cursor.left;return cursor.type===ast_node_types_1.AST_NODE_TYPES.Identifier?cursor:null}function createOriginClassifier(modules){const loadsDriver=(0,module_evidence_1.createModuleEvidence)({packages:modules});const classOrigin=(cls,value,depth,seen)=>cls.superClass===null?"foreign":classify((0,value_follow_1.valueAt)(cls.superClass,value.env),depth+1,seen);const declarationOrigin=(value,depth,seen)=>{const node=value.node;if(node.type===ast_node_types_1.AST_NODE_TYPES.TSTypeAliasDeclaration){return typeOrigin((0,value_follow_1.valueAt)(node.typeAnnotation,value.env),depth+1,seen)}if(node.type===ast_node_types_1.AST_NODE_TYPES.TSInterfaceDeclaration){return node.extends.map(base=>classify((0,value_follow_1.valueAt)(base.expression,value.env),depth+1,seen)).includes("ours")?"ours":"foreign"}return classify(value,depth+1,seen)};const typeOrigin=(value,depth,seen)=>{const node=value.node;if(node.type===ast_node_types_1.AST_NODE_TYPES.TSUnionType||node.type===ast_node_types_1.AST_NODE_TYPES.TSIntersectionType){return node.types.map(t=>typeOrigin((0,value_follow_1.valueAt)(t,value.env),depth+1,seen)).reduce(eitherOrigin)}if(node.type===ast_node_types_1.AST_NODE_TYPES.TSTypeQuery){const root2=node.exprName.type===ast_node_types_1.AST_NODE_TYPES.TSImportType?null:typeRoot(node.exprName);return root2===null?"unknown":classify((0,value_follow_1.valueAt)(root2,value.env),depth+1,seen)}if(node.type!==ast_node_types_1.AST_NODE_TYPES.TSTypeReference)return"foreign";const root=typeRoot(node.typeName);const variable=(0,value_follow_1.lookup)(root.name,value.env.scopeOf(root));if(variable===null)return"unknown";if(variable.defs.length===0)return"foreign";const specifier=(0,value_follow_1.importSpecifierOf)(variable);if(specifier!==null){if(sourceMatches(specifier.source,modules))return"ours";if(!(0,value_follow_1.isRelativeSpecifier)(specifier.source))return isPathAlias(specifier.source)?"unknown":"foreign";const origin=(0,value_follow_1.importOrigin)(variable,value.env);const exported=origin===null||origin.name==="*"?null:(0,value_follow_1.exportedValue)(origin.module,origin.name,value.env.parser);return exported===null?"unknown":declarationOrigin(exported,depth+1,seen)}return declarationOrigin((0,value_follow_1.valueAt)(variable.defs[0].node,value.env),depth,seen)};const thisOrigin=(at,key,value,depth,seen)=>{const cls=enclosingClass(at);if(cls===void 0)return"unknown";for(const element of key===null?[]:cls.body.body){if(element.type===ast_node_types_1.AST_NODE_TYPES.PropertyDefinition&&!element.computed&&element.key.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&element.key.name===key){if(element.typeAnnotation){return typeOrigin((0,value_follow_1.valueAt)(element.typeAnnotation.typeAnnotation,value.env),depth+1,seen)}return element.value===null?"unknown":classify((0,value_follow_1.valueAt)(element.value,value.env),depth+1,seen)}if(element.type!==ast_node_types_1.AST_NODE_TYPES.MethodDefinition||element.kind!=="constructor"){continue}for(const param of element.value.params){if(param.type!==ast_node_types_1.AST_NODE_TYPES.TSParameterProperty)continue;const inner=param.parameter.type===ast_node_types_1.AST_NODE_TYPES.AssignmentPattern?param.parameter.left:param.parameter;if(inner.type!==ast_node_types_1.AST_NODE_TYPES.Identifier||inner.name!==key)continue;const annotation=writtenAnnotation(inner);return annotation===void 0?"unknown":typeOrigin((0,value_follow_1.valueAt)(annotation,value.env),depth+1,seen)}for(const statement of element.value.body?.body??[]){if(statement.type===ast_node_types_1.AST_NODE_TYPES.ExpressionStatement&&statement.expression.type===ast_node_types_1.AST_NODE_TYPES.AssignmentExpression&&statement.expression.left.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&statement.expression.left.object.type===ast_node_types_1.AST_NODE_TYPES.ThisExpression&&(0,spellings_1.propertyName)(statement.expression.left)===key){return classify((0,value_follow_1.valueAt)(statement.expression.right,value.env),depth+1,seen)}}}return classOrigin(cls,value,depth,seen)==="ours"?"ours":"unknown"};const parameterOrigin=(identifier,fn,value,depth,seen)=>{const annotation=writtenAnnotation(identifier);if(annotation!==void 0){return typeOrigin((0,value_follow_1.valueAt)(annotation,value.env),depth+1,seen)}const call=fn.parent;if(call?.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&call.callee.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&call.arguments.some(argument=>argument===fn)&&classify((0,value_follow_1.valueAt)(call.callee.object,value.env),depth+1,seen)==="ours"){return"ours"}return"unknown"};const identifierOrigin=(node,value,depth,seen)=>{const variable=(0,value_follow_1.lookup)(node.name,value.scope);if(variable===null)return PLATFORM_GLOBALS.has(node.name)?"foreign":"unknown";if(variable.defs.length===0)return"foreign";const specifier=(0,value_follow_1.importSpecifierOf)(variable);if(specifier!==null){if(sourceMatches(specifier.source,modules))return"ours";if(!(0,value_follow_1.isRelativeSpecifier)(specifier.source))return isPathAlias(specifier.source)?"unknown":"foreign";const followed2=(0,value_follow_1.follow)(value);const judged=followed2.node===value.node||followed2.node.type===ast_node_types_1.AST_NODE_TYPES.Program?"unknown":classify(followed2,depth+1,seen);if(judged!=="unknown")return judged;const origin=(0,value_follow_1.importOrigin)(variable,value.env);return origin!==null&&loadsDriver(origin.module.ast)?"ours":judged}const def=variable.defs[0];if(def.type==="Parameter"){return parameterOrigin(variable.identifiers[0],def.node,value,depth,seen)}if(def.type==="ClassName"){return classOrigin(def.node,value,depth,seen)}if(def.type!=="Variable")return"foreign";const annotation=writtenAnnotation(def.node.id);if(annotation!==void 0){const typed=typeOrigin((0,value_follow_1.valueAt)(annotation,value.env),depth+1,seen);if(typed!=="unknown")return typed}const followed=(0,value_follow_1.follow)(value);if(followed.node!==value.node)return classify(followed,depth+1,seen);const writes=variable.references.filter(r=>r.isWrite()&&r.writeExpr).map(r=>classify((0,value_follow_1.valueAt)(r.writeExpr,value.env),depth+1,seen));return writes.length===0?"unknown":writes.reduce(eitherOrigin)};function classify(value,depth=0,seen=new Set){if(depth>value_follow_1.MAX_FOLLOW_DEPTH||seen.has(value.node))return"unknown";seen.add(value.node);const node=(0,type_syntax_1.unwrapTypeSyntax)(value.node);const inner=target=>classify((0,value_follow_1.valueAt)(target,value.env),depth+1,seen);switch(node.type){case ast_node_types_1.AST_NODE_TYPES.AwaitExpression:return inner(node.argument);case ast_node_types_1.AST_NODE_TYPES.ChainExpression:return inner(node.expression);case ast_node_types_1.AST_NODE_TYPES.Identifier:return identifierOrigin(node,(0,value_follow_1.valueAt)(node,value.env),depth,seen);case ast_node_types_1.AST_NODE_TYPES.ThisExpression:return thisOrigin(node,null,value,depth,seen);case ast_node_types_1.AST_NODE_TYPES.MemberExpression:{if(node.object.type===ast_node_types_1.AST_NODE_TYPES.ThisExpression)return thisOrigin(node,(0,spellings_1.propertyName)(node),value,depth,seen);const followed=(0,value_follow_1.follow)((0,value_follow_1.valueAt)(node,value.env));if(followed.node!==node)return classify(followed,depth+1,seen);return inner(node.object)}case ast_node_types_1.AST_NODE_TYPES.CallExpression:{const callee=(0,value_follow_1.follow)((0,value_follow_1.valueAt)(node.callee,value.env));if(isFunctionNode(callee.node)){const returned=(0,value_follow_1.returnedValue)(callee.node);return returned===null?"unknown":classify((0,value_follow_1.valueAt)(returned,callee.env),depth+1,seen)}return inner(node.callee)}case ast_node_types_1.AST_NODE_TYPES.NewExpression:return inner(node.callee);case ast_node_types_1.AST_NODE_TYPES.ClassDeclaration:case ast_node_types_1.AST_NODE_TYPES.ClassExpression:return classOrigin(node,value,depth,seen);case ast_node_types_1.AST_NODE_TYPES.LogicalExpression:return eitherOrigin(inner(node.left),inner(node.right));case ast_node_types_1.AST_NODE_TYPES.ConditionalExpression:return eitherOrigin(inner(node.consequent),inner(node.alternate));case ast_node_types_1.AST_NODE_TYPES.Literal:case ast_node_types_1.AST_NODE_TYPES.TemplateLiteral:case ast_node_types_1.AST_NODE_TYPES.ObjectExpression:case ast_node_types_1.AST_NODE_TYPES.ArrayExpression:case ast_node_types_1.AST_NODE_TYPES.FunctionExpression:case ast_node_types_1.AST_NODE_TYPES.ArrowFunctionExpression:case ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration:return"foreign";default:return"unknown"}}return Object.assign(value=>classify(value),{ofType:value=>typeOrigin(value,0,new Set)})}
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.isPathAlias=isPathAlias;exports.sourceMatches=sourceMatches;exports.eitherOrigin=eitherOrigin;exports.isFunctionNode=isFunctionNode;exports.writtenAnnotation=writtenAnnotation;exports.createOriginClassifier=createOriginClassifier;const ast_node_types_1=require("../ast-node-types");const spellings_1=require("../ast/spellings");const type_syntax_1=require("../ast/type-syntax");const module_evidence_1=require("./module-evidence");const value_follow_1=require("./value-follow");const PLATFORM_GLOBALS=new Set(["Array","Map","Set","WeakMap","WeakSet","Object","JSON","Math","Promise","Reflect","URL","URLSearchParams","Headers","FormData","Date","globalThis","window","document","console","process","Buffer"]);function isPathAlias(specifier){return/^(?:[@~]\/|~|#)/.test(specifier)}function sourceMatches(source,modules){return modules.some(m=>source===m||source.startsWith(`${m}/`))}function eitherOrigin(a,b){if(a==="ours"||b==="ours")return"ours";return a==="foreign"&&b==="foreign"?"foreign":"unknown"}function isFunctionNode(node){return 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}function writtenAnnotation(identifier){const holder=identifier;return holder.typeAnnotation?.typeAnnotation}function enclosingClass(node){for(let n=node.parent;n;n=n.parent){if(n.type===ast_node_types_1.AST_NODE_TYPES.ClassDeclaration||n.type===ast_node_types_1.AST_NODE_TYPES.ClassExpression){return n}}return void 0}function typeRoot(name){let cursor=name;while(cursor.type===ast_node_types_1.AST_NODE_TYPES.TSQualifiedName)cursor=cursor.left;return cursor.type===ast_node_types_1.AST_NODE_TYPES.Identifier?cursor:null}function createOriginClassifier(modules){const loadsDriver=(0,module_evidence_1.createModuleEvidence)({packages:modules});const classOrigin=(cls,value,depth,seen)=>cls.superClass===null?"foreign":classify((0,value_follow_1.valueAt)(cls.superClass,value.env),depth+1,seen);const declarationOrigin=(value,depth,seen)=>{const node=value.node;if(node.type===ast_node_types_1.AST_NODE_TYPES.TSTypeAliasDeclaration){return typeOrigin((0,value_follow_1.valueAt)(node.typeAnnotation,value.env),depth+1,seen)}if(node.type===ast_node_types_1.AST_NODE_TYPES.TSInterfaceDeclaration){return node.extends.map(base=>classify((0,value_follow_1.valueAt)(base.expression,value.env),depth+1,seen)).includes("ours")?"ours":"foreign"}return classify(value,depth+1,seen)};const typeOrigin=(value,depth,seen)=>{const node=value.node;if(node.type===ast_node_types_1.AST_NODE_TYPES.TSUnionType||node.type===ast_node_types_1.AST_NODE_TYPES.TSIntersectionType){return node.types.map(t=>typeOrigin((0,value_follow_1.valueAt)(t,value.env),depth+1,seen)).reduce(eitherOrigin)}if(node.type===ast_node_types_1.AST_NODE_TYPES.TSTypeQuery){const root2=node.exprName.type===ast_node_types_1.AST_NODE_TYPES.TSImportType?null:typeRoot(node.exprName);return root2===null?"unknown":classify((0,value_follow_1.valueAt)(root2,value.env),depth+1,seen)}if(node.type===ast_node_types_1.AST_NODE_TYPES.TSAnyKeyword||node.type===ast_node_types_1.AST_NODE_TYPES.TSUnknownKeyword){return"unknown"}if(node.type!==ast_node_types_1.AST_NODE_TYPES.TSTypeReference)return"foreign";const root=typeRoot(node.typeName);const variable=(0,value_follow_1.lookup)(root.name,value.env.scopeOf(root));if(variable===null)return"unknown";if(variable.defs.length===0)return"foreign";const specifier=(0,value_follow_1.importSpecifierOf)(variable);if(specifier!==null){if(sourceMatches(specifier.source,modules))return"ours";if(!(0,value_follow_1.isRelativeSpecifier)(specifier.source))return isPathAlias(specifier.source)?"unknown":"foreign";const origin=(0,value_follow_1.importOrigin)(variable,value.env);const exported=origin===null||origin.name==="*"?null:(0,value_follow_1.exportedValue)(origin.module,origin.name,value.env.parser);return exported===null?"unknown":declarationOrigin(exported,depth+1,seen)}return declarationOrigin((0,value_follow_1.valueAt)(variable.defs[0].node,value.env),depth,seen)};const thisOrigin=(at,key,value,depth,seen)=>{const cls=enclosingClass(at);if(cls===void 0)return"unknown";for(const element of key===null?[]:cls.body.body){if(element.type===ast_node_types_1.AST_NODE_TYPES.PropertyDefinition&&!element.computed&&element.key.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&element.key.name===key){if(element.typeAnnotation){return typeOrigin((0,value_follow_1.valueAt)(element.typeAnnotation.typeAnnotation,value.env),depth+1,seen)}return element.value===null?"unknown":classify((0,value_follow_1.valueAt)(element.value,value.env),depth+1,seen)}if(element.type!==ast_node_types_1.AST_NODE_TYPES.MethodDefinition||element.kind!=="constructor"){continue}for(const param of element.value.params){if(param.type!==ast_node_types_1.AST_NODE_TYPES.TSParameterProperty)continue;const inner=param.parameter.type===ast_node_types_1.AST_NODE_TYPES.AssignmentPattern?param.parameter.left:param.parameter;if(inner.type!==ast_node_types_1.AST_NODE_TYPES.Identifier||inner.name!==key)continue;const annotation=writtenAnnotation(inner);return annotation===void 0?"unknown":typeOrigin((0,value_follow_1.valueAt)(annotation,value.env),depth+1,seen)}for(const statement of element.value.body?.body??[]){if(statement.type===ast_node_types_1.AST_NODE_TYPES.ExpressionStatement&&statement.expression.type===ast_node_types_1.AST_NODE_TYPES.AssignmentExpression&&statement.expression.left.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&statement.expression.left.object.type===ast_node_types_1.AST_NODE_TYPES.ThisExpression&&(0,spellings_1.propertyName)(statement.expression.left)===key){return classify((0,value_follow_1.valueAt)(statement.expression.right,value.env),depth+1,seen)}}}return classOrigin(cls,value,depth,seen)==="ours"?"ours":"unknown"};const parameterOrigin=(identifier,fn,value,depth,seen)=>{const annotation=writtenAnnotation(identifier);if(annotation!==void 0){return typeOrigin((0,value_follow_1.valueAt)(annotation,value.env),depth+1,seen)}const call=fn.parent;if(call?.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&call.callee.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&call.arguments.some(argument=>argument===fn)&&classify((0,value_follow_1.valueAt)(call.callee.object,value.env),depth+1,seen)==="ours"){return"ours"}return"unknown"};const identifierOrigin=(node,value,depth,seen)=>{const variable=(0,value_follow_1.lookup)(node.name,value.scope);if(variable===null)return PLATFORM_GLOBALS.has(node.name)?"foreign":"unknown";if(variable.defs.length===0)return"foreign";const specifier=(0,value_follow_1.importSpecifierOf)(variable);if(specifier!==null){if(sourceMatches(specifier.source,modules))return"ours";if(!(0,value_follow_1.isRelativeSpecifier)(specifier.source))return isPathAlias(specifier.source)?"unknown":"foreign";const followed2=(0,value_follow_1.follow)(value);const judged=followed2.node===value.node||followed2.node.type===ast_node_types_1.AST_NODE_TYPES.Program?"unknown":classify(followed2,depth+1,seen);if(judged!=="unknown")return judged;const origin=(0,value_follow_1.importOrigin)(variable,value.env);return origin!==null&&loadsDriver(origin.module.ast)?"ours":judged}const def=variable.defs[0];if(def.type==="Parameter"){return parameterOrigin(variable.identifiers[0],def.node,value,depth,seen)}if(def.type==="ClassName"){return classOrigin(def.node,value,depth,seen)}if(def.type!=="Variable")return"foreign";const annotation=writtenAnnotation(def.node.id);if(annotation!==void 0){const typed=typeOrigin((0,value_follow_1.valueAt)(annotation,value.env),depth+1,seen);if(typed!=="unknown")return typed}const followed=(0,value_follow_1.follow)(value);if(followed.node!==value.node)return classify(followed,depth+1,seen);const writes=variable.references.filter(r=>r.isWrite()&&r.writeExpr).map(r=>classify((0,value_follow_1.valueAt)(r.writeExpr,value.env),depth+1,seen));return writes.length===0?"unknown":writes.reduce(eitherOrigin)};function classify(value,depth=0,seen=new Set){if(depth>value_follow_1.MAX_FOLLOW_DEPTH||seen.has(value.node))return"unknown";seen.add(value.node);const node=(0,type_syntax_1.unwrapTypeSyntax)(value.node);const inner=target=>classify((0,value_follow_1.valueAt)(target,value.env),depth+1,seen);switch(node.type){case ast_node_types_1.AST_NODE_TYPES.AwaitExpression:return inner(node.argument);case ast_node_types_1.AST_NODE_TYPES.ChainExpression:return inner(node.expression);case ast_node_types_1.AST_NODE_TYPES.Identifier:return identifierOrigin(node,(0,value_follow_1.valueAt)(node,value.env),depth,seen);case ast_node_types_1.AST_NODE_TYPES.ThisExpression:return thisOrigin(node,null,value,depth,seen);case ast_node_types_1.AST_NODE_TYPES.MemberExpression:{if(node.object.type===ast_node_types_1.AST_NODE_TYPES.ThisExpression)return thisOrigin(node,(0,spellings_1.propertyName)(node),value,depth,seen);const followed=(0,value_follow_1.follow)((0,value_follow_1.valueAt)(node,value.env));if(followed.node!==node)return classify(followed,depth+1,seen);return inner(node.object)}case ast_node_types_1.AST_NODE_TYPES.CallExpression:{const callee=(0,value_follow_1.follow)((0,value_follow_1.valueAt)(node.callee,value.env));if(isFunctionNode(callee.node)){const returned=(0,value_follow_1.returnedValue)(callee.node);return returned===null?"unknown":classify((0,value_follow_1.valueAt)(returned,callee.env),depth+1,seen)}return inner(node.callee)}case ast_node_types_1.AST_NODE_TYPES.NewExpression:return inner(node.callee);case ast_node_types_1.AST_NODE_TYPES.ClassDeclaration:case ast_node_types_1.AST_NODE_TYPES.ClassExpression:return classOrigin(node,value,depth,seen);case ast_node_types_1.AST_NODE_TYPES.LogicalExpression:return eitherOrigin(inner(node.left),inner(node.right));case ast_node_types_1.AST_NODE_TYPES.ConditionalExpression:return eitherOrigin(inner(node.consequent),inner(node.alternate));case ast_node_types_1.AST_NODE_TYPES.Literal:case ast_node_types_1.AST_NODE_TYPES.TemplateLiteral:case ast_node_types_1.AST_NODE_TYPES.ObjectExpression:case ast_node_types_1.AST_NODE_TYPES.ArrayExpression:case ast_node_types_1.AST_NODE_TYPES.FunctionExpression:case ast_node_types_1.AST_NODE_TYPES.ArrowFunctionExpression:case ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration:return"foreign";default:return"unknown"}}return Object.assign(value=>classify(value),{ofType:value=>typeOrigin(value,0,new Set)})}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.classifyPayload=classifyPayload;exports.createMassAssignmentRule=createMassAssignmentRule;const rule_creator_1=require("./rule-creator");const ast_node_types_1=require("../ast-node-types");const messaging_1=require("../messaging");const module_evidence_1=require("./module-evidence");const rule_creator_2=require("./rule-creator");const driver_origin_1=require("./driver-origin");const request_source_1=require("./request-source");const unscoped_mutation_rule_1=require("./unscoped-mutation-rule");const value_follow_1=require("./value-follow");function classifyPayload(node,env,isOurs=()=>false){if((0,request_source_1.isRequestPayload)(node,env))return"untrustedPayload";if(node.type===ast_node_types_1.AST_NODE_TYPES.Identifier){const variable=(0,value_follow_1.lookup)(node.name,env.scopeOf(node));const filled=variable?.references.some(reference=>{const call=reference.identifier.parent;if(call===void 0||!isObjectAssign(call))return false;const args=call.arguments;return args[0]===reference.identifier&&!isOurs(reference.identifier)&&args.slice(1).some(source=>(0,request_source_1.isRequestPayload)(source,env))});if(filled===true)return"untrustedPayload"}if(node.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression){for(const prop of node.properties){if(prop.type!==ast_node_types_1.AST_NODE_TYPES.SpreadElement)continue;if((0,request_source_1.isRequestPayload)(prop.argument,env))return"untrustedSpread"}}return false}function isObjectAssign(node){return node.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&node.callee.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&node.callee.object.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&node.callee.object.name==="Object"&&!node.callee.computed&&node.callee.property.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&node.callee.property.name==="assign"}const WRITE_VERBS=new Set(["insert","update"]);function createMassAssignmentRule(config){const methods=new Set(config.methods);const payloadKeys=new Set(config.payloadKeys??[]);const usesModule=(0,module_evidence_1.createModuleEvidence)({packages:config.modules});const originOf=(0,driver_origin_1.createOriginClassifier)(config.modules);return rule_creator_1.RuleCreator.withoutDocs({meta:{interop:{runtimes:["eslint","oxlint"]},type:config.meta.type,docs:{...config.meta.docs},messages:{untrustedPayload:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Mass Assignment",description:"The request object is written straight to the database, so every column the model exposes is settable by the caller \u2014 including ones this code never mentions.",severity:"HIGH",cwe:config.meta.docs.cwe,owasp:"A04:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"medium",fix:config.fix,documentationLink:config.documentationLink}),untrustedSpread:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Mass Assignment",description:"Spreading the request into the payload carries every key it happens to hold, not just the ones written beside it.",severity:"HIGH",cwe:config.meta.docs.cwe,owasp:"A04:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"medium",fix:config.fix,documentationLink:config.documentationLink})},schema:[]},defaultOptions:[],createOnce(context){let env;let ownsFile;const isOurs=node=>originOf((0,value_follow_1.valueAt)(node,env))==="ours";const hasDistinctiveShape=node=>{const callee=node.callee;switch(config.distinctiveShape){case"model-accessor":return callee.object.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression;case"table-call":{const chained=(0,unscoped_mutation_rule_1.chainMethodNames)(node);return(0,unscoped_mutation_rule_1.startsWithTable)(callee.object)||["into","table","from"].some(m=>chained.has(m))}case"builder-chain":{const object=callee.object;return object.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&object.callee.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&WRITE_VERBS.has(object.callee
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.classifyPayload=classifyPayload;exports.createMassAssignmentRule=createMassAssignmentRule;const rule_creator_1=require("./rule-creator");const ast_node_types_1=require("../ast-node-types");const messaging_1=require("../messaging");const module_evidence_1=require("./module-evidence");const rule_creator_2=require("./rule-creator");const driver_origin_1=require("./driver-origin");const request_source_1=require("./request-source");const spellings_1=require("../ast/spellings");const unscoped_mutation_rule_1=require("./unscoped-mutation-rule");const value_follow_1=require("./value-follow");function classifyPayload(node,env,isOurs=()=>false){if((0,request_source_1.isRequestPayload)(node,env))return"untrustedPayload";if(node.type===ast_node_types_1.AST_NODE_TYPES.Identifier){const variable=(0,value_follow_1.lookup)(node.name,env.scopeOf(node));const filled=variable?.references.some(reference=>{const call=reference.identifier.parent;if(call===void 0||!isObjectAssign(call))return false;const args=call.arguments;return args[0]===reference.identifier&&!isOurs(reference.identifier)&&args.slice(1).some(source=>(0,request_source_1.isRequestPayload)(source,env))});if(filled===true)return"untrustedPayload"}if(node.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression){for(const prop of node.properties){if(prop.type!==ast_node_types_1.AST_NODE_TYPES.SpreadElement)continue;if((0,request_source_1.isRequestPayload)(prop.argument,env))return"untrustedSpread"}}return false}function isObjectAssign(node){return node.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&node.callee.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&node.callee.object.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&node.callee.object.name==="Object"&&!node.callee.computed&&node.callee.property.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&node.callee.property.name==="assign"}const WRITE_VERBS=new Set(["insert","update"]);function createMassAssignmentRule(config){const methods=new Set(config.methods);const payloadKeys=new Set(config.payloadKeys??[]);const usesModule=(0,module_evidence_1.createModuleEvidence)({packages:config.modules});const originOf=(0,driver_origin_1.createOriginClassifier)(config.modules);return rule_creator_1.RuleCreator.withoutDocs({meta:{interop:{runtimes:["eslint","oxlint"]},type:config.meta.type,docs:{...config.meta.docs},messages:{untrustedPayload:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Mass Assignment",description:"The request object is written straight to the database, so every column the model exposes is settable by the caller \u2014 including ones this code never mentions.",severity:"HIGH",cwe:config.meta.docs.cwe,owasp:"A04:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"medium",fix:config.fix,documentationLink:config.documentationLink}),untrustedSpread:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Mass Assignment",description:"Spreading the request into the payload carries every key it happens to hold, not just the ones written beside it.",severity:"HIGH",cwe:config.meta.docs.cwe,owasp:"A04:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"medium",fix:config.fix,documentationLink:config.documentationLink})},schema:[]},defaultOptions:[],createOnce(context){let env;let ownsFile;const isOurs=node=>originOf((0,value_follow_1.valueAt)(node,env))==="ours";const hasDistinctiveShape=node=>{const callee=node.callee;switch(config.distinctiveShape){case"model-accessor":return callee.object.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression;case"table-call":{const chained=(0,unscoped_mutation_rule_1.chainMethodNames)(node);return(0,unscoped_mutation_rule_1.startsWithTable)(callee.object)||["into","table","from"].some(m=>chained.has(m))}case"builder-chain":{const object=callee.object;return object.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&object.callee.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&WRITE_VERBS.has((0,spellings_1.propertyName)(object.callee)??"")}default:return false}};return{before(){if((0,rule_creator_2.isTestFilePath)(context.filename))return false;env=(0,value_follow_1.envOf)(context);ownsFile=false;return true},Program(program){ownsFile=usesModule(program)},CallExpression(node){if(isObjectAssign(node)){const[target,...sources]=node.arguments;if(target===void 0||!isOurs(target))return;for(const source of sources){if((0,request_source_1.isRequestPayload)(source,env))context.report({node:source,messageId:"untrustedPayload"})}return}if(node.callee.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression||!methods.has((0,spellings_1.propertyName)(node.callee)??"")){return}const origin=originOf((0,value_follow_1.valueAt)(node.callee.object,env));if(origin==="foreign")return;if(origin==="unknown"&&!(ownsFile&&hasDistinctiveShape(node))){return}if(config.allowlistOption!==void 0&&node.arguments.some(arg=>arg.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression&&arg.properties.some(p=>p.type===ast_node_types_1.AST_NODE_TYPES.Property&&(0,unscoped_mutation_rule_1.propertyKeyName)(p)===config.allowlistOption))){return}for(const arg of node.arguments){if(arg.type===ast_node_types_1.AST_NODE_TYPES.SpreadElement)continue;const direct=classifyPayload(arg,env,isOurs);if(direct){context.report({node:arg,messageId:direct});continue}if(arg.type!==ast_node_types_1.AST_NODE_TYPES.ObjectExpression)continue;for(const prop of arg.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||!payloadKeys.has(name))continue;const nested=classifyPayload(prop.value,env,isOurs);if(nested)context.report({node:prop.value,messageId:nested})}}}}}})}
|
|
@@ -84,6 +84,12 @@ export declare function looksLikeConnectionConfig(obj: TSESTree.ObjectExpression
|
|
|
84
84
|
* driver parses as a connection option, so this is not a finding.
|
|
85
85
|
*/
|
|
86
86
|
export declare function urlDisablesTls(value: string, schemes: readonly string[]): boolean;
|
|
87
|
+
/**
|
|
88
|
+
* What a connection URL turns off: TLS itself (`sslmode=disable`,
|
|
89
|
+
* `ssl=false`), or only the certificate check (`sslmode=no-verify`, which
|
|
90
|
+
* pg-connection-string maps to `ssl: { rejectUnauthorized: false }`).
|
|
91
|
+
*/
|
|
92
|
+
export declare function urlTlsFinding(value: string, schemes: readonly string[]): RequireTlsMessageIds | null;
|
|
87
93
|
/** Is `inner` inside (or the same node as) `outer`? */
|
|
88
94
|
export declare function within(inner: TSESTree.Node, outer: TSESTree.Node): boolean;
|
|
89
95
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.isLiteralBoolean=isLiteralBoolean;exports.looksLikeConnectionConfig=looksLikeConnectionConfig;exports.urlDisablesTls=urlDisablesTls;exports.within=within;exports.inConnectionPosition=inConnectionPosition;exports.createRequireTlsRule=createRequireTlsRule;const rule_creator_1=require("./rule-creator");const ast_node_types_1=require("../ast-node-types");const messaging_1=require("../messaging");const unscoped_mutation_rule_1=require("./unscoped-mutation-rule");const module_evidence_1=require("./module-evidence");const rule_creator_2=require("./rule-creator");const driver_origin_1=require("./driver-origin");const connection_config_1=require("./connection-config");const value_follow_1=require("./value-follow");const DEFAULT_CONNECTION_KEYS=["host","hostname","port","user","username","password","database","connectionString","socketPath","uri","url","connection","pool","schema"];const TLS_OFF_KEYS=["ssl","secure","tls","encrypt","useSSL","requireSSL"];const VERIFY_OFF_KEYS=["rejectUnauthorized","verifyServerCertificate","checkServerIdentity","trustServerCertificate"];function isLiteralBoolean(value,expect){if(value.type!==ast_node_types_1.AST_NODE_TYPES.Literal)return false;if(typeof value.value!=="boolean"){return expect===false&&!value.value}return value.value===expect}function looksLikeConnectionConfig(obj,connectionKeys){return obj.properties.some(prop=>{const name=(0,unscoped_mutation_rule_1.propertyKeyName)(prop);return name!==void 0&&connectionKeys.includes(name)})}function urlDisablesTls(value,schemes){if(!schemes.some(scheme=>value.startsWith(`${scheme}://`)))return
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.isLiteralBoolean=isLiteralBoolean;exports.looksLikeConnectionConfig=looksLikeConnectionConfig;exports.urlDisablesTls=urlDisablesTls;exports.urlTlsFinding=urlTlsFinding;exports.within=within;exports.inConnectionPosition=inConnectionPosition;exports.createRequireTlsRule=createRequireTlsRule;const rule_creator_1=require("./rule-creator");const ast_node_types_1=require("../ast-node-types");const messaging_1=require("../messaging");const unscoped_mutation_rule_1=require("./unscoped-mutation-rule");const module_evidence_1=require("./module-evidence");const rule_creator_2=require("./rule-creator");const driver_origin_1=require("./driver-origin");const connection_config_1=require("./connection-config");const value_follow_1=require("./value-follow");const DEFAULT_CONNECTION_KEYS=["host","hostname","port","user","username","password","database","connectionString","socketPath","uri","url","connection","pool","schema"];const TLS_OFF_KEYS=["ssl","secure","tls","encrypt","useSSL","requireSSL"];const VERIFY_OFF_KEYS=["rejectUnauthorized","verifyServerCertificate","checkServerIdentity","trustServerCertificate"];function isLiteralBoolean(value,expect){if(value.type!==ast_node_types_1.AST_NODE_TYPES.Literal)return false;if(typeof value.value!=="boolean"){return expect===false&&!value.value}return value.value===expect}function looksLikeConnectionConfig(obj,connectionKeys){return obj.properties.some(prop=>{const name=(0,unscoped_mutation_rule_1.propertyKeyName)(prop);return name!==void 0&&connectionKeys.includes(name)})}function urlDisablesTls(value,schemes){return urlTlsFinding(value,schemes)==="tlsDisabled"}function urlTlsFinding(value,schemes){if(!schemes.some(scheme=>value.startsWith(`${scheme}://`)))return null;const query=value.split("#")[0];if(/[?&](?:sslmode=disable|ssl=(?:false|0))(?:&|$)/i.test(query))return"tlsDisabled";if(/[?&]sslmode=no-verify(?:&|$)/i.test(query))return"certificateValidationDisabled";return null}function within(inner,outer){return outer.range[0]<=inner.range[0]&&inner.range[1]<=outer.range[1]}function inConnectionPosition(node,connectionKeys,isDriverCallee){const parent=node.parent;if(!parent)return false;if(parent.type===ast_node_types_1.AST_NODE_TYPES.Property&&parent.value===node){const name=(0,unscoped_mutation_rule_1.propertyKeyName)(parent);return name!==void 0&&connectionKeys.includes(name)}if(parent.type===ast_node_types_1.AST_NODE_TYPES.CallExpression||parent.type===ast_node_types_1.AST_NODE_TYPES.NewExpression){if(!parent.arguments.includes(node))return false;return isDriverCallee(parent.callee)}return false}function createRequireTlsRule(config){const connectionKeys=[...DEFAULT_CONNECTION_KEYS,...config.connectionKeys??[]];const urlSchemes=config.urlSchemes??[];const usesModule=(0,module_evidence_1.createModuleEvidence)({packages:config.modules,extraEvidence:(0,connection_config_1.configShapeEvidence)(config.configShapes??[])});const originOf=(0,driver_origin_1.createOriginClassifier)(config.modules);return rule_creator_1.RuleCreator.withoutDocs({meta:{interop:{runtimes:["eslint","oxlint"]},type:config.meta.type,docs:{...config.meta.docs},messages:{tlsDisabled:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Cleartext Database Connection",description:"TLS is explicitly disabled on this database connection. Queries, result rows and the credentials that open the session all cross the network in cleartext.",severity:"HIGH",cwe:config.meta.docs.cwe,owasp:"A02:2021",compliance:["SOC2","PCI-DSS","HIPAA","NIST-CSF"],effort:"low",fix:config.fix,documentationLink:config.documentationLink}),certificateValidationDisabled:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Unverified Database TLS Peer",description:"Certificate validation is disabled on this database connection. The traffic is encrypted but the server is never authenticated, so it encrypts just as willingly to whoever answered in its place.",severity:"HIGH",cwe:config.meta.docs.cwe,owasp:"A02:2021",compliance:["SOC2","PCI-DSS","HIPAA","NIST-CSF"],effort:"medium",fix:"Supply the server CA instead of switching verification off \u2014 `ssl: { ca: fs.readFileSync(caPath) }`. If the certificate is self-signed, add that CA to the trust store rather than trusting everything.",documentationLink:config.documentationLink})},schema:[]},defaultOptions:[],createOnce(context){let env;let ownsFile;let reported;function isDriverCallee(callee){return originOf((0,value_follow_1.valueAt)(callee,env))==="ours"}const report=(node,messageId)=>{if(reported.has(node))return;reported.add(node);context.report({node,messageId})};const judgeProperty=(prop,name,depth)=>{const tlsKey=TLS_OFF_KEYS.includes(name);const verifyKey=VERIFY_OFF_KEYS.includes(name);const inverted=name==="trustServerCertificate";for(const value of(0,connection_config_1.possibleValues)((0,value_follow_1.valueAt)(prop.value,env))){const inline=value.env===env&&within(value.node,prop.value);if(tlsKey&&isLiteralBoolean(value.node,false)){report(prop,"tlsDisabled");return}if(verifyKey&&isLiteralBoolean(value.node,inverted)){report(prop,"certificateValidationDisabled");return}if(value.node.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression&&(tlsKey||connectionKeys.includes(name))){if(inline)scanTlsKeys(value.node,depth+1);else if(disablesTls(value.node))report(prop,"certificateValidationDisabled")}}};const disablesTls=obj=>obj.properties.some(prop=>prop.type===ast_node_types_1.AST_NODE_TYPES.Property&&VERIFY_OFF_KEYS.includes((0,unscoped_mutation_rule_1.propertyKeyName)(prop))&&isLiteralBoolean(prop.value,(0,unscoped_mutation_rule_1.propertyKeyName)(prop)==="trustServerCertificate"));const scanTlsKeys=(obj,depth=0)=>{if(depth>4)return;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;judgeProperty(prop,name,depth)}};return{before(){if((0,rule_creator_2.isTestFilePath)(context.filename))return false;env=(0,value_follow_1.envOf)(context);ownsFile=false;reported=new WeakSet;return true},Program(program){ownsFile=usesModule(program)},ObjectExpression(node){if(!ownsFile)return;if(!looksLikeConnectionConfig(node,connectionKeys))return;if((0,connection_config_1.passedToForeign)(node,originOf,env))return;scanTlsKeys(node)},Literal(node){if(!ownsFile)return;if(typeof node.value!=="string")return;const finding=urlTlsFinding(node.value,urlSchemes);if(finding===null)return;if(!inConnectionPosition(node,connectionKeys,isDriverCallee))return;const owner=node.parent.parent;if(owner.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression&&(0,connection_config_1.passedToForeign)(owner,originOf,env)){return}report(node,finding)},"CallExpression, NewExpression"(node){if(!ownsFile||!isDriverCallee(node.callee))return;for(const argument of node.arguments){if(argument.type===ast_node_types_1.AST_NODE_TYPES.Literal)continue;for(const value of(0,connection_config_1.possibleValues)((0,value_follow_1.valueAt)(argument,env))){const literal=value.node;if(literal.type!==ast_node_types_1.AST_NODE_TYPES.Literal||typeof literal.value!=="string"){continue}const finding=urlTlsFinding(literal.value,urlSchemes);if(finding!==null){report(argument,finding);break}}}}}}})}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.looksLikeSqlStatement=looksLikeSqlStatement;exports.createSqlInjectionRule=createSqlInjectionRule;const rule_creator_1=require("./rule-creator");const ast_node_types_1=require("../ast-node-types");const spellings_1=require("../ast/spellings");const type_syntax_1=require("../ast/type-syntax");const messaging_1=require("../messaging");const module_evidence_1=require("./module-evidence");const rule_creator_2=require("./rule-creator");const driver_origin_1=require("./driver-origin");const request_source_1=require("./request-source");const sql_safety_1=require("./sql-safety");const sql_position_1=require("./sql-position");const value_follow_1=require("./value-follow");const SQL_STATEMENTS=[/^[\s(]*select\b[\s\S]*\bfrom\b/i,/^[\s(]*select\s/i,/^\s*insert\s+(?:or\s+\w+\s+|ignore\s+)?into\b/i,/^\s*update\b[\s\S]*\bset\b/i,/^\s*delete\s+from\b/i,/^\s*(?:replace|merge)\s+into\b/i,/^\s*with\b[\s\S]*\bas\s*\(/i,/^\s*(?:create|drop|alter|truncate)\s+(?:unique\s+|temp(?:orary)?\s+|or\s+replace\s+)*(?:table|index|view|schema|database|sequence|trigger|function|procedure|user|role)\b/i,/^\s*(?:pragma|attach|detach|vacuum|analyze|reindex|show|describe|explain)\b/i,/^\s*call\s+[\w".`]+\s*\(/i];const PARTITION_SHARED_SINKS=new Set(["query","execute"]);function looksLikeSqlStatement(text){return SQL_STATEMENTS.some(pattern=>pattern.test(text))}function enclosingFunction(node){for(let n=node.parent;n;n=n.parent){if((0,driver_origin_1.isFunctionNode)(n))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 createSqlInjectionRule(config){const statementSinks=new Set(config.methods);const fragmentSinks=new Map;for(const sink of config.fragmentMethods??[]){const spec=typeof sink==="string"?{name:sink}:sink;fragmentSinks.set(spec.name,{name:spec.name,arg:spec.arg??0,requireDriverReceiver:spec.requireDriverReceiver??false})}const fragmentFunctions=new Set(config.fragmentFunctions??[]);const aliasCallbacks=new Set(config.aliasCallbacks??[]);const sqlTextProperties=new Set(config.sqlTextProperties??[]);const statementFactories=new Set(config.statementFactories??[]);const usesModule=(0,module_evidence_1.createModuleEvidence)({packages:config.modules});const originOf=(0,driver_origin_1.createOriginClassifier)(config.modules);const safety=(0,sql_safety_1.createSqlSafety)({origin:originOf,escapers:new Set(config.escapers??[]),fragmentFactories:new Set(config.fragmentFactories??[]),modules:config.modules,escaperModules:config.escaperModules});return rule_creator_1.RuleCreator.withoutDocs({meta:{interop:{runtimes:["eslint","oxlint"]},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:[],createOnce(context){let env;let wrappers;let definitions;let wrapperFns;let pending;let ownsFile;let requestTextSink;const judge=(node,gate,depth=0)=>{const arg=(0,type_syntax_1.unwrapTypeSyntax)(node);if(arg.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral||arg.type===ast_node_types_1.AST_NODE_TYPES.BinaryExpression&&arg.operator==="+"){if(arg.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral&&arg.expressions.length===0){return false}const kind=arg.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral?"template":"concat";const text=(0,sql_safety_1.sqlStaticText)(arg);if(kind==="concat"&&text.trim()==="")return false;if(gate&&!looksLikeSqlStatement(text))return false;return(0,sql_position_1.sqlHoles)(arg).some(isUnsafeHole)?kind:false}if(depth>2)return false;if(arg.type===ast_node_types_1.AST_NODE_TYPES.Identifier){return judgeBinding(arg,gate,depth)}if(arg.type===ast_node_types_1.AST_NODE_TYPES.CallExpression){const target=(0,value_follow_1.follow)((0,value_follow_1.valueAt)(arg.callee,env));if(target.env!==env||!(0,driver_origin_1.isFunctionNode)(target.node))return false;for(const result of(0,value_follow_1.returnedValues)(target.node)??[]){const kind=judge(result,gate,depth+1);if(kind)return kind}}return false};const isUnsafeHole=({expression,preceding})=>!safety.isSafe((0,value_follow_1.valueAt)(expression,env),(0,sql_position_1.isQuoted)(preceding))&&((0,sql_position_1.isValuePosition)(preceding,config.doubleQuotedStrings)||(0,sql_position_1.isRequestDerived)(expression,env));const judgeBinding=(node,gate,depth)=>{const variable=(0,value_follow_1.lookup)(node.name,env.scopeOf(node));if(variable===null||!variable.defs.some(def=>def.type==="Variable")||variable.defs.some(def=>def.type==="Parameter")){return false}const writes=variable.references.filter(r=>r.isWrite()&&r.writeExpr).map(r=>r.writeExpr);const text=writes.map(sql_safety_1.sqlStaticText).join(" ");if(gate&&!looksLikeSqlStatement(text))return false;for(const write of writes){const kind=judge(write,false,depth+1);if(kind)return kind}return false};const isRequestText=node=>{let cursor=(0,type_syntax_1.unwrapTypeSyntax)(node);while(cursor.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&cursor.callee.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&cursor.callee.name==="String"&&cursor.arguments.length===1){cursor=(0,type_syntax_1.unwrapTypeSyntax)(cursor.arguments[0])}for(let link=cursor;;link=link.object){if((0,request_source_1.isRequestPayload)(link,env))return true;if(link.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression)return false}};const report=(node,kind)=>{context.report({node,messageId:kind==="template"?"unsafeTemplateLiteral":"noUnsafeQuery"})};const isStatementObject=receiver=>{const value=(0,type_syntax_1.unwrapTypeSyntax)((0,value_follow_1.follow)((0,value_follow_1.valueAt)(receiver,env)).node);return value.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&value.callee.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&statementFactories.has((0,spellings_1.propertyName)(value.callee)??"")};const check=(arg,gate)=>{let text=(0,type_syntax_1.unwrapTypeSyntax)(arg);if(text.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression&&sqlTextProperties.size>0){const property=text.properties.find(p=>p.type===ast_node_types_1.AST_NODE_TYPES.Property&&sqlTextProperties.has((0,spellings_1.objectKeyName)(p)??""));if(property===void 0)return;text=property.value}const kind=judge(text,gate);if(kind){report(text,kind);return}if(requestTextSink&&isRequestText(text))report(text,"concat")};const importedName=callee=>{const variable=(0,value_follow_1.lookup)(callee.name,env.scopeOf(callee));const specifier=variable===null?null:(0,value_follow_1.importSpecifierOf)(variable);return specifier!==null&&config.modules.some(m=>specifier.source===m||specifier.source.startsWith(`${m}/`))?specifier.name:null};const checkAliasCallback=node=>{const callback=node.arguments[0];if(!callback||!(0,driver_origin_1.isFunctionNode)(callback))return;const alias=callback.params[0];for(const result of(0,value_follow_1.returnedValues)(callback)??[]){const text=(0,type_syntax_1.unwrapTypeSyntax)(result);if(text.type!==ast_node_types_1.AST_NODE_TYPES.TemplateLiteral)continue;const unsafe=(0,sql_position_1.sqlHoles)(text).some(hole=>!(hole.expression.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&alias?.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&hole.expression.name===alias.name)&&isUnsafeHole(hole));if(unsafe)report(text,"template")}};return{before(){if((0,rule_creator_2.isTestFilePath)(context.filename))return false;env=(0,value_follow_1.envOf)(context);wrappers=new Map;definitions=new Map;wrapperFns=new Map;pending=[];ownsFile=false;requestTextSink=true;return true},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)},CallExpression(node){const callee=node.callee;const isMember=callee.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression;const calleeName=isMember?(0,spellings_1.propertyName)(callee)??void 0:callee.type===ast_node_types_1.AST_NODE_TYPES.Identifier?callee.name:void 0;if(calleeName===void 0)return;if(!isMember&&ownsFile){const imported=importedName(callee);if(imported!==null&&fragmentFunctions.has(imported)){requestTextSink=true;if(node.arguments[0])check(node.arguments[0],false);return}if(imported!==null&&aliasCallbacks.has(imported)){checkAliasCallback(node);return}}const fragment=isMember?fragmentSinks.get(calleeName):void 0;if(!isMember||!statementSinks.has(calleeName)&&!fragment){for(const[index,arg]of node.arguments.entries()){if(arg.type===ast_node_types_1.AST_NODE_TYPES.SpreadElement)continue;pending.push({name:calleeName,index,arg})}return}const receiver=originOf((0,value_follow_1.valueAt)(callee.object,env));if(receiver==="foreign")return;if(fragment?.requireDriverReceiver&&receiver!=="ours")return;if(!ownsFile&&!(receiver==="ours"&&!PARTITION_SHARED_SINKS.has(calleeName))){return}const queryArg=node.arguments[fragment?.arg??0];if(!queryArg||queryArg.type===ast_node_types_1.AST_NODE_TYPES.SpreadElement)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 gate=fragment===void 0&&config.requireSqlKeywords&&receiver!=="ours";requestTextSink=!isStatementObject(callee.object);check(queryArg,gate)},"Program:exit"(){if(!ownsFile)return;const proven=new Map;for(const name of wrapperFns.values())proven.set(name,(proven.get(name)??0)+1);for(const{name,index,arg}of pending){if(wrappers.get(name)!==index)continue;if(definitions.get(name)!==proven.get(name))continue;const kind=judge(arg,true);if(kind)report((0,type_syntax_1.unwrapTypeSyntax)(arg),kind)}}}}})}
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.looksLikeSqlStatement=looksLikeSqlStatement;exports.createSqlInjectionRule=createSqlInjectionRule;const rule_creator_1=require("./rule-creator");const ast_node_types_1=require("../ast-node-types");const spellings_1=require("../ast/spellings");const type_syntax_1=require("../ast/type-syntax");const messaging_1=require("../messaging");const module_evidence_1=require("./module-evidence");const rule_creator_2=require("./rule-creator");const driver_origin_1=require("./driver-origin");const request_source_1=require("./request-source");const sql_safety_1=require("./sql-safety");const sql_position_1=require("./sql-position");const value_follow_1=require("./value-follow");const SQL_STATEMENTS=[/^[\s(]*select\b[\s\S]*\bfrom\b/i,/^[\s(]*select\s/i,/^\s*insert\s+(?:or\s+\w+\s+|ignore\s+)?into\b/i,/^\s*update\b[\s\S]*\bset\b/i,/^\s*delete\s+from\b/i,/^\s*(?:replace|merge)\s+into\b/i,/^\s*with\b[\s\S]*\bas\s*\(/i,/^\s*(?:create|drop|alter|truncate)\s+(?:unique\s+|temp(?:orary)?\s+|or\s+replace\s+)*(?:table|index|view|schema|database|sequence|trigger|function|procedure|user|role)\b/i,/^\s*(?:pragma|attach|detach|vacuum|analyze|reindex|show|describe|explain)\b/i,/^\s*call\s+[\w".`]+\s*\(/i];const PARTITION_SHARED_SINKS=new Set(["query","execute"]);function looksLikeSqlStatement(text){return SQL_STATEMENTS.some(pattern=>pattern.test(text))}function enclosingFunction(node){for(let n=node.parent;n;n=n.parent){if((0,driver_origin_1.isFunctionNode)(n))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 createSqlInjectionRule(config){const statementSinks=new Set(config.methods);const fragmentSinks=new Map;for(const sink of config.fragmentMethods??[]){const spec=typeof sink==="string"?{name:sink}:sink;fragmentSinks.set(spec.name,{name:spec.name,arg:spec.arg??0,requireDriverReceiver:spec.requireDriverReceiver??false})}const fragmentFunctions=new Set(config.fragmentFunctions??[]);const aliasCallbacks=new Set(config.aliasCallbacks??[]);const sqlTextProperties=new Set(config.sqlTextProperties??[]);const statementFactories=new Set(config.statementFactories??[]);const usesModule=(0,module_evidence_1.createModuleEvidence)({packages:config.modules});const originOf=(0,driver_origin_1.createOriginClassifier)(config.modules);const safety=(0,sql_safety_1.createSqlSafety)({origin:originOf,escapers:new Set(config.escapers??[]),fragmentFactories:new Set(config.fragmentFactories??[]),modules:config.modules,escaperModules:config.escaperModules});return rule_creator_1.RuleCreator.withoutDocs({meta:{interop:{runtimes:["eslint","oxlint"]},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:[],createOnce(context){let env;let wrappers;let definitions;let wrapperFns;let pending;let ownsFile;const judge=(node,gate,depth=0)=>{const arg=(0,type_syntax_1.unwrapTypeSyntax)(node);if(arg.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral||arg.type===ast_node_types_1.AST_NODE_TYPES.BinaryExpression&&arg.operator==="+"){if(arg.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral&&arg.expressions.length===0){return false}const kind=arg.type===ast_node_types_1.AST_NODE_TYPES.TemplateLiteral?"template":"concat";const text=(0,sql_safety_1.sqlStaticText)(arg);if(kind==="concat"&&text.trim()==="")return false;if(gate&&!looksLikeSqlStatement(text))return false;return(0,sql_position_1.sqlHoles)(arg).some(isUnsafeHole)?kind:false}if(depth>2)return false;if(arg.type===ast_node_types_1.AST_NODE_TYPES.Identifier){return judgeBinding(arg,gate,depth)}if(arg.type===ast_node_types_1.AST_NODE_TYPES.CallExpression){const target=(0,value_follow_1.follow)((0,value_follow_1.valueAt)(arg.callee,env));if(target.env!==env||!(0,driver_origin_1.isFunctionNode)(target.node))return false;for(const result of(0,value_follow_1.returnedValues)(target.node)??[]){const kind=judge(result,gate,depth+1);if(kind)return kind}}return false};const isUnsafeHole=({expression,preceding})=>!safety.isSafe((0,value_follow_1.valueAt)(expression,env),(0,sql_position_1.isQuoted)(preceding))&&((0,sql_position_1.isValuePosition)(preceding,config.doubleQuotedStrings)||(0,sql_position_1.isRequestDerived)(expression,env));const judgeBinding=(node,gate,depth)=>{const variable=(0,value_follow_1.lookup)(node.name,env.scopeOf(node));if(variable===null||!variable.defs.some(def=>def.type==="Variable")||variable.defs.some(def=>def.type==="Parameter")){return false}const writes=variable.references.filter(r=>r.isWrite()&&r.writeExpr).map(r=>r.writeExpr);const text=writes.map(sql_safety_1.sqlStaticText).join(" ");if(gate&&!looksLikeSqlStatement(text))return false;for(const write of writes){const kind=judge(write,false,depth+1);if(kind)return kind}return false};const isRequestText=node=>{let cursor=(0,type_syntax_1.unwrapTypeSyntax)(node);while(cursor.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&cursor.callee.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&cursor.callee.name==="String"&&cursor.arguments.length===1){cursor=(0,type_syntax_1.unwrapTypeSyntax)(cursor.arguments[0])}for(let link=cursor;;link=link.object){if((0,request_source_1.isRequestPayload)(link,env))return true;if(link.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression)return false}};const report=(node,kind)=>{context.report({node,messageId:kind==="template"?"unsafeTemplateLiteral":"noUnsafeQuery"})};const isStatementObject=receiver=>{const value=(0,type_syntax_1.unwrapTypeSyntax)((0,value_follow_1.follow)((0,value_follow_1.valueAt)(receiver,env)).node);return value.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&value.callee.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&statementFactories.has((0,spellings_1.propertyName)(value.callee)??"")};const check=(arg,gate)=>{let text=(0,type_syntax_1.unwrapTypeSyntax)(arg);if(text.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression&&sqlTextProperties.size>0){const property=text.properties.find(p=>p.type===ast_node_types_1.AST_NODE_TYPES.Property&&sqlTextProperties.has((0,spellings_1.objectKeyName)(p)??""));if(property===void 0)return;text=property.value}const kind=judge(text,gate);if(kind){report(text,kind);return}if(isRequestText(text))report(text,"concat")};const importedName=callee=>{const variable=(0,value_follow_1.lookup)(callee.name,env.scopeOf(callee));const specifier=variable===null?null:(0,value_follow_1.importSpecifierOf)(variable);return specifier!==null&&config.modules.some(m=>specifier.source===m||specifier.source.startsWith(`${m}/`))?specifier.name:null};const checkAliasCallback=node=>{const callback=node.arguments[0];if(!callback||!(0,driver_origin_1.isFunctionNode)(callback))return;const alias=callback.params[0];for(const result of(0,value_follow_1.returnedValues)(callback)??[]){const text=(0,type_syntax_1.unwrapTypeSyntax)(result);if(text.type!==ast_node_types_1.AST_NODE_TYPES.TemplateLiteral){if(isRequestText(text))report(text,"concat");continue}const unsafe=(0,sql_position_1.sqlHoles)(text).some(hole=>!(hole.expression.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&alias?.type===ast_node_types_1.AST_NODE_TYPES.Identifier&&hole.expression.name===alias.name)&&isUnsafeHole(hole));if(unsafe)report(text,"template")}};return{before(){if((0,rule_creator_2.isTestFilePath)(context.filename))return false;env=(0,value_follow_1.envOf)(context);wrappers=new Map;definitions=new Map;wrapperFns=new Map;pending=[];ownsFile=false;return true},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)},CallExpression(node){const callee=node.callee;const isMember=callee.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression;const calleeName=isMember?(0,spellings_1.propertyName)(callee)??void 0:callee.type===ast_node_types_1.AST_NODE_TYPES.Identifier?callee.name:void 0;if(calleeName===void 0)return;if(!isMember&&ownsFile){const imported=importedName(callee);if(imported!==null&&fragmentFunctions.has(imported)){if(node.arguments[0])check(node.arguments[0],false);return}if(imported!==null&&aliasCallbacks.has(imported)){checkAliasCallback(node);return}}const fragment=isMember?fragmentSinks.get(calleeName):void 0;if(!isMember||!statementSinks.has(calleeName)&&!fragment){for(const[index,arg]of node.arguments.entries()){if(arg.type===ast_node_types_1.AST_NODE_TYPES.SpreadElement)continue;pending.push({name:calleeName,index,arg})}return}const receiver=originOf((0,value_follow_1.valueAt)(callee.object,env));if(receiver==="foreign")return;if(fragment?.requireDriverReceiver&&receiver!=="ours")return;if(!ownsFile&&!(receiver==="ours"&&!PARTITION_SHARED_SINKS.has(calleeName))){return}const queryArg=node.arguments[fragment?.arg??0];if(!queryArg||queryArg.type===ast_node_types_1.AST_NODE_TYPES.SpreadElement)return;if(isStatementObject(callee.object))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 gate=fragment===void 0&&config.requireSqlKeywords&&receiver!=="ours";check(queryArg,gate)},"Program:exit"(){if(!ownsFile)return;const proven=new Map;for(const name of wrapperFns.values())proven.set(name,(proven.get(name)??0)+1);for(const{name,index,arg}of pending){if(wrappers.get(name)!==index)continue;if(definitions.get(name)!==proven.get(name))continue;const kind=judge(arg,true);if(kind)report((0,type_syntax_1.unwrapTypeSyntax)(arg),kind)}}}}})}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.propertyKeyName=propertyKeyName;exports.hasTruthyKey=hasTruthyKey;exports.hasArgumentScope=hasArgumentScope;exports.chainMethodNames=chainMethodNames;exports.driverBindings=driverBindings;exports.receiverBaseName=receiverBaseName;exports.createUnscopedMutationRule=createUnscopedMutationRule;exports.startsWithTable=startsWithTable;exports.insideAny=insideAny;exports.exportedFunctions=exportedFunctions;exports.scopedElsewhere=scopedElsewhere;const rule_creator_1=require("./rule-creator");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 rule_creator_2=require("./rule-creator");const driver_origin_1=require("./driver-origin");const value_follow_1=require("./value-follow");const DEFAULT_SCOPE_KEYS=["where"];const DEFAULT_SCOPE_METHODS=["where"];function propertyKeyName(prop){if(prop.type!==ast_node_types_1.AST_NODE_TYPES.Property)return void 0;if(prop.computed)return void 0;if(prop.key.type===ast_node_types_1.AST_NODE_TYPES.Identifier)return prop.key.name;if(prop.key.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof prop.key.value==="string"){return prop.key.value}return void 0}function hasTruthyKey(obj,keys){return obj.properties.some(prop=>{if(prop.type!==ast_node_types_1.AST_NODE_TYPES.Property)return false;const name=propertyKeyName(prop);if(name===void 0||!keys.includes(name))return false;return!(prop.value.type===ast_node_types_1.AST_NODE_TYPES.Literal&&!prop.value.value)})}function hasArgumentScope(args,scopeKeys){return args.some(arg=>{if(arg.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression){return arg.properties.some(prop=>{if(prop.type===ast_node_types_1.AST_NODE_TYPES.SpreadElement)return true;const name=propertyKeyName(prop);if(name===void 0)return true;if(!scopeKeys.includes(name))return false;if(prop.type===ast_node_types_1.AST_NODE_TYPES.Property&&prop.value.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression&&prop.value.properties.length===0){return false}return true})}return arg.type!==ast_node_types_1.AST_NODE_TYPES.Literal})}function chainMethodNames(node){let root=node;for(;;){const parent=root.parent;if(!parent)break;if(parent.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&parent.object===root){root=parent;continue}if(parent.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&parent.callee===root){root=parent;continue}break}const names=new Set;let cursor=root;for(;;){if(cursor.type===ast_node_types_1.AST_NODE_TYPES.CallExpression){cursor=cursor.callee;continue}if(cursor.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression){if(!cursor.computed&&cursor.property.type===ast_node_types_1.AST_NODE_TYPES.Identifier){names.add(cursor.property.name)}cursor=cursor.object;continue}break}return names}function driverBindings(program,modules){const names=new Set;const isDriver=source=>modules.some(m=>source===m||source.startsWith(`${m}/`));for(const stmt of program.body){if(stmt.type===ast_node_types_1.AST_NODE_TYPES.ImportDeclaration){if(typeof stmt.source.value!=="string"||!isDriver(stmt.source.value))continue;for(const spec of stmt.specifiers)names.add(spec.local.name);continue}if(stmt.type!==ast_node_types_1.AST_NODE_TYPES.VariableDeclaration)continue;for(const decl of stmt.declarations){const init=decl.init;if(!init||init.type!==ast_node_types_1.AST_NODE_TYPES.CallExpression||init.callee.type!==ast_node_types_1.AST_NODE_TYPES.Identifier||init.callee.name!=="require"){continue}const arg=init.arguments[0];if(!arg||arg.type!==ast_node_types_1.AST_NODE_TYPES.Literal||typeof arg.value!=="string"||!isDriver(arg.value)){continue}if(decl.id.type===ast_node_types_1.AST_NODE_TYPES.Identifier)names.add(decl.id.name);if(decl.id.type===ast_node_types_1.AST_NODE_TYPES.ObjectPattern){for(const prop of decl.id.properties){if(prop.type===ast_node_types_1.AST_NODE_TYPES.Property&&prop.value.type===ast_node_types_1.AST_NODE_TYPES.Identifier){names.add(prop.value.name)}}}}}return names}function receiverBaseName(node){let cursor=node.object;for(;;){if(cursor.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression){if(cursor.object.type===ast_node_types_1.AST_NODE_TYPES.ThisExpression&&!cursor.computed&&cursor.property.type===ast_node_types_1.AST_NODE_TYPES.Identifier){return cursor.property.name}cursor=cursor.object;continue}if(cursor.type===ast_node_types_1.AST_NODE_TYPES.CallExpression){cursor=cursor.callee;continue}return cursor.type===ast_node_types_1.AST_NODE_TYPES.Identifier?cursor.name:void 0}}function createUnscopedMutationRule(config){const sinks=new Set(config.methods);const scopeKeys=config.scopeKeys??DEFAULT_SCOPE_KEYS;const scopeMethods=config.scopeMethods??DEFAULT_SCOPE_METHODS;const truncateKeys=config.truncateKeys??[];const argumentRole=config.argumentRole??"options";const usesModule=(0,module_evidence_1.createModuleEvidence)({packages:config.modules});const originOf=(0,driver_origin_1.createOriginClassifier)(config.modules);return rule_creator_1.RuleCreator.withoutDocs({meta:{interop:{runtimes:["eslint","oxlint"]},type:config.meta.type,docs:{...config.meta.docs},messages:{unscopedMutation:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Unscoped Bulk Mutation",description:"Bulk mutation with no filter detected. This rewrites or deletes every row in the table.",severity:"HIGH",cwe:config.meta.docs.cwe,owasp:"A01:2021",compliance:["SOC2","NIST-CSF"],effort:"low",fix:config.fix,documentationLink:config.documentationLink}),explicitTruncate:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Unscoped Bulk Mutation",description:"Table truncation detected. This ignores any filter and empties the whole table.",severity:"HIGH",cwe:config.meta.docs.cwe,owasp:"A01:2021",compliance:["SOC2","NIST-CSF"],effort:"low",fix:"Remove the truncate flag and delete by filter, or move the truncation to an explicit maintenance script.",documentationLink:config.documentationLink})},schema:[]},defaultOptions:[],createOnce(context){let env;let ownsFile;let migrations;function hasDistinctiveShape(node){const callee=node.callee;switch(config.distinctiveShape){case"model-accessor":return callee.object.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression;case"table-call":return startsWithTable(callee.object);case"table-argument":{const[table]=node.arguments;return table!==void 0&&table.type!==ast_node_types_1.AST_NODE_TYPES.SpreadElement&&originOf((0,value_follow_1.valueAt)(table,env))==="ours"}default:return false}}return{before(){if((0,rule_creator_2.isTestFilePath)(context.filename))return false;env=(0,value_follow_1.envOf)(context);ownsFile=false;migrations=new Set;return true},Program(program){ownsFile=usesModule(program);migrations=exportedFunctions(program,config.migrationExports??[],context)},CallExpression(node){if(node.callee.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression||node.callee.computed||node.callee.property.type!==ast_node_types_1.AST_NODE_TYPES.Identifier||!sinks.has(node.callee.property.name)){return}const origin=originOf((0,value_follow_1.valueAt)(node.callee.object,env));if(origin==="foreign")return;if(origin==="unknown"&&!(ownsFile&&hasDistinctiveShape(node))){return}if(insideAny(node,migrations))return;if(truncateKeys.length>0){const truncating=node.arguments.some(arg=>arg.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression&&hasTruthyKey(arg,truncateKeys));if(truncating){context.report({node,messageId:"explicitTruncate"});return}}if(argumentRole==="options"){if(config.requireOptionsObject===true&&!node.arguments.some(arg=>arg.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression)){return}if(hasArgumentScope(node.arguments,scopeKeys))return}const chained=chainMethodNames(node);if(scopeMethods.some(name=>chained.has(name)))return;if(scopedElsewhere(node,scopeMethods,env))return;context.report({node,messageId:"unscopedMutation"})}}}})}const TABLE_STARTERS=new Set(["table","from","into","queryBuilder","withSchema"]);function startsWithTable(receiver){let cursor=receiver;for(;;){if(cursor.type===ast_node_types_1.AST_NODE_TYPES.CallExpression){if(cursor.callee.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression)return true;if(TABLE_STARTERS.has((0,spellings_1.propertyName)(cursor.callee)??""))return true;cursor=cursor.callee.object;continue}if(cursor.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression){cursor=cursor.object;continue}return false}}function insideAny(node,functions){for(let n=node.parent;n;n=n.parent){if(functions.has(n))return true}return false}function exportedFunctions(program,names,context){const found=new Set;const scopeManager=context.sourceCode.scopeManager;if(names.length===0||scopeManager===null)return found;const module={file:context.physicalFilename,ast:program,scopeManager};for(const name of names){const exported=(0,value_follow_1.exportedValue)(module,name,context.languageOptions?.parser);if(exported===null)continue;const target=(0,value_follow_1.follow)(exported);if((0,driver_origin_1.isFunctionNode)(target.node))found.add(target.node)}return found}function scopedElsewhere(node,scopeMethods,env){let root=node.callee;while(root.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression||root.type===ast_node_types_1.AST_NODE_TYPES.CallExpression){root=root.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression?root.object:root.callee}if(root.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)return false;const variable=(0,value_follow_1.lookup)(root.name,env.scopeOf(root));if(variable===null||variable.defs[0]?.type!=="Variable")return false;return variable.references.some(reference=>{const use=reference.identifier.parent;if(use?.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&use.object===reference.identifier){return scopeMethods.includes((0,spellings_1.propertyName)(use)??"")}return(use?.type===ast_node_types_1.AST_NODE_TYPES.CallExpression||use?.type===ast_node_types_1.AST_NODE_TYPES.NewExpression)&&use.arguments.some(argument=>argument===reference.identifier)})}
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.propertyKeyName=propertyKeyName;exports.hasTruthyKey=hasTruthyKey;exports.hasArgumentScope=hasArgumentScope;exports.chainMethodNames=chainMethodNames;exports.driverBindings=driverBindings;exports.receiverBaseName=receiverBaseName;exports.createUnscopedMutationRule=createUnscopedMutationRule;exports.startsWithTable=startsWithTable;exports.insideAny=insideAny;exports.exportedFunctions=exportedFunctions;exports.scopedElsewhere=scopedElsewhere;const rule_creator_1=require("./rule-creator");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 rule_creator_2=require("./rule-creator");const driver_origin_1=require("./driver-origin");const value_follow_1=require("./value-follow");const DEFAULT_SCOPE_KEYS=["where"];const DEFAULT_SCOPE_METHODS=["where"];function propertyKeyName(prop){if(prop.type!==ast_node_types_1.AST_NODE_TYPES.Property)return void 0;if(prop.computed)return void 0;if(prop.key.type===ast_node_types_1.AST_NODE_TYPES.Identifier)return prop.key.name;if(prop.key.type===ast_node_types_1.AST_NODE_TYPES.Literal&&typeof prop.key.value==="string"){return prop.key.value}return void 0}function hasTruthyKey(obj,keys){return obj.properties.some(prop=>{if(prop.type!==ast_node_types_1.AST_NODE_TYPES.Property)return false;const name=propertyKeyName(prop);if(name===void 0||!keys.includes(name))return false;return!(prop.value.type===ast_node_types_1.AST_NODE_TYPES.Literal&&!prop.value.value)})}function hasArgumentScope(args,scopeKeys){return args.some(arg=>{if(arg.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression){return arg.properties.some(prop=>{if(prop.type===ast_node_types_1.AST_NODE_TYPES.SpreadElement)return true;const name=propertyKeyName(prop);if(name===void 0)return true;if(!scopeKeys.includes(name))return false;if(prop.type===ast_node_types_1.AST_NODE_TYPES.Property&&prop.value.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression&&prop.value.properties.length===0){return false}return true})}return arg.type!==ast_node_types_1.AST_NODE_TYPES.Literal})}function chainMethodNames(node){let root=node;for(;;){const parent=root.parent;if(!parent)break;if(parent.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&parent.object===root){root=parent;continue}if(parent.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&parent.callee===root){root=parent;continue}break}const names=new Set;let cursor=root;for(;;){if(cursor.type===ast_node_types_1.AST_NODE_TYPES.CallExpression){cursor=cursor.callee;continue}if(cursor.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression){const name=(0,spellings_1.propertyName)(cursor);if(name!==null)names.add(name);cursor=cursor.object;continue}break}return names}function driverBindings(program,modules){const names=new Set;const isDriver=source=>modules.some(m=>source===m||source.startsWith(`${m}/`));for(const stmt of program.body){if(stmt.type===ast_node_types_1.AST_NODE_TYPES.ImportDeclaration){if(typeof stmt.source.value!=="string"||!isDriver(stmt.source.value))continue;for(const spec of stmt.specifiers)names.add(spec.local.name);continue}if(stmt.type!==ast_node_types_1.AST_NODE_TYPES.VariableDeclaration)continue;for(const decl of stmt.declarations){const init=decl.init;if(!init||init.type!==ast_node_types_1.AST_NODE_TYPES.CallExpression||init.callee.type!==ast_node_types_1.AST_NODE_TYPES.Identifier||init.callee.name!=="require"){continue}const arg=init.arguments[0];if(!arg||arg.type!==ast_node_types_1.AST_NODE_TYPES.Literal||typeof arg.value!=="string"||!isDriver(arg.value)){continue}if(decl.id.type===ast_node_types_1.AST_NODE_TYPES.Identifier)names.add(decl.id.name);if(decl.id.type===ast_node_types_1.AST_NODE_TYPES.ObjectPattern){for(const prop of decl.id.properties){if(prop.type===ast_node_types_1.AST_NODE_TYPES.Property&&prop.value.type===ast_node_types_1.AST_NODE_TYPES.Identifier){names.add(prop.value.name)}}}}}return names}function receiverBaseName(node){let cursor=node.object;for(;;){if(cursor.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression){if(cursor.object.type===ast_node_types_1.AST_NODE_TYPES.ThisExpression&&!cursor.computed&&cursor.property.type===ast_node_types_1.AST_NODE_TYPES.Identifier){return cursor.property.name}cursor=cursor.object;continue}if(cursor.type===ast_node_types_1.AST_NODE_TYPES.CallExpression){cursor=cursor.callee;continue}return cursor.type===ast_node_types_1.AST_NODE_TYPES.Identifier?cursor.name:void 0}}function createUnscopedMutationRule(config){const sinks=new Set(config.methods);const scopeKeys=config.scopeKeys??DEFAULT_SCOPE_KEYS;const scopeMethods=config.scopeMethods??DEFAULT_SCOPE_METHODS;const truncateKeys=config.truncateKeys??[];const argumentRole=config.argumentRole??"options";const usesModule=(0,module_evidence_1.createModuleEvidence)({packages:config.modules});const originOf=(0,driver_origin_1.createOriginClassifier)(config.modules);return rule_creator_1.RuleCreator.withoutDocs({meta:{interop:{runtimes:["eslint","oxlint"]},type:config.meta.type,docs:{...config.meta.docs},messages:{unscopedMutation:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Unscoped Bulk Mutation",description:"Bulk mutation with no filter detected. This rewrites or deletes every row in the table.",severity:"HIGH",cwe:config.meta.docs.cwe,owasp:"A01:2021",compliance:["SOC2","NIST-CSF"],effort:"low",fix:config.fix,documentationLink:config.documentationLink}),explicitTruncate:(0,messaging_1.formatLLMMessage)({icon:messaging_1.MessageIcons.SECURITY,issueName:"Unscoped Bulk Mutation",description:"Table truncation detected. This ignores any filter and empties the whole table.",severity:"HIGH",cwe:config.meta.docs.cwe,owasp:"A01:2021",compliance:["SOC2","NIST-CSF"],effort:"low",fix:"Remove the truncate flag and delete by filter, or move the truncation to an explicit maintenance script.",documentationLink:config.documentationLink})},schema:[]},defaultOptions:[],createOnce(context){let env;let ownsFile;let migrations;function hasDistinctiveShape(node){const callee=node.callee;switch(config.distinctiveShape){case"model-accessor":return callee.object.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression;case"table-call":return startsWithTable(callee.object);case"table-argument":{const[table]=node.arguments;return table!==void 0&&table.type!==ast_node_types_1.AST_NODE_TYPES.SpreadElement&&originOf((0,value_follow_1.valueAt)(table,env))==="ours"}default:return false}}return{before(){if((0,rule_creator_2.isTestFilePath)(context.filename))return false;env=(0,value_follow_1.envOf)(context);ownsFile=false;migrations=new Set;return true},Program(program){ownsFile=usesModule(program);migrations=exportedFunctions(program,config.migrationExports??[],context)},CallExpression(node){if(node.callee.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression||!sinks.has((0,spellings_1.propertyName)(node.callee)??"")){return}const origin=originOf((0,value_follow_1.valueAt)(node.callee.object,env));if(origin==="foreign")return;if(origin==="unknown"&&!(ownsFile&&hasDistinctiveShape(node))){return}if(insideAny(node,migrations))return;if(truncateKeys.length>0){const truncating=node.arguments.some(arg=>arg.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression&&hasTruthyKey(arg,truncateKeys));if(truncating){context.report({node,messageId:"explicitTruncate"});return}}if(argumentRole==="options"){if(config.requireOptionsObject===true&&!node.arguments.some(arg=>arg.type===ast_node_types_1.AST_NODE_TYPES.ObjectExpression)){return}if(hasArgumentScope(node.arguments,scopeKeys))return}const chained=chainMethodNames(node);if(scopeMethods.some(name=>chained.has(name)))return;if(scopedElsewhere(node,scopeMethods,env))return;context.report({node,messageId:"unscopedMutation"})}}}})}const TABLE_STARTERS=new Set(["table","from","into","queryBuilder","withSchema"]);function startsWithTable(receiver){let cursor=receiver;for(;;){if(cursor.type===ast_node_types_1.AST_NODE_TYPES.CallExpression){if(cursor.callee.type!==ast_node_types_1.AST_NODE_TYPES.MemberExpression)return true;if(TABLE_STARTERS.has((0,spellings_1.propertyName)(cursor.callee)??""))return true;cursor=cursor.callee.object;continue}if(cursor.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression){cursor=cursor.object;continue}return false}}function insideAny(node,functions){for(let n=node.parent;n;n=n.parent){if(functions.has(n))return true}return false}function exportedFunctions(program,names,context){const found=new Set;const scopeManager=context.sourceCode.scopeManager;if(names.length===0||scopeManager===null)return found;const module={file:context.physicalFilename,ast:program,scopeManager};for(const name of names){const exported=(0,value_follow_1.exportedValue)(module,name,context.languageOptions?.parser);if(exported===null)continue;const target=(0,value_follow_1.follow)(exported);if((0,driver_origin_1.isFunctionNode)(target.node))found.add(target.node)}return found}function heldThenScoped(node,scopeMethods,env){let top=node;while(top.parent?.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&top.parent.object===top||top.parent?.type===ast_node_types_1.AST_NODE_TYPES.CallExpression&&top.parent.callee===top){top=top.parent}const declarator=top.parent;if(declarator?.type!==ast_node_types_1.AST_NODE_TYPES.VariableDeclarator||declarator.id.type!==ast_node_types_1.AST_NODE_TYPES.Identifier){return false}const variable=(0,value_follow_1.lookup)(declarator.id.name,env.scopeOf(declarator.id));return variable.references.some(reference=>{const use=reference.identifier.parent;if(use.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression)return scopeMethods.includes((0,spellings_1.propertyName)(use));return(use.type===ast_node_types_1.AST_NODE_TYPES.CallExpression||use.type===ast_node_types_1.AST_NODE_TYPES.NewExpression)&&use.arguments.some(argument=>argument===reference.identifier)})}function scopedElsewhere(node,scopeMethods,env){let root=node.callee;while(root.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression||root.type===ast_node_types_1.AST_NODE_TYPES.CallExpression){root=root.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression?root.object:root.callee}if(heldThenScoped(node,scopeMethods,env))return true;if(root.type!==ast_node_types_1.AST_NODE_TYPES.Identifier)return false;const variable=(0,value_follow_1.lookup)(root.name,env.scopeOf(root));if(variable===null||variable.defs[0]?.type!=="Variable")return false;return variable.references.some(reference=>{const use=reference.identifier.parent;if(use?.type===ast_node_types_1.AST_NODE_TYPES.MemberExpression&&use.object===reference.identifier){return scopeMethods.includes((0,spellings_1.propertyName)(use)??"")}return(use?.type===ast_node_types_1.AST_NODE_TYPES.CallExpression||use?.type===ast_node_types_1.AST_NODE_TYPES.NewExpression)&&use.arguments.some(argument=>argument===reference.identifier)})}
|