eslint-plugin-postgresql-security 1.5.0 → 1.5.1
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 +4 -0
- package/src/index.d.ts +17 -0
- package/src/index.js +75 -0
- package/src/oxlint.d.ts +17 -0
- package/src/oxlint.js +3 -0
- package/{dist/src → src}/rules/check-query-params/index.js +2 -13
- package/{dist/src → src}/rules/no-batch-insert-loop/index.js +1 -14
- package/{dist/src → src}/rules/no-floating-query/index.js +1 -16
- package/{dist/src → src}/rules/no-hardcoded-credentials/index.js +1 -10
- package/{dist/src → src}/rules/no-insecure-ssl/index.js +0 -6
- package/{dist/src → src}/rules/no-missing-client-release/index.js +0 -17
- package/{dist/src → src}/rules/no-select-all/index.js +0 -13
- package/{dist/src → src}/rules/no-transaction-on-pool/index.js +0 -12
- package/{dist/src → src}/rules/no-unsafe-copy-from/index.js +2 -28
- package/{dist/src → src}/rules/no-unsafe-query/index.js +0 -26
- package/{dist/src → src}/rules/no-unsafe-search-path/index.js +0 -43
- package/{dist/src → src}/rules/prefer-pool-query/index.js +3 -20
- package/{dist/src → src}/rules/prevent-double-release/index.js +0 -75
- package/src/types/index.js +2 -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/no-batch-insert-loop/index.d.ts +0 -8
- package/dist/src/rules/no-floating-query/index.d.ts +0 -8
- package/dist/src/rules/no-hardcoded-credentials/index.d.ts +0 -8
- package/dist/src/rules/no-insecure-ssl/index.d.ts +0 -8
- package/dist/src/rules/no-missing-client-release/index.d.ts +0 -8
- package/dist/src/rules/no-select-all/index.d.ts +0 -8
- package/dist/src/rules/no-transaction-on-pool/index.d.ts +0 -8
- package/dist/src/rules/no-unsafe-copy-from/index.d.ts +0 -10
- package/dist/src/rules/no-unsafe-query/index.d.ts +0 -8
- package/dist/src/rules/no-unsafe-search-path/index.d.ts +0 -8
- package/dist/src/rules/prefer-pool-query/index.d.ts +0 -8
- package/dist/src/rules/prevent-double-release/index.d.ts +0 -8
- 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,21 +1,11 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.noUnsafeQuery = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
-
/**
|
|
11
|
-
* Check if a node contains unsafe string construction (concatenation or interpolation)
|
|
12
|
-
*/
|
|
13
5
|
function isUnsafeStringConstruction(node) {
|
|
14
|
-
// Direct concatenation: "SELECT..." + var
|
|
15
6
|
if (node.type === eslint_devkit_1.AST_NODE_TYPES.BinaryExpression && node.operator === '+') {
|
|
16
7
|
return 'concat';
|
|
17
8
|
}
|
|
18
|
-
// Template literal with expressions: `SELECT...${var}`
|
|
19
9
|
if (node.type === eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral && node.expressions.length > 0) {
|
|
20
10
|
return 'template';
|
|
21
11
|
}
|
|
@@ -24,15 +14,9 @@ function isUnsafeStringConstruction(node) {
|
|
|
24
14
|
exports.noUnsafeQuery = {
|
|
25
15
|
meta: {
|
|
26
16
|
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
17
|
docs: {
|
|
33
18
|
description: 'Prevent SQL injection by disallowing string concatenation or unsafe template literals in queries.',
|
|
34
19
|
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
20
|
cwe: 'CWE-89',
|
|
37
21
|
cvss: 9.8,
|
|
38
22
|
confidence: 'high',
|
|
@@ -63,12 +47,8 @@ exports.noUnsafeQuery = {
|
|
|
63
47
|
},
|
|
64
48
|
defaultOptions: [],
|
|
65
49
|
create(context) {
|
|
66
|
-
// Track variables tainted by unsafe string construction
|
|
67
50
|
const taintedVariables = new Map();
|
|
68
51
|
return {
|
|
69
|
-
// Track variable declarations with unsafe init:
|
|
70
|
-
// const query = "SELECT..." + userId;
|
|
71
|
-
// const query = `SELECT...${email}`;
|
|
72
52
|
VariableDeclarator(node) {
|
|
73
53
|
if (node.id.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
74
54
|
node.init) {
|
|
@@ -78,8 +58,6 @@ exports.noUnsafeQuery = {
|
|
|
78
58
|
}
|
|
79
59
|
}
|
|
80
60
|
},
|
|
81
|
-
// Track augmented assignment: query += " AND ..." + var
|
|
82
|
-
// or: query += `...${var}`
|
|
83
61
|
AssignmentExpression(node) {
|
|
84
62
|
if (node.operator === '+=' &&
|
|
85
63
|
node.left.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
@@ -90,7 +68,6 @@ exports.noUnsafeQuery = {
|
|
|
90
68
|
}
|
|
91
69
|
},
|
|
92
70
|
CallExpression(node) {
|
|
93
|
-
// Check if the method being called is 'query'
|
|
94
71
|
if (node.callee.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression ||
|
|
95
72
|
node.callee.property.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier ||
|
|
96
73
|
node.callee.property.name !== 'query') {
|
|
@@ -100,7 +77,6 @@ exports.noUnsafeQuery = {
|
|
|
100
77
|
if (args.length === 0)
|
|
101
78
|
return;
|
|
102
79
|
const queryArg = args[0];
|
|
103
|
-
// 1. Check for String Concatenation: query('SELECT * FROM users WHERE id = ' + id)
|
|
104
80
|
if (queryArg.type === eslint_devkit_1.AST_NODE_TYPES.BinaryExpression && queryArg.operator === '+') {
|
|
105
81
|
context.report({
|
|
106
82
|
node: queryArg,
|
|
@@ -108,7 +84,6 @@ exports.noUnsafeQuery = {
|
|
|
108
84
|
});
|
|
109
85
|
return;
|
|
110
86
|
}
|
|
111
|
-
// 2. Check for Template Literals with Expressions: query(`SELECT * FROM users WHERE id = ${id}`)
|
|
112
87
|
if (queryArg.type === eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral && queryArg.expressions.length > 0) {
|
|
113
88
|
context.report({
|
|
114
89
|
node: queryArg,
|
|
@@ -116,7 +91,6 @@ exports.noUnsafeQuery = {
|
|
|
116
91
|
});
|
|
117
92
|
return;
|
|
118
93
|
}
|
|
119
|
-
// 3. Check for tainted variables: const q = "..." + var; db.query(q)
|
|
120
94
|
if (queryArg.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
121
95
|
const taint = taintedVariables.get(queryArg.name);
|
|
122
96
|
if (taint) {
|
|
@@ -1,13 +1,7 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.noUnsafeSearchPath = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
-
// Pre-compiled pattern check for search_path
|
|
11
5
|
const hasSearchPath = (str) => str.toLowerCase().includes('set search_path');
|
|
12
6
|
exports.noUnsafeSearchPath = {
|
|
13
7
|
meta: {
|
|
@@ -47,44 +41,14 @@ exports.noUnsafeSearchPath = {
|
|
|
47
41
|
if (args.length === 0)
|
|
48
42
|
return;
|
|
49
43
|
const queryArg = args[0];
|
|
50
|
-
// 1. Literal Check: "SET search_path = " + variable
|
|
51
44
|
if (queryArg.type === eslint_devkit_1.AST_NODE_TYPES.BinaryExpression && queryArg.operator === '+') {
|
|
52
|
-
// We need to check if the left or right side contains "SET search_path"
|
|
53
|
-
// This requires a bit of deep traversal if it's a chain, but let's check simple cases first.
|
|
54
|
-
// Or, we can just check if *any* part of the expression is a string literal containing "SET search_path"
|
|
55
|
-
// A simpler approach for BinaryExpression:
|
|
56
|
-
// If it isn't a Literal, it's dynamic. If we can prove it contains "SET search_path", flag it.
|
|
57
|
-
// But statically analyzing the concatenated string is hard.
|
|
58
|
-
// However, we ALREADY forbid string concatenation in queries via `no-unsafe-query`.
|
|
59
|
-
// So this rule specifically should target WHERE `no-unsafe-query` might miss (e.g. valid syntax but unsafe LOGIC),
|
|
60
|
-
// OR reinforce it.
|
|
61
|
-
// Actually `no-unsafe-query` blocks `query('SET search_path = ' + val)`.
|
|
62
|
-
// But what about `query('SET search_path = $1', [val])`?
|
|
63
|
-
// PostgreSQL DOES NOT allow parameters for `SET search_path`. It must be a literal identifier.
|
|
64
|
-
// usages like `SET search_path TO $1` throw syntax error in PG.
|
|
65
|
-
// So users MUST use string interpolation or literal.
|
|
66
|
-
// `no-unsafe-query` will block the string interpolation/concatenation.
|
|
67
|
-
// So the only "safe" way usually is `query('SET search_path = public')`.
|
|
68
|
-
// Wait, if `no-unsafe-query` blocks all dynamic queries, then `no-unsafe-search-path` is redundant?
|
|
69
|
-
// Not quite. `no-unsafe-query` allows template literals without expressions.
|
|
70
|
-
// It blocks concatenation.
|
|
71
|
-
// But sometimes people disable `no-unsafe-query` or use helpers.
|
|
72
|
-
// Also, `pg-format` usage: `format('SET search_path = %I', val)`.
|
|
73
|
-
// This IS safe from SQLi in general, but for `search_path`, allows changing to ARBITRARY schema.
|
|
74
|
-
// If attacker controls `val`, they can point to `hack_public` schema.
|
|
75
|
-
// So we want to ensure `search_path` is HARDCODED or strictly validated.
|
|
76
|
-
// Let's detect `SET search_path` in any string literal part of the arg.
|
|
77
|
-
// If we find it, and the argument is NOT a pure String Literal (i.e. it involves vars/functions), WARN.
|
|
78
45
|
}
|
|
79
46
|
if (queryArg.type === eslint_devkit_1.AST_NODE_TYPES.Literal) {
|
|
80
47
|
if (typeof queryArg.value === 'string' && hasSearchPath(queryArg.value)) {
|
|
81
|
-
// Safe: Literal string "SET search_path = public"
|
|
82
48
|
return;
|
|
83
49
|
}
|
|
84
50
|
}
|
|
85
|
-
// If it's a TemplateLiteral
|
|
86
51
|
if (queryArg.type === eslint_devkit_1.AST_NODE_TYPES.TemplateLiteral) {
|
|
87
|
-
// Check quasis
|
|
88
52
|
const rawText = queryArg.quasis.map(q => q.value.raw).join(' ');
|
|
89
53
|
if (hasSearchPath(rawText)) {
|
|
90
54
|
if (queryArg.expressions.length > 0) {
|
|
@@ -96,11 +60,7 @@ exports.noUnsafeSearchPath = {
|
|
|
96
60
|
}
|
|
97
61
|
return;
|
|
98
62
|
}
|
|
99
|
-
// If it's a BinaryExpression (concat)
|
|
100
63
|
if (queryArg.type === eslint_devkit_1.AST_NODE_TYPES.BinaryExpression) {
|
|
101
|
-
// We can't easily reconstruction the full string.
|
|
102
|
-
// But if we detect "search_path" in any literal part, and it's being concatenated, flag it.
|
|
103
|
-
// This is a simplified visitor for the binary expression tree
|
|
104
64
|
const parts = [];
|
|
105
65
|
const stack = [queryArg];
|
|
106
66
|
for (let curr = stack.pop(); curr !== undefined; curr = stack.pop()) {
|
|
@@ -121,14 +81,11 @@ exports.noUnsafeSearchPath = {
|
|
|
121
81
|
}
|
|
122
82
|
return;
|
|
123
83
|
}
|
|
124
|
-
// CallExpression (e.g. pg-format)
|
|
125
|
-
// format('SET search_path = %I', schema) -> This is arguably unsafe if schema is user-controlled.
|
|
126
84
|
if (queryArg.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
|
|
127
85
|
if (queryArg.arguments.length > 0 &&
|
|
128
86
|
queryArg.arguments[0].type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
129
87
|
typeof queryArg.arguments[0].value === 'string' &&
|
|
130
88
|
hasSearchPath(queryArg.arguments[0].value)) {
|
|
131
|
-
// Fix: If all arguments are Literals, assume it is safe.
|
|
132
89
|
const allLiterals = queryArg.arguments.every(arg => arg.type === eslint_devkit_1.AST_NODE_TYPES.Literal);
|
|
133
90
|
if (allLiterals)
|
|
134
91
|
return;
|
|
@@ -1,9 +1,4 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.preferPoolQuery = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
@@ -32,9 +27,6 @@ exports.preferPoolQuery = {
|
|
|
32
27
|
create(context) {
|
|
33
28
|
return {
|
|
34
29
|
VariableDeclarator(node) {
|
|
35
|
-
// Look for: const client = await pool.connect()
|
|
36
|
-
// And ensure it is used for exactly one query, then released.
|
|
37
|
-
// This is complex for a perfect check, but we can look for basic patterns.
|
|
38
30
|
if (!node.init ||
|
|
39
31
|
node.init.type !== eslint_devkit_1.AST_NODE_TYPES.AwaitExpression ||
|
|
40
32
|
node.init.argument.type !== eslint_devkit_1.AST_NODE_TYPES.CallExpression ||
|
|
@@ -47,28 +39,19 @@ exports.preferPoolQuery = {
|
|
|
47
39
|
if (!variable)
|
|
48
40
|
return;
|
|
49
41
|
const references = variable.references;
|
|
50
|
-
// Count meaningful usages
|
|
51
|
-
// 1. Definition (init)
|
|
52
|
-
// 2. usage in query
|
|
53
|
-
// 3. usage in release
|
|
54
|
-
// roughly 3 refs total? (maybe 2 if release is forgot, but that is handled by other rule)
|
|
55
|
-
// If we have just: connect, query, release.
|
|
56
|
-
// And NO transactions (BEGIN/COMMIT).
|
|
57
42
|
let queryCallCount = 0;
|
|
58
43
|
let releaseCallCount = 0;
|
|
59
44
|
let otherUsageCount = 0;
|
|
60
45
|
for (const ref of references) {
|
|
61
46
|
if (ref.isWrite())
|
|
62
|
-
continue;
|
|
47
|
+
continue;
|
|
63
48
|
const id = ref.identifier;
|
|
64
|
-
// Check for member access
|
|
65
49
|
if (id.parent?.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
66
50
|
id.parent.object === id &&
|
|
67
51
|
id.parent.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
68
52
|
const method = id.parent.property.name;
|
|
69
53
|
if (method === 'query') {
|
|
70
54
|
queryCallCount++;
|
|
71
|
-
// Check if it is a transaction command
|
|
72
55
|
if (id.parent.parent?.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
|
|
73
56
|
const call = id.parent.parent;
|
|
74
57
|
if (call.arguments.length > 0 &&
|
|
@@ -76,7 +59,7 @@ exports.preferPoolQuery = {
|
|
|
76
59
|
typeof call.arguments[0].value === 'string') {
|
|
77
60
|
const q = call.arguments[0].value.toUpperCase().trim();
|
|
78
61
|
if (q.startsWith('BEGIN') || q.startsWith('COMMIT') || q.startsWith('ROLLBACK')) {
|
|
79
|
-
return;
|
|
62
|
+
return;
|
|
80
63
|
}
|
|
81
64
|
}
|
|
82
65
|
}
|
|
@@ -89,7 +72,7 @@ exports.preferPoolQuery = {
|
|
|
89
72
|
}
|
|
90
73
|
}
|
|
91
74
|
else {
|
|
92
|
-
otherUsageCount++;
|
|
75
|
+
otherUsageCount++;
|
|
93
76
|
}
|
|
94
77
|
}
|
|
95
78
|
if (queryCallCount === 1 && releaseCallCount === 1 && otherUsageCount === 0) {
|
|
@@ -1,15 +1,7 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.preventDoubleRelease = void 0;
|
|
9
4
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
|
-
/**
|
|
11
|
-
* Finds the nearest ancestor of a given type.
|
|
12
|
-
*/
|
|
13
5
|
function findAncestor(node, predicate) {
|
|
14
6
|
let current = node?.parent;
|
|
15
7
|
while (current) {
|
|
@@ -19,15 +11,9 @@ function findAncestor(node, predicate) {
|
|
|
19
11
|
}
|
|
20
12
|
return null;
|
|
21
13
|
}
|
|
22
|
-
/**
|
|
23
|
-
* Checks if node is inside a TryStatement's catch block
|
|
24
|
-
*/
|
|
25
14
|
function isInCatchBlock(node) {
|
|
26
15
|
return findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.CatchClause);
|
|
27
16
|
}
|
|
28
|
-
/**
|
|
29
|
-
* Checks if node is inside a TryStatement's finally block
|
|
30
|
-
*/
|
|
31
17
|
function isInFinallyBlock(node) {
|
|
32
18
|
const tryStmt = findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.TryStatement);
|
|
33
19
|
if (!tryStmt?.finalizer)
|
|
@@ -39,9 +25,6 @@ function isInFinallyBlock(node) {
|
|
|
39
25
|
}
|
|
40
26
|
return null;
|
|
41
27
|
}
|
|
42
|
-
/**
|
|
43
|
-
* Checks if node is inside a TryStatement's try block
|
|
44
|
-
*/
|
|
45
28
|
function isInTryBlock(node) {
|
|
46
29
|
const tryStmt = findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.TryStatement);
|
|
47
30
|
if (!tryStmt)
|
|
@@ -53,23 +36,16 @@ function isInTryBlock(node) {
|
|
|
53
36
|
}
|
|
54
37
|
return null;
|
|
55
38
|
}
|
|
56
|
-
/**
|
|
57
|
-
* Gets the containing function for a node
|
|
58
|
-
*/
|
|
59
39
|
function getContainingFunction(node) {
|
|
60
40
|
return findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.FunctionDeclaration ||
|
|
61
41
|
n.type === eslint_devkit_1.AST_NODE_TYPES.FunctionExpression ||
|
|
62
42
|
n.type === eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression);
|
|
63
43
|
}
|
|
64
|
-
/**
|
|
65
|
-
* Checks if node is guarded by a conditional check (e.g., if (!released))
|
|
66
|
-
*/
|
|
67
44
|
function isGuardedByCondition(node) {
|
|
68
45
|
const ifStmt = findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement);
|
|
69
46
|
if (!ifStmt)
|
|
70
47
|
return false;
|
|
71
48
|
const conditionText = ifStmt.test;
|
|
72
|
-
// Check for patterns like: !released, released === false, !client.released
|
|
73
49
|
if (conditionText.type === eslint_devkit_1.AST_NODE_TYPES.UnaryExpression && conditionText.operator === '!') {
|
|
74
50
|
const arg = conditionText.argument;
|
|
75
51
|
if (arg.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
@@ -83,15 +59,11 @@ function isGuardedByCondition(node) {
|
|
|
83
59
|
}
|
|
84
60
|
return false;
|
|
85
61
|
}
|
|
86
|
-
/**
|
|
87
|
-
* Checks if a node is in an else-if/else branch that's mutually exclusive from another
|
|
88
|
-
*/
|
|
89
62
|
function areMutuallyExclusiveBranches(nodeA, nodeB) {
|
|
90
63
|
const ifA = findAncestor(nodeA, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement);
|
|
91
64
|
const ifB = findAncestor(nodeB, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement);
|
|
92
65
|
if (!ifA || !ifB)
|
|
93
66
|
return false;
|
|
94
|
-
// Check if both are in the same if/else chain
|
|
95
67
|
if (ifA === ifB) {
|
|
96
68
|
const nodeARange = nodeA.range;
|
|
97
69
|
const nodeBRange = nodeB.range;
|
|
@@ -107,9 +79,6 @@ function areMutuallyExclusiveBranches(nodeA, nodeB) {
|
|
|
107
79
|
}
|
|
108
80
|
return false;
|
|
109
81
|
}
|
|
110
|
-
/**
|
|
111
|
-
* Checks if there's a return/throw statement between two positions in a block
|
|
112
|
-
*/
|
|
113
82
|
function hasExitBetween(block, startIdx, endIdx) {
|
|
114
83
|
for (let k = startIdx + 1; k < endIdx; k++) {
|
|
115
84
|
const stmt = block.body[k];
|
|
@@ -120,9 +89,6 @@ function hasExitBetween(block, startIdx, endIdx) {
|
|
|
120
89
|
}
|
|
121
90
|
return false;
|
|
122
91
|
}
|
|
123
|
-
/**
|
|
124
|
-
* Checks if a switch case has a break/return/throw
|
|
125
|
-
*/
|
|
126
92
|
function caseHasExit(switchCase) {
|
|
127
93
|
for (const stmt of switchCase.consequent) {
|
|
128
94
|
if (stmt.type === eslint_devkit_1.AST_NODE_TYPES.ReturnStatement)
|
|
@@ -134,23 +100,13 @@ function caseHasExit(switchCase) {
|
|
|
134
100
|
}
|
|
135
101
|
return false;
|
|
136
102
|
}
|
|
137
|
-
/**
|
|
138
|
-
* Gets the switch case containing a node
|
|
139
|
-
*/
|
|
140
103
|
function getSwitchCase(node) {
|
|
141
104
|
return findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.SwitchCase);
|
|
142
105
|
}
|
|
143
|
-
/**
|
|
144
|
-
* Gets the if statement directly containing a node (not ancestors)
|
|
145
|
-
*/
|
|
146
106
|
function getDirectIfStatement(node) {
|
|
147
107
|
return findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement);
|
|
148
108
|
}
|
|
149
|
-
/**
|
|
150
|
-
* Checks if the if statement's branch containing the release has a return/throw after it
|
|
151
|
-
*/
|
|
152
109
|
function ifBranchHasExitAfterRelease(releaseNode, ifStmt) {
|
|
153
|
-
// Find which branch contains the release
|
|
154
110
|
const releaseRange = releaseNode.range;
|
|
155
111
|
const consequentRange = ifStmt.consequent.range;
|
|
156
112
|
const inConsequent = releaseRange[0] >= consequentRange[0] && releaseRange[1] <= consequentRange[1];
|
|
@@ -163,7 +119,6 @@ function ifBranchHasExitAfterRelease(releaseNode, ifStmt) {
|
|
|
163
119
|
}
|
|
164
120
|
if (!branchBlock)
|
|
165
121
|
return false;
|
|
166
|
-
// Find the release statement index
|
|
167
122
|
const releaseParentStmt = releaseNode.parent?.type === eslint_devkit_1.AST_NODE_TYPES.ExpressionStatement
|
|
168
123
|
? releaseNode.parent
|
|
169
124
|
: null;
|
|
@@ -172,7 +127,6 @@ function ifBranchHasExitAfterRelease(releaseNode, ifStmt) {
|
|
|
172
127
|
const releaseIdx = branchBlock.body.indexOf(releaseParentStmt);
|
|
173
128
|
if (releaseIdx === -1)
|
|
174
129
|
return false;
|
|
175
|
-
// Check if there's a return/throw after the release
|
|
176
130
|
for (let i = releaseIdx + 1; i < branchBlock.body.length; i++) {
|
|
177
131
|
const stmt = branchBlock.body[i];
|
|
178
132
|
if (stmt.type === eslint_devkit_1.AST_NODE_TYPES.ReturnStatement || stmt.type === eslint_devkit_1.AST_NODE_TYPES.ThrowStatement) {
|
|
@@ -253,21 +207,16 @@ exports.preventDoubleRelease = {
|
|
|
253
207
|
for (let j = i + 1; j < releaseCalls.length; j++) {
|
|
254
208
|
const callA = releaseCalls[i];
|
|
255
209
|
const callB = releaseCalls[j];
|
|
256
|
-
// Skip if already reported
|
|
257
210
|
if (reported.has(callB.node))
|
|
258
211
|
continue;
|
|
259
|
-
// Skip if both are guarded
|
|
260
212
|
if (callA.guarded && callB.guarded)
|
|
261
213
|
continue;
|
|
262
|
-
// Skip if mutually exclusive branches
|
|
263
214
|
if (areMutuallyExclusiveBranches(callA.node, callB.node))
|
|
264
215
|
continue;
|
|
265
|
-
// Same function scope check
|
|
266
216
|
const funcA = getContainingFunction(callA.node);
|
|
267
217
|
const funcB = getContainingFunction(callB.node);
|
|
268
218
|
if (funcA !== funcB)
|
|
269
219
|
continue;
|
|
270
|
-
// Case 1: Try + Catch (not finally) - both paths release
|
|
271
220
|
const tryA = isInTryBlock(callA.node);
|
|
272
221
|
const catchB = isInCatchBlock(callB.node);
|
|
273
222
|
if (tryA && catchB && catchB.parent === tryA) {
|
|
@@ -275,7 +224,6 @@ exports.preventDoubleRelease = {
|
|
|
275
224
|
reported.add(callB.node);
|
|
276
225
|
continue;
|
|
277
226
|
}
|
|
278
|
-
// Case 2: Catch + Try (reversed order in source)
|
|
279
227
|
const catchA = isInCatchBlock(callA.node);
|
|
280
228
|
const tryB = isInTryBlock(callB.node);
|
|
281
229
|
if (catchA && tryB && catchA.parent === tryB) {
|
|
@@ -283,15 +231,12 @@ exports.preventDoubleRelease = {
|
|
|
283
231
|
reported.add(callB.node);
|
|
284
232
|
continue;
|
|
285
233
|
}
|
|
286
|
-
// Case 3: Switch fallthrough
|
|
287
234
|
const switchCaseA = getSwitchCase(callA.node);
|
|
288
235
|
const switchCaseB = getSwitchCase(callB.node);
|
|
289
236
|
if (switchCaseA && switchCaseB && switchCaseA !== switchCaseB) {
|
|
290
|
-
// Check if same switch statement
|
|
291
237
|
const switchA = findAncestor(switchCaseA, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.SwitchStatement);
|
|
292
238
|
const switchB = findAncestor(switchCaseB, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.SwitchStatement);
|
|
293
239
|
if (switchA === switchB && switchA) {
|
|
294
|
-
// Check if caseA doesn't have break/return/throw (fallthrough)
|
|
295
240
|
if (!caseHasExit(switchCaseA)) {
|
|
296
241
|
context.report({ node: callB.node, messageId: 'doubleRelease' });
|
|
297
242
|
reported.add(callB.node);
|
|
@@ -299,18 +244,14 @@ exports.preventDoubleRelease = {
|
|
|
299
244
|
}
|
|
300
245
|
}
|
|
301
246
|
}
|
|
302
|
-
// Case 4: If without else + sequential release
|
|
303
247
|
const ifA = getDirectIfStatement(callA.node);
|
|
304
248
|
if (ifA && !ifA.alternate) {
|
|
305
|
-
// B is after the if statement
|
|
306
249
|
if (callB.node.range[0] > ifA.range[1]) {
|
|
307
|
-
// Check if B is in same block as the if
|
|
308
250
|
const parentBlock = findAncestor(ifA, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement);
|
|
309
251
|
if (parentBlock) {
|
|
310
252
|
const bParentStmt = callB.node.parent?.type === eslint_devkit_1.AST_NODE_TYPES.ExpressionStatement
|
|
311
253
|
? callB.node.parent : null;
|
|
312
254
|
if (bParentStmt && parentBlock.body.includes(bParentStmt)) {
|
|
313
|
-
// Check if the if branch has exit after release
|
|
314
255
|
if (!ifBranchHasExitAfterRelease(callA.node, ifA)) {
|
|
315
256
|
context.report({ node: callB.node, messageId: 'doubleRelease' });
|
|
316
257
|
reported.add(callB.node);
|
|
@@ -320,14 +261,11 @@ exports.preventDoubleRelease = {
|
|
|
320
261
|
}
|
|
321
262
|
}
|
|
322
263
|
}
|
|
323
|
-
// Case 5: Two sequential if statements (neither has else)
|
|
324
264
|
const ifB = getDirectIfStatement(callB.node);
|
|
325
265
|
if (ifA && ifB && ifA !== ifB && !ifA.alternate && !ifB.alternate) {
|
|
326
|
-
// Both are different if statements at same level
|
|
327
266
|
const parentBlockA = ifA.parent;
|
|
328
267
|
const parentBlockB = ifB.parent;
|
|
329
268
|
if (parentBlockA === parentBlockB && parentBlockA?.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement) {
|
|
330
|
-
// Check if ifA doesn't exit after release
|
|
331
269
|
if (!ifBranchHasExitAfterRelease(callA.node, ifA)) {
|
|
332
270
|
context.report({ node: callB.node, messageId: 'doubleRelease' });
|
|
333
271
|
reported.add(callB.node);
|
|
@@ -337,7 +275,6 @@ exports.preventDoubleRelease = {
|
|
|
337
275
|
}
|
|
338
276
|
const parentA = callA.node.parent?.parent;
|
|
339
277
|
const parentB = callB.node.parent?.parent;
|
|
340
|
-
// Case 6: Same Block - direct sequential releases
|
|
341
278
|
if (parentA &&
|
|
342
279
|
parentB &&
|
|
343
280
|
parentA === parentB &&
|
|
@@ -351,7 +288,6 @@ exports.preventDoubleRelease = {
|
|
|
351
288
|
continue;
|
|
352
289
|
}
|
|
353
290
|
}
|
|
354
|
-
// Case 7: Try/Catch + Finally pattern
|
|
355
291
|
const catchAForFinally = isInCatchBlock(callA.node);
|
|
356
292
|
const finallyB = isInFinallyBlock(callB.node);
|
|
357
293
|
if (catchAForFinally && finallyB) {
|
|
@@ -362,7 +298,6 @@ exports.preventDoubleRelease = {
|
|
|
362
298
|
continue;
|
|
363
299
|
}
|
|
364
300
|
}
|
|
365
|
-
// Case 8: Try block + Finally pattern
|
|
366
301
|
const tryAForFinally = isInTryBlock(callA.node);
|
|
367
302
|
const finallyB2 = isInFinallyBlock(callB.node);
|
|
368
303
|
if (tryAForFinally && finallyB2 && tryAForFinally === finallyB2) {
|
|
@@ -370,7 +305,6 @@ exports.preventDoubleRelease = {
|
|
|
370
305
|
reported.add(callB.node);
|
|
371
306
|
continue;
|
|
372
307
|
}
|
|
373
|
-
// Case 9: Finally + After try pattern
|
|
374
308
|
const finallyA = isInFinallyBlock(callA.node);
|
|
375
309
|
if (finallyA) {
|
|
376
310
|
const tryEndPos = finallyA.range[1];
|
|
@@ -380,10 +314,8 @@ exports.preventDoubleRelease = {
|
|
|
380
314
|
continue;
|
|
381
315
|
}
|
|
382
316
|
}
|
|
383
|
-
// Case 10: Try block release + after try (with or without finally)
|
|
384
317
|
const tryAWithOrWithout = isInTryBlock(callA.node);
|
|
385
318
|
if (tryAWithOrWithout) {
|
|
386
|
-
// B is after the entire try statement (including any finally)
|
|
387
319
|
const tryEndPos = tryAWithOrWithout.range[1];
|
|
388
320
|
if (callB.node.range[0] > tryEndPos && !isInFinallyBlock(callB.node)) {
|
|
389
321
|
context.report({ node: callB.node, messageId: 'doubleRelease' });
|
|
@@ -391,7 +323,6 @@ exports.preventDoubleRelease = {
|
|
|
391
323
|
continue;
|
|
392
324
|
}
|
|
393
325
|
}
|
|
394
|
-
// Case 11: Early return branch + Finally (without exit)
|
|
395
326
|
const ifStmtA = findAncestor(callA.node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement);
|
|
396
327
|
if (ifStmtA && finallyB) {
|
|
397
328
|
if (!ifBranchHasExitAfterRelease(callA.node, ifStmtA)) {
|
|
@@ -400,7 +331,6 @@ exports.preventDoubleRelease = {
|
|
|
400
331
|
continue;
|
|
401
332
|
}
|
|
402
333
|
}
|
|
403
|
-
// Case 12: Catch block + after try (no finally, or B is outside finally)
|
|
404
334
|
const catchAForAfter = isInCatchBlock(callA.node);
|
|
405
335
|
if (catchAForAfter) {
|
|
406
336
|
const tryOfCatchA = catchAForAfter.parent;
|
|
@@ -411,8 +341,6 @@ exports.preventDoubleRelease = {
|
|
|
411
341
|
continue;
|
|
412
342
|
}
|
|
413
343
|
}
|
|
414
|
-
// Case 13: Expression-based release (ternary, short-circuit) + sequential
|
|
415
|
-
// Check if A is in a ternary/conditional expression or logical expression
|
|
416
344
|
const isInExpression = (exprNode) => {
|
|
417
345
|
let current = exprNode.parent;
|
|
418
346
|
while (current) {
|
|
@@ -429,14 +357,11 @@ exports.preventDoubleRelease = {
|
|
|
429
357
|
return false;
|
|
430
358
|
};
|
|
431
359
|
if (isInExpression(callA.node)) {
|
|
432
|
-
// A is in a conditional/logical expression, B comes after
|
|
433
|
-
// Find the containing expression statement for A
|
|
434
360
|
let exprStmtA = callA.node;
|
|
435
361
|
while (exprStmtA && exprStmtA.type !== eslint_devkit_1.AST_NODE_TYPES.ExpressionStatement) {
|
|
436
362
|
exprStmtA = exprStmtA.parent ?? null;
|
|
437
363
|
}
|
|
438
364
|
if (exprStmtA && callB.node.range[0] > exprStmtA.range[1]) {
|
|
439
|
-
// Check they're in the same block
|
|
440
365
|
const blockA = findAncestor(exprStmtA, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement);
|
|
441
366
|
const blockB = findAncestor(callB.node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement);
|
|
442
367
|
if (blockA && blockA === blockB) {
|