@interlace/eslint-devkit 1.4.4 → 1.5.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/CHANGELOG.md +28 -0
- package/package.json +2 -2
- package/src/rule-creation/index.d.ts +1 -0
- package/src/rule-creation/index.js +3 -0
- package/src/rule-creation/mock-context.d.ts +5 -0
- package/src/rule-creation/mock-context.js +1 -0
- package/src/rule-creation/sql-injection-rule.d.ts +68 -0
- package/src/rule-creation/sql-injection-rule.js +168 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
## [1.4.0] - 2026-05-03
|
|
2
2
|
|
|
3
|
+
## 1.5.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
Extract the raw-SQL-injection detector (CWE-89) into
|
|
8
|
+
`@interlace/eslint-devkit` as `createSqlInjectionRule`, so every driver plugin
|
|
9
|
+
can instantiate it with its own sinks and remediation copy.
|
|
10
|
+
|
|
11
|
+
Background: scanning OWASP Juice Shop with the recommended presets of
|
|
12
|
+
`secure-coding`, `node-security`, `express-security` and `mongodb-security`
|
|
13
|
+
produced zero findings on its two flagship SQL injections
|
|
14
|
+
(`routes/search.ts`, `routes/login.ts` — both `sequelize.query()` template
|
|
15
|
+
literals). The detection was never the problem: `pg/no-unsafe-query` matches
|
|
16
|
+
any `.query()` member call and flags both correctly. The problem is
|
|
17
|
+
distribution — nobody on Sequelize installs the Postgres plugin.
|
|
18
|
+
|
|
19
|
+
The factory takes the sink list, a SQL-keyword precision gate, and the
|
|
20
|
+
remediation copy, which is everything that actually differs between drivers.
|
|
21
|
+
`pg/no-unsafe-query` is now an instantiation of it: same rule id, message
|
|
22
|
+
ids, sink and behaviour, and all 28 pre-existing rule tests pass untouched.
|
|
23
|
+
|
|
24
|
+
Also raises the timeout on the `no-deprecated-plugin-references` guard in
|
|
25
|
+
devkit. Both layers shell out to a repo-wide `grep`, which cannot finish
|
|
26
|
+
inside vitest's 5s default once the suite has enough test files running in
|
|
27
|
+
parallel — it failed as a timeout, not a violation.
|
|
28
|
+
|
|
29
|
+
Driver-scoped plugins that instantiate the factory ship separately.
|
|
30
|
+
|
|
3
31
|
## 1.4.4
|
|
4
32
|
|
|
5
33
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@interlace/eslint-devkit",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Toolkit for building ESLint plugins in TypeScript — AST helpers, type utilities, rule-creation scaffolding, security benchmarks, and SARIF output.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"homepage": "https://github.com/ofri-peretz/eslint/tree/main/packages/eslint-devkit#readme",
|
|
24
24
|
"repository": {
|
|
25
25
|
"type": "git",
|
|
26
|
-
"url": "https://github.com/ofri-peretz/eslint",
|
|
26
|
+
"url": "git+https://github.com/ofri-peretz/eslint.git",
|
|
27
27
|
"directory": "packages/eslint-devkit"
|
|
28
28
|
},
|
|
29
29
|
"bugs": {
|
|
@@ -9,3 +9,6 @@ const tslib_1 = require("tslib");
|
|
|
9
9
|
// Rule creation utilities for building ESLint rules
|
|
10
10
|
tslib_1.__exportStar(require("./rule-creator"), exports);
|
|
11
11
|
tslib_1.__exportStar(require("./mock-context"), exports);
|
|
12
|
+
// Shared CWE-89 detector — see sql-injection-rule.ts for why it lives here
|
|
13
|
+
// rather than inside a single database plugin.
|
|
14
|
+
tslib_1.__exportStar(require("./sql-injection-rule"), exports);
|
|
@@ -24,6 +24,11 @@ export interface MockContextOptions {
|
|
|
24
24
|
filename?: string;
|
|
25
25
|
/** Text returned by the `sourceCode.getText()` stub. */
|
|
26
26
|
sourceText?: string;
|
|
27
|
+
/**
|
|
28
|
+
* Program node exposed as `sourceCode.ast`. Defaults to an empty program —
|
|
29
|
+
* rules that pre-scan the file at `create()` time need this to exist.
|
|
30
|
+
*/
|
|
31
|
+
ast?: unknown;
|
|
27
32
|
}
|
|
28
33
|
export interface MockContextResult {
|
|
29
34
|
/** Visitor listeners returned by `rule.create(context)`. */
|
|
@@ -15,6 +15,7 @@ function createWithMockContext(rule, opts = {}) {
|
|
|
15
15
|
const filename = opts.filename ?? 'mock.ts';
|
|
16
16
|
const emptyScope = { variables: [], references: [], childScopes: [] };
|
|
17
17
|
const sourceCode = {
|
|
18
|
+
ast: opts.ast ?? { type: 'Program', body: [], tokens: [], comments: [] },
|
|
18
19
|
text: opts.sourceText ?? '',
|
|
19
20
|
getText: () => opts.sourceText ?? '',
|
|
20
21
|
getScope: () => emptyScope,
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Ofri Peretz
|
|
3
|
+
* Licensed under the MIT License. Use of this source code is governed by the
|
|
4
|
+
* MIT license that can be found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Shared raw-SQL-injection detector (CWE-89).
|
|
8
|
+
*
|
|
9
|
+
* The AST work — string concatenation or an interpolated template literal
|
|
10
|
+
* reaching a raw-SQL sink — is identical for every driver. What differs is
|
|
11
|
+
* the sink list and the remediation copy (`$1, $2` for Postgres, `?` for
|
|
12
|
+
* mysql2, `replacements` for Sequelize), so those are the parameters.
|
|
13
|
+
*
|
|
14
|
+
* This exists because the detection first shipped inside `eslint-plugin-pg`,
|
|
15
|
+
* where it works fine on `sequelize.query()` — but nobody on Sequelize,
|
|
16
|
+
* SQLite or MySQL installs the Postgres plugin, so textbook injections in
|
|
17
|
+
* those stacks went unreported. Each driver plugin now instantiates this
|
|
18
|
+
* factory, which keeps one implementation and one finding per line.
|
|
19
|
+
*
|
|
20
|
+
* The rule must stay in a driver-scoped plugin: it keys on driver method
|
|
21
|
+
* names, not language primitives, so it does not belong in the code-agnostic
|
|
22
|
+
* plugins (`secure-coding`, `node-security`, `browser-security`).
|
|
23
|
+
*/
|
|
24
|
+
import { TSESLint } from '@typescript-eslint/utils';
|
|
25
|
+
/** Message ids every rule built by this factory reports. */
|
|
26
|
+
export type SqlInjectionMessageIds = 'noUnsafeQuery' | 'unsafeTemplateLiteral';
|
|
27
|
+
export interface SqlInjectionRuleConfig {
|
|
28
|
+
/**
|
|
29
|
+
* The rule's own `meta.type` + `meta.docs`, spelled out by the caller.
|
|
30
|
+
*
|
|
31
|
+
* Deliberately NOT derived inside this factory: `scripts/audit-rule-meta-completeness.ts`
|
|
32
|
+
* reads rule metadata by *statically parsing the rule's source file*, so
|
|
33
|
+
* metadata hidden in here is invisible to it — a factory-built rule scores
|
|
34
|
+
* 0% and fails the strict gate. Keeping it at the call site also puts each
|
|
35
|
+
* driver's CWE/CVSS where a reader expects to find it.
|
|
36
|
+
*/
|
|
37
|
+
readonly meta: {
|
|
38
|
+
readonly type: 'problem';
|
|
39
|
+
readonly docs: {
|
|
40
|
+
readonly description: string;
|
|
41
|
+
readonly url: string;
|
|
42
|
+
readonly cwe: string;
|
|
43
|
+
readonly cvss: number;
|
|
44
|
+
readonly confidence: 'high' | 'medium' | 'low';
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
/** Method names treated as raw-SQL sinks, e.g. `['query']` or `['query', 'raw', 'execute']`. */
|
|
48
|
+
readonly methods: readonly string[];
|
|
49
|
+
/**
|
|
50
|
+
* Require SQL keywords in the *static* part of the string before reporting.
|
|
51
|
+
* Precision guard for broad sink lists (`.raw()`, `.execute()` are not
|
|
52
|
+
* SQL-only names). `false` keeps the historical pg behaviour: any
|
|
53
|
+
* interpolation into a sink is a finding.
|
|
54
|
+
*/
|
|
55
|
+
readonly requireSqlKeywords: boolean;
|
|
56
|
+
/** Remediation line in the emitted message. */
|
|
57
|
+
readonly fix: string;
|
|
58
|
+
/** Reference link in the emitted message. */
|
|
59
|
+
readonly documentationLink: string;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Build a CWE-89 rule for the given sinks and remediation copy.
|
|
63
|
+
*
|
|
64
|
+
* Detects three shapes: direct concatenation into a sink, direct
|
|
65
|
+
* interpolation into a sink, and a variable tainted by either (including via
|
|
66
|
+
* `+=`) that is later passed to a sink.
|
|
67
|
+
*/
|
|
68
|
+
export declare function createSqlInjectionRule(config: SqlInjectionRuleConfig): TSESLint.RuleModule<SqlInjectionMessageIds, []>;
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
+
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
+
* MIT license that can be found in the LICENSE file.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.createSqlInjectionRule = createSqlInjectionRule;
|
|
9
|
+
/**
|
|
10
|
+
* Shared raw-SQL-injection detector (CWE-89).
|
|
11
|
+
*
|
|
12
|
+
* The AST work — string concatenation or an interpolated template literal
|
|
13
|
+
* reaching a raw-SQL sink — is identical for every driver. What differs is
|
|
14
|
+
* the sink list and the remediation copy (`$1, $2` for Postgres, `?` for
|
|
15
|
+
* mysql2, `replacements` for Sequelize), so those are the parameters.
|
|
16
|
+
*
|
|
17
|
+
* This exists because the detection first shipped inside `eslint-plugin-pg`,
|
|
18
|
+
* where it works fine on `sequelize.query()` — but nobody on Sequelize,
|
|
19
|
+
* SQLite or MySQL installs the Postgres plugin, so textbook injections in
|
|
20
|
+
* those stacks went unreported. Each driver plugin now instantiates this
|
|
21
|
+
* factory, which keeps one implementation and one finding per line.
|
|
22
|
+
*
|
|
23
|
+
* The rule must stay in a driver-scoped plugin: it keys on driver method
|
|
24
|
+
* names, not language primitives, so it does not belong in the code-agnostic
|
|
25
|
+
* plugins (`secure-coding`, `node-security`, `browser-security`).
|
|
26
|
+
*/
|
|
27
|
+
const utils_1 = require("@typescript-eslint/utils");
|
|
28
|
+
const messaging_1 = require("../messaging");
|
|
29
|
+
const SQL_KEYWORDS = /\b(?:select|insert|update|delete|drop|alter|truncate|union|from|where|values|set)\b/i;
|
|
30
|
+
/** Literal text of a string expression, ignoring interpolated/concatenated values. */
|
|
31
|
+
function staticText(node) {
|
|
32
|
+
if (node.type === utils_1.AST_NODE_TYPES.TemplateLiteral) {
|
|
33
|
+
return node.quasis.map((q) => q.value.raw).join(' ');
|
|
34
|
+
}
|
|
35
|
+
if (node.type === utils_1.AST_NODE_TYPES.BinaryExpression) {
|
|
36
|
+
return `${staticText(node.left)} ${staticText(node.right)}`;
|
|
37
|
+
}
|
|
38
|
+
if (node.type === utils_1.AST_NODE_TYPES.Literal && typeof node.value === 'string') {
|
|
39
|
+
return node.value;
|
|
40
|
+
}
|
|
41
|
+
return '';
|
|
42
|
+
}
|
|
43
|
+
/** Classify an expression as unsafe SQL construction, or `false` if it is not. */
|
|
44
|
+
function classify(node, requireSqlKeywords) {
|
|
45
|
+
let kind = false;
|
|
46
|
+
// Concatenation: "SELECT ... " + value
|
|
47
|
+
if (node.type === utils_1.AST_NODE_TYPES.BinaryExpression && node.operator === '+') {
|
|
48
|
+
kind = 'concat';
|
|
49
|
+
}
|
|
50
|
+
// Interpolation: `SELECT ... ${value}`
|
|
51
|
+
if (node.type === utils_1.AST_NODE_TYPES.TemplateLiteral && node.expressions.length > 0) {
|
|
52
|
+
kind = 'template';
|
|
53
|
+
}
|
|
54
|
+
if (kind === false)
|
|
55
|
+
return false;
|
|
56
|
+
// `+` is also numeric addition. Without at least one string literal in the
|
|
57
|
+
// expression there is no evidence this builds SQL at all, and an ungated
|
|
58
|
+
// instance would report `db.query(1 + offset)`. Templates are exempt: a
|
|
59
|
+
// template literal is always a string.
|
|
60
|
+
if (kind === 'concat' && staticText(node).trim() === '')
|
|
61
|
+
return false;
|
|
62
|
+
if (requireSqlKeywords && !SQL_KEYWORDS.test(staticText(node)))
|
|
63
|
+
return false;
|
|
64
|
+
return kind;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Build a CWE-89 rule for the given sinks and remediation copy.
|
|
68
|
+
*
|
|
69
|
+
* Detects three shapes: direct concatenation into a sink, direct
|
|
70
|
+
* interpolation into a sink, and a variable tainted by either (including via
|
|
71
|
+
* `+=`) that is later passed to a sink.
|
|
72
|
+
*/
|
|
73
|
+
function createSqlInjectionRule(config) {
|
|
74
|
+
const sinks = new Set(config.methods);
|
|
75
|
+
return {
|
|
76
|
+
meta: {
|
|
77
|
+
type: config.meta.type,
|
|
78
|
+
// CWE / CVSS surface in @interlace/eslint-formatter (devkit augments
|
|
79
|
+
// RuleMetaDataDocs) and are locked against the emitted message by
|
|
80
|
+
// security-cvss-docs-consistency.lock.test.ts.
|
|
81
|
+
docs: { ...config.meta.docs },
|
|
82
|
+
messages: {
|
|
83
|
+
noUnsafeQuery: (0, messaging_1.formatLLMMessage)({
|
|
84
|
+
icon: messaging_1.MessageIcons.SECURITY,
|
|
85
|
+
issueName: 'SQL Injection Risk',
|
|
86
|
+
description: 'Unsafe SQL query detected. Variable interpolation found.',
|
|
87
|
+
severity: 'CRITICAL',
|
|
88
|
+
// Same source as meta.docs.cwe, so the emitted CVSS can never drift
|
|
89
|
+
// from the documented one (security-cvss-docs-consistency.lock).
|
|
90
|
+
cwe: config.meta.docs.cwe,
|
|
91
|
+
owasp: 'A03:2021',
|
|
92
|
+
compliance: ['SOC2', 'PCI-DSS', 'NIST-CSF'],
|
|
93
|
+
effort: 'high',
|
|
94
|
+
fix: config.fix,
|
|
95
|
+
documentationLink: config.documentationLink,
|
|
96
|
+
}),
|
|
97
|
+
unsafeTemplateLiteral: (0, messaging_1.formatLLMMessage)({
|
|
98
|
+
icon: messaging_1.MessageIcons.SECURITY,
|
|
99
|
+
issueName: 'SQL Injection Risk',
|
|
100
|
+
description: 'Unsafe SQL query construction detected (template literal).',
|
|
101
|
+
severity: 'CRITICAL',
|
|
102
|
+
fix: config.fix,
|
|
103
|
+
documentationLink: config.documentationLink,
|
|
104
|
+
}),
|
|
105
|
+
},
|
|
106
|
+
schema: [],
|
|
107
|
+
},
|
|
108
|
+
defaultOptions: [],
|
|
109
|
+
create(context) {
|
|
110
|
+
const tainted = new Map();
|
|
111
|
+
// Variables seeded with SQL-looking text. A dynamic-query builder puts
|
|
112
|
+
// the keywords in the seed (`let sql = 'SELECT * FROM t WHERE 1=1'`)
|
|
113
|
+
// and the injection in the appended fragment (`sql += ` AND n =
|
|
114
|
+
// ${n}``), which carries no keyword of its own.
|
|
115
|
+
const sqlish = new Set();
|
|
116
|
+
const report = (node, kind) => {
|
|
117
|
+
context.report({
|
|
118
|
+
node,
|
|
119
|
+
messageId: kind === 'template' ? 'unsafeTemplateLiteral' : 'noUnsafeQuery',
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
return {
|
|
123
|
+
// const query = "SELECT ..." + userId;
|
|
124
|
+
// const query = `SELECT ...${email}`;
|
|
125
|
+
VariableDeclarator(node) {
|
|
126
|
+
if (node.id.type === utils_1.AST_NODE_TYPES.Identifier && node.init) {
|
|
127
|
+
const kind = classify(node.init, config.requireSqlKeywords);
|
|
128
|
+
if (kind)
|
|
129
|
+
tainted.set(node.id.name, kind);
|
|
130
|
+
if (SQL_KEYWORDS.test(staticText(node.init)))
|
|
131
|
+
sqlish.add(node.id.name);
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
// query += ` AND name = '${name}'`;
|
|
135
|
+
AssignmentExpression(node) {
|
|
136
|
+
if (node.operator === '+=' && node.left.type === utils_1.AST_NODE_TYPES.Identifier) {
|
|
137
|
+
// Skip the keyword gate when the target was already seeded with
|
|
138
|
+
// SQL — the fragment alone rarely contains a keyword.
|
|
139
|
+
const gate = config.requireSqlKeywords && !sqlish.has(node.left.name);
|
|
140
|
+
const kind = classify(node.right, gate);
|
|
141
|
+
if (kind)
|
|
142
|
+
tainted.set(node.left.name, kind);
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
CallExpression(node) {
|
|
146
|
+
if (node.callee.type !== utils_1.AST_NODE_TYPES.MemberExpression ||
|
|
147
|
+
node.callee.property.type !== utils_1.AST_NODE_TYPES.Identifier ||
|
|
148
|
+
!sinks.has(node.callee.property.name)) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const queryArg = node.arguments[0];
|
|
152
|
+
if (!queryArg)
|
|
153
|
+
return;
|
|
154
|
+
const direct = classify(queryArg, config.requireSqlKeywords);
|
|
155
|
+
if (direct) {
|
|
156
|
+
report(queryArg, direct);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (queryArg.type === utils_1.AST_NODE_TYPES.Identifier) {
|
|
160
|
+
const taint = tainted.get(queryArg.name);
|
|
161
|
+
if (taint)
|
|
162
|
+
report(queryArg, taint);
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
}
|