@interlace/eslint-devkit 1.7.0 → 1.9.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.
@@ -4,6 +4,34 @@ exports.createSqlInjectionRule = createSqlInjectionRule;
4
4
  const ast_node_types_1 = require("../ast-node-types");
5
5
  const messaging_1 = require("../messaging");
6
6
  const SQL_KEYWORDS = /\b(?:select|insert|update|delete|drop|alter|truncate|union|from|where|values|set)\b/i;
7
+ const FUNCTION_TYPES = new Set([
8
+ ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration,
9
+ ast_node_types_1.AST_NODE_TYPES.FunctionExpression,
10
+ ast_node_types_1.AST_NODE_TYPES.ArrowFunctionExpression,
11
+ ]);
12
+ function enclosingFunction(node) {
13
+ for (let n = node.parent; n; n = n.parent) {
14
+ if (FUNCTION_TYPES.has(n.type))
15
+ return n;
16
+ }
17
+ return undefined;
18
+ }
19
+ function callableName(fn) {
20
+ if (fn.type === ast_node_types_1.AST_NODE_TYPES.FunctionDeclaration && fn.id)
21
+ return fn.id.name;
22
+ const parent = fn.parent;
23
+ if (parent.type === ast_node_types_1.AST_NODE_TYPES.VariableDeclarator &&
24
+ parent.id.type === ast_node_types_1.AST_NODE_TYPES.Identifier) {
25
+ return parent.id.name;
26
+ }
27
+ if ((parent.type === ast_node_types_1.AST_NODE_TYPES.MethodDefinition ||
28
+ parent.type === ast_node_types_1.AST_NODE_TYPES.PropertyDefinition ||
29
+ parent.type === ast_node_types_1.AST_NODE_TYPES.Property) &&
30
+ parent.key.type === ast_node_types_1.AST_NODE_TYPES.Identifier) {
31
+ return parent.key.name;
32
+ }
33
+ return undefined;
34
+ }
7
35
  function staticText(node) {
8
36
  if (node.type === ast_node_types_1.AST_NODE_TYPES.TemplateLiteral) {
9
37
  return node.quasis.map((q) => q.value.raw).join(' ');
@@ -21,7 +49,8 @@ function classify(node, requireSqlKeywords) {
21
49
  if (node.type === ast_node_types_1.AST_NODE_TYPES.BinaryExpression && node.operator === '+') {
22
50
  kind = 'concat';
23
51
  }
24
- if (node.type === ast_node_types_1.AST_NODE_TYPES.TemplateLiteral && node.expressions.length > 0) {
52
+ if (node.type === ast_node_types_1.AST_NODE_TYPES.TemplateLiteral &&
53
+ node.expressions.length > 0) {
25
54
  kind = 'template';
26
55
  }
27
56
  if (kind === false)
@@ -56,6 +85,10 @@ function createSqlInjectionRule(config) {
56
85
  issueName: 'SQL Injection Risk',
57
86
  description: 'Unsafe SQL query construction detected (template literal).',
58
87
  severity: 'CRITICAL',
88
+ cwe: config.meta.docs.cwe,
89
+ owasp: 'A03:2021',
90
+ compliance: ['SOC2', 'PCI-DSS', 'NIST-CSF'],
91
+ effort: 'high',
59
92
  fix: config.fix,
60
93
  documentationLink: config.documentationLink,
61
94
  }),
@@ -66,6 +99,10 @@ function createSqlInjectionRule(config) {
66
99
  create(context) {
67
100
  const tainted = new Map();
68
101
  const sqlish = new Set();
102
+ const wrappers = new Map();
103
+ const definitions = new Map();
104
+ const wrapperFns = new Map();
105
+ const pending = [];
69
106
  const report = (node, kind) => {
70
107
  context.report({
71
108
  node,
@@ -73,6 +110,11 @@ function createSqlInjectionRule(config) {
73
110
  });
74
111
  };
75
112
  return {
113
+ 'FunctionDeclaration, FunctionExpression, ArrowFunctionExpression'(node) {
114
+ const name = callableName(node);
115
+ if (name !== undefined)
116
+ definitions.set(name, (definitions.get(name) ?? 0) + 1);
117
+ },
76
118
  VariableDeclarator(node) {
77
119
  if (node.id.type === ast_node_types_1.AST_NODE_TYPES.Identifier && node.init) {
78
120
  const kind = classify(node.init, config.requireSqlKeywords);
@@ -83,22 +125,52 @@ function createSqlInjectionRule(config) {
83
125
  }
84
126
  },
85
127
  AssignmentExpression(node) {
86
- if (node.operator === '+=' && node.left.type === ast_node_types_1.AST_NODE_TYPES.Identifier) {
128
+ if (node.operator === '+=' &&
129
+ node.left.type === ast_node_types_1.AST_NODE_TYPES.Identifier) {
87
130
  const gate = config.requireSqlKeywords && !sqlish.has(node.left.name);
88
131
  const kind = classify(node.right, gate);
89
132
  if (kind)
90
133
  tainted.set(node.left.name, kind);
134
+ if (SQL_KEYWORDS.test(staticText(node.right)))
135
+ sqlish.add(node.left.name);
91
136
  }
92
137
  },
93
138
  CallExpression(node) {
94
- if (node.callee.type !== ast_node_types_1.AST_NODE_TYPES.MemberExpression ||
95
- node.callee.property.type !== ast_node_types_1.AST_NODE_TYPES.Identifier ||
96
- !sinks.has(node.callee.property.name)) {
139
+ const isMember = node.callee.type === ast_node_types_1.AST_NODE_TYPES.MemberExpression &&
140
+ node.callee.property.type === ast_node_types_1.AST_NODE_TYPES.Identifier;
141
+ const calleeName = isMember
142
+ ? node.callee
143
+ .property.name
144
+ : node.callee.type === ast_node_types_1.AST_NODE_TYPES.Identifier
145
+ ? node.callee.name
146
+ : undefined;
147
+ if (calleeName === undefined)
148
+ return;
149
+ if (!isMember || !sinks.has(calleeName)) {
150
+ for (const [index, arg] of node.arguments.entries()) {
151
+ if (arg.type === ast_node_types_1.AST_NODE_TYPES.SpreadElement)
152
+ continue;
153
+ const kind = classify(arg, true);
154
+ if (kind)
155
+ pending.push({ name: calleeName, index, arg, kind });
156
+ else if (arg.type === ast_node_types_1.AST_NODE_TYPES.Identifier)
157
+ pending.push({ name: calleeName, index, arg, kind: undefined });
158
+ }
97
159
  return;
98
160
  }
99
161
  const queryArg = node.arguments[0];
100
162
  if (!queryArg)
101
163
  return;
164
+ if (queryArg.type === ast_node_types_1.AST_NODE_TYPES.Identifier) {
165
+ const fn = enclosingFunction(node);
166
+ const index = fn?.params.findIndex((p) => p.type === ast_node_types_1.AST_NODE_TYPES.Identifier &&
167
+ p.name === queryArg.name) ?? -1;
168
+ const name = fn && index >= 0 ? callableName(fn) : undefined;
169
+ if (name !== undefined && fn) {
170
+ wrappers.set(name, index);
171
+ wrapperFns.set(fn, name);
172
+ }
173
+ }
102
174
  const direct = classify(queryArg, config.requireSqlKeywords);
103
175
  if (direct) {
104
176
  report(queryArg, direct);
@@ -110,6 +182,25 @@ function createSqlInjectionRule(config) {
110
182
  report(queryArg, taint);
111
183
  }
112
184
  },
185
+ 'Program:exit'() {
186
+ const proven = new Map();
187
+ for (const name of wrapperFns.values())
188
+ proven.set(name, (proven.get(name) ?? 0) + 1);
189
+ for (const { name, index, arg, kind } of pending) {
190
+ if (wrappers.get(name) !== index)
191
+ continue;
192
+ if (definitions.get(name) !== proven.get(name))
193
+ continue;
194
+ if (kind) {
195
+ report(arg, kind);
196
+ continue;
197
+ }
198
+ const varName = arg.name;
199
+ const taint = tainted.get(varName);
200
+ if (taint && sqlish.has(varName))
201
+ report(arg, taint);
202
+ }
203
+ },
113
204
  };
114
205
  },
115
206
  };
@@ -1,4 +1,17 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./security-utils"), exports);
17
+ __exportStar(require("./security-utils"), exports);
@@ -1,5 +1,18 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./type-utils"), exports);
17
+ __exportStar(require("./type-utils"), exports);
5
18
  require("./meta-augmentation");