eslint-plugin-postgresql-security 1.5.0 → 1.5.2
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/README.md +5 -5
- package/package.json +9 -23
- package/src/constants.js +1 -0
- package/src/index.d.ts +17 -0
- package/src/index.js +1 -0
- package/src/oxlint.d.ts +17 -0
- package/src/oxlint.js +1 -0
- package/src/rules/check-query-params/index.js +1 -0
- package/src/rules/no-batch-insert-loop/index.js +1 -0
- package/src/rules/no-floating-query/index.js +1 -0
- package/src/rules/no-hardcoded-credentials/index.js +1 -0
- package/src/rules/no-insecure-ssl/index.js +1 -0
- package/src/rules/no-missing-client-release/index.js +1 -0
- package/src/rules/no-select-all/index.js +1 -0
- package/src/rules/no-transaction-on-pool/index.js +1 -0
- package/src/rules/no-unsafe-copy-from/index.js +1 -0
- package/src/rules/no-unsafe-query/index.js +1 -0
- package/src/rules/no-unsafe-search-path/index.js +1 -0
- package/src/rules/prefer-pool-query/index.js +1 -0
- package/src/rules/prevent-double-release/index.js +1 -0
- package/src/types/index.js +1 -0
- package/CHANGELOG.md +0 -150
- package/dist/AGENTS.md +0 -118
- package/dist/CHANGELOG.md +0 -150
- package/dist/LICENSE +0 -23
- package/dist/README.md +0 -169
- package/dist/package.json +0 -99
- package/dist/src/constants.d.ts +0 -6
- package/dist/src/constants.js +0 -13
- package/dist/src/index.d.ts +0 -26
- package/dist/src/index.js +0 -116
- package/dist/src/oxlint.d.ts +0 -20
- package/dist/src/oxlint.js +0 -21
- package/dist/src/rules/check-query-params/index.d.ts +0 -8
- package/dist/src/rules/check-query-params/index.js +0 -90
- package/dist/src/rules/no-batch-insert-loop/index.d.ts +0 -8
- package/dist/src/rules/no-batch-insert-loop/index.js +0 -90
- package/dist/src/rules/no-floating-query/index.d.ts +0 -8
- package/dist/src/rules/no-floating-query/index.js +0 -74
- package/dist/src/rules/no-hardcoded-credentials/index.d.ts +0 -8
- package/dist/src/rules/no-hardcoded-credentials/index.js +0 -86
- package/dist/src/rules/no-insecure-ssl/index.d.ts +0 -8
- package/dist/src/rules/no-insecure-ssl/index.js +0 -66
- package/dist/src/rules/no-missing-client-release/index.d.ts +0 -8
- package/dist/src/rules/no-missing-client-release/index.js +0 -85
- package/dist/src/rules/no-select-all/index.d.ts +0 -8
- package/dist/src/rules/no-select-all/index.js +0 -79
- package/dist/src/rules/no-transaction-on-pool/index.d.ts +0 -8
- package/dist/src/rules/no-transaction-on-pool/index.js +0 -71
- package/dist/src/rules/no-unsafe-copy-from/index.d.ts +0 -10
- package/dist/src/rules/no-unsafe-copy-from/index.js +0 -205
- package/dist/src/rules/no-unsafe-query/index.d.ts +0 -8
- package/dist/src/rules/no-unsafe-query/index.js +0 -132
- package/dist/src/rules/no-unsafe-search-path/index.d.ts +0 -8
- package/dist/src/rules/no-unsafe-search-path/index.js +0 -144
- package/dist/src/rules/prefer-pool-query/index.d.ts +0 -8
- package/dist/src/rules/prefer-pool-query/index.js +0 -104
- package/dist/src/rules/prevent-double-release/index.d.ts +0 -8
- package/dist/src/rules/prevent-double-release/index.js +0 -456
- package/dist/src/types/index.js +0 -7
- package/src/__compatibility__/pg-interface.spec.ts +0 -495
- package/src/constants.ts +0 -11
- package/src/index.spec.ts +0 -14
- package/src/index.test.ts +0 -59
- package/src/index.ts +0 -130
- package/src/oxlint.spec.ts +0 -70
- package/src/oxlint.ts +0 -23
- package/src/rules/check-query-params/check-query-params.test.ts +0 -68
- package/src/rules/check-query-params/index.spec.ts +0 -117
- package/src/rules/check-query-params/index.ts +0 -106
- package/src/rules/no-batch-insert-loop/index.spec.ts +0 -105
- package/src/rules/no-batch-insert-loop/index.ts +0 -105
- package/src/rules/no-batch-insert-loop/no-batch-insert-loop.test.ts +0 -68
- package/src/rules/no-floating-query/index.spec.ts +0 -112
- package/src/rules/no-floating-query/index.ts +0 -81
- package/src/rules/no-floating-query/no-floating-query.test.ts +0 -68
- package/src/rules/no-hardcoded-credentials/index.spec.ts +0 -38
- package/src/rules/no-hardcoded-credentials/index.ts +0 -93
- package/src/rules/no-hardcoded-credentials/no-hardcoded-credentials.test.ts +0 -68
- package/src/rules/no-insecure-ssl/index.spec.ts +0 -40
- package/src/rules/no-insecure-ssl/index.ts +0 -81
- package/src/rules/no-insecure-ssl/no-insecure-ssl.test.ts +0 -68
- package/src/rules/no-missing-client-release/coverage-gaps.spec.ts +0 -42
- package/src/rules/no-missing-client-release/index.spec.ts +0 -52
- package/src/rules/no-missing-client-release/index.ts +0 -100
- package/src/rules/no-missing-client-release/no-missing-client-release.test.ts +0 -68
- package/src/rules/no-select-all/index.spec.ts +0 -44
- package/src/rules/no-select-all/index.ts +0 -92
- package/src/rules/no-select-all/no-select-all.test.ts +0 -68
- package/src/rules/no-transaction-on-pool/index.spec.ts +0 -36
- package/src/rules/no-transaction-on-pool/index.ts +0 -85
- package/src/rules/no-transaction-on-pool/no-transaction-on-pool.test.ts +0 -68
- package/src/rules/no-unsafe-copy-from/coverage-gaps.spec.ts +0 -76
- package/src/rules/no-unsafe-copy-from/index.spec.ts +0 -274
- package/src/rules/no-unsafe-copy-from/index.ts +0 -226
- package/src/rules/no-unsafe-copy-from/no-unsafe-copy-from.test.ts +0 -78
- package/src/rules/no-unsafe-query/coverage-gaps.spec.ts +0 -61
- package/src/rules/no-unsafe-query/index.spec.ts +0 -103
- package/src/rules/no-unsafe-query/index.ts +0 -149
- package/src/rules/no-unsafe-query/no-unsafe-query.test.ts +0 -73
- package/src/rules/no-unsafe-search-path/index.spec.ts +0 -46
- package/src/rules/no-unsafe-search-path/index.ts +0 -164
- package/src/rules/no-unsafe-search-path/no-unsafe-search-path.test.ts +0 -68
- package/src/rules/prefer-pool-query/coverage-gaps.spec.ts +0 -76
- package/src/rules/prefer-pool-query/index.spec.ts +0 -97
- package/src/rules/prefer-pool-query/index.ts +0 -118
- package/src/rules/prefer-pool-query/prefer-pool-query.test.ts +0 -68
- package/src/rules/prevent-double-release/coverage-gaps.spec.ts +0 -538
- package/src/rules/prevent-double-release/index.spec.ts +0 -824
- package/src/rules/prevent-double-release/index.ts +0 -534
- package/src/rules/prevent-double-release/prevent-double-release.test.ts +0 -70
- package/src/types/index.ts +0 -58
- /package/{dist/src → src}/types/index.d.ts +0 -0
|
@@ -1,79 +0,0 @@
|
|
|
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.noSelectAll = void 0;
|
|
9
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
-
// Pre-compiled regex patterns for performance
|
|
11
|
-
const COUNT_STAR_REGEX = /COUNT\s*\(\s*\*\s*\)/g;
|
|
12
|
-
const SELECT_STAR_REGEX = /\bSELECT\s+\*/;
|
|
13
|
-
const COMMA_STAR_REGEX = /,\s*\*/;
|
|
14
|
-
const SELECT_KEYWORD_REGEX = /\bSELECT\b/;
|
|
15
|
-
const UNNEST_PATTERN_REGEX = /\bSELECT\s+\*\s+FROM\s+UNNEST\s*\(/;
|
|
16
|
-
exports.noSelectAll = {
|
|
17
|
-
meta: {
|
|
18
|
-
type: 'problem',
|
|
19
|
-
docs: {
|
|
20
|
-
description: 'Prevent using * in SELECT statements (implicit columns).',
|
|
21
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/no-select-all.md',
|
|
22
|
-
cwe: 'CWE-1049',
|
|
23
|
-
cvss: 5,
|
|
24
|
-
},
|
|
25
|
-
messages: {
|
|
26
|
-
noSelectAll: (0, eslint_devkit_1.formatLLMMessage)({
|
|
27
|
-
icon: eslint_devkit_1.MessageIcons.PERFORMANCE,
|
|
28
|
-
issueName: 'Select All',
|
|
29
|
-
description: 'Avoid using "SELECT *" which fetches all columns.',
|
|
30
|
-
severity: 'MEDIUM',
|
|
31
|
-
cwe: 'CWE-1049',
|
|
32
|
-
effort: 'low',
|
|
33
|
-
fix: 'Explicitly list the columns you need (e.g., SELECT id, name FROM ...).',
|
|
34
|
-
documentationLink: 'https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_SELECT_.2A',
|
|
35
|
-
}),
|
|
36
|
-
},
|
|
37
|
-
schema: [],
|
|
38
|
-
},
|
|
39
|
-
defaultOptions: [],
|
|
40
|
-
create(context) {
|
|
41
|
-
return {
|
|
42
|
-
CallExpression(node) {
|
|
43
|
-
if (node.callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression ||
|
|
44
|
-
node.callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier ||
|
|
45
|
-
node.callee.property.name !== 'query') {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
const args = node.arguments;
|
|
49
|
-
if (args.length === 0)
|
|
50
|
-
return;
|
|
51
|
-
const queryArg = args[0];
|
|
52
|
-
if (queryArg.type !== eslint_devkit_1.AST_NODE_TYPES.Literal || typeof queryArg.value !== 'string') {
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
const queryText = queryArg.value;
|
|
56
|
-
const upperQuery = queryText.toUpperCase();
|
|
57
|
-
// Exclude count(*) from the check by removing it temporarily
|
|
58
|
-
const cleanedQuery = upperQuery.replace(COUNT_STAR_REGEX, 'COUNT_ALL');
|
|
59
|
-
// Check for "SELECT *"
|
|
60
|
-
// 1. SELECT followed immediately by * (with spaces) -> SELECT *
|
|
61
|
-
// 2. Comma followed by * -> SELECT a, *
|
|
62
|
-
// BUT exclude SELECT * FROM UNNEST which is idiomatic for bulk inserts
|
|
63
|
-
if (SELECT_STAR_REGEX.test(cleanedQuery) || COMMA_STAR_REGEX.test(cleanedQuery)) {
|
|
64
|
-
// Ensure it is actually a SELECT query
|
|
65
|
-
if (SELECT_KEYWORD_REGEX.test(cleanedQuery)) {
|
|
66
|
-
// Exclude SELECT * FROM UNNEST (idiomatic bulk insert pattern)
|
|
67
|
-
if (UNNEST_PATTERN_REGEX.test(cleanedQuery)) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
context.report({
|
|
71
|
-
node: queryArg,
|
|
72
|
-
messageId: 'noSelectAll',
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
},
|
|
77
|
-
};
|
|
78
|
-
},
|
|
79
|
-
};
|
|
@@ -1,8 +0,0 @@
|
|
|
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
|
-
import { TSESLint } from '@interlace/eslint-devkit';
|
|
7
|
-
import { NoTransactionOnPoolOptions } from '../../types';
|
|
8
|
-
export declare const noTransactionOnPool: TSESLint.RuleModule<'noTransactionOnPool', NoTransactionOnPoolOptions>;
|
|
@@ -1,71 +0,0 @@
|
|
|
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.noTransactionOnPool = void 0;
|
|
9
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
-
// Pre-defined set of transaction keywords for fast lookup
|
|
11
|
-
const TRANSACTION_KEYWORDS = new Set(['BEGIN', 'COMMIT', 'ROLLBACK']);
|
|
12
|
-
exports.noTransactionOnPool = {
|
|
13
|
-
meta: {
|
|
14
|
-
type: 'problem',
|
|
15
|
-
docs: {
|
|
16
|
-
description: 'Prevent starting transactions directly on the Pool, which is unsafe due to lack of client affinity.',
|
|
17
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/no-transaction-on-pool.md',
|
|
18
|
-
cwe: 'CWE-662',
|
|
19
|
-
cweJustification: 'CWE-662 (Improper Synchronization) — running BEGIN/COMMIT on a connection pool can split a logical transaction across different physical clients, breaking ACID atomicity.',
|
|
20
|
-
confidence: 'high',
|
|
21
|
-
},
|
|
22
|
-
messages: {
|
|
23
|
-
noTransactionOnPool: (0, eslint_devkit_1.formatLLMMessage)({
|
|
24
|
-
icon: eslint_devkit_1.MessageIcons.WARNING,
|
|
25
|
-
issueName: 'Transaction on Pool',
|
|
26
|
-
description: 'Transactions should not be started on the Pool directly.',
|
|
27
|
-
severity: 'HIGH',
|
|
28
|
-
effort: 'low',
|
|
29
|
-
fix: 'Use "await pool.connect()" to get a client, then start the transaction on the client.',
|
|
30
|
-
documentationLink: 'https://node-postgres.com/features/transactions',
|
|
31
|
-
}),
|
|
32
|
-
},
|
|
33
|
-
schema: [],
|
|
34
|
-
},
|
|
35
|
-
defaultOptions: [],
|
|
36
|
-
create(context) {
|
|
37
|
-
return {
|
|
38
|
-
CallExpression(node) {
|
|
39
|
-
if (node.callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression ||
|
|
40
|
-
node.callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier ||
|
|
41
|
-
node.callee.property.name !== 'query') {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
const args = node.arguments;
|
|
45
|
-
if (args.length === 0)
|
|
46
|
-
return;
|
|
47
|
-
const queryArg = args[0];
|
|
48
|
-
// Only check literals
|
|
49
|
-
if (queryArg.type !== eslint_devkit_1.AST_NODE_TYPES.Literal || typeof queryArg.value !== 'string') {
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
const queryText = queryArg.value.toUpperCase().trim();
|
|
53
|
-
// Extract first word (handles "BEGIN", "BEGIN;", "BEGIN TRANSACTION", etc.)
|
|
54
|
-
const firstWord = queryText.split(/[;\s]/)[0];
|
|
55
|
-
if (TRANSACTION_KEYWORDS.has(firstWord)) {
|
|
56
|
-
// Heuristic: If the variable name is 'pool' or ends with 'Pool', stronger signal.
|
|
57
|
-
// However, for safety, if we see pool.query('BEGIN'), it's almost always wrong unless it's a test mock.
|
|
58
|
-
// Let's rely on variable name hint for false positive reduction, or just flag it generally.
|
|
59
|
-
// The most common error is `pool.query('BEGIN')`.
|
|
60
|
-
if (node.callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
61
|
-
(node.callee.object.name === 'pool' || node.callee.object.name.toLowerCase().includes('pool'))) {
|
|
62
|
-
context.report({
|
|
63
|
-
node: queryArg,
|
|
64
|
-
messageId: 'noTransactionOnPool',
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
},
|
|
69
|
-
};
|
|
70
|
-
},
|
|
71
|
-
};
|
|
@@ -1,10 +0,0 @@
|
|
|
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
|
-
import { TSESLint } from '@interlace/eslint-devkit';
|
|
7
|
-
import { NoUnsafeCopyFromOptions } from '../../types';
|
|
8
|
-
type MessageIds = 'dynamicPath' | 'hardcodedPath' | 'unverifiablePath';
|
|
9
|
-
export declare const noUnsafeCopyFrom: TSESLint.RuleModule<MessageIds, NoUnsafeCopyFromOptions>;
|
|
10
|
-
export {};
|
|
@@ -1,205 +0,0 @@
|
|
|
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.noUnsafeCopyFrom = void 0;
|
|
9
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
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
|
-
// Helper to check for COPY FROM (but not COPY FROM STDIN)
|
|
16
|
-
const hasCopyFrom = (str) => COPY_FROM_REGEX.test(str) && !STDIN_REGEX.test(str);
|
|
17
|
-
// Extract file path from COPY statement
|
|
18
|
-
const extractFilePath = (str) => {
|
|
19
|
-
const match = FILE_PATH_REGEX.exec(str);
|
|
20
|
-
return match ? match[1] : null;
|
|
21
|
-
};
|
|
22
|
-
exports.noUnsafeCopyFrom = {
|
|
23
|
-
meta: {
|
|
24
|
-
type: 'problem',
|
|
25
|
-
docs: {
|
|
26
|
-
description: 'Prevent unsafe COPY FROM usage with dynamic file paths, which can lead to arbitrary file read/RCE.',
|
|
27
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/no-unsafe-copy-from.md',
|
|
28
|
-
cwe: 'CWE-73',
|
|
29
|
-
cvss: 9.5,
|
|
30
|
-
},
|
|
31
|
-
messages: {
|
|
32
|
-
dynamicPath: (0, eslint_devkit_1.formatLLMMessage)({
|
|
33
|
-
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
34
|
-
issueName: 'COPY FROM Injection',
|
|
35
|
-
description: 'Dynamic file path in COPY FROM detected - potential arbitrary file read.',
|
|
36
|
-
severity: 'CRITICAL',
|
|
37
|
-
cwe: 'CWE-73',
|
|
38
|
-
owasp: 'A03:2021',
|
|
39
|
-
compliance: ['SOC2', 'PCI-DSS'],
|
|
40
|
-
effort: 'low',
|
|
41
|
-
fix: 'Never use user input in COPY FROM paths. Use COPY FROM STDIN for user data.',
|
|
42
|
-
documentationLink: 'https://www.postgresql.org/docs/current/sql-copy.html',
|
|
43
|
-
}),
|
|
44
|
-
hardcodedPath: (0, eslint_devkit_1.formatLLMMessage)({
|
|
45
|
-
icon: eslint_devkit_1.MessageIcons.WARNING,
|
|
46
|
-
issueName: 'Server-side COPY FROM',
|
|
47
|
-
description: 'Hardcoded file path in COPY FROM - server-side file access.',
|
|
48
|
-
severity: 'MEDIUM',
|
|
49
|
-
cwe: 'CWE-73',
|
|
50
|
-
effort: 'low',
|
|
51
|
-
fix: 'Prefer COPY FROM STDIN for application code. Use allowHardcodedPaths option if this is an admin script.',
|
|
52
|
-
documentationLink: 'https://www.postgresql.org/docs/current/sql-copy.html',
|
|
53
|
-
}),
|
|
54
|
-
unverifiablePath: (0, eslint_devkit_1.formatLLMMessage)({
|
|
55
|
-
icon: eslint_devkit_1.MessageIcons.WARNING,
|
|
56
|
-
issueName: 'Unverifiable COPY FROM',
|
|
57
|
-
description: 'Cannot statically verify COPY FROM source - potential injection risk.',
|
|
58
|
-
severity: 'MEDIUM',
|
|
59
|
-
cwe: 'CWE-73',
|
|
60
|
-
effort: 'medium',
|
|
61
|
-
fix: 'Ensure the query source does not contain user input, or refactor to use COPY FROM STDIN.',
|
|
62
|
-
documentationLink: 'https://www.postgresql.org/docs/current/sql-copy.html',
|
|
63
|
-
}),
|
|
64
|
-
},
|
|
65
|
-
schema: [
|
|
66
|
-
{
|
|
67
|
-
type: 'object',
|
|
68
|
-
properties: {
|
|
69
|
-
allowHardcodedPaths: {
|
|
70
|
-
type: 'boolean',
|
|
71
|
-
description: 'Allow hardcoded file paths (for admin/migration scripts)',
|
|
72
|
-
},
|
|
73
|
-
allowedPaths: {
|
|
74
|
-
type: 'array',
|
|
75
|
-
items: { type: 'string' },
|
|
76
|
-
description: 'List of allowed file path patterns (regex strings)',
|
|
77
|
-
},
|
|
78
|
-
},
|
|
79
|
-
additionalProperties: false,
|
|
80
|
-
},
|
|
81
|
-
],
|
|
82
|
-
},
|
|
83
|
-
defaultOptions: [{}],
|
|
84
|
-
create(context) {
|
|
85
|
-
const options = context.options[0] ?? {};
|
|
86
|
-
const allowHardcodedPaths = options.allowHardcodedPaths ?? false;
|
|
87
|
-
const allowedPaths = (options.allowedPaths ?? []).map((p) => new RegExp(p));
|
|
88
|
-
// Check if a file path matches any allowed pattern
|
|
89
|
-
const isPathAllowed = (filePath) => {
|
|
90
|
-
return allowedPaths.some((pattern) => pattern.test(filePath));
|
|
91
|
-
};
|
|
92
|
-
return {
|
|
93
|
-
CallExpression(node) {
|
|
94
|
-
if (node.callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression ||
|
|
95
|
-
node.callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier ||
|
|
96
|
-
node.callee.property.name !== 'query') {
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
const args = node.arguments;
|
|
100
|
-
if (args.length === 0)
|
|
101
|
-
return;
|
|
102
|
-
const queryArg = args[0];
|
|
103
|
-
// Case 1: Literal string - can fully analyze
|
|
104
|
-
if (queryArg.type === eslint_devkit_1.AST_NODE_TYPES.Literal && typeof queryArg.value === 'string') {
|
|
105
|
-
if (hasCopyFrom(queryArg.value)) {
|
|
106
|
-
const filePath = extractFilePath(queryArg.value);
|
|
107
|
-
// Check if path is in allowlist
|
|
108
|
-
if (filePath && isPathAllowed(filePath)) {
|
|
109
|
-
return; // Explicitly allowed
|
|
110
|
-
}
|
|
111
|
-
// Hardcoded path - report as medium severity (or skip if allowed)
|
|
112
|
-
if (allowHardcodedPaths) {
|
|
113
|
-
return; // User opted to allow hardcoded paths
|
|
114
|
-
}
|
|
115
|
-
context.report({
|
|
116
|
-
node: queryArg,
|
|
117
|
-
messageId: 'hardcodedPath',
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
// Case 2: Template literal - check for dynamic expressions
|
|
123
|
-
if (queryArg.type === eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral) {
|
|
124
|
-
const staticText = queryArg.quasis.map(q => q.value.raw).join(' ');
|
|
125
|
-
if (hasCopyFrom(staticText)) {
|
|
126
|
-
// Has expressions = dynamic = CRITICAL
|
|
127
|
-
if (queryArg.expressions.length > 0) {
|
|
128
|
-
context.report({
|
|
129
|
-
node: queryArg,
|
|
130
|
-
messageId: 'dynamicPath',
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
// No expressions = effectively a static string
|
|
135
|
-
const filePath = extractFilePath(staticText);
|
|
136
|
-
if (filePath && isPathAllowed(filePath)) {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
if (!allowHardcodedPaths) {
|
|
140
|
-
context.report({
|
|
141
|
-
node: queryArg,
|
|
142
|
-
messageId: 'hardcodedPath',
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
// Case 3: Binary expression (string concatenation)
|
|
150
|
-
if (queryArg.type === eslint_devkit_1.AST_NODE_TYPES.BinaryExpression) {
|
|
151
|
-
const parts = [];
|
|
152
|
-
let hasDynamicPart = false;
|
|
153
|
-
const stack = [queryArg];
|
|
154
|
-
for (let curr = stack.pop(); curr !== undefined; curr = stack.pop()) {
|
|
155
|
-
if (curr.type === eslint_devkit_1.AST_NODE_TYPES.BinaryExpression && curr.operator === '+') {
|
|
156
|
-
stack.push(curr.right);
|
|
157
|
-
stack.push(curr.left);
|
|
158
|
-
}
|
|
159
|
-
else if (curr.type === eslint_devkit_1.AST_NODE_TYPES.Literal && typeof curr.value === 'string') {
|
|
160
|
-
parts.push(curr.value);
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
// Non-literal part = dynamic
|
|
164
|
-
hasDynamicPart = true;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
const staticText = parts.join(' ');
|
|
168
|
-
if (hasCopyFrom(staticText)) {
|
|
169
|
-
if (hasDynamicPart) {
|
|
170
|
-
context.report({
|
|
171
|
-
node: queryArg,
|
|
172
|
-
messageId: 'dynamicPath',
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
else {
|
|
176
|
-
// All literals - treat as hardcoded
|
|
177
|
-
const filePath = extractFilePath(staticText);
|
|
178
|
-
if (filePath && isPathAllowed(filePath)) {
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
if (!allowHardcodedPaths) {
|
|
182
|
-
context.report({
|
|
183
|
-
node: queryArg,
|
|
184
|
-
messageId: 'hardcodedPath',
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
// Case 4: Identifier or CallExpression - cannot verify statically
|
|
192
|
-
if (queryArg.type === eslint_devkit_1.AST_NODE_TYPES.Identifier ||
|
|
193
|
-
queryArg.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
|
|
194
|
-
// We can't see what's in the variable/function - flag as unverifiable
|
|
195
|
-
// This catches: client.query(sqlQuery) or client.query(buildQuery())
|
|
196
|
-
// We report as medium severity since we can't confirm it's COPY FROM
|
|
197
|
-
// Actually, we can't even confirm it's COPY FROM, so let's not report
|
|
198
|
-
// unless we want to be very aggressive. For now, skip these.
|
|
199
|
-
// The user would need to ensure the source is safe.
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
},
|
|
203
|
-
};
|
|
204
|
-
},
|
|
205
|
-
};
|
|
@@ -1,8 +0,0 @@
|
|
|
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
|
-
import { TSESLint } from '@interlace/eslint-devkit';
|
|
7
|
-
import { NoUnsafeQueryOptions } from '../../types';
|
|
8
|
-
export declare const noUnsafeQuery: TSESLint.RuleModule<'noUnsafeQuery' | 'unsafeTemplateLiteral', NoUnsafeQueryOptions>;
|
|
@@ -1,132 +0,0 @@
|
|
|
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.noUnsafeQuery = void 0;
|
|
9
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
-
/**
|
|
11
|
-
* Check if a node contains unsafe string construction (concatenation or interpolation)
|
|
12
|
-
*/
|
|
13
|
-
function isUnsafeStringConstruction(node) {
|
|
14
|
-
// Direct concatenation: "SELECT..." + var
|
|
15
|
-
if (node.type === eslint_devkit_1.AST_NODE_TYPES.BinaryExpression && node.operator === '+') {
|
|
16
|
-
return 'concat';
|
|
17
|
-
}
|
|
18
|
-
// Template literal with expressions: `SELECT...${var}`
|
|
19
|
-
if (node.type === eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral && node.expressions.length > 0) {
|
|
20
|
-
return 'template';
|
|
21
|
-
}
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
|
-
exports.noUnsafeQuery = {
|
|
25
|
-
meta: {
|
|
26
|
-
type: 'problem',
|
|
27
|
-
// CWE / CVSS lifted to meta.docs (Interlace extension) so
|
|
28
|
-
// @interlace/eslint-formatter renders them inline. Previously these
|
|
29
|
-
// values lived only inside the `messages` factory below, where the
|
|
30
|
-
// whole-run formatter cannot see them. See docs/META_HYGIENE.md for
|
|
31
|
-
// the fleet-wide audit and tracker P1 #5 for the rollout plan.
|
|
32
|
-
docs: {
|
|
33
|
-
description: 'Prevent SQL injection by disallowing string concatenation or unsafe template literals in queries.',
|
|
34
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/no-unsafe-query.md',
|
|
35
|
-
// CWE / CVSS surfaces in the formatter (devkit augments RuleMetaDataDocs).
|
|
36
|
-
cwe: 'CWE-89',
|
|
37
|
-
cvss: 9.8,
|
|
38
|
-
confidence: 'high',
|
|
39
|
-
},
|
|
40
|
-
messages: {
|
|
41
|
-
noUnsafeQuery: (0, eslint_devkit_1.formatLLMMessage)({
|
|
42
|
-
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
43
|
-
issueName: 'SQL Injection Risk',
|
|
44
|
-
description: 'Unsafe SQL query detected. Variable interpolation found.',
|
|
45
|
-
severity: 'CRITICAL',
|
|
46
|
-
cwe: 'CWE-89',
|
|
47
|
-
owasp: 'A03:2021',
|
|
48
|
-
compliance: ['SOC2', 'PCI-DSS', 'NIST-CSF'],
|
|
49
|
-
effort: 'high',
|
|
50
|
-
fix: 'Use parameterized queries ($1, $2) instead of string concatenation.',
|
|
51
|
-
documentationLink: 'https://node-postgres.com/features/queries#parameterized-queries',
|
|
52
|
-
}),
|
|
53
|
-
unsafeTemplateLiteral: (0, eslint_devkit_1.formatLLMMessage)({
|
|
54
|
-
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
55
|
-
issueName: 'SQL Injection Risk',
|
|
56
|
-
description: 'Unsafe SQL query construction detected (template literal).',
|
|
57
|
-
severity: 'CRITICAL',
|
|
58
|
-
fix: 'Use parameterized queries ($1, $2) instead of interpolating values.',
|
|
59
|
-
documentationLink: 'https://owasp.org/www-community/attacks/SQL_Injection',
|
|
60
|
-
}),
|
|
61
|
-
},
|
|
62
|
-
schema: [],
|
|
63
|
-
},
|
|
64
|
-
defaultOptions: [],
|
|
65
|
-
create(context) {
|
|
66
|
-
// Track variables tainted by unsafe string construction
|
|
67
|
-
const taintedVariables = new Map();
|
|
68
|
-
return {
|
|
69
|
-
// Track variable declarations with unsafe init:
|
|
70
|
-
// const query = "SELECT..." + userId;
|
|
71
|
-
// const query = `SELECT...${email}`;
|
|
72
|
-
VariableDeclarator(node) {
|
|
73
|
-
if (node.id.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
74
|
-
node.init) {
|
|
75
|
-
const unsafe = isUnsafeStringConstruction(node.init);
|
|
76
|
-
if (unsafe) {
|
|
77
|
-
taintedVariables.set(node.id.name, unsafe);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
},
|
|
81
|
-
// Track augmented assignment: query += " AND ..." + var
|
|
82
|
-
// or: query += `...${var}`
|
|
83
|
-
AssignmentExpression(node) {
|
|
84
|
-
if (node.operator === '+=' &&
|
|
85
|
-
node.left.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
86
|
-
const unsafe = isUnsafeStringConstruction(node.right);
|
|
87
|
-
if (unsafe) {
|
|
88
|
-
taintedVariables.set(node.left.name, unsafe);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
},
|
|
92
|
-
CallExpression(node) {
|
|
93
|
-
// Check if the method being called is 'query'
|
|
94
|
-
if (node.callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression ||
|
|
95
|
-
node.callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier ||
|
|
96
|
-
node.callee.property.name !== 'query') {
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
const args = node.arguments;
|
|
100
|
-
if (args.length === 0)
|
|
101
|
-
return;
|
|
102
|
-
const queryArg = args[0];
|
|
103
|
-
// 1. Check for String Concatenation: query('SELECT * FROM users WHERE id = ' + id)
|
|
104
|
-
if (queryArg.type === eslint_devkit_1.AST_NODE_TYPES.BinaryExpression && queryArg.operator === '+') {
|
|
105
|
-
context.report({
|
|
106
|
-
node: queryArg,
|
|
107
|
-
messageId: 'noUnsafeQuery',
|
|
108
|
-
});
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
// 2. Check for Template Literals with Expressions: query(`SELECT * FROM users WHERE id = ${id}`)
|
|
112
|
-
if (queryArg.type === eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral && queryArg.expressions.length > 0) {
|
|
113
|
-
context.report({
|
|
114
|
-
node: queryArg,
|
|
115
|
-
messageId: 'unsafeTemplateLiteral',
|
|
116
|
-
});
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
// 3. Check for tainted variables: const q = "..." + var; db.query(q)
|
|
120
|
-
if (queryArg.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
121
|
-
const taint = taintedVariables.get(queryArg.name);
|
|
122
|
-
if (taint) {
|
|
123
|
-
context.report({
|
|
124
|
-
node: queryArg,
|
|
125
|
-
messageId: taint === 'template' ? 'unsafeTemplateLiteral' : 'noUnsafeQuery',
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
},
|
|
130
|
-
};
|
|
131
|
-
},
|
|
132
|
-
};
|
|
@@ -1,8 +0,0 @@
|
|
|
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
|
-
import { TSESLint } from '@interlace/eslint-devkit';
|
|
7
|
-
import { NoUnsafeSearchPathOptions } from '../../types';
|
|
8
|
-
export declare const noUnsafeSearchPath: TSESLint.RuleModule<'noUnsafeSearchPath', NoUnsafeSearchPathOptions>;
|