eslint-plugin-postgresql-security 3.0.1 → 3.0.3

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,7 +1,7 @@
1
1
  {
2
2
  "name": "eslint-plugin-postgresql-security",
3
- "version": "3.0.1",
4
- "description": "ESLint plugin for the pg PostgreSQL driver — detects SQL injection, unreleased clients, floating queries, unsafe search_path, and insecure SSL.",
3
+ "version": "3.0.3",
4
+ "description": "ESLint Postgres security rules for the pg PostgreSQL driver — detects SQL injection, unreleased clients, floating queries, unsafe search_path, and insecure SSL.",
5
5
  "type": "commonjs",
6
6
  "sideEffects": false,
7
7
  "main": "./src/index.js",
@@ -80,7 +80,7 @@
80
80
  }
81
81
  },
82
82
  "dependencies": {
83
- "@interlace/eslint-devkit": "^2.0.1"
83
+ "@interlace/eslint-devkit": "^2.0.4"
84
84
  },
85
85
  "interlace": {
86
86
  "surface": {
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:"3.0.1"},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:"3.0.3"},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.checkQueryParams=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");const utils_1=require("../../utils");const sql_scan_1=require("../../utils/sql-scan");const SQL_SINK_METHODS=new Set(["query","execute"]);const PLACEHOLDER=/\$(\d+)/g;const MAX_RESOLUTION_DEPTH=4;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 isStringRawTag(node){const{tag}=node;return tag.type===eslint_devkit_2.AST_NODE_TYPES.MemberExpression&&tag.object.type===eslint_devkit_2.AST_NODE_TYPES.Identifier&&tag.object.name==="String"&&(0,eslint_devkit_2.propertyName)(tag)==="raw"}function singleAssignedInit(variable){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 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_2.AST_NODE_TYPES.ArrowFunctionExpression&&init.type!==eslint_devkit_2.AST_NODE_TYPES.FunctionExpression){return null}return init}function returnedExpression(body){if(body.type===eslint_devkit_2.AST_NODE_TYPES.BlockStatement){const[only]=body.body;if(body.body.length!==1||only.type!==eslint_devkit_2.AST_NODE_TYPES.ReturnStatement){return null}return only.argument===null?null:returnedExpression(only.argument)}return body}function effectiveExpression(node,scope,depth=0){if(depth>=MAX_RESOLUTION_DEPTH)return node;if(node.type===eslint_devkit_2.AST_NODE_TYPES.TaggedTemplateExpression){return isStringRawTag(node)?node.quasi:node}if(node.type===eslint_devkit_2.AST_NODE_TYPES.Identifier){const variable=resolveVariable(node.name,scope);const init=variable===null?null:singleAssignedInit(variable);return init===null?node:effectiveExpression(init,scope,depth+1)}if(node.type===eslint_devkit_2.AST_NODE_TYPES.CallExpression){if(node.callee.type!==eslint_devkit_2.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:effectiveExpression(returned,scope,depth+1)}return node}function knowableText(node){if(node.type===eslint_devkit_2.AST_NODE_TYPES.Literal){return typeof node.value==="string"?node.value:null}if(node.type===eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral){if(node.expressions.length>0)return null;const[{value}]=node.quasis;return value.cooked??value.raw}if(node.type===eslint_devkit_2.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"){const left=knowableText(node.left);const right=knowableText(node.right);return left===null||right===null?null:left+right}return null}function highestPlaceholder(text){const executable=(0,sql_scan_1.blankNonPlaceholderText)(text);let highest=0;for(const match of executable.matchAll(PLACEHOLDER)){highest=Math.max(highest,Number.parseInt(match[1],10))}return highest}function knowableValueCount(node,scope){const resolved=effectiveExpression(node,scope);if(resolved.type!==eslint_devkit_2.AST_NODE_TYPES.ArrayExpression)return null;if(resolved.elements.some(el=>el!==null&&el.type===eslint_devkit_2.AST_NODE_TYPES.SpreadElement)){return null}return resolved.elements.length}function configPair(node){let text=null;let values=null;for(const property of node.properties){if(property.type!==eslint_devkit_2.AST_NODE_TYPES.Property)continue;const name=(0,eslint_devkit_2.objectKeyName)(property);if(name==="text")text=property.value;if(name==="values")values=property.value}return text!==null?{text,values}:null}function isPrepare(text){return/^\s*prepare\b/i.test((0,sql_scan_1.stripComments)(text))}function bindsNothing(node){return node===void 0||node.type===eslint_devkit_2.AST_NODE_TYPES.ArrowFunctionExpression||node.type===eslint_devkit_2.AST_NODE_TYPES.FunctionExpression}exports.checkQueryParams=eslint_devkit_1.RuleCreator.withoutDocs({meta:{interop:{runtimes:["eslint","oxlint"]},type:"problem",docs:{description:"Ensure the number of query parameters matches the arguments array.",url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/check-query-params.md",cwe:"CWE-20",cvss:7.5},messages:{parameterCountMismatch:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.QUALITY,issueName:"Parameter Count Mismatch",description:"Query parameter count mismatch detected: {{expected}} placeholder(s), {{actual}} value(s).",severity:"HIGH",cwe:"CWE-20",effort:"low",fix:"Ensure the number of arguments matches the number of placeholders ($1, $2, etc.).",documentationLink:"https://node-postgres.com/features/queries"})},schema:[]},defaultOptions:[],createOnce(context){const check=(textNode,valuesNode,scope)=>{const text=knowableText(effectiveExpression(textNode,scope));if(text===null)return;if(valuesNode===null&&isPrepare(text))return;const provided=valuesNode===null?0:knowableValueCount(valuesNode,scope);if(provided===null)return;const highest=highestPlaceholder(text);const mismatched=provided<highest||highest>=1&&provided>highest;if(!mismatched)return;context.report({node:valuesNode??textNode,messageId:"parameterCountMismatch",data:{expected:highest.toString(),actual:provided.toString()}})};return{before(){if(!(0,utils_1.usesPostgres)(context))return false;return true},CallExpression(node){if(node.callee.type!==eslint_devkit_2.AST_NODE_TYPES.MemberExpression||!SQL_SINK_METHODS.has((0,eslint_devkit_2.propertyName)(node.callee))){return}const[first,second]=node.arguments;if(first===void 0||first.type===eslint_devkit_2.AST_NODE_TYPES.SpreadElement)return;const scope=context.sourceCode.getScope(node);const resolved=effectiveExpression(first,scope);if(resolved.type===eslint_devkit_2.AST_NODE_TYPES.ObjectExpression){const pair=configPair(resolved);if(pair!==null)check(pair.text,pair.values,scope);return}if(bindsNothing(second)){check(first,null,scope);return}if(second.type===eslint_devkit_2.AST_NODE_TYPES.SpreadElement)return;check(first,second,scope)}}}});
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.checkQueryParams=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");const utils_1=require("../../utils");const sql_scan_1=require("../../utils/sql-scan");const SQL_SINK_METHODS=new Set(["query","execute"]);const PLACEHOLDER=/\$(\d+)/g;const MAX_RESOLUTION_DEPTH=4;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 isStringRawTag(node){const{tag}=node;return tag.type===eslint_devkit_2.AST_NODE_TYPES.MemberExpression&&tag.object.type===eslint_devkit_2.AST_NODE_TYPES.Identifier&&tag.object.name==="String"&&(0,eslint_devkit_2.propertyName)(tag)==="raw"}function singleAssignedInit(variable){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 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_2.AST_NODE_TYPES.ArrowFunctionExpression&&init.type!==eslint_devkit_2.AST_NODE_TYPES.FunctionExpression){return null}return init}function returnedExpression(body){if(body.type===eslint_devkit_2.AST_NODE_TYPES.BlockStatement){const[only]=body.body;if(body.body.length!==1||only.type!==eslint_devkit_2.AST_NODE_TYPES.ReturnStatement){return null}return only.argument===null?null:returnedExpression(only.argument)}return body}function effectiveExpression(node,scope,depth=0){if(depth>=MAX_RESOLUTION_DEPTH)return node;if(node.type===eslint_devkit_2.AST_NODE_TYPES.TaggedTemplateExpression){return isStringRawTag(node)?node.quasi:node}if(node.type===eslint_devkit_2.AST_NODE_TYPES.Identifier){const variable=resolveVariable(node.name,scope);const init=variable===null?null:singleAssignedInit(variable);return init===null?node:effectiveExpression(init,scope,depth+1)}if(node.type===eslint_devkit_2.AST_NODE_TYPES.CallExpression){if(node.callee.type!==eslint_devkit_2.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:effectiveExpression(returned,scope,depth+1)}return node}function knowableText(node){if(node.type===eslint_devkit_2.AST_NODE_TYPES.Literal){return typeof node.value==="string"?node.value:null}if(node.type===eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral){if(node.expressions.length>0)return null;const[{value}]=node.quasis;return value.cooked??value.raw}if(node.type===eslint_devkit_2.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"){const left=knowableText(node.left);const right=knowableText(node.right);return left===null||right===null?null:left+right}return null}function highestPlaceholder(text){const executable=(0,sql_scan_1.blankNonPlaceholderText)(text);let highest=0;for(const match of executable.matchAll(PLACEHOLDER)){highest=Math.max(highest,Number.parseInt(match[1],10))}return highest}function knowableValueCount(node,scope){const resolved=effectiveExpression(node,scope);if(resolved.type!==eslint_devkit_2.AST_NODE_TYPES.ArrayExpression)return null;if(resolved.elements.some(el=>el!==null&&el.type===eslint_devkit_2.AST_NODE_TYPES.SpreadElement)){return null}return resolved.elements.length}function configPair(node){let text=null;let values=null;for(const property of node.properties){if(property.type!==eslint_devkit_2.AST_NODE_TYPES.Property)continue;const name=(0,eslint_devkit_2.objectKeyName)(property);if(name==="text")text=property.value;if(name==="values")values=property.value}return text!==null?{text,values}:null}function isPrepare(text){return/^\s*prepare\b/i.test((0,sql_scan_1.stripComments)(text))}function bindsNothing(node){return node===void 0||node.type===eslint_devkit_2.AST_NODE_TYPES.ArrowFunctionExpression||node.type===eslint_devkit_2.AST_NODE_TYPES.FunctionExpression}exports.checkQueryParams=eslint_devkit_1.RuleCreator.withoutDocs({meta:{interop:{runtimes:["eslint","oxlint"]},type:"problem",docs:{description:"Ensure the number of query parameters matches the arguments array.",url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/check-query-params.md",cwe:"CWE-20",cvss:7.5},messages:{parameterCountMismatch:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.QUALITY,issueName:"Parameter Count Mismatch",description:"Query parameter count mismatch detected: {{expected}} placeholder(s), {{actual}} value(s).",severity:"HIGH",cwe:"CWE-20",effort:"low",fix:"Ensure the number of arguments matches the number of placeholders ($1, $2, etc.).",documentationLink:"https://node-postgres.com/features/queries"})},schema:[]},defaultOptions:[],createOnce(context){const check=(textNode,valuesNode,scope)=>{const text=knowableText(effectiveExpression(textNode,scope));if(text===null)return;if(valuesNode===null&&isPrepare(text))return;const provided=valuesNode===null?0:knowableValueCount(valuesNode,scope);if(provided===null)return;const highest=highestPlaceholder(text);const mismatched=provided<highest||highest>=1&&provided>highest;if(!mismatched)return;context.report({node:valuesNode??textNode,messageId:"parameterCountMismatch",data:{expected:highest.toString(),actual:provided.toString()}})};return{before(){if(!(0,utils_1.usesPostgres)(context))return false;return true},CallExpression(node){if(node.callee.type!==eslint_devkit_2.AST_NODE_TYPES.MemberExpression||!(0,eslint_devkit_2.namesOneOf)((0,eslint_devkit_2.propertyName)(node.callee),SQL_SINK_METHODS)){return}const[first,second]=node.arguments;if(first===void 0||first.type===eslint_devkit_2.AST_NODE_TYPES.SpreadElement)return;const scope=context.sourceCode.getScope(node);const resolved=effectiveExpression(first,scope);if(resolved.type===eslint_devkit_2.AST_NODE_TYPES.ObjectExpression){const pair=configPair(resolved);if(pair!==null)check(pair.text,pair.values,scope);return}if(bindsNothing(second)){check(first,null,scope);return}if(second.type===eslint_devkit_2.AST_NODE_TYPES.SpreadElement)return;check(first,second,scope)}}}});
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noUnsafeCopyFrom=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");const utils_1=require("../../utils");const sql_scan_1=require("../../utils/sql-scan");const SQL_SINK_METHODS=new Set(["query","execute"]);const QUOTED_SOURCE=/from\s+['"]([^'"]+)['"]/i;const FROM_STDIN=/^\s*stdin\b/i;const TO_STDOUT=/^\s*stdout\b/i;const MAX_RESOLUTION_DEPTH=4;function staticText(node){if(node.type===eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral){return node.quasis.map(q=>q.value.cooked??q.value.raw).join("")}if(node.type===eslint_devkit_2.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"){return`${staticText(node.left)}${staticText(node.right)}`}return(0,eslint_devkit_2.staticString)(node)??""}function copyClauses(text){const found=[];const stripped=(0,sql_scan_1.stripComments)(text);for(const statement of stripped.split(";")){const head=/^\s*copy\b/i.exec(statement);if(head===null)continue;let index=head[0].length;while(index<statement.length&&/\s/.test(statement[index]))index+=1;if(statement[index]==="("){let depth=0;for(;index<statement.length;index+=1){if(statement[index]==="(")depth+=1;else if(statement[index]===")"){depth-=1;if(depth===0){index+=1;break}}}}const direction=/\b(from|to)\b/i.exec(statement.slice(index));if(direction===null)continue;found.push({direction:direction[1].toLowerCase(),clause:statement.slice(index+direction.index+direction[1].length)})}return found}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 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_2.AST_NODE_TYPES.ArrowFunctionExpression&&init.type!==eslint_devkit_2.AST_NODE_TYPES.FunctionExpression){return null}return init}function returnedExpression(body){if(body.type===eslint_devkit_2.AST_NODE_TYPES.BlockStatement){const[only]=body.body;if(body.body.length!==1||only.type!==eslint_devkit_2.AST_NODE_TYPES.ReturnStatement){return null}return only.argument===null?null:returnedExpression(only.argument)}return body.type===eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral||body.type===eslint_devkit_2.AST_NODE_TYPES.BinaryExpression||(0,eslint_devkit_2.staticString)(body)!==null?body:null}function isStringRawTag(node){const{tag}=node;return tag.type===eslint_devkit_2.AST_NODE_TYPES.MemberExpression&&tag.object.type===eslint_devkit_2.AST_NODE_TYPES.Identifier&&tag.object.name==="String"&&(0,eslint_devkit_2.propertyName)(tag)==="raw"}function singleAssignedInit(variable){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 effectiveExpression(node,scope,depth=0){if(depth>=MAX_RESOLUTION_DEPTH)return node;if(node.type===eslint_devkit_2.AST_NODE_TYPES.TaggedTemplateExpression){return isStringRawTag(node)?node.quasi:node}if(node.type===eslint_devkit_2.AST_NODE_TYPES.Identifier){const variable=resolveVariable(node.name,scope);const init=variable===null?null:singleAssignedInit(variable);return init===null?node:effectiveExpression(init,scope,depth+1)}if(node.type===eslint_devkit_2.AST_NODE_TYPES.CallExpression){if(node.callee.type!==eslint_devkit_2.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:effectiveExpression(returned,scope,depth+1)}return node}function hasDynamicPart(node,scope){if(node.type===eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral){return node.expressions.some(part=>hasDynamicPart(part,scope))}if(node.type===eslint_devkit_2.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"){return hasDynamicPart(node.left,scope)||hasDynamicPart(node.right,scope)}return!(0,eslint_devkit_2.isStaticExpression)({node,scope})}exports.noUnsafeCopyFrom=eslint_devkit_1.RuleCreator.withoutDocs({meta:{interop:{runtimes:["eslint","oxlint"]},type:"problem",docs:{description:"Prevent unsafe COPY FROM usage with dynamic file paths, which can lead to arbitrary file read/RCE.",url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/no-unsafe-copy-from.md",cwe:"CWE-73",cvss:9.5},messages:{dynamicPath:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"COPY FROM Injection",description:"Dynamic file path in COPY FROM detected - potential arbitrary file read.",severity:"CRITICAL",cwe:"CWE-73",owasp:"A03:2021",compliance:["SOC2","PCI-DSS"],effort:"low",fix:"Never use user input in COPY FROM paths. Use COPY FROM STDIN for user data.",documentationLink:"https://www.postgresql.org/docs/current/sql-copy.html"}),dynamicWritePath:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"COPY TO Injection",description:"Dynamic file path in COPY TO detected - potential arbitrary file write on the database server.",severity:"CRITICAL",cwe:"CWE-73",owasp:"A03:2021",compliance:["SOC2","PCI-DSS"],effort:"low",fix:"Never use user input in COPY TO paths. Use COPY TO STDOUT and write the file from the application.",documentationLink:"https://www.postgresql.org/docs/current/sql-copy.html"}),hardcodedPath:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.WARNING,issueName:"Server-side COPY FROM",description:"Hardcoded file path in COPY FROM - server-side file access.",severity:"MEDIUM",cwe:"CWE-73",effort:"low",fix:"Prefer COPY FROM STDIN for application code. Use allowHardcodedPaths option if this is an admin script.",documentationLink:"https://www.postgresql.org/docs/current/sql-copy.html"}),unverifiablePath:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.WARNING,issueName:"Unverifiable COPY FROM",description:"Cannot statically verify COPY FROM source - potential injection risk.",severity:"MEDIUM",cwe:"CWE-73",effort:"medium",fix:"Ensure the query source does not contain user input, or refactor to use COPY FROM STDIN.",documentationLink:"https://www.postgresql.org/docs/current/sql-copy.html"})},schema:[{type:"object",properties:{allowHardcodedPaths:{type:"boolean",description:"Allow hardcoded file paths (for admin/migration scripts)"},allowedPaths:{type:"array",items:{type:"string"},description:"List of allowed file path patterns (regex strings)"}},additionalProperties:false}]},defaultOptions:[{}],createOnce(context){let allowHardcodedPaths;let allowedPaths;const isPathAllowed=filePath=>allowedPaths.some(pattern=>pattern.test(filePath));return{before(){if(!(0,utils_1.usesPostgres)(context))return false;const options=context.options[0]??{};allowHardcodedPaths=options.allowHardcodedPaths??false;allowedPaths=(options.allowedPaths??[]).map(p=>new RegExp(p));return true},CallExpression(node){if(node.callee.type!==eslint_devkit_2.AST_NODE_TYPES.MemberExpression||!SQL_SINK_METHODS.has((0,eslint_devkit_2.propertyName)(node.callee))){return}const[queryArg]=node.arguments;if(queryArg===void 0||queryArg.type===eslint_devkit_2.AST_NODE_TYPES.SpreadElement){return}const scope=context.sourceCode.getScope(node);const expression=effectiveExpression(queryArg,scope);const copies=copyClauses(staticText(expression));if(copies.some(copy=>copy.direction==="to"&&!TO_STDOUT.test(copy.clause))&&hasDynamicPart(expression,scope)){context.report({node:queryArg,messageId:"dynamicWritePath"});return}const source=copies.find(copy=>copy.direction==="from")?.clause;if(source===void 0)return;if(FROM_STDIN.test(source))return;if(hasDynamicPart(expression,scope)){context.report({node:queryArg,messageId:"dynamicPath"});return}const filePath=QUOTED_SOURCE.exec(`from ${source}`);if(filePath!==null&&isPathAllowed(filePath[1]))return;if(allowHardcodedPaths)return;context.report({node:queryArg,messageId:"hardcodedPath"})}}}});
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noUnsafeCopyFrom=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");const utils_1=require("../../utils");const sql_scan_1=require("../../utils/sql-scan");const SQL_SINK_METHODS=new Set(["query","execute"]);const QUOTED_SOURCE=/from\s+['"]([^'"]+)['"]/i;const FROM_STDIN=/^\s*stdin\b/i;const TO_STDOUT=/^\s*stdout\b/i;const MAX_RESOLUTION_DEPTH=4;function staticText(node){if(node.type===eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral){return node.quasis.map(q=>q.value.cooked??q.value.raw).join("")}if(node.type===eslint_devkit_2.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"){return`${staticText(node.left)}${staticText(node.right)}`}return(0,eslint_devkit_2.staticString)(node)??""}function copyClauses(text){const found=[];const stripped=(0,sql_scan_1.stripComments)(text);for(const statement of stripped.split(";")){const head=/^\s*copy\b/i.exec(statement);if(head===null)continue;let index=head[0].length;while(index<statement.length&&/\s/.test(statement[index]))index+=1;if(statement[index]==="("){let depth=0;for(;index<statement.length;index+=1){if(statement[index]==="(")depth+=1;else if(statement[index]===")"){depth-=1;if(depth===0){index+=1;break}}}}const direction=/\b(from|to)\b/i.exec(statement.slice(index));if(direction===null)continue;found.push({direction:direction[1].toLowerCase(),clause:statement.slice(index+direction.index+direction[1].length)})}return found}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 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_2.AST_NODE_TYPES.ArrowFunctionExpression&&init.type!==eslint_devkit_2.AST_NODE_TYPES.FunctionExpression){return null}return init}function returnedExpression(body){if(body.type===eslint_devkit_2.AST_NODE_TYPES.BlockStatement){const[only]=body.body;if(body.body.length!==1||only.type!==eslint_devkit_2.AST_NODE_TYPES.ReturnStatement){return null}return only.argument===null?null:returnedExpression(only.argument)}return body.type===eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral||body.type===eslint_devkit_2.AST_NODE_TYPES.BinaryExpression||(0,eslint_devkit_2.staticString)(body)!==null?body:null}function isStringRawTag(node){const{tag}=node;return tag.type===eslint_devkit_2.AST_NODE_TYPES.MemberExpression&&tag.object.type===eslint_devkit_2.AST_NODE_TYPES.Identifier&&tag.object.name==="String"&&(0,eslint_devkit_2.propertyName)(tag)==="raw"}function singleAssignedInit(variable){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 effectiveExpression(node,scope,depth=0){if(depth>=MAX_RESOLUTION_DEPTH)return node;if(node.type===eslint_devkit_2.AST_NODE_TYPES.TaggedTemplateExpression){return isStringRawTag(node)?node.quasi:node}if(node.type===eslint_devkit_2.AST_NODE_TYPES.Identifier){const variable=resolveVariable(node.name,scope);const init=variable===null?null:singleAssignedInit(variable);return init===null?node:effectiveExpression(init,scope,depth+1)}if(node.type===eslint_devkit_2.AST_NODE_TYPES.CallExpression){if(node.callee.type!==eslint_devkit_2.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:effectiveExpression(returned,scope,depth+1)}return node}function hasDynamicPart(node,scope){if(node.type===eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral){return node.expressions.some(part=>hasDynamicPart(part,scope))}if(node.type===eslint_devkit_2.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"){return hasDynamicPart(node.left,scope)||hasDynamicPart(node.right,scope)}return!(0,eslint_devkit_2.isStaticExpression)({node,scope})}exports.noUnsafeCopyFrom=eslint_devkit_1.RuleCreator.withoutDocs({meta:{interop:{runtimes:["eslint","oxlint"]},type:"problem",docs:{description:"Prevent unsafe COPY FROM usage with dynamic file paths, which can lead to arbitrary file read/RCE.",url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/no-unsafe-copy-from.md",cwe:"CWE-73",cvss:9.5},messages:{dynamicPath:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"COPY FROM Injection",description:"Dynamic file path in COPY FROM detected - potential arbitrary file read.",severity:"CRITICAL",cwe:"CWE-73",owasp:"A03:2021",compliance:["SOC2","PCI-DSS"],effort:"low",fix:"Never use user input in COPY FROM paths. Use COPY FROM STDIN for user data.",documentationLink:"https://www.postgresql.org/docs/current/sql-copy.html"}),dynamicWritePath:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"COPY TO Injection",description:"Dynamic file path in COPY TO detected - potential arbitrary file write on the database server.",severity:"CRITICAL",cwe:"CWE-73",owasp:"A03:2021",compliance:["SOC2","PCI-DSS"],effort:"low",fix:"Never use user input in COPY TO paths. Use COPY TO STDOUT and write the file from the application.",documentationLink:"https://www.postgresql.org/docs/current/sql-copy.html"}),hardcodedPath:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.WARNING,issueName:"Server-side COPY FROM",description:"Hardcoded file path in COPY FROM - server-side file access.",severity:"MEDIUM",cwe:"CWE-73",effort:"low",fix:"Prefer COPY FROM STDIN for application code. Use allowHardcodedPaths option if this is an admin script.",documentationLink:"https://www.postgresql.org/docs/current/sql-copy.html"}),unverifiablePath:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.WARNING,issueName:"Unverifiable COPY FROM",description:"Cannot statically verify COPY FROM source - potential injection risk.",severity:"MEDIUM",cwe:"CWE-73",effort:"medium",fix:"Ensure the query source does not contain user input, or refactor to use COPY FROM STDIN.",documentationLink:"https://www.postgresql.org/docs/current/sql-copy.html"})},schema:[{type:"object",properties:{allowHardcodedPaths:{type:"boolean",description:"Allow hardcoded file paths (for admin/migration scripts)"},allowedPaths:{type:"array",items:{type:"string"},description:"List of allowed file path patterns (regex strings)"}},additionalProperties:false}]},defaultOptions:[{}],createOnce(context){let allowHardcodedPaths;let allowedPaths;const isPathAllowed=filePath=>allowedPaths.some(pattern=>pattern.test(filePath));return{before(){if(!(0,utils_1.usesPostgres)(context))return false;const options=context.options[0]??{};allowHardcodedPaths=options.allowHardcodedPaths??false;allowedPaths=(options.allowedPaths??[]).map(p=>new RegExp(p));return true},CallExpression(node){if(node.callee.type!==eslint_devkit_2.AST_NODE_TYPES.MemberExpression||!(0,eslint_devkit_2.namesOneOf)((0,eslint_devkit_2.propertyName)(node.callee),SQL_SINK_METHODS)){return}const[queryArg]=node.arguments;if(queryArg===void 0||queryArg.type===eslint_devkit_2.AST_NODE_TYPES.SpreadElement){return}const scope=context.sourceCode.getScope(node);const expression=effectiveExpression(queryArg,scope);const copies=copyClauses(staticText(expression));if(copies.some(copy=>copy.direction==="to"&&!TO_STDOUT.test(copy.clause))&&hasDynamicPart(expression,scope)){context.report({node:queryArg,messageId:"dynamicWritePath"});return}const source=copies.find(copy=>copy.direction==="from")?.clause;if(source===void 0)return;if(FROM_STDIN.test(source))return;if(hasDynamicPart(expression,scope)){context.report({node:queryArg,messageId:"dynamicPath"});return}const filePath=QUOTED_SOURCE.exec(`from ${source}`);if(filePath!==null&&isPathAllowed(filePath[1]))return;if(allowHardcodedPaths)return;context.report({node:queryArg,messageId:"hardcodedPath"})}}}});
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noUnsafeSearchPath=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");const utils_1=require("../../utils");const connection_config_1=require("../../utils/connection-config");const cross_file_1=require("../../utils/cross-file");const sql_scan_1=require("../../utils/sql-scan");const SQL_SINK_METHODS=new Set(["query","execute"]);const SEARCH_PATH_STATEMENT=/^\s*set\s+(?:local\s+|session\s+)?search_path\b/i;const SET_CONFIG_SEARCH_PATH=/\bset_config\s*\(\s*'search_path'/i;const SEARCH_PATH_OPTION=/(?:^|\s)-c\s*search_path\s*=/i;function configProperty(object,name){const found=object.properties.find(p=>p.type===eslint_devkit_2.AST_NODE_TYPES.Property&&(0,eslint_devkit_2.objectKeyName)(p)===name);return found?.value}const MAX_RESOLUTION_DEPTH=4;function staticText(node){if(node.type===eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral){return node.quasis.map(q=>q.value.cooked??q.value.raw).join("")}if(node.type===eslint_devkit_2.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"){return`${staticText(node.left)}${staticText(node.right)}`}return(0,eslint_devkit_2.staticString)(node)??""}function setsSearchPath(text){return(0,sql_scan_1.stripComments)(text).split(";").some(statement=>SEARCH_PATH_STATEMENT.test(statement))}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 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_2.AST_NODE_TYPES.ArrowFunctionExpression&&init.type!==eslint_devkit_2.AST_NODE_TYPES.FunctionExpression){return null}return init}function returnedExpression(body){if(body.type===eslint_devkit_2.AST_NODE_TYPES.BlockStatement){const[only]=body.body;if(body.body.length!==1||only.type!==eslint_devkit_2.AST_NODE_TYPES.ReturnStatement){return null}return only.argument===null?null:returnedExpression(only.argument)}return body.type===eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral||body.type===eslint_devkit_2.AST_NODE_TYPES.BinaryExpression||(0,eslint_devkit_2.staticString)(body)!==null?body:null}function isStringRawTag(node){const{tag}=node;return tag.type===eslint_devkit_2.AST_NODE_TYPES.MemberExpression&&tag.object.type===eslint_devkit_2.AST_NODE_TYPES.Identifier&&tag.object.name==="String"&&(0,eslint_devkit_2.propertyName)(tag)==="raw"}function singleAssignedInit(variable){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;const init=def.node.init;return init??null}function effectiveExpression(node,scope,depth=0){if(depth>=MAX_RESOLUTION_DEPTH)return node;if(node.type===eslint_devkit_2.AST_NODE_TYPES.TaggedTemplateExpression){return isStringRawTag(node)?node.quasi:node}if(node.type===eslint_devkit_2.AST_NODE_TYPES.Identifier){const variable=resolveVariable(node.name,scope);const init=variable===null?null:singleAssignedInit(variable);return init===null?node:effectiveExpression(init,scope,depth+1)}if(node.type===eslint_devkit_2.AST_NODE_TYPES.CallExpression){if(node.callee.type!==eslint_devkit_2.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:effectiveExpression(returned,scope,depth+1)}return node}function exitsUnconditionally(node){if(node.type===eslint_devkit_2.AST_NODE_TYPES.ThrowStatement||node.type===eslint_devkit_2.AST_NODE_TYPES.ReturnStatement){return true}if(node.type===eslint_devkit_2.AST_NODE_TYPES.BlockStatement){return node.body.some(exitsUnconditionally)}return false}function isAssertionAbout(call,index,scope){if(call.callee.type!==eslint_devkit_2.AST_NODE_TYPES.Identifier)return false;const variable=resolveVariable(call.callee.name,scope);const fn=variable===null?null:functionImplementation(variable);const predicate=fn?.returnType?.typeAnnotation;const param=fn?.params[index];return predicate?.type===eslint_devkit_2.AST_NODE_TYPES.TSTypePredicate&&predicate.asserts&&predicate.parameterName.type===eslint_devkit_2.AST_NODE_TYPES.Identifier&&param?.type===eslint_devkit_2.AST_NODE_TYPES.Identifier&&param.name===predicate.parameterName.name}function isAllowlistGuarded(variable,sink){return variable.references.some(ref=>{const call=ref.identifier.parent;if(call?.type===eslint_devkit_2.AST_NODE_TYPES.CallExpression&&call.range[1]<=sink.range[0]&&isAssertionAbout(call,call.arguments.indexOf(ref.identifier),ref.from)){return true}for(let current=ref.identifier;current!==void 0&&current!==null;current=current.parent){const parent=current.parent;if(parent!==void 0&&parent!==null&&parent.type===eslint_devkit_2.AST_NODE_TYPES.IfStatement&&parent.test===current){return parent.range[1]<=sink.range[0]&&exitsUnconditionally(parent.consequent)}}return false})}function isRawPart(part,scope,sink){if(part.type===eslint_devkit_2.AST_NODE_TYPES.BinaryExpression||part.type===eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral){return hasRawPart(part,scope,sink)}if((0,eslint_devkit_2.isStaticExpression)({node:part,scope}))return false;if(part.type===eslint_devkit_2.AST_NODE_TYPES.Identifier){const variable=resolveVariable(part.name,scope);if(variable!==null&&isAllowlistGuarded(variable,sink))return false}return true}function hasRawPart(node,scope,sink){const parts=node.type===eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral?[...node.expressions]:[node.left,node.right];return parts.some(part=>isRawPart(part,scope,sink))}exports.noUnsafeSearchPath=eslint_devkit_1.RuleCreator.withoutDocs({meta:{interop:{runtimes:["eslint","oxlint"]},type:"problem",docs:{description:"Prevent unsafe SET search_path usage with dynamic values, which can lead to schema hijacking.",url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/no-unsafe-search-path.md",cwe:"CWE-426",cvss:7.5},messages:{noUnsafeSearchPath:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"Schema Hijacking Risk",description:'Unsafe "SET search_path" detected.',severity:"CRITICAL",cwe:"CWE-426",owasp:"A05:2021",compliance:["SOC2","PCI-DSS"],effort:"low",fix:"Do not use dynamic values for search_path. Use static strings or strict validation.",documentationLink:"https://www.postgresql.org/docs/current/ddl-schemas.html#DDL-SCHEMAS-PATH"})},schema:[]},defaultOptions:[],createOnce(context){return{before(){if(!(0,utils_1.usesPostgres)(context))return false;return true},CallExpression(node){if(node.callee.type!==eslint_devkit_2.AST_NODE_TYPES.MemberExpression||!SQL_SINK_METHODS.has((0,eslint_devkit_2.propertyName)(node.callee))){return}const[queryArg]=node.arguments;if(queryArg===void 0||queryArg.type===eslint_devkit_2.AST_NODE_TYPES.SpreadElement){return}const scope=context.sourceCode.getScope(node);const config=queryArg.type===eslint_devkit_2.AST_NODE_TYPES.ObjectExpression?queryArg:null;const textNode=config===null?queryArg:configProperty(config,"text");const valuesNode=config===null?node.arguments[1]:configProperty(config,"values");if(textNode!==void 0&&SET_CONFIG_SEARCH_PATH.test((0,sql_scan_1.stripComments)(staticText(textNode)))&&valuesNode?.type===eslint_devkit_2.AST_NODE_TYPES.ArrayExpression&&valuesNode.elements.some(element=>element!==null&&(element.type===eslint_devkit_2.AST_NODE_TYPES.SpreadElement||isRawPart(element,scope,node)))){context.report({node:queryArg,messageId:"noUnsafeSearchPath"});return}const expression=effectiveExpression(queryArg,scope);if(expression.type===eslint_devkit_2.AST_NODE_TYPES.CallExpression){const statement=expression.arguments.find(arg=>arg.type!==eslint_devkit_2.AST_NODE_TYPES.SpreadElement&&setsSearchPath(staticText(arg)));if(statement===void 0)return;const dynamic=expression.arguments.some(arg=>arg.type===eslint_devkit_2.AST_NODE_TYPES.SpreadElement||!(0,eslint_devkit_2.isStaticExpression)({node:arg,scope}));if(dynamic){context.report({node:queryArg,messageId:"noUnsafeSearchPath"})}return}if(expression.type!==eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral&&!(expression.type===eslint_devkit_2.AST_NODE_TYPES.BinaryExpression&&expression.operator==="+")){return}if(!setsSearchPath(staticText(expression)))return;if(!hasRawPart(expression,scope,node))return;context.report({node:queryArg,messageId:"noUnsafeSearchPath"})},NewExpression(node){const scope=context.sourceCode.getScope(node);for(const argument of(0,connection_config_1.connectionConfigArguments)(node,scope)){const config=(0,cross_file_1.follow)({node:argument,scope,env:(0,cross_file_1.envOf)(context)}).node;if(config.type!==eslint_devkit_2.AST_NODE_TYPES.ObjectExpression)continue;const options=configProperty(config,"options");if(options!==void 0&&(options.type===eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral||options.type===eslint_devkit_2.AST_NODE_TYPES.BinaryExpression)&&SEARCH_PATH_OPTION.test(staticText(options))&&hasRawPart(options,scope,node)){context.report({node:options,messageId:"noUnsafeSearchPath"})}}}}}});
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noUnsafeSearchPath=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");const utils_1=require("../../utils");const connection_config_1=require("../../utils/connection-config");const cross_file_1=require("../../utils/cross-file");const sql_scan_1=require("../../utils/sql-scan");const SQL_SINK_METHODS=new Set(["query","execute"]);const SEARCH_PATH_STATEMENT=/^\s*set\s+(?:local\s+|session\s+)?search_path\b/i;const SET_CONFIG_SEARCH_PATH=/\bset_config\s*\(\s*'search_path'/i;const SEARCH_PATH_OPTION=/(?:^|\s)-c\s*search_path\s*=/i;function configProperty(object,name){const found=object.properties.find(p=>p.type===eslint_devkit_2.AST_NODE_TYPES.Property&&(0,eslint_devkit_2.objectKeyName)(p)===name);return found?.value}const MAX_RESOLUTION_DEPTH=4;function staticText(node){if(node.type===eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral){return node.quasis.map(q=>q.value.cooked??q.value.raw).join("")}if(node.type===eslint_devkit_2.AST_NODE_TYPES.BinaryExpression&&node.operator==="+"){return`${staticText(node.left)}${staticText(node.right)}`}return(0,eslint_devkit_2.staticString)(node)??""}function setsSearchPath(text){return(0,sql_scan_1.stripComments)(text).split(";").some(statement=>SEARCH_PATH_STATEMENT.test(statement))}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 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_2.AST_NODE_TYPES.ArrowFunctionExpression&&init.type!==eslint_devkit_2.AST_NODE_TYPES.FunctionExpression){return null}return init}function returnedExpression(body){if(body.type===eslint_devkit_2.AST_NODE_TYPES.BlockStatement){const[only]=body.body;if(body.body.length!==1||only.type!==eslint_devkit_2.AST_NODE_TYPES.ReturnStatement){return null}return only.argument===null?null:returnedExpression(only.argument)}return body.type===eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral||body.type===eslint_devkit_2.AST_NODE_TYPES.BinaryExpression||(0,eslint_devkit_2.staticString)(body)!==null?body:null}function isStringRawTag(node){const{tag}=node;return tag.type===eslint_devkit_2.AST_NODE_TYPES.MemberExpression&&tag.object.type===eslint_devkit_2.AST_NODE_TYPES.Identifier&&tag.object.name==="String"&&(0,eslint_devkit_2.propertyName)(tag)==="raw"}function singleAssignedInit(variable){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;const init=def.node.init;return init??null}function effectiveExpression(node,scope,depth=0){if(depth>=MAX_RESOLUTION_DEPTH)return node;if(node.type===eslint_devkit_2.AST_NODE_TYPES.TaggedTemplateExpression){return isStringRawTag(node)?node.quasi:node}if(node.type===eslint_devkit_2.AST_NODE_TYPES.Identifier){const variable=resolveVariable(node.name,scope);const init=variable===null?null:singleAssignedInit(variable);return init===null?node:effectiveExpression(init,scope,depth+1)}if(node.type===eslint_devkit_2.AST_NODE_TYPES.CallExpression){if(node.callee.type!==eslint_devkit_2.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:effectiveExpression(returned,scope,depth+1)}return node}function exitsUnconditionally(node){if(node.type===eslint_devkit_2.AST_NODE_TYPES.ThrowStatement||node.type===eslint_devkit_2.AST_NODE_TYPES.ReturnStatement){return true}if(node.type===eslint_devkit_2.AST_NODE_TYPES.BlockStatement){return node.body.some(exitsUnconditionally)}return false}function isAssertionAbout(call,index,scope){if(call.callee.type!==eslint_devkit_2.AST_NODE_TYPES.Identifier)return false;const variable=resolveVariable(call.callee.name,scope);const fn=variable===null?null:functionImplementation(variable);const predicate=fn?.returnType?.typeAnnotation;const param=fn?.params[index];return predicate?.type===eslint_devkit_2.AST_NODE_TYPES.TSTypePredicate&&predicate.asserts&&predicate.parameterName.type===eslint_devkit_2.AST_NODE_TYPES.Identifier&&param?.type===eslint_devkit_2.AST_NODE_TYPES.Identifier&&param.name===predicate.parameterName.name}function isAllowlistGuarded(variable,sink){return variable.references.some(ref=>{const call=ref.identifier.parent;if(call?.type===eslint_devkit_2.AST_NODE_TYPES.CallExpression&&call.range[1]<=sink.range[0]&&isAssertionAbout(call,call.arguments.indexOf(ref.identifier),ref.from)){return true}for(let current=ref.identifier;current!==void 0&&current!==null;current=current.parent){const parent=current.parent;if(parent!==void 0&&parent!==null&&parent.type===eslint_devkit_2.AST_NODE_TYPES.IfStatement&&parent.test===current){return parent.range[1]<=sink.range[0]&&exitsUnconditionally(parent.consequent)}}return false})}function isRawPart(part,scope,sink){if(part.type===eslint_devkit_2.AST_NODE_TYPES.BinaryExpression||part.type===eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral){return hasRawPart(part,scope,sink)}if((0,eslint_devkit_2.isStaticExpression)({node:part,scope}))return false;if(part.type===eslint_devkit_2.AST_NODE_TYPES.Identifier){const variable=resolveVariable(part.name,scope);if(variable!==null&&isAllowlistGuarded(variable,sink))return false}return true}function hasRawPart(node,scope,sink){const parts=node.type===eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral?[...node.expressions]:[node.left,node.right];return parts.some(part=>isRawPart(part,scope,sink))}exports.noUnsafeSearchPath=eslint_devkit_1.RuleCreator.withoutDocs({meta:{interop:{runtimes:["eslint","oxlint"]},type:"problem",docs:{description:"Prevent unsafe SET search_path usage with dynamic values, which can lead to schema hijacking.",url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/no-unsafe-search-path.md",cwe:"CWE-426",cvss:7.5},messages:{noUnsafeSearchPath:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"Schema Hijacking Risk",description:'Unsafe "SET search_path" detected.',severity:"CRITICAL",cwe:"CWE-426",owasp:"A05:2021",compliance:["SOC2","PCI-DSS"],effort:"low",fix:"Do not use dynamic values for search_path. Use static strings or strict validation.",documentationLink:"https://www.postgresql.org/docs/current/ddl-schemas.html#DDL-SCHEMAS-PATH"})},schema:[]},defaultOptions:[],createOnce(context){return{before(){if(!(0,utils_1.usesPostgres)(context))return false;return true},CallExpression(node){if(node.callee.type!==eslint_devkit_2.AST_NODE_TYPES.MemberExpression||!(0,eslint_devkit_2.namesOneOf)((0,eslint_devkit_2.propertyName)(node.callee),SQL_SINK_METHODS)){return}const[queryArg]=node.arguments;if(queryArg===void 0||queryArg.type===eslint_devkit_2.AST_NODE_TYPES.SpreadElement){return}const scope=context.sourceCode.getScope(node);const config=queryArg.type===eslint_devkit_2.AST_NODE_TYPES.ObjectExpression?queryArg:null;const textNode=config===null?queryArg:configProperty(config,"text");const valuesNode=config===null?node.arguments[1]:configProperty(config,"values");if(textNode!==void 0&&SET_CONFIG_SEARCH_PATH.test((0,sql_scan_1.stripComments)(staticText(textNode)))&&valuesNode?.type===eslint_devkit_2.AST_NODE_TYPES.ArrayExpression&&valuesNode.elements.some(element=>element!==null&&(element.type===eslint_devkit_2.AST_NODE_TYPES.SpreadElement||isRawPart(element,scope,node)))){context.report({node:queryArg,messageId:"noUnsafeSearchPath"});return}const expression=effectiveExpression(queryArg,scope);if(expression.type===eslint_devkit_2.AST_NODE_TYPES.CallExpression){const statement=expression.arguments.find(arg=>arg.type!==eslint_devkit_2.AST_NODE_TYPES.SpreadElement&&setsSearchPath(staticText(arg)));if(statement===void 0)return;const dynamic=expression.arguments.some(arg=>arg.type===eslint_devkit_2.AST_NODE_TYPES.SpreadElement||!(0,eslint_devkit_2.isStaticExpression)({node:arg,scope}));if(dynamic){context.report({node:queryArg,messageId:"noUnsafeSearchPath"})}return}if(expression.type!==eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral&&!(expression.type===eslint_devkit_2.AST_NODE_TYPES.BinaryExpression&&expression.operator==="+")){return}if(!setsSearchPath(staticText(expression)))return;if(!hasRawPart(expression,scope,node))return;context.report({node:queryArg,messageId:"noUnsafeSearchPath"})},NewExpression(node){const scope=context.sourceCode.getScope(node);for(const argument of(0,connection_config_1.connectionConfigArguments)(node,scope)){const config=(0,cross_file_1.follow)({node:argument,scope,env:(0,cross_file_1.envOf)(context)}).node;if(config.type!==eslint_devkit_2.AST_NODE_TYPES.ObjectExpression)continue;const options=configProperty(config,"options");if(options!==void 0&&(options.type===eslint_devkit_2.AST_NODE_TYPES.TemplateLiteral||options.type===eslint_devkit_2.AST_NODE_TYPES.BinaryExpression)&&SEARCH_PATH_OPTION.test(staticText(options))&&hasRawPart(options,scope,node)){context.report({node:options,messageId:"noUnsafeSearchPath"})}}}}}});