eslint-plugin-postgresql-security 2.5.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-postgresql-security",
3
- "version": "2.5.0",
3
+ "version": "2.6.0",
4
4
  "description": "ESLint plugin for the pg PostgreSQL driver — detects SQL injection, unreleased clients, floating queries, unsafe search_path, and insecure SSL.",
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.js",
@@ -76,7 +76,7 @@
76
76
  }
77
77
  },
78
78
  "dependencies": {
79
- "@interlace/eslint-devkit": "^1.19.2"
79
+ "@interlace/eslint-devkit": "^1.20.0"
80
80
  },
81
81
  "funding": {
82
82
  "type": "github",
package/src/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.configs=exports.plugin=exports.rules=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");exports.rules={get"no-unsafe-query"(){return require("./rules/no-unsafe-query").noUnsafeQuery},get"no-insecure-ssl"(){return require("./rules/no-insecure-ssl").noInsecureSsl},get"no-hardcoded-credentials"(){return require("./rules/no-hardcoded-credentials").noHardcodedCredentials},get"check-query-params"(){return require("./rules/check-query-params").checkQueryParams},get"no-missing-client-release"(){return require("./rules/no-missing-client-release").noMissingClientRelease},get"no-transaction-on-pool"(){return require("./rules/no-transaction-on-pool").noTransactionOnPool},get"no-floating-query"(){return require("./rules/no-floating-query").noFloatingQuery},get"no-select-all"(){return require("./rules/no-select-all").noSelectAll},get"prefer-pool-query"(){return require("./rules/prefer-pool-query").preferPoolQuery},get"no-batch-insert-loop"(){return require("./rules/no-batch-insert-loop").noBatchInsertLoop},get"no-unsafe-search-path"(){return require("./rules/no-unsafe-search-path").noUnsafeSearchPath},get"no-unsafe-copy-from"(){return require("./rules/no-unsafe-copy-from").noUnsafeCopyFrom},get"prevent-double-release"(){return require("./rules/prevent-double-release").preventDoubleRelease}};(0,eslint_devkit_1.withCanonicalDocsUrls)("plugin-postgresql-security",exports.rules);exports.plugin={meta:{name:"eslint-plugin-postgresql-security",version:"2.5.0"},rules:exports.rules};exports.configs={flagship:{plugins:{"postgresql-security":exports.plugin,"pg":exports.plugin},rules:{"postgresql-security/no-unsafe-query":"error"}},recommended:{plugins:{"postgresql-security":exports.plugin,"pg":exports.plugin},rules:{"postgresql-security/no-unsafe-query":"error","postgresql-security/no-insecure-ssl":"error","postgresql-security/no-hardcoded-credentials":"error","postgresql-security/no-unsafe-search-path":"error","postgresql-security/no-unsafe-copy-from":"error","postgresql-security/no-transaction-on-pool":"error","postgresql-security/no-missing-client-release":"error","postgresql-security/prevent-double-release":"warn","postgresql-security/no-floating-query":"warn","postgresql-security/check-query-params":"warn","postgresql-security/no-select-all":"warn","postgresql-security/prefer-pool-query":"warn","postgresql-security/no-batch-insert-loop":"warn"}},strict:{plugins:{"postgresql-security":exports.plugin,"pg":exports.plugin},rules:{"postgresql-security/no-unsafe-query":"error","postgresql-security/no-insecure-ssl":"error","postgresql-security/no-hardcoded-credentials":"error","postgresql-security/no-unsafe-search-path":"error","postgresql-security/no-unsafe-copy-from":"error","postgresql-security/no-transaction-on-pool":"error","postgresql-security/no-missing-client-release":"error","postgresql-security/prevent-double-release":"error","postgresql-security/no-floating-query":"error","postgresql-security/check-query-params":"error","postgresql-security/no-select-all":"error","postgresql-security/prefer-pool-query":"error","postgresql-security/no-batch-insert-loop":"error"}}};exports.default=exports.plugin;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.configs=exports.plugin=exports.rules=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");exports.rules={get"no-unsafe-query"(){return require("./rules/no-unsafe-query").noUnsafeQuery},get"no-insecure-ssl"(){return require("./rules/no-insecure-ssl").noInsecureSsl},get"no-hardcoded-credentials"(){return require("./rules/no-hardcoded-credentials").noHardcodedCredentials},get"check-query-params"(){return require("./rules/check-query-params").checkQueryParams},get"no-missing-client-release"(){return require("./rules/no-missing-client-release").noMissingClientRelease},get"no-transaction-on-pool"(){return require("./rules/no-transaction-on-pool").noTransactionOnPool},get"no-floating-query"(){return require("./rules/no-floating-query").noFloatingQuery},get"no-select-all"(){return require("./rules/no-select-all").noSelectAll},get"prefer-pool-query"(){return require("./rules/prefer-pool-query").preferPoolQuery},get"no-batch-insert-loop"(){return require("./rules/no-batch-insert-loop").noBatchInsertLoop},get"no-unsafe-search-path"(){return require("./rules/no-unsafe-search-path").noUnsafeSearchPath},get"no-unsafe-copy-from"(){return require("./rules/no-unsafe-copy-from").noUnsafeCopyFrom},get"prevent-double-release"(){return require("./rules/prevent-double-release").preventDoubleRelease}};(0,eslint_devkit_1.withCanonicalDocsUrls)("plugin-postgresql-security",exports.rules);exports.plugin={meta:{name:"eslint-plugin-postgresql-security",version:"2.6.0"},rules:exports.rules};exports.configs={flagship:{plugins:{"postgresql-security":exports.plugin,"pg":exports.plugin},rules:{"postgresql-security/no-unsafe-query":"error"}},recommended:{plugins:{"postgresql-security":exports.plugin,"pg":exports.plugin},rules:{"postgresql-security/no-unsafe-query":"error","postgresql-security/no-insecure-ssl":"error","postgresql-security/no-hardcoded-credentials":"error","postgresql-security/no-unsafe-search-path":"error","postgresql-security/no-unsafe-copy-from":"error","postgresql-security/no-transaction-on-pool":"error","postgresql-security/no-missing-client-release":"error","postgresql-security/prevent-double-release":"warn","postgresql-security/no-floating-query":"warn","postgresql-security/check-query-params":"warn","postgresql-security/no-select-all":"warn","postgresql-security/prefer-pool-query":"warn","postgresql-security/no-batch-insert-loop":"warn"}},strict:{plugins:{"postgresql-security":exports.plugin,"pg":exports.plugin},rules:{"postgresql-security/no-unsafe-query":"error","postgresql-security/no-insecure-ssl":"error","postgresql-security/no-hardcoded-credentials":"error","postgresql-security/no-unsafe-search-path":"error","postgresql-security/no-unsafe-copy-from":"error","postgresql-security/no-transaction-on-pool":"error","postgresql-security/no-missing-client-release":"error","postgresql-security/prevent-double-release":"error","postgresql-security/no-floating-query":"error","postgresql-security/check-query-params":"error","postgresql-security/no-select-all":"error","postgresql-security/prefer-pool-query":"error","postgresql-security/no-batch-insert-loop":"error"}}};exports.default=exports.plugin;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noUnsafeQuery=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const utils_1=require("../../utils");const cross_file_1=require("../../utils/cross-file");const SQL_SINK_METHODS=new Set(["query","execute","none","one","oneOrNone","many","manyOrNone","any","result","multi","multiResult","unsafe"]);const SQL_STATEMENTS=[/^\s*select\b[\s\S]*\bfrom\b/i,/^\s*insert\s+into\b/i,/^\s*update\b[\s\S]*\bset\b/i,/^\s*delete\s+from\b/i,/^\s*replace\s+into\b/i,/^\s*merge\s+into\b/i,/^\s*with\b[\s\S]*\bas\s*\(/i,/^\s*copy\b[\s\S]*\b(?:from|to)\b/i,/^\s*grant\b[\s\S]*\bon\b/i,/^\s*(create|drop|alter|truncate)\s+(table|index|view|schema|database|sequence|materialized|type|function|trigger|role|user|extension)\b/i,/^\s*select\s/i,/^\s*call\s+[\w".]+\s*\(/i,/^\s*(?:un)?listen\s+[\w"*]*\s*(?:;|$)/i,/^\s*notify\s+[\w"]*\s*(?:,|;|$)/i];const PG_ESCAPER_METHODS=new Set(["escapeIdentifier","escapeLiteral"]);const NUMBER_CONVERSIONS=new Set(["Number","parseInt","parseFloat"]);const NUMBER_NAMESPACES=new Set(["Math","Number"]);const MAPPING_METHODS=new Set(["map","flatMap","from"]);function staticText(node){if(node.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral){return node.quasis.map(q=>q.value.raw).join("")}if(node.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"){return`${staticText(node.left)}${staticText(node.right)}`}return(0,eslint_devkit_1.staticString)(node)??""}function trailingText(node){if(node.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral){return node.quasis.at(-1).value.raw}if(node.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"){return trailingText(node.right)}return(0,eslint_devkit_1.staticString)(node)??""}function looksLikeSqlStatement(text){return SQL_STATEMENTS.some(pattern=>pattern.test(text))}function resolveVariable(name,scope){for(let current=scope;current!==null;current=current.upper){const variable=current.set.get(name);if(variable!==void 0)return variable}return null}function isGlobal(name,scope){const variable=resolveVariable(name,scope);return variable===null||variable.defs.length===0}function singleInit(variable){if(variable===null)return null;if(variable.references.filter(ref=>ref.isWrite()).length!==1)return null;const def=variable.defs.find(d=>d.type==="Variable");if(def===void 0)return null;return def.node.init??null}function endsWithPlaceholderPrefix(text){return text.endsWith("$")&&!text.endsWith("$$")}function isIndexShaped(node,scope){return isNumeric((0,eslint_devkit_1.unwrapTypeSyntax)(node),scope,new Set)}const INDEX_PARAMETER=new Map([["map",1],["flatMap",1],["forEach",1],["filter",1],["some",1],["every",1],["find",1],["findIndex",1],["findLast",1],["findLastIndex",1],["from",1],["reduce",2],["reduceRight",2]]);function isNumericParameter(name){if(name.typeAnnotation?.typeAnnotation.type===eslint_devkit_1.AST_NODE_TYPES.TSNumberKeyword)return true;const fn=name.parent;const call=fn?.parent;if(fn?.type!==eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression&&fn?.type!==eslint_devkit_1.AST_NODE_TYPES.FunctionExpression||call?.type!==eslint_devkit_1.AST_NODE_TYPES.CallExpression||call.callee.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression){return false}const method=(0,eslint_devkit_1.propertyName)(call.callee);const index=method===null?void 0:INDEX_PARAMETER.get(method);return index!==void 0&&fn.params[index]===name}function enclosingParameter(node){for(let current=node.parent;current;current=current.parent){if(current.type===eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression||current.type===eslint_devkit_1.AST_NODE_TYPES.FunctionExpression||current.type===eslint_devkit_1.AST_NODE_TYPES.FunctionDeclaration){const param=current.params.find(p=>p.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&p.name===node.name);if(param!==void 0)return param}}return null}const NUMERIC_OPERATORS=new Set(["-","*","%","**","|","&","^","<<",">>",">>>"]);function isNumeric(node,scope,seen){switch(node.type){case eslint_devkit_1.AST_NODE_TYPES.Literal:return typeof node.value==="number";case eslint_devkit_1.AST_NODE_TYPES.UpdateExpression:return true;case eslint_devkit_1.AST_NODE_TYPES.UnaryExpression:return node.operator==="+"||node.operator==="-"||node.operator==="~";case eslint_devkit_1.AST_NODE_TYPES.MemberExpression:return(0,eslint_devkit_1.propertyName)(node)==="length";case eslint_devkit_1.AST_NODE_TYPES.CallExpression:return node.callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&(0,eslint_devkit_1.propertyName)(node.callee)==="push"||isNumberConversion(node,scope);case eslint_devkit_1.AST_NODE_TYPES.BinaryExpression:if(node.operator==="+"){return isNumeric(node.left,scope,seen)&&isNumeric(node.right,scope,seen)}return NUMERIC_OPERATORS.has(node.operator);case eslint_devkit_1.AST_NODE_TYPES.Identifier:return isNumericBinding(node,scope,seen);default:return false}}function isNumberConversion(call,scope){const{callee}=call;if(callee.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){return NUMBER_CONVERSIONS.has(callee.name)&&isGlobal(callee.name,scope)}return callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&callee.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&NUMBER_NAMESPACES.has(callee.object.name)&&isGlobal(callee.object.name,scope)}function isNumericBinding(node,scope,seen){const param=enclosingParameter(node);if(param!==null)return isNumericParameter(param);const variable=resolveVariable(node.name,scope);if(variable===null||variable.defs.length===0)return false;if(seen.has(variable))return true;seen.add(variable);const[def]=variable.defs;if(def.type==="Parameter")return isNumericParameter(def.name);if(def.type!=="Variable")return false;const declarator=def.node;if(declarator.id.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&declarator.id.typeAnnotation?.typeAnnotation.type===eslint_devkit_1.AST_NODE_TYPES.TSNumberKeyword){return true}const loop=declarator.parent.parent;if(loop?.type===eslint_devkit_1.AST_NODE_TYPES.ForInStatement||loop?.type===eslint_devkit_1.AST_NODE_TYPES.ForOfStatement){return false}const writes=variable.references.filter(reference=>reference.isWrite());return writes.length>0&&writes.every(write=>{const parent=write.identifier.parent;if(parent?.type===eslint_devkit_1.AST_NODE_TYPES.UpdateExpression)return true;if(parent?.type===eslint_devkit_1.AST_NODE_TYPES.AssignmentExpression&&parent.operator!=="="){return parent.operator!=="+="||isNumeric(parent.right,write.from,seen)}return write.writeExpr!=null&&isNumeric((0,eslint_devkit_1.unwrapTypeSyntax)(write.writeExpr),write.from,seen)})}function isPlaceholderIndex(textBefore,expression,scope){return endsWithPlaceholderPrefix(textBefore)&&isIndexShaped(expression,scope)}function isPlaceholderOnly(node,scope){if((0,eslint_devkit_1.staticString)(node)!==null)return true;if(node.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral){return node.expressions.every((expression,i)=>isPlaceholderIndex(node.quasis[i].value.raw,expression,scope))}if(node.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"){const left=node.left;return isPlaceholderOnly(left,scope)&&(isPlaceholderOnly(node.right,scope)||isPlaceholderIndex(trailingText(left),node.right,scope))}return false}function callbackResult(fn){if(fn.type!==eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression&&fn.type!==eslint_devkit_1.AST_NODE_TYPES.FunctionExpression){return null}if(fn.body.type!==eslint_devkit_1.AST_NODE_TYPES.BlockStatement)return fn.body;const last=fn.body.body.at(-1);return last?.type===eslint_devkit_1.AST_NODE_TYPES.ReturnStatement?last.argument:null}function isPlaceholderList(node,scope){if(node.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const variable=resolveVariable(node.name,scope);const init=(0,eslint_devkit_1.unwrapTypeSyntax)(singleInit(variable));if(init?.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression)return isPlaceholderList(init,scope);return variable!==null&&init?.type===eslint_devkit_1.AST_NODE_TYPES.ArrayExpression&&isFixedTextList(variable,init)}if(node.type!==eslint_devkit_1.AST_NODE_TYPES.CallExpression||node.callee.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression){return false}const method=(0,eslint_devkit_1.propertyName)(node.callee);if(method===null||!MAPPING_METHODS.has(method))return false;const mapper=node.arguments.at(-1);const result=mapper===void 0?null:callbackResult(mapper);return result!==null&&isPlaceholderOnly(result,scope)}function isFixedText(node,scope){if((0,eslint_devkit_1.staticString)(node)!==null)return true;return(node.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral||node.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression)&&!hasRawPart(node,scope,null)}function isFixedTextList(variable,init){if(!init.elements.every(element=>element!==null&&element.type!==eslint_devkit_1.AST_NODE_TYPES.SpreadElement&&isFixedText(element,variable.scope))){return false}return variable.references.every(reference=>{if(reference.isWrite())return true;const member=reference.identifier.parent;if(member?.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression||member.object!==reference.identifier){return false}const method=(0,eslint_devkit_1.propertyName)(member);if(method==="join"||method==="length")return true;const call=member.parent;return method==="push"&&call?.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression&&call.arguments.every(argument=>argument.type!==eslint_devkit_1.AST_NODE_TYPES.SpreadElement&&isFixedText(argument,reference.from))})}function isFixedLoopElement(variable,scope){const def=variable.defs[0];if(def?.type!=="Variable")return false;const declaration=def.node.parent;const loop=declaration.parent;if(loop?.type!==eslint_devkit_1.AST_NODE_TYPES.ForOfStatement||loop.left!==declaration)return false;const right=(0,eslint_devkit_1.unwrapTypeSyntax)(loop.right);const list=right.type===eslint_devkit_1.AST_NODE_TYPES.Identifier?(0,eslint_devkit_1.unwrapTypeSyntax)(singleInit(resolveVariable(right.name,scope))):right;return list?.type===eslint_devkit_1.AST_NODE_TYPES.ArrayExpression&&list.elements.every(element=>element!==null&&element.type!==eslint_devkit_1.AST_NODE_TYPES.SpreadElement&&(0,eslint_devkit_1.isStaticExpression)({node:element,scope}))}function isSafeCall(call,scope){const{callee}=call;if(callee.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){if(NUMBER_CONVERSIONS.has(callee.name)&&isGlobal(callee.name,scope))return true}else if(callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression){const method=(0,eslint_devkit_1.propertyName)(callee);if(method!==null&&PG_ESCAPER_METHODS.has(method))return true;if(callee.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&NUMBER_NAMESPACES.has(callee.object.name)&&isGlobal(callee.object.name,scope)){return true}if(callee.object.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&(0,eslint_devkit_1.propertyName)(callee.object)==="as"){return true}if(method==="join"&&isPlaceholderList(callee.object,scope))return true}const binding=(0,eslint_devkit_1.resolveModuleBinding)(callee,scope);if(binding!==void 0){if(binding.module==="pg-format")return true;const exported=binding.path.at(-1);if(exported!==void 0&&PG_ESCAPER_METHODS.has(exported))return true}return(0,eslint_devkit_1.isStaticExpression)({node:call,scope})}function isConstantMember(node,scope){const key=(0,eslint_devkit_1.propertyName)(node);if(key===null||node.object.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return false;const variable=resolveVariable(node.object.name,scope);if(variable===null)return false;const enumDef=variable.defs.find(d=>d.type==="TSEnumName");if(enumDef!==void 0){const declaration=enumDef.node;const member=declaration.body.members.find(m=>m.id.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&m.id.name===key);return member!==void 0&&(member.initializer===void 0||member.initializer.type===eslint_devkit_1.AST_NODE_TYPES.Literal)}const init=(0,eslint_devkit_1.unwrapTypeSyntax)(singleInit(variable));if(init===null||init.type!==eslint_devkit_1.AST_NODE_TYPES.ObjectExpression)return false;const property=init.properties.find(p=>p.type===eslint_devkit_1.AST_NODE_TYPES.Property&&(0,eslint_devkit_1.objectKeyName)(p)===key);return property!==void 0&&(0,eslint_devkit_1.isStaticExpression)({node:property.value,scope})}function isRawValue(value,scope,self){const part=(0,eslint_devkit_1.unwrapTypeSyntax)(value);if(part.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression||part.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral){return hasRawPart(part,scope,self)}if((0,eslint_devkit_1.isStaticExpression)({node:part,scope}))return false;if(part.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression)return!isSafeCall(part,scope);if(part.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression)return!isConstantMember(part,scope);if(part.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const variable=resolveVariable(part.name,scope);if(variable!==null&&variable===self)return false;if(variable!==null&&isFixedLoopElement(variable,scope))return false;const init=(0,eslint_devkit_1.unwrapTypeSyntax)(singleInit(variable));return init===null||init.type!==eslint_devkit_1.AST_NODE_TYPES.CallExpression||!isSafeCall(init,scope)}return true}function hasRawPart(node,scope,self){if(node.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral){return node.expressions.some((expression,i)=>!isPlaceholderIndex(node.quasis[i].value.raw,expression,scope)&&isRawValue(expression,scope,self))}const left=node.left;if(isPlaceholderIndex(trailingText(left),node.right,scope)){return isRawValue(left,scope,self)}return isRawValue(left,scope,self)||isRawValue(node.right,scope,self)}function effectiveExpression(node,scope){if(node.type===eslint_devkit_1.AST_NODE_TYPES.ObjectExpression){const text=node.properties.find(prop=>prop.type===eslint_devkit_1.AST_NODE_TYPES.Property&&(prop.key.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&!prop.computed&&prop.key.name==="text"||prop.key.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&prop.key.value==="text"));return text===void 0?node:effectiveExpression(text.value,scope)}if(node.type===eslint_devkit_1.AST_NODE_TYPES.NewExpression){const[first]=node.arguments;return first===void 0?node:effectiveExpression(first,scope)}if(node.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const init=singleInit(resolveVariable(node.name,scope));return init!==null&&init.type===eslint_devkit_1.AST_NODE_TYPES.ObjectExpression?effectiveExpression(init,scope):node}if(node.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression){if(node.callee.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return node;const fn=resolveVariable(node.callee.name,scope);const impl=fn===null?null:functionImplementation(fn);if(impl===null)return node;const returned=returnedExpression(impl.body);return returned===null?node:returned}return node}function functionImplementation(variable){const def=variable.defs.find(d=>d.type==="FunctionName"||d.type==="Variable");if(def===void 0)return null;if(def.type==="FunctionName"){return def.node}if(variable.references.filter(ref=>ref.isWrite()).length!==1)return null;const init=def.node.init;if(init===null||init===void 0||init.type!==eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression&&init.type!==eslint_devkit_1.AST_NODE_TYPES.FunctionExpression){return null}return init}function returnedExpression(body){if(body.type===eslint_devkit_1.AST_NODE_TYPES.BlockStatement){const last=body.body.at(-1);if(last?.type!==eslint_devkit_1.AST_NODE_TYPES.ReturnStatement||last.argument===null)return null;if(last.argument.type===eslint_devkit_1.AST_NODE_TYPES.Identifier)return last.argument;return body.body.length===1?returnedExpression(last.argument):null}return body.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral||body.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression||body.type===eslint_devkit_1.AST_NODE_TYPES.Identifier?body:null}function importedBuilderResult(node,scope,env){if(node.type!==eslint_devkit_1.AST_NODE_TYPES.CallExpression||node.callee.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier){return null}const variable=resolveVariable(node.callee.name,scope);const origin=variable===null?null:(0,cross_file_1.importOrigin)(variable,env);if(origin===null)return null;const exported=(0,cross_file_1.exportedValue)(origin.module,origin.name,env.parser);const fn=exported===null?null:(0,cross_file_1.follow)(exported);const returned=fn===null?null:(0,cross_file_1.returnedValue)(fn.node);return fn===null||returned===null?null:{node:returned,scope:fn.env.scopeOf(returned),env:fn.env}}function writtenFragments(variable){let parts=[];const writes=variable.references.filter(reference=>reference.isWrite()&&reference.writeExpr!=null).sort((a,b)=>a.identifier.range[0]-b.identifier.range[0]);for(const write of writes){const value=write.writeExpr;const assignment=write.identifier.parent;const appends=assignment?.type===eslint_devkit_1.AST_NODE_TYPES.AssignmentExpression&&(assignment.operator==="+="||leftmostOperand(value)!==value&&isSameBinding(leftmostOperand(value),variable));if(!isStringText(value))parts=[];else if(appends)parts.push(value);else parts=[value]}return parts}function isSameBinding(node,variable){return variable.references.some(reference=>reference.identifier===node)}function isStringText(node){return node.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral||node.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"||(0,eslint_devkit_1.staticString)(node)!==null}function leftmostOperand(node){let current=node;while(current.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression&&current.operator==="+"){current=current.left}return current}exports.noUnsafeQuery={meta:{type:"problem",docs:{description:"Prevent SQL injection by disallowing string concatenation or unsafe template literals in queries.",url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/no-unsafe-query.md",cwe:"CWE-89",cvss:9.8,confidence:"high"},messages:{noUnsafeQuery:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"SQL Injection Risk",description:"Unsafe SQL query detected. Variable interpolation found.",severity:"CRITICAL",cwe:"CWE-89",owasp:"A03:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"high",fix:"Use parameterized queries ($1, $2) instead of string concatenation.",documentationLink:"https://node-postgres.com/features/queries#parameterized-queries"}),unsafeTemplateLiteral:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"SQL Injection Risk",description:"Unsafe SQL query construction detected (template literal).",severity:"CRITICAL",fix:"Use parameterized queries ($1, $2) instead of interpolating values.",documentationLink:"https://owasp.org/www-community/attacks/SQL_Injection"})},schema:[]},defaultOptions:[],create(context){const{sourceCode}=context;const env=(0,cross_file_1.envOf)(context);if(!(0,utils_1.usesPostgres)(context))return{};const fragments=new Map;const keyFor=id=>resolveVariable(id.name,sourceCode.getScope(id))??`global:${id.name}`;const isBuilt=node=>node.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral&&node.expressions.length>0||node.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression&&node.operator==="+";const isStringish=node=>isBuilt(node)||node.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral||(0,eslint_devkit_1.staticString)(node)!==null;const reportIfUnsafe=(reportNode,parts,self,scopeOf=node=>sourceCode.getScope(node))=>{const selfVariable=typeof self==="string"?null:self;let kind=null;let raw=false;let text="";for(const part of parts){text+=staticText(part);const scope=scopeOf(part);if(part.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression&&part.operator==="+"){kind="concat";if(hasRawPart(part,scope,selfVariable))raw=true}else if(part.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral&&part.expressions.length>0){kind="template";if(hasRawPart(part,scope,selfVariable))raw=true}}if(kind===null||!raw)return;if(!looksLikeSqlStatement(text))return;context.report({node:reportNode,messageId:kind==="template"?"unsafeTemplateLiteral":"noUnsafeQuery"})};return{VariableDeclarator(node){if(node.id.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.init&&isStringish(node.init)){fragments.set(keyFor(node.id),[node.init])}},AssignmentExpression(node){if(node.left.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return;const key=keyFor(node.left);if(node.operator==="+="){const existing2=fragments.get(key);if(existing2===void 0)fragments.set(key,[node.right]);else existing2.push(node.right);return}if(node.operator!=="=")return;if(!isStringish(node.right)){fragments.delete(key);return}const head=leftmostOperand(node.right);const existing=fragments.get(key);if(existing!==void 0&&head.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&keyFor(head)===key){existing.push(node.right)}else{fragments.set(key,[node.right])}},CallExpression(node){if(node.callee.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression||!SQL_SINK_METHODS.has((0,eslint_devkit_1.propertyName)(node.callee)??"")){return}const[queryArg]=node.arguments;if(queryArg===void 0||queryArg.type===eslint_devkit_1.AST_NODE_TYPES.SpreadElement){return}const scope=sourceCode.getScope(node);const imported=importedBuilderResult(queryArg,scope,env);if(imported!==null){const{node:result,env:builderEnv}=imported;if(isBuilt(result)){reportIfUnsafe(queryArg,[result],null,builderEnv.scopeOf)}else if(result.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const variable=resolveVariable(result.name,builderEnv.scopeOf(result));if(variable!==null){reportIfUnsafe(queryArg,writtenFragments(variable),variable,builderEnv.scopeOf)}}return}const expression=effectiveExpression(queryArg,scope);if(isBuilt(expression)){reportIfUnsafe(queryArg,[expression],null);return}if(expression.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const key=keyFor(expression);const parts=fragments.get(key);if(parts!==void 0)reportIfUnsafe(queryArg,parts,key)}}}}};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noUnsafeQuery=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const utils_1=require("../../utils");const cross_file_1=require("../../utils/cross-file");const SQL_SINK_METHODS=new Set(["query","execute","none","one","oneOrNone","many","manyOrNone","any","result","multi","multiResult","unsafe"]);const SQL_STATEMENTS=[/^\s*select\b[\s\S]*\bfrom\b/i,/^\s*insert\s+into\b/i,/^\s*update\b[\s\S]*\bset\b/i,/^\s*delete\s+from\b/i,/^\s*replace\s+into\b/i,/^\s*merge\s+into\b/i,/^\s*with\b[\s\S]*\bas\s*\(/i,/^\s*copy\b[\s\S]*\b(?:from|to)\b/i,/^\s*grant\b[\s\S]*\bon\b/i,/^\s*(create|drop|alter|truncate)\s+(table|index|view|schema|database|sequence|materialized|type|function|trigger|role|user|extension)\b/i,/^\s*select\s/i,/^\s*call\s+[\w".]+\s*\(/i,/^\s*(?:un)?listen\s+[\w"*]*\s*(?:;|$)/i,/^\s*notify\s+[\w"]*\s*(?:,|;|$)/i];const PG_ESCAPER_METHODS=new Set(["escapeIdentifier","escapeLiteral"]);const NUMBER_CONVERSIONS=new Set(["Number","parseInt","parseFloat"]);const NUMBER_NAMESPACES=new Set(["Math","Number"]);const PURE_TEXT_METHODS=new Set(["trim","trimStart","trimEnd","toLowerCase","toUpperCase","join","slice","substring","padStart","padEnd","toString","toFixed","concat"]);const MUTATING_METHODS=new Set(["push","unshift","splice","fill","copyWithin","set","add","assign"]);const MAX_VALUE_HOPS=8;const MAPPING_METHODS=new Set(["map","flatMap","from"]);function staticText(node){if(node.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral){return node.quasis.map(q=>q.value.raw).join("")}if(node.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"){return`${staticText(node.left)}${staticText(node.right)}`}return(0,eslint_devkit_1.staticString)(node)??""}function trailingText(node){if(node.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral){return node.quasis.at(-1).value.raw}if(node.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"){return trailingText(node.right)}return(0,eslint_devkit_1.staticString)(node)??""}function looksLikeSqlStatement(text){return SQL_STATEMENTS.some(pattern=>pattern.test(text))}function resolveVariable(name,scope){for(let current=scope;current!==null;current=current.upper){const variable=current.set.get(name);if(variable!==void 0)return variable}return null}function isGlobal(name,scope){const variable=resolveVariable(name,scope);return variable===null||variable.defs.length===0}function singleInit(variable){if(variable===null)return null;if(variable.references.filter(ref=>ref.isWrite()).length!==1)return null;const def=variable.defs.find(d=>d.type==="Variable");if(def===void 0)return null;return def.node.init??null}function endsWithPlaceholderPrefix(text){return text.endsWith("$")&&!text.endsWith("$$")}function isIndexShaped(node,scope){return isNumeric((0,eslint_devkit_1.unwrapTypeSyntax)(node),scope,new Set)}const INDEX_PARAMETER=new Map([["map",1],["flatMap",1],["forEach",1],["filter",1],["some",1],["every",1],["find",1],["findIndex",1],["findLast",1],["findLastIndex",1],["from",1],["reduce",2],["reduceRight",2]]);function isNumericParameter(name){if(name.typeAnnotation?.typeAnnotation.type===eslint_devkit_1.AST_NODE_TYPES.TSNumberKeyword)return true;const fn=name.parent;const call=fn?.parent;if(fn?.type!==eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression&&fn?.type!==eslint_devkit_1.AST_NODE_TYPES.FunctionExpression||call?.type!==eslint_devkit_1.AST_NODE_TYPES.CallExpression||call.callee.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression){return false}const method=(0,eslint_devkit_1.propertyName)(call.callee);const index=method===null?void 0:INDEX_PARAMETER.get(method);return index!==void 0&&fn.params[index]===name}function enclosingParameter(node){for(let current=node.parent;current;current=current.parent){if(current.type===eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression||current.type===eslint_devkit_1.AST_NODE_TYPES.FunctionExpression||current.type===eslint_devkit_1.AST_NODE_TYPES.FunctionDeclaration){const param=current.params.find(p=>p.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&p.name===node.name);if(param!==void 0)return param}}return null}const NUMERIC_OPERATORS=new Set(["-","*","%","**","|","&","^","<<",">>",">>>"]);function isNumeric(node,scope,seen){switch(node.type){case eslint_devkit_1.AST_NODE_TYPES.Literal:return typeof node.value==="number";case eslint_devkit_1.AST_NODE_TYPES.UpdateExpression:return true;case eslint_devkit_1.AST_NODE_TYPES.UnaryExpression:return node.operator==="+"||node.operator==="-"||node.operator==="~";case eslint_devkit_1.AST_NODE_TYPES.MemberExpression:return(0,eslint_devkit_1.propertyName)(node)==="length";case eslint_devkit_1.AST_NODE_TYPES.CallExpression:return node.callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&(0,eslint_devkit_1.propertyName)(node.callee)==="push"||isNumberConversion(node,scope);case eslint_devkit_1.AST_NODE_TYPES.BinaryExpression:if(node.operator==="+"){return isNumeric(node.left,scope,seen)&&isNumeric(node.right,scope,seen)}return NUMERIC_OPERATORS.has(node.operator);case eslint_devkit_1.AST_NODE_TYPES.Identifier:return isNumericBinding(node,scope,seen);default:return false}}function isNumberConversion(call,scope){const{callee}=call;if(callee.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){return NUMBER_CONVERSIONS.has(callee.name)&&isGlobal(callee.name,scope)}return callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&callee.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&NUMBER_NAMESPACES.has(callee.object.name)&&isGlobal(callee.object.name,scope)}function isNumericBinding(node,scope,seen){const param=enclosingParameter(node);if(param!==null)return isNumericParameter(param);const variable=resolveVariable(node.name,scope);if(variable===null||variable.defs.length===0)return false;if(seen.has(variable))return true;seen.add(variable);const[def]=variable.defs;if(def.type==="Parameter")return isNumericParameter(def.name);if(def.type!=="Variable")return false;const declarator=def.node;if(declarator.id.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&declarator.id.typeAnnotation?.typeAnnotation.type===eslint_devkit_1.AST_NODE_TYPES.TSNumberKeyword){return true}const loop=declarator.parent.parent;if(loop?.type===eslint_devkit_1.AST_NODE_TYPES.ForInStatement||loop?.type===eslint_devkit_1.AST_NODE_TYPES.ForOfStatement){return false}const writes=variable.references.filter(reference=>reference.isWrite());return writes.length>0&&writes.every(write=>{const parent=write.identifier.parent;if(parent?.type===eslint_devkit_1.AST_NODE_TYPES.UpdateExpression)return true;if(parent?.type===eslint_devkit_1.AST_NODE_TYPES.AssignmentExpression&&parent.operator!=="="){return parent.operator!=="+="||isNumeric(parent.right,write.from,seen)}return write.writeExpr!=null&&isNumeric((0,eslint_devkit_1.unwrapTypeSyntax)(write.writeExpr),write.from,seen)})}function isPlaceholderIndex(textBefore,expression,scope){return endsWithPlaceholderPrefix(textBefore)&&isIndexShaped(expression,scope)}function isPlaceholderOnly(node,scope){if((0,eslint_devkit_1.staticString)(node)!==null)return true;if(node.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral){return node.expressions.every((expression,i)=>isPlaceholderIndex(node.quasis[i].value.raw,expression,scope))}if(node.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"){const left=node.left;return isPlaceholderOnly(left,scope)&&(isPlaceholderOnly(node.right,scope)||isPlaceholderIndex(trailingText(left),node.right,scope))}return false}function callbackResult(fn){if(fn.type!==eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression&&fn.type!==eslint_devkit_1.AST_NODE_TYPES.FunctionExpression){return null}if(fn.body.type!==eslint_devkit_1.AST_NODE_TYPES.BlockStatement)return fn.body;const last=fn.body.body.at(-1);return last?.type===eslint_devkit_1.AST_NODE_TYPES.ReturnStatement?last.argument:null}function isPlaceholderList(node,scope){if(node.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const variable=resolveVariable(node.name,scope);const init=(0,eslint_devkit_1.unwrapTypeSyntax)(singleInit(variable));if(init?.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression)return isPlaceholderList(init,scope);return variable!==null&&init?.type===eslint_devkit_1.AST_NODE_TYPES.ArrayExpression&&isFixedTextList(variable,init)}if(node.type!==eslint_devkit_1.AST_NODE_TYPES.CallExpression||node.callee.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression){return false}const method=(0,eslint_devkit_1.propertyName)(node.callee);if(method===null||!MAPPING_METHODS.has(method))return false;const mapper=node.arguments.at(-1);const result=mapper===void 0?null:callbackResult(mapper);return result!==null&&isPlaceholderOnly(result,scope)}function isFixedText(node,scope){if((0,eslint_devkit_1.staticString)(node)!==null)return true;return(node.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral||node.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression)&&!hasRawPart(node,scope,null)}function isFixedTextList(variable,init){if(!init.elements.every(element=>element!==null&&element.type!==eslint_devkit_1.AST_NODE_TYPES.SpreadElement&&isFixedText(element,variable.scope))){return false}return variable.references.every(reference=>{if(reference.isWrite())return true;const member=reference.identifier.parent;if(member?.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression||member.object!==reference.identifier){return false}const method=(0,eslint_devkit_1.propertyName)(member);if(method==="join"||method==="length")return true;const call=member.parent;return method==="push"&&call?.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression&&call.arguments.every(argument=>argument.type!==eslint_devkit_1.AST_NODE_TYPES.SpreadElement&&isFixedText(argument,reference.from))})}function isFixedLoopElement(variable,scope){const def=variable.defs[0];if(def?.type!=="Variable")return false;const declaration=def.node.parent;const loop=declaration.parent;if(loop?.type!==eslint_devkit_1.AST_NODE_TYPES.ForOfStatement||loop.left!==declaration)return false;const right=(0,eslint_devkit_1.unwrapTypeSyntax)(loop.right);const list=right.type===eslint_devkit_1.AST_NODE_TYPES.Identifier?(0,eslint_devkit_1.unwrapTypeSyntax)(singleInit(resolveVariable(right.name,scope))):right;return list?.type===eslint_devkit_1.AST_NODE_TYPES.ArrayExpression&&list.elements.every(element=>element!==null&&element.type!==eslint_devkit_1.AST_NODE_TYPES.SpreadElement&&(0,eslint_devkit_1.isStaticExpression)({node:element,scope}))}function isSafeCall(call,scope,depth=0){const{callee}=call;const argsSafe=()=>call.arguments.every(a=>!isRawValue(a,scope,null,depth+1));if(callee.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){if(NUMBER_CONVERSIONS.has(callee.name)&&isGlobal(callee.name,scope))return true;if(callee.name==="String"&&isGlobal("String",scope)&&argsSafe())return true;if(call.arguments.length===0&&helperReturnsSafe(callee,scope,depth))return true}else if(callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression){const method=(0,eslint_devkit_1.propertyName)(callee);if(method!==null&&PURE_TEXT_METHODS.has(method)&&!isRawValue(callee.object,scope,null,depth+1)&&argsSafe()){return true}if(method!==null&&PG_ESCAPER_METHODS.has(method))return true;if(callee.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&NUMBER_NAMESPACES.has(callee.object.name)&&isGlobal(callee.object.name,scope)){return true}if(callee.object.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&(0,eslint_devkit_1.propertyName)(callee.object)==="as"){return true}if(method==="join"&&isPlaceholderList(callee.object,scope))return true}const binding=(0,eslint_devkit_1.resolveModuleBinding)(callee,scope);if(binding!==void 0){if(binding.module==="pg-format")return true;const exported=binding.path.at(-1);if(exported!==void 0&&PG_ESCAPER_METHODS.has(exported))return true}return(0,eslint_devkit_1.isStaticExpression)({node:call,scope})}function returnExpressions(fn){const body=fn.body;if(body.type!==eslint_devkit_1.AST_NODE_TYPES.BlockStatement)return[body];const found=[];const visit=node=>{if(node.type===eslint_devkit_1.AST_NODE_TYPES.ReturnStatement){if(node.argument!==null)found.push(node.argument);return}if(node.type===eslint_devkit_1.AST_NODE_TYPES.FunctionDeclaration||node.type===eslint_devkit_1.AST_NODE_TYPES.FunctionExpression||node.type===eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression){return}for(const key of Object.keys(node)){if(key==="parent")continue;const child=node[key];if(Array.isArray(child)){for(const c of child)if(c&&typeof c.type==="string")visit(c)}else if(child&&typeof child.type==="string"){visit(child)}}};visit(body);return found}function helperReturnsSafe(callee,scope,depth){if(depth>=MAX_VALUE_HOPS)return false;const variable=resolveVariable(callee.name,scope);if(variable===null)return false;const fn=functionImplementation(variable);if(fn===null||fn.async||fn.generator)return false;const returns=returnExpressions(fn);if(returns.length===0)return false;const inner=variable.scope.childScopes.flatMap(child=>[child,...child.childScopes]).find(child=>child.block===fn&&child.type==="function");return returns.every(r=>!isRawValue(r,inner,null,depth+1))}function writtenValues(variable){if(variable.defs.length!==1||variable.defs[0].type!=="Variable")return null;const declarator=variable.defs[0].node;if(declarator.id.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return null;const values=[];for(const ref of variable.references){if(!ref.isWrite())continue;const parent=ref.identifier.parent;if(parent?.type===eslint_devkit_1.AST_NODE_TYPES.UpdateExpression)continue;if(parent?.type===eslint_devkit_1.AST_NODE_TYPES.AssignmentExpression&&parent.operator!=="="&&parent.operator!=="+="){continue}values.push(ref.writeExpr)}const container=values.some(v=>{const u=(0,eslint_devkit_1.unwrapTypeSyntax)(v);return u.type===eslint_devkit_1.AST_NODE_TYPES.ArrayExpression||u.type===eslint_devkit_1.AST_NODE_TYPES.ObjectExpression});if(container&&variable.references.some(ref=>!ref.isWrite()&&ref.identifier.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&changesOrEscapes(ref.identifier))){return null}return values}function changesOrEscapes(id){const parent=id.parent;if(parent.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&parent.object===id){const outer=parent.parent;if(outer?.type===eslint_devkit_1.AST_NODE_TYPES.AssignmentExpression&&outer.left===parent)return true;if(outer?.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression&&outer.callee===parent){const method=(0,eslint_devkit_1.propertyName)(parent);return method===null||MUTATING_METHODS.has(method)}return false}if(parent.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression||parent.type===eslint_devkit_1.AST_NODE_TYPES.NewExpression){return parent.arguments.includes(id)}return parent.type===eslint_devkit_1.AST_NODE_TYPES.SpreadElement||parent.type===eslint_devkit_1.AST_NODE_TYPES.ReturnStatement}function isConstantMember(node,scope){const key=(0,eslint_devkit_1.propertyName)(node);if(key===null||node.object.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return false;const variable=resolveVariable(node.object.name,scope);if(variable===null)return false;const enumDef=variable.defs.find(d=>d.type==="TSEnumName");if(enumDef!==void 0){const declaration=enumDef.node;const member=declaration.body.members.find(m=>m.id.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&m.id.name===key);return member!==void 0&&(member.initializer===void 0||member.initializer.type===eslint_devkit_1.AST_NODE_TYPES.Literal)}const init=(0,eslint_devkit_1.unwrapTypeSyntax)(singleInit(variable));if(init===null||init.type!==eslint_devkit_1.AST_NODE_TYPES.ObjectExpression)return false;if(variable.references.some(ref=>!ref.isWrite()&&ref.identifier.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&changesOrEscapes(ref.identifier))){return false}const property=init.properties.find(p=>p.type===eslint_devkit_1.AST_NODE_TYPES.Property&&(0,eslint_devkit_1.objectKeyName)(p)===key);return property!==void 0&&(0,eslint_devkit_1.isStaticExpression)({node:property.value,scope})}function isRawValue(value,scope,self,depth=0){const part=(0,eslint_devkit_1.unwrapTypeSyntax)(value);if(part.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression||part.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral){return hasRawPart(part,scope,self,depth)}if((0,eslint_devkit_1.isStaticExpression)({node:part,scope}))return false;if(part.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression)return!isSafeCall(part,scope,depth);if(part.type===eslint_devkit_1.AST_NODE_TYPES.ArrayExpression){return part.elements.some(e=>e===null||e.type===eslint_devkit_1.AST_NODE_TYPES.SpreadElement||isRawValue(e,scope,null,depth+1))}if(part.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression){if((0,eslint_devkit_1.propertyName)(part)==="length"&&!isRawValue(part.object,scope,null,depth+1)){return false}return!isConstantMember(part,scope)}if(part.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const variable=resolveVariable(part.name,scope);if(variable!==null&&variable===self)return false;if(variable!==null&&isFixedLoopElement(variable,scope))return false;const init=(0,eslint_devkit_1.unwrapTypeSyntax)(singleInit(variable));if(init!==null&&init.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression&&isSafeCall(init,scope,depth)){return false}if(variable===null||depth>=MAX_VALUE_HOPS)return true;const values=writtenValues(variable);if(values===null||values.length===0)return true;return values.some(w=>isRawValue(w,scope,variable,depth+1))}return true}function hasRawPart(node,scope,self,depth=0){if(node.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral){return node.expressions.some((expression,i)=>!isPlaceholderIndex(node.quasis[i].value.raw,expression,scope)&&isRawValue(expression,scope,self,depth))}const left=node.left;if(isPlaceholderIndex(trailingText(left),node.right,scope)){return isRawValue(left,scope,self,depth)}return isRawValue(left,scope,self,depth)||isRawValue(node.right,scope,self,depth)}function effectiveExpression(node,scope){if(node.type===eslint_devkit_1.AST_NODE_TYPES.ObjectExpression){const text=node.properties.find(prop=>prop.type===eslint_devkit_1.AST_NODE_TYPES.Property&&(prop.key.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&!prop.computed&&prop.key.name==="text"||prop.key.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&prop.key.value==="text"));return text===void 0?node:effectiveExpression(text.value,scope)}if(node.type===eslint_devkit_1.AST_NODE_TYPES.NewExpression){const[first]=node.arguments;return first===void 0?node:effectiveExpression(first,scope)}if(node.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const init=singleInit(resolveVariable(node.name,scope));return init!==null&&init.type===eslint_devkit_1.AST_NODE_TYPES.ObjectExpression?effectiveExpression(init,scope):node}if(node.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression){if(node.callee.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return node;const fn=resolveVariable(node.callee.name,scope);const impl=fn===null?null:functionImplementation(fn);if(impl===null)return node;const returned=returnedExpression(impl.body);return returned===null?node:returned}return node}function functionImplementation(variable){const def=variable.defs.find(d=>d.type==="FunctionName"||d.type==="Variable");if(def===void 0)return null;if(def.type==="FunctionName"){return def.node}if(variable.references.filter(ref=>ref.isWrite()).length!==1)return null;const init=def.node.init;if(init===null||init===void 0||init.type!==eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression&&init.type!==eslint_devkit_1.AST_NODE_TYPES.FunctionExpression){return null}return init}function returnedExpression(body){if(body.type===eslint_devkit_1.AST_NODE_TYPES.BlockStatement){const last=body.body.at(-1);if(last?.type!==eslint_devkit_1.AST_NODE_TYPES.ReturnStatement||last.argument===null)return null;if(last.argument.type===eslint_devkit_1.AST_NODE_TYPES.Identifier)return last.argument;return body.body.length===1?returnedExpression(last.argument):null}return body.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral||body.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression||body.type===eslint_devkit_1.AST_NODE_TYPES.Identifier?body:null}function importedBuilderResult(node,scope,env){if(node.type!==eslint_devkit_1.AST_NODE_TYPES.CallExpression||node.callee.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier){return null}const variable=resolveVariable(node.callee.name,scope);const origin=variable===null?null:(0,cross_file_1.importOrigin)(variable,env);if(origin===null)return null;const exported=(0,cross_file_1.exportedValue)(origin.module,origin.name,env.parser);const fn=exported===null?null:(0,cross_file_1.follow)(exported);const returned=fn===null?null:(0,cross_file_1.returnedValue)(fn.node);return fn===null||returned===null?null:{node:returned,scope:fn.env.scopeOf(returned),env:fn.env}}function writtenFragments(variable){let parts=[];const writes=variable.references.filter(reference=>reference.isWrite()&&reference.writeExpr!=null).sort((a,b)=>a.identifier.range[0]-b.identifier.range[0]);for(const write of writes){const value=write.writeExpr;const assignment=write.identifier.parent;const appends=assignment?.type===eslint_devkit_1.AST_NODE_TYPES.AssignmentExpression&&(assignment.operator==="+="||leftmostOperand(value)!==value&&isSameBinding(leftmostOperand(value),variable));if(!isStringText(value))parts=[];else if(appends)parts.push(value);else parts=[value]}return parts}function isSameBinding(node,variable){return variable.references.some(reference=>reference.identifier===node)}function isStringText(node){return node.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral||node.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"||(0,eslint_devkit_1.staticString)(node)!==null}function leftmostOperand(node){let current=node;while(current.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression&&current.operator==="+"){current=current.left}return current}exports.noUnsafeQuery={meta:{type:"problem",docs:{description:"Prevent SQL injection by disallowing string concatenation or unsafe template literals in queries.",url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/no-unsafe-query.md",cwe:"CWE-89",cvss:9.8,confidence:"high"},messages:{noUnsafeQuery:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"SQL Injection Risk",description:"Unsafe SQL query detected. Variable interpolation found.",severity:"CRITICAL",cwe:"CWE-89",owasp:"A03:2021",compliance:["SOC2","PCI-DSS","NIST-CSF"],effort:"high",fix:"Use parameterized queries ($1, $2) instead of string concatenation.",documentationLink:"https://node-postgres.com/features/queries#parameterized-queries"}),unsafeTemplateLiteral:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"SQL Injection Risk",description:"Unsafe SQL query construction detected (template literal).",severity:"CRITICAL",fix:"Use parameterized queries ($1, $2) instead of interpolating values.",documentationLink:"https://owasp.org/www-community/attacks/SQL_Injection"})},schema:[]},defaultOptions:[],create(context){const{sourceCode}=context;const env=(0,cross_file_1.envOf)(context);if(!(0,utils_1.usesPostgres)(context))return{};const fragments=new Map;const keyFor=id=>resolveVariable(id.name,sourceCode.getScope(id))??`global:${id.name}`;const isBuilt=node=>node.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral&&node.expressions.length>0||node.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression&&node.operator==="+";const isStringish=node=>isBuilt(node)||node.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral||(0,eslint_devkit_1.staticString)(node)!==null;const reportIfUnsafe=(reportNode,parts,self,scopeOf=node=>sourceCode.getScope(node))=>{const selfVariable=typeof self==="string"?null:self;let kind=null;let raw=false;let text="";for(const part of parts){text+=staticText(part);const scope=scopeOf(part);if(part.type===eslint_devkit_1.AST_NODE_TYPES.BinaryExpression&&part.operator==="+"){kind="concat";if(hasRawPart(part,scope,selfVariable))raw=true}else if(part.type===eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral&&part.expressions.length>0){kind="template";if(hasRawPart(part,scope,selfVariable))raw=true}}if(kind===null||!raw)return;if(!looksLikeSqlStatement(text))return;context.report({node:reportNode,messageId:kind==="template"?"unsafeTemplateLiteral":"noUnsafeQuery"})};return{VariableDeclarator(node){if(node.id.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.init&&isStringish(node.init)){fragments.set(keyFor(node.id),[node.init])}},AssignmentExpression(node){if(node.left.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier)return;const key=keyFor(node.left);if(node.operator==="+="){const existing2=fragments.get(key);if(existing2===void 0)fragments.set(key,[node.right]);else existing2.push(node.right);return}if(node.operator!=="=")return;if(!isStringish(node.right)){fragments.delete(key);return}const head=leftmostOperand(node.right);const existing=fragments.get(key);if(existing!==void 0&&head.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&keyFor(head)===key){existing.push(node.right)}else{fragments.set(key,[node.right])}},CallExpression(node){if(node.callee.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression||!SQL_SINK_METHODS.has((0,eslint_devkit_1.propertyName)(node.callee)??"")){return}const[queryArg]=node.arguments;if(queryArg===void 0||queryArg.type===eslint_devkit_1.AST_NODE_TYPES.SpreadElement){return}const scope=sourceCode.getScope(node);const imported=importedBuilderResult(queryArg,scope,env);if(imported!==null){const{node:result,env:builderEnv}=imported;if(isBuilt(result)){reportIfUnsafe(queryArg,[result],null,builderEnv.scopeOf)}else if(result.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const variable=resolveVariable(result.name,builderEnv.scopeOf(result));if(variable!==null){reportIfUnsafe(queryArg,writtenFragments(variable),variable,builderEnv.scopeOf)}}return}const expression=effectiveExpression(queryArg,scope);if(isBuilt(expression)){reportIfUnsafe(queryArg,[expression],null);return}if(expression.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const key=keyFor(expression);const parts=fragments.get(key);if(parts!==void 0)reportIfUnsafe(queryArg,parts,key)}}}}};