eslint-plugin-postgresql-security 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 +150 -0
- package/LICENSE +23 -0
- package/README.md +169 -0
- package/dist/AGENTS.md +118 -0
- package/dist/CHANGELOG.md +150 -0
- package/dist/LICENSE +23 -0
- package/dist/README.md +169 -0
- package/dist/package.json +99 -0
- package/dist/src/constants.d.ts +6 -0
- package/dist/src/constants.js +13 -0
- package/dist/src/index.d.ts +26 -0
- package/dist/src/index.js +116 -0
- package/dist/src/oxlint.d.ts +20 -0
- package/dist/src/oxlint.js +21 -0
- package/dist/src/rules/check-query-params/index.d.ts +8 -0
- package/dist/src/rules/check-query-params/index.js +90 -0
- package/dist/src/rules/no-batch-insert-loop/index.d.ts +8 -0
- package/dist/src/rules/no-batch-insert-loop/index.js +90 -0
- package/dist/src/rules/no-floating-query/index.d.ts +8 -0
- package/dist/src/rules/no-floating-query/index.js +74 -0
- package/dist/src/rules/no-hardcoded-credentials/index.d.ts +8 -0
- package/dist/src/rules/no-hardcoded-credentials/index.js +86 -0
- package/dist/src/rules/no-insecure-ssl/index.d.ts +8 -0
- package/dist/src/rules/no-insecure-ssl/index.js +66 -0
- package/dist/src/rules/no-missing-client-release/index.d.ts +8 -0
- package/dist/src/rules/no-missing-client-release/index.js +85 -0
- package/dist/src/rules/no-select-all/index.d.ts +8 -0
- package/dist/src/rules/no-select-all/index.js +79 -0
- package/dist/src/rules/no-transaction-on-pool/index.d.ts +8 -0
- package/dist/src/rules/no-transaction-on-pool/index.js +71 -0
- package/dist/src/rules/no-unsafe-copy-from/index.d.ts +10 -0
- package/dist/src/rules/no-unsafe-copy-from/index.js +205 -0
- package/dist/src/rules/no-unsafe-query/index.d.ts +8 -0
- package/dist/src/rules/no-unsafe-query/index.js +132 -0
- package/dist/src/rules/no-unsafe-search-path/index.d.ts +8 -0
- package/dist/src/rules/no-unsafe-search-path/index.js +144 -0
- package/dist/src/rules/prefer-pool-query/index.d.ts +8 -0
- package/dist/src/rules/prefer-pool-query/index.js +104 -0
- package/dist/src/rules/prevent-double-release/index.d.ts +8 -0
- package/dist/src/rules/prevent-double-release/index.js +456 -0
- package/dist/src/types/index.d.ts +54 -0
- package/dist/src/types/index.js +7 -0
- package/package.json +99 -0
- package/src/__compatibility__/pg-interface.spec.ts +495 -0
- package/src/constants.ts +11 -0
- package/src/index.spec.ts +14 -0
- package/src/index.test.ts +59 -0
- package/src/index.ts +130 -0
- package/src/oxlint.spec.ts +70 -0
- package/src/oxlint.ts +23 -0
- package/src/rules/check-query-params/check-query-params.test.ts +68 -0
- package/src/rules/check-query-params/index.spec.ts +117 -0
- package/src/rules/check-query-params/index.ts +106 -0
- package/src/rules/no-batch-insert-loop/index.spec.ts +105 -0
- package/src/rules/no-batch-insert-loop/index.ts +105 -0
- package/src/rules/no-batch-insert-loop/no-batch-insert-loop.test.ts +68 -0
- package/src/rules/no-floating-query/index.spec.ts +112 -0
- package/src/rules/no-floating-query/index.ts +81 -0
- package/src/rules/no-floating-query/no-floating-query.test.ts +68 -0
- package/src/rules/no-hardcoded-credentials/index.spec.ts +38 -0
- package/src/rules/no-hardcoded-credentials/index.ts +93 -0
- package/src/rules/no-hardcoded-credentials/no-hardcoded-credentials.test.ts +68 -0
- package/src/rules/no-insecure-ssl/index.spec.ts +40 -0
- package/src/rules/no-insecure-ssl/index.ts +81 -0
- package/src/rules/no-insecure-ssl/no-insecure-ssl.test.ts +68 -0
- package/src/rules/no-missing-client-release/coverage-gaps.spec.ts +42 -0
- package/src/rules/no-missing-client-release/index.spec.ts +52 -0
- package/src/rules/no-missing-client-release/index.ts +100 -0
- package/src/rules/no-missing-client-release/no-missing-client-release.test.ts +68 -0
- package/src/rules/no-select-all/index.spec.ts +44 -0
- package/src/rules/no-select-all/index.ts +92 -0
- package/src/rules/no-select-all/no-select-all.test.ts +68 -0
- package/src/rules/no-transaction-on-pool/index.spec.ts +36 -0
- package/src/rules/no-transaction-on-pool/index.ts +85 -0
- package/src/rules/no-transaction-on-pool/no-transaction-on-pool.test.ts +68 -0
- package/src/rules/no-unsafe-copy-from/coverage-gaps.spec.ts +76 -0
- package/src/rules/no-unsafe-copy-from/index.spec.ts +274 -0
- package/src/rules/no-unsafe-copy-from/index.ts +226 -0
- package/src/rules/no-unsafe-copy-from/no-unsafe-copy-from.test.ts +78 -0
- package/src/rules/no-unsafe-query/coverage-gaps.spec.ts +61 -0
- package/src/rules/no-unsafe-query/index.spec.ts +103 -0
- package/src/rules/no-unsafe-query/index.ts +149 -0
- package/src/rules/no-unsafe-query/no-unsafe-query.test.ts +73 -0
- package/src/rules/no-unsafe-search-path/index.spec.ts +46 -0
- package/src/rules/no-unsafe-search-path/index.ts +164 -0
- package/src/rules/no-unsafe-search-path/no-unsafe-search-path.test.ts +68 -0
- package/src/rules/prefer-pool-query/coverage-gaps.spec.ts +76 -0
- package/src/rules/prefer-pool-query/index.spec.ts +97 -0
- package/src/rules/prefer-pool-query/index.ts +118 -0
- package/src/rules/prefer-pool-query/prefer-pool-query.test.ts +68 -0
- package/src/rules/prevent-double-release/coverage-gaps.spec.ts +538 -0
- package/src/rules/prevent-double-release/index.spec.ts +824 -0
- package/src/rules/prevent-double-release/index.ts +534 -0
- package/src/rules/prevent-double-release/prevent-double-release.test.ts +70 -0
- package/src/types/index.ts +58 -0
|
@@ -0,0 +1,226 @@
|
|
|
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
|
+
import { TSESLint, AST_NODE_TYPES, TSESTree, formatLLMMessage, MessageIcons } from '@interlace/eslint-devkit';
|
|
8
|
+
import { NoUnsafeCopyFromOptions } from '../../types';
|
|
9
|
+
|
|
10
|
+
// Pre-compiled regex patterns for performance
|
|
11
|
+
const COPY_FROM_REGEX = /\bCOPY\b.*\bFROM\b/i;
|
|
12
|
+
const STDIN_REGEX = /\bFROM\s+STDIN\b/i;
|
|
13
|
+
// Extract file path from COPY FROM 'path' pattern
|
|
14
|
+
const FILE_PATH_REGEX = /FROM\s+['"]([^'"]+)['"]/i;
|
|
15
|
+
|
|
16
|
+
// Helper to check for COPY FROM (but not COPY FROM STDIN)
|
|
17
|
+
const hasCopyFrom = (str: string) => COPY_FROM_REGEX.test(str) && !STDIN_REGEX.test(str);
|
|
18
|
+
|
|
19
|
+
// Extract file path from COPY statement
|
|
20
|
+
const extractFilePath = (str: string): string | null => {
|
|
21
|
+
const match = FILE_PATH_REGEX.exec(str);
|
|
22
|
+
return match ? match[1] : null;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
type MessageIds = 'dynamicPath' | 'hardcodedPath' | 'unverifiablePath';
|
|
26
|
+
|
|
27
|
+
export const noUnsafeCopyFrom: TSESLint.RuleModule<
|
|
28
|
+
MessageIds,
|
|
29
|
+
NoUnsafeCopyFromOptions
|
|
30
|
+
> = {
|
|
31
|
+
meta: {
|
|
32
|
+
type: 'problem',
|
|
33
|
+
docs: {
|
|
34
|
+
description: 'Prevent unsafe COPY FROM usage with dynamic file paths, which can lead to arbitrary file read/RCE.',
|
|
35
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/no-unsafe-copy-from.md',
|
|
36
|
+
cwe: 'CWE-73',
|
|
37
|
+
cvss: 9.5,
|
|
38
|
+
},
|
|
39
|
+
messages: {
|
|
40
|
+
dynamicPath: formatLLMMessage({
|
|
41
|
+
icon: MessageIcons.SECURITY,
|
|
42
|
+
issueName: 'COPY FROM Injection',
|
|
43
|
+
description: 'Dynamic file path in COPY FROM detected - potential arbitrary file read.',
|
|
44
|
+
severity: 'CRITICAL',
|
|
45
|
+
cwe: 'CWE-73',
|
|
46
|
+
owasp: 'A03:2021',
|
|
47
|
+
compliance: ['SOC2', 'PCI-DSS'],
|
|
48
|
+
effort: 'low',
|
|
49
|
+
fix: 'Never use user input in COPY FROM paths. Use COPY FROM STDIN for user data.',
|
|
50
|
+
documentationLink: 'https://www.postgresql.org/docs/current/sql-copy.html',
|
|
51
|
+
}),
|
|
52
|
+
hardcodedPath: formatLLMMessage({
|
|
53
|
+
icon: MessageIcons.WARNING,
|
|
54
|
+
issueName: 'Server-side COPY FROM',
|
|
55
|
+
description: 'Hardcoded file path in COPY FROM - server-side file access.',
|
|
56
|
+
severity: 'MEDIUM',
|
|
57
|
+
cwe: 'CWE-73',
|
|
58
|
+
effort: 'low',
|
|
59
|
+
fix: 'Prefer COPY FROM STDIN for application code. Use allowHardcodedPaths option if this is an admin script.',
|
|
60
|
+
documentationLink: 'https://www.postgresql.org/docs/current/sql-copy.html',
|
|
61
|
+
}),
|
|
62
|
+
unverifiablePath: formatLLMMessage({
|
|
63
|
+
icon: MessageIcons.WARNING,
|
|
64
|
+
issueName: 'Unverifiable COPY FROM',
|
|
65
|
+
description: 'Cannot statically verify COPY FROM source - potential injection risk.',
|
|
66
|
+
severity: 'MEDIUM',
|
|
67
|
+
cwe: 'CWE-73',
|
|
68
|
+
effort: 'medium',
|
|
69
|
+
fix: 'Ensure the query source does not contain user input, or refactor to use COPY FROM STDIN.',
|
|
70
|
+
documentationLink: 'https://www.postgresql.org/docs/current/sql-copy.html',
|
|
71
|
+
}),
|
|
72
|
+
},
|
|
73
|
+
schema: [
|
|
74
|
+
{
|
|
75
|
+
type: 'object',
|
|
76
|
+
properties: {
|
|
77
|
+
allowHardcodedPaths: {
|
|
78
|
+
type: 'boolean',
|
|
79
|
+
description: 'Allow hardcoded file paths (for admin/migration scripts)',
|
|
80
|
+
},
|
|
81
|
+
allowedPaths: {
|
|
82
|
+
type: 'array',
|
|
83
|
+
items: { type: 'string' },
|
|
84
|
+
description: 'List of allowed file path patterns (regex strings)',
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
additionalProperties: false,
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
defaultOptions: [{}],
|
|
92
|
+
create(context) {
|
|
93
|
+
const options = context.options[0] ?? {};
|
|
94
|
+
const allowHardcodedPaths = options.allowHardcodedPaths ?? false;
|
|
95
|
+
const allowedPaths = (options.allowedPaths ?? []).map((p: string) => new RegExp(p));
|
|
96
|
+
|
|
97
|
+
// Check if a file path matches any allowed pattern
|
|
98
|
+
const isPathAllowed = (filePath: string): boolean => {
|
|
99
|
+
return allowedPaths.some((pattern: RegExp) => pattern.test(filePath));
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
CallExpression(node: TSESTree.CallExpression) {
|
|
104
|
+
if (
|
|
105
|
+
node.callee.type !== AST_NODE_TYPES.MemberExpression ||
|
|
106
|
+
node.callee.property.type !== AST_NODE_TYPES.Identifier ||
|
|
107
|
+
node.callee.property.name !== 'query'
|
|
108
|
+
) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const args = node.arguments;
|
|
113
|
+
if (args.length === 0) return;
|
|
114
|
+
|
|
115
|
+
const queryArg = args[0];
|
|
116
|
+
|
|
117
|
+
// Case 1: Literal string - can fully analyze
|
|
118
|
+
if (queryArg.type === AST_NODE_TYPES.Literal && typeof queryArg.value === 'string') {
|
|
119
|
+
if (hasCopyFrom(queryArg.value)) {
|
|
120
|
+
const filePath = extractFilePath(queryArg.value);
|
|
121
|
+
|
|
122
|
+
// Check if path is in allowlist
|
|
123
|
+
if (filePath && isPathAllowed(filePath)) {
|
|
124
|
+
return; // Explicitly allowed
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Hardcoded path - report as medium severity (or skip if allowed)
|
|
128
|
+
if (allowHardcodedPaths) {
|
|
129
|
+
return; // User opted to allow hardcoded paths
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
context.report({
|
|
133
|
+
node: queryArg,
|
|
134
|
+
messageId: 'hardcodedPath',
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Case 2: Template literal - check for dynamic expressions
|
|
141
|
+
if (queryArg.type === AST_NODE_TYPES.TemplateLiteral) {
|
|
142
|
+
const staticText = queryArg.quasis.map(q => q.value.raw).join(' ');
|
|
143
|
+
|
|
144
|
+
if (hasCopyFrom(staticText)) {
|
|
145
|
+
// Has expressions = dynamic = CRITICAL
|
|
146
|
+
if (queryArg.expressions.length > 0) {
|
|
147
|
+
context.report({
|
|
148
|
+
node: queryArg,
|
|
149
|
+
messageId: 'dynamicPath',
|
|
150
|
+
});
|
|
151
|
+
} else {
|
|
152
|
+
// No expressions = effectively a static string
|
|
153
|
+
const filePath = extractFilePath(staticText);
|
|
154
|
+
if (filePath && isPathAllowed(filePath)) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (!allowHardcodedPaths) {
|
|
158
|
+
context.report({
|
|
159
|
+
node: queryArg,
|
|
160
|
+
messageId: 'hardcodedPath',
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Case 3: Binary expression (string concatenation)
|
|
169
|
+
if (queryArg.type === AST_NODE_TYPES.BinaryExpression) {
|
|
170
|
+
const parts: string[] = [];
|
|
171
|
+
let hasDynamicPart = false;
|
|
172
|
+
|
|
173
|
+
const stack: TSESTree.Node[] = [queryArg];
|
|
174
|
+
for (let curr = stack.pop(); curr !== undefined; curr = stack.pop()) {
|
|
175
|
+
if (curr.type === AST_NODE_TYPES.BinaryExpression && curr.operator === '+') {
|
|
176
|
+
stack.push(curr.right);
|
|
177
|
+
stack.push(curr.left);
|
|
178
|
+
} else if (curr.type === AST_NODE_TYPES.Literal && typeof curr.value === 'string') {
|
|
179
|
+
parts.push(curr.value);
|
|
180
|
+
} else {
|
|
181
|
+
// Non-literal part = dynamic
|
|
182
|
+
hasDynamicPart = true;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const staticText = parts.join(' ');
|
|
187
|
+
if (hasCopyFrom(staticText)) {
|
|
188
|
+
if (hasDynamicPart) {
|
|
189
|
+
context.report({
|
|
190
|
+
node: queryArg,
|
|
191
|
+
messageId: 'dynamicPath',
|
|
192
|
+
});
|
|
193
|
+
} else {
|
|
194
|
+
// All literals - treat as hardcoded
|
|
195
|
+
const filePath = extractFilePath(staticText);
|
|
196
|
+
if (filePath && isPathAllowed(filePath)) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (!allowHardcodedPaths) {
|
|
200
|
+
context.report({
|
|
201
|
+
node: queryArg,
|
|
202
|
+
messageId: 'hardcodedPath',
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Case 4: Identifier or CallExpression - cannot verify statically
|
|
211
|
+
if (
|
|
212
|
+
queryArg.type === AST_NODE_TYPES.Identifier ||
|
|
213
|
+
queryArg.type === AST_NODE_TYPES.CallExpression
|
|
214
|
+
) {
|
|
215
|
+
// We can't see what's in the variable/function - flag as unverifiable
|
|
216
|
+
// This catches: client.query(sqlQuery) or client.query(buildQuery())
|
|
217
|
+
// We report as medium severity since we can't confirm it's COPY FROM
|
|
218
|
+
// Actually, we can't even confirm it's COPY FROM, so let's not report
|
|
219
|
+
// unless we want to be very aggressive. For now, skip these.
|
|
220
|
+
// The user would need to ensure the source is safe.
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
},
|
|
226
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for no-unsafe-copy-from
|
|
3
|
+
* CWE-73 — Prevent unsafe COPY FROM usage with dynamic file paths, which can lead to arbitrary file read/RCE.
|
|
4
|
+
*
|
|
5
|
+
* Generated by: node scripts/generate-rule-tests.mjs
|
|
6
|
+
* Standard: Engineering Standards §5 (side-by-side), §6 (≥5 valid + ≥5 invalid), §8 (RuleTester at describe level)
|
|
7
|
+
*/
|
|
8
|
+
import { RuleTester } from '@typescript-eslint/rule-tester';
|
|
9
|
+
import { describe, it, afterAll } from 'vitest';
|
|
10
|
+
import parser from '@typescript-eslint/parser';
|
|
11
|
+
import { noUnsafeCopyFrom } from './index';
|
|
12
|
+
|
|
13
|
+
RuleTester.afterAll = afterAll;
|
|
14
|
+
RuleTester.it = it;
|
|
15
|
+
RuleTester.itOnly = it.only;
|
|
16
|
+
RuleTester.describe = describe;
|
|
17
|
+
|
|
18
|
+
const ruleTester = new RuleTester({
|
|
19
|
+
languageOptions: {
|
|
20
|
+
parser,
|
|
21
|
+
ecmaVersion: 2022,
|
|
22
|
+
sourceType: 'module',
|
|
23
|
+
parserOptions: { ecmaFeatures: { jsx: true } },
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('no-unsafe-copy-from', () => {
|
|
28
|
+
describe('Valid Code', () => {
|
|
29
|
+
ruleTester.run('valid - safe patterns', noUnsafeCopyFrom, {
|
|
30
|
+
valid: [
|
|
31
|
+
// Unrelated code should not trigger
|
|
32
|
+
{
|
|
33
|
+
code: `const x = 1;`,
|
|
34
|
+
},
|
|
35
|
+
// Array operations are safe
|
|
36
|
+
{
|
|
37
|
+
code: `const arr = [1, 2, 3];`,
|
|
38
|
+
},
|
|
39
|
+
// Non-targeted method calls are safe
|
|
40
|
+
{
|
|
41
|
+
code: `obj.safeMethod();`,
|
|
42
|
+
},
|
|
43
|
+
// Environment variable usage is safe (padding case 4)
|
|
44
|
+
{
|
|
45
|
+
code: `const safe3 = process.env.DB_HOST;`,
|
|
46
|
+
},
|
|
47
|
+
// Environment variable usage is safe (padding case 5)
|
|
48
|
+
{
|
|
49
|
+
code: `const safe4 = process.env.DB_HOST;`,
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
invalid: [],
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('Invalid Code', () => {
|
|
57
|
+
ruleTester.run('invalid - dangerous patterns', noUnsafeCopyFrom, {
|
|
58
|
+
valid: [],
|
|
59
|
+
invalid: [
|
|
60
|
+
// Triggers dynamicPath
|
|
61
|
+
{
|
|
62
|
+
code: `client.query(\`COPY users FROM '\${filePath}'\`)`,
|
|
63
|
+
errors: [{ messageId: 'dynamicPath' }],
|
|
64
|
+
},
|
|
65
|
+
// Triggers hardcodedPath
|
|
66
|
+
{
|
|
67
|
+
code: `client.query("COPY users FROM '/tmp/data.csv'")`,
|
|
68
|
+
errors: [{ messageId: 'hardcodedPath' }],
|
|
69
|
+
},
|
|
70
|
+
// Triggers dynamicPath: string concatenation with COPY FROM
|
|
71
|
+
{
|
|
72
|
+
code: `client.query('COPY users FROM ' + filePath)`,
|
|
73
|
+
errors: [{ messageId: 'dynamicPath' }],
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
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
|
+
/**
|
|
8
|
+
* Coverage-gap tests for no-unsafe-query: taint-tracking listener guards
|
|
9
|
+
* (declarators without init / destructuring, non-`+=` and non-identifier
|
|
10
|
+
* assignment targets, safe `+=` right-hand sides).
|
|
11
|
+
*/
|
|
12
|
+
import { RuleTester } from '@typescript-eslint/rule-tester';
|
|
13
|
+
import { describe, it, afterAll } from 'vitest';
|
|
14
|
+
import * as parser from '@typescript-eslint/parser';
|
|
15
|
+
import { noUnsafeQuery } from './index';
|
|
16
|
+
|
|
17
|
+
RuleTester.afterAll = afterAll;
|
|
18
|
+
RuleTester.it = it;
|
|
19
|
+
RuleTester.itOnly = it.only;
|
|
20
|
+
RuleTester.describe = describe;
|
|
21
|
+
|
|
22
|
+
const ruleTester = new RuleTester({
|
|
23
|
+
languageOptions: {
|
|
24
|
+
parser,
|
|
25
|
+
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('no-unsafe-query — coverage gaps', () => {
|
|
30
|
+
ruleTester.run('taint-listener guards', noUnsafeQuery, {
|
|
31
|
+
valid: [
|
|
32
|
+
{
|
|
33
|
+
name: 'declarator without init is not tracked',
|
|
34
|
+
code: `let q; db.query('SELECT 1');`,
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: 'destructured declarator id is not tracked',
|
|
38
|
+
code: `const { sql } = cfg; db.query(sql);`,
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
name: 'plain string += does not taint',
|
|
42
|
+
code: `let q = 'SELECT 1'; q += ' WHERE active'; db.query(q);`,
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
name: 'non-+= assignment operator is ignored',
|
|
46
|
+
code: `let n = 1; n -= step; db.query(n);`,
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
name: 'member-expression += target is ignored',
|
|
50
|
+
code: `state.q += 'a' + suffix; db.query(state.q);`,
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
invalid: [
|
|
54
|
+
{
|
|
55
|
+
name: 'template-tainted variable via += reports unsafeTemplateLiteral',
|
|
56
|
+
code: 'let q = "SELECT 1"; q += ` AND id = ${id}`; db.query(q);',
|
|
57
|
+
errors: [{ messageId: 'unsafeTemplateLiteral' }],
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { RuleTester } from '@typescript-eslint/rule-tester';
|
|
2
|
+
import { describe, it, afterAll } from 'vitest';
|
|
3
|
+
import * as parser from '@typescript-eslint/parser';
|
|
4
|
+
import { noUnsafeQuery } from './index';
|
|
5
|
+
|
|
6
|
+
RuleTester.afterAll = afterAll;
|
|
7
|
+
RuleTester.it = it;
|
|
8
|
+
RuleTester.itOnly = it.only;
|
|
9
|
+
RuleTester.describe = describe;
|
|
10
|
+
|
|
11
|
+
const ruleTester = new RuleTester({
|
|
12
|
+
languageOptions: {
|
|
13
|
+
parser,
|
|
14
|
+
ecmaVersion: 2022,
|
|
15
|
+
sourceType: 'module',
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe('no-unsafe-query', () => {
|
|
20
|
+
describe('Valid - Parameterized Queries', () => {
|
|
21
|
+
ruleTester.run('valid - safe queries', noUnsafeQuery, {
|
|
22
|
+
valid: [
|
|
23
|
+
// Ignored cases (coverage)
|
|
24
|
+
"client.query()", // Empty args
|
|
25
|
+
"client.other('SELECT ' + 1)", // Not query method
|
|
26
|
+
// Standard parameterized query
|
|
27
|
+
"client.query('SELECT * FROM users WHERE id = $1', [1])",
|
|
28
|
+
// Simple string literal
|
|
29
|
+
"client.query('SELECT * FROM users')",
|
|
30
|
+
// Template literal without expressions
|
|
31
|
+
"pool.query(`SELECT * FROM users`)",
|
|
32
|
+
// Parameterized with template literal (safe if no expressions used for values)
|
|
33
|
+
"client.query(`SELECT * FROM users WHERE id = $1`, [id])",
|
|
34
|
+
// pg-format usage (assumed safe)
|
|
35
|
+
"client.query(format('SELECT * FROM %I', table))",
|
|
36
|
+
// Variable with safe init (no concat/template)
|
|
37
|
+
`const query = 'SELECT * FROM users'; db.query(query);`,
|
|
38
|
+
],
|
|
39
|
+
invalid: [],
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe('Invalid - Direct Concatenation/Interpolation', () => {
|
|
44
|
+
ruleTester.run('invalid - direct', noUnsafeQuery, {
|
|
45
|
+
valid: [],
|
|
46
|
+
invalid: [
|
|
47
|
+
{
|
|
48
|
+
code: "client.query('SELECT * FROM users WHERE id = ' + id)",
|
|
49
|
+
errors: [{ messageId: 'noUnsafeQuery' }],
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
code: "pool.query(`SELECT * FROM users WHERE id = ${id}`)",
|
|
53
|
+
errors: [{ messageId: 'unsafeTemplateLiteral' }],
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
code: "client.query('INSERT INTO users VALUES (' + val + ')')",
|
|
57
|
+
errors: [{ messageId: 'noUnsafeQuery' }],
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe('Invalid - Variable Taint Tracking', () => {
|
|
64
|
+
ruleTester.run('invalid - tainted variables', noUnsafeQuery, {
|
|
65
|
+
valid: [],
|
|
66
|
+
invalid: [
|
|
67
|
+
// vuln_sql_string_concat — concat assigned to variable, then passed to .query()
|
|
68
|
+
{
|
|
69
|
+
code: `
|
|
70
|
+
const query = "SELECT * FROM users WHERE id = '" + userId + "'";
|
|
71
|
+
db.query(query);
|
|
72
|
+
`,
|
|
73
|
+
errors: [{ messageId: 'noUnsafeQuery' }],
|
|
74
|
+
},
|
|
75
|
+
// vuln_sql_template_literal — template literal assigned to variable
|
|
76
|
+
{
|
|
77
|
+
code: `
|
|
78
|
+
const query = \`SELECT * FROM users WHERE email = '\${email}'\`;
|
|
79
|
+
db.query(query);
|
|
80
|
+
`,
|
|
81
|
+
errors: [{ messageId: 'unsafeTemplateLiteral' }],
|
|
82
|
+
},
|
|
83
|
+
// vuln_sql_dynamic_column — template literal for ORDER BY
|
|
84
|
+
{
|
|
85
|
+
code: `
|
|
86
|
+
const query = \`SELECT * FROM users ORDER BY \${sortColumn}\`;
|
|
87
|
+
db.query(query);
|
|
88
|
+
`,
|
|
89
|
+
errors: [{ messageId: 'unsafeTemplateLiteral' }],
|
|
90
|
+
},
|
|
91
|
+
// vuln_sql_conditional — += augmented assignment with template
|
|
92
|
+
{
|
|
93
|
+
code: `
|
|
94
|
+
let query = "SELECT * FROM products WHERE 1=1";
|
|
95
|
+
query += \` AND name = '\${name}'\`;
|
|
96
|
+
db.query(query);
|
|
97
|
+
`,
|
|
98
|
+
errors: [{ messageId: 'unsafeTemplateLiteral' }],
|
|
99
|
+
},
|
|
100
|
+
],
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
});
|
|
@@ -0,0 +1,149 @@
|
|
|
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
|
+
import { TSESLint, AST_NODE_TYPES, TSESTree, formatLLMMessage, MessageIcons } from '@interlace/eslint-devkit';
|
|
8
|
+
import { NoUnsafeQueryOptions } from '../../types';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Check if a node contains unsafe string construction (concatenation or interpolation)
|
|
12
|
+
*/
|
|
13
|
+
function isUnsafeStringConstruction(node: TSESTree.Node): 'concat' | 'template' | false {
|
|
14
|
+
// Direct concatenation: "SELECT..." + var
|
|
15
|
+
if (node.type === AST_NODE_TYPES.BinaryExpression && node.operator === '+') {
|
|
16
|
+
return 'concat';
|
|
17
|
+
}
|
|
18
|
+
// Template literal with expressions: `SELECT...${var}`
|
|
19
|
+
if (node.type === AST_NODE_TYPES.TemplateLiteral && node.expressions.length > 0) {
|
|
20
|
+
return 'template';
|
|
21
|
+
}
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const noUnsafeQuery: TSESLint.RuleModule<
|
|
26
|
+
'noUnsafeQuery' | 'unsafeTemplateLiteral',
|
|
27
|
+
NoUnsafeQueryOptions
|
|
28
|
+
> = {
|
|
29
|
+
meta: {
|
|
30
|
+
type: 'problem',
|
|
31
|
+
// CWE / CVSS lifted to meta.docs (Interlace extension) so
|
|
32
|
+
// @interlace/eslint-formatter renders them inline. Previously these
|
|
33
|
+
// values lived only inside the `messages` factory below, where the
|
|
34
|
+
// whole-run formatter cannot see them. See docs/META_HYGIENE.md for
|
|
35
|
+
// the fleet-wide audit and tracker P1 #5 for the rollout plan.
|
|
36
|
+
docs: {
|
|
37
|
+
description: 'Prevent SQL injection by disallowing string concatenation or unsafe template literals in queries.',
|
|
38
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/no-unsafe-query.md',
|
|
39
|
+
// CWE / CVSS surfaces in the formatter (devkit augments RuleMetaDataDocs).
|
|
40
|
+
cwe: 'CWE-89',
|
|
41
|
+
cvss: 9.8,
|
|
42
|
+
confidence: 'high',
|
|
43
|
+
},
|
|
44
|
+
messages: {
|
|
45
|
+
noUnsafeQuery: formatLLMMessage({
|
|
46
|
+
icon: MessageIcons.SECURITY,
|
|
47
|
+
issueName: 'SQL Injection Risk',
|
|
48
|
+
description: 'Unsafe SQL query detected. Variable interpolation found.',
|
|
49
|
+
severity: 'CRITICAL',
|
|
50
|
+
cwe: 'CWE-89',
|
|
51
|
+
owasp: 'A03:2021',
|
|
52
|
+
compliance: ['SOC2', 'PCI-DSS', 'NIST-CSF'],
|
|
53
|
+
effort: 'high',
|
|
54
|
+
fix: 'Use parameterized queries ($1, $2) instead of string concatenation.',
|
|
55
|
+
documentationLink: 'https://node-postgres.com/features/queries#parameterized-queries',
|
|
56
|
+
}),
|
|
57
|
+
unsafeTemplateLiteral: formatLLMMessage({
|
|
58
|
+
icon: MessageIcons.SECURITY,
|
|
59
|
+
issueName: 'SQL Injection Risk',
|
|
60
|
+
description: 'Unsafe SQL query construction detected (template literal).',
|
|
61
|
+
severity: 'CRITICAL',
|
|
62
|
+
fix: 'Use parameterized queries ($1, $2) instead of interpolating values.',
|
|
63
|
+
documentationLink: 'https://owasp.org/www-community/attacks/SQL_Injection',
|
|
64
|
+
}),
|
|
65
|
+
},
|
|
66
|
+
schema: [],
|
|
67
|
+
},
|
|
68
|
+
defaultOptions: [],
|
|
69
|
+
create(context) {
|
|
70
|
+
// Track variables tainted by unsafe string construction
|
|
71
|
+
const taintedVariables = new Map<string, 'concat' | 'template'>();
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
// Track variable declarations with unsafe init:
|
|
75
|
+
// const query = "SELECT..." + userId;
|
|
76
|
+
// const query = `SELECT...${email}`;
|
|
77
|
+
VariableDeclarator(node: TSESTree.VariableDeclarator) {
|
|
78
|
+
if (
|
|
79
|
+
node.id.type === AST_NODE_TYPES.Identifier &&
|
|
80
|
+
node.init
|
|
81
|
+
) {
|
|
82
|
+
const unsafe = isUnsafeStringConstruction(node.init);
|
|
83
|
+
if (unsafe) {
|
|
84
|
+
taintedVariables.set(node.id.name, unsafe);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
// Track augmented assignment: query += " AND ..." + var
|
|
90
|
+
// or: query += `...${var}`
|
|
91
|
+
AssignmentExpression(node: TSESTree.AssignmentExpression) {
|
|
92
|
+
if (
|
|
93
|
+
node.operator === '+=' &&
|
|
94
|
+
node.left.type === AST_NODE_TYPES.Identifier
|
|
95
|
+
) {
|
|
96
|
+
const unsafe = isUnsafeStringConstruction(node.right);
|
|
97
|
+
if (unsafe) {
|
|
98
|
+
taintedVariables.set(node.left.name, unsafe);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
CallExpression(node: TSESTree.CallExpression) {
|
|
104
|
+
// Check if the method being called is 'query'
|
|
105
|
+
if (
|
|
106
|
+
node.callee.type !== AST_NODE_TYPES.MemberExpression ||
|
|
107
|
+
node.callee.property.type !== AST_NODE_TYPES.Identifier ||
|
|
108
|
+
node.callee.property.name !== 'query'
|
|
109
|
+
) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const args = node.arguments;
|
|
114
|
+
if (args.length === 0) return;
|
|
115
|
+
|
|
116
|
+
const queryArg = args[0];
|
|
117
|
+
|
|
118
|
+
// 1. Check for String Concatenation: query('SELECT * FROM users WHERE id = ' + id)
|
|
119
|
+
if (queryArg.type === AST_NODE_TYPES.BinaryExpression && queryArg.operator === '+') {
|
|
120
|
+
context.report({
|
|
121
|
+
node: queryArg,
|
|
122
|
+
messageId: 'noUnsafeQuery',
|
|
123
|
+
});
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// 2. Check for Template Literals with Expressions: query(`SELECT * FROM users WHERE id = ${id}`)
|
|
128
|
+
if (queryArg.type === AST_NODE_TYPES.TemplateLiteral && queryArg.expressions.length > 0) {
|
|
129
|
+
context.report({
|
|
130
|
+
node: queryArg,
|
|
131
|
+
messageId: 'unsafeTemplateLiteral',
|
|
132
|
+
});
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// 3. Check for tainted variables: const q = "..." + var; db.query(q)
|
|
137
|
+
if (queryArg.type === AST_NODE_TYPES.Identifier) {
|
|
138
|
+
const taint = taintedVariables.get(queryArg.name);
|
|
139
|
+
if (taint) {
|
|
140
|
+
context.report({
|
|
141
|
+
node: queryArg,
|
|
142
|
+
messageId: taint === 'template' ? 'unsafeTemplateLiteral' : 'noUnsafeQuery',
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
},
|
|
149
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for no-unsafe-query
|
|
3
|
+
* CWE-89 — Prevent SQL injection by disallowing string concatenation or unsafe template literals in queries.
|
|
4
|
+
*
|
|
5
|
+
* Generated by: node scripts/generate-rule-tests.mjs
|
|
6
|
+
* Standard: Engineering Standards §5 (side-by-side), §6 (≥5 valid + ≥5 invalid), §8 (RuleTester at describe level)
|
|
7
|
+
*/
|
|
8
|
+
import { RuleTester } from '@typescript-eslint/rule-tester';
|
|
9
|
+
import { describe, it, afterAll } from 'vitest';
|
|
10
|
+
import parser from '@typescript-eslint/parser';
|
|
11
|
+
import { noUnsafeQuery } from './index';
|
|
12
|
+
|
|
13
|
+
RuleTester.afterAll = afterAll;
|
|
14
|
+
RuleTester.it = it;
|
|
15
|
+
RuleTester.itOnly = it.only;
|
|
16
|
+
RuleTester.describe = describe;
|
|
17
|
+
|
|
18
|
+
const ruleTester = new RuleTester({
|
|
19
|
+
languageOptions: {
|
|
20
|
+
parser,
|
|
21
|
+
ecmaVersion: 2022,
|
|
22
|
+
sourceType: 'module',
|
|
23
|
+
parserOptions: { ecmaFeatures: { jsx: true } },
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('no-unsafe-query', () => {
|
|
28
|
+
describe('Valid Code', () => {
|
|
29
|
+
ruleTester.run('valid - safe patterns', noUnsafeQuery, {
|
|
30
|
+
valid: [
|
|
31
|
+
// Unrelated code should not trigger
|
|
32
|
+
{
|
|
33
|
+
code: `const x = 1;`,
|
|
34
|
+
},
|
|
35
|
+
// Array operations are safe
|
|
36
|
+
{
|
|
37
|
+
code: `const arr = [1, 2, 3];`,
|
|
38
|
+
},
|
|
39
|
+
// Non-targeted method calls are safe
|
|
40
|
+
{
|
|
41
|
+
code: `obj.safeMethod();`,
|
|
42
|
+
},
|
|
43
|
+
// Environment variable usage is safe (padding case 4)
|
|
44
|
+
{
|
|
45
|
+
code: `const safe3 = process.env.DB_HOST;`,
|
|
46
|
+
},
|
|
47
|
+
// Environment variable usage is safe (padding case 5)
|
|
48
|
+
{
|
|
49
|
+
code: `const safe4 = process.env.DB_HOST;`,
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
invalid: [],
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('Invalid Code', () => {
|
|
57
|
+
ruleTester.run('invalid - dangerous patterns', noUnsafeQuery, {
|
|
58
|
+
valid: [],
|
|
59
|
+
invalid: [
|
|
60
|
+
// Triggers noUnsafeQuery
|
|
61
|
+
{
|
|
62
|
+
code: `client.query('SELECT * FROM users WHERE id = ' + userId)`,
|
|
63
|
+
errors: [{ messageId: 'noUnsafeQuery' }],
|
|
64
|
+
},
|
|
65
|
+
// Triggers unsafeTemplateLiteral
|
|
66
|
+
{
|
|
67
|
+
code: 'client.query(`SELECT * FROM users WHERE id = ${userId}`)',
|
|
68
|
+
errors: [{ messageId: 'unsafeTemplateLiteral' }],
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
});
|