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,534 +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
|
-
|
|
7
|
-
import {
|
|
8
|
-
TSESLint,
|
|
9
|
-
AST_NODE_TYPES,
|
|
10
|
-
TSESTree,
|
|
11
|
-
formatLLMMessage,
|
|
12
|
-
MessageIcons,
|
|
13
|
-
} from '@interlace/eslint-devkit';
|
|
14
|
-
import { PreventDoubleReleaseOptions } from '../../types';
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Finds the nearest ancestor of a given type.
|
|
18
|
-
*/
|
|
19
|
-
function findAncestor<T extends TSESTree.Node>(
|
|
20
|
-
node: TSESTree.Node | undefined,
|
|
21
|
-
predicate: (n: TSESTree.Node) => n is T
|
|
22
|
-
): T | null {
|
|
23
|
-
let current = node?.parent;
|
|
24
|
-
while (current) {
|
|
25
|
-
if (predicate(current)) return current;
|
|
26
|
-
current = current.parent;
|
|
27
|
-
}
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Checks if node is inside a TryStatement's catch block
|
|
33
|
-
*/
|
|
34
|
-
function isInCatchBlock(node: TSESTree.Node): TSESTree.CatchClause | null {
|
|
35
|
-
return findAncestor(node, (n): n is TSESTree.CatchClause =>
|
|
36
|
-
n.type === AST_NODE_TYPES.CatchClause
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Checks if node is inside a TryStatement's finally block
|
|
42
|
-
*/
|
|
43
|
-
function isInFinallyBlock(node: TSESTree.Node): TSESTree.TryStatement | null {
|
|
44
|
-
const tryStmt = findAncestor(node, (n): n is TSESTree.TryStatement =>
|
|
45
|
-
n.type === AST_NODE_TYPES.TryStatement
|
|
46
|
-
);
|
|
47
|
-
if (!tryStmt?.finalizer) return null;
|
|
48
|
-
|
|
49
|
-
const nodeRange = node.range;
|
|
50
|
-
const finalizerRange = tryStmt.finalizer.range;
|
|
51
|
-
if (nodeRange[0] >= finalizerRange[0] && nodeRange[1] <= finalizerRange[1]) {
|
|
52
|
-
return tryStmt;
|
|
53
|
-
}
|
|
54
|
-
return null;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Checks if node is inside a TryStatement's try block
|
|
59
|
-
*/
|
|
60
|
-
function isInTryBlock(node: TSESTree.Node): TSESTree.TryStatement | null {
|
|
61
|
-
const tryStmt = findAncestor(node, (n): n is TSESTree.TryStatement =>
|
|
62
|
-
n.type === AST_NODE_TYPES.TryStatement
|
|
63
|
-
);
|
|
64
|
-
if (!tryStmt) return null;
|
|
65
|
-
|
|
66
|
-
const nodeRange = node.range;
|
|
67
|
-
const blockRange = tryStmt.block.range;
|
|
68
|
-
if (nodeRange[0] >= blockRange[0] && nodeRange[1] <= blockRange[1]) {
|
|
69
|
-
return tryStmt;
|
|
70
|
-
}
|
|
71
|
-
return null;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Gets the containing function for a node
|
|
76
|
-
*/
|
|
77
|
-
function getContainingFunction(node: TSESTree.Node): TSESTree.Node | null {
|
|
78
|
-
return findAncestor(node, (n): n is TSESTree.FunctionDeclaration | TSESTree.FunctionExpression | TSESTree.ArrowFunctionExpression =>
|
|
79
|
-
n.type === AST_NODE_TYPES.FunctionDeclaration ||
|
|
80
|
-
n.type === AST_NODE_TYPES.FunctionExpression ||
|
|
81
|
-
n.type === AST_NODE_TYPES.ArrowFunctionExpression
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Checks if node is guarded by a conditional check (e.g., if (!released))
|
|
87
|
-
*/
|
|
88
|
-
function isGuardedByCondition(node: TSESTree.Node): boolean {
|
|
89
|
-
const ifStmt = findAncestor(node, (n): n is TSESTree.IfStatement =>
|
|
90
|
-
n.type === AST_NODE_TYPES.IfStatement
|
|
91
|
-
);
|
|
92
|
-
if (!ifStmt) return false;
|
|
93
|
-
|
|
94
|
-
const conditionText = ifStmt.test;
|
|
95
|
-
|
|
96
|
-
// Check for patterns like: !released, released === false, !client.released
|
|
97
|
-
if (conditionText.type === AST_NODE_TYPES.UnaryExpression && conditionText.operator === '!') {
|
|
98
|
-
const arg = conditionText.argument;
|
|
99
|
-
if (arg.type === AST_NODE_TYPES.Identifier) {
|
|
100
|
-
const name = arg.name.toLowerCase();
|
|
101
|
-
return name.includes('released') || name.includes('done') || name.includes('closed');
|
|
102
|
-
}
|
|
103
|
-
if (arg.type === AST_NODE_TYPES.MemberExpression && arg.property.type === AST_NODE_TYPES.Identifier) {
|
|
104
|
-
const name = arg.property.name.toLowerCase();
|
|
105
|
-
return name.includes('released') || name.includes('done') || name.includes('closed');
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return false;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Checks if a node is in an else-if/else branch that's mutually exclusive from another
|
|
114
|
-
*/
|
|
115
|
-
function areMutuallyExclusiveBranches(nodeA: TSESTree.Node, nodeB: TSESTree.Node): boolean {
|
|
116
|
-
const ifA = findAncestor(nodeA, (n): n is TSESTree.IfStatement => n.type === AST_NODE_TYPES.IfStatement);
|
|
117
|
-
const ifB = findAncestor(nodeB, (n): n is TSESTree.IfStatement => n.type === AST_NODE_TYPES.IfStatement);
|
|
118
|
-
|
|
119
|
-
if (!ifA || !ifB) return false;
|
|
120
|
-
|
|
121
|
-
// Check if both are in the same if/else chain
|
|
122
|
-
if (ifA === ifB) {
|
|
123
|
-
const nodeARange = nodeA.range;
|
|
124
|
-
const nodeBRange = nodeB.range;
|
|
125
|
-
const consequentRange = ifA.consequent.range;
|
|
126
|
-
const alternateRange = ifA.alternate?.range;
|
|
127
|
-
|
|
128
|
-
if (!alternateRange) return false;
|
|
129
|
-
|
|
130
|
-
const aInConsequent = nodeARange[0] >= consequentRange[0] && nodeARange[1] <= consequentRange[1];
|
|
131
|
-
const bInConsequent = nodeBRange[0] >= consequentRange[0] && nodeBRange[1] <= consequentRange[1];
|
|
132
|
-
const aInAlternate = nodeARange[0] >= alternateRange[0] && nodeARange[1] <= alternateRange[1];
|
|
133
|
-
const bInAlternate = nodeBRange[0] >= alternateRange[0] && nodeBRange[1] <= alternateRange[1];
|
|
134
|
-
|
|
135
|
-
return (aInConsequent && bInAlternate) || (aInAlternate && bInConsequent);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
return false;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Checks if there's a return/throw statement between two positions in a block
|
|
143
|
-
*/
|
|
144
|
-
function hasExitBetween(block: TSESTree.BlockStatement, startIdx: number, endIdx: number): boolean {
|
|
145
|
-
for (let k = startIdx + 1; k < endIdx; k++) {
|
|
146
|
-
const stmt = block.body[k];
|
|
147
|
-
if (stmt.type === AST_NODE_TYPES.ReturnStatement) return true;
|
|
148
|
-
if (stmt.type === AST_NODE_TYPES.ThrowStatement) return true;
|
|
149
|
-
}
|
|
150
|
-
return false;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Checks if a switch case has a break/return/throw
|
|
155
|
-
*/
|
|
156
|
-
function caseHasExit(switchCase: TSESTree.SwitchCase): boolean {
|
|
157
|
-
for (const stmt of switchCase.consequent) {
|
|
158
|
-
if (stmt.type === AST_NODE_TYPES.ReturnStatement) return true;
|
|
159
|
-
if (stmt.type === AST_NODE_TYPES.ThrowStatement) return true;
|
|
160
|
-
if (stmt.type === AST_NODE_TYPES.BreakStatement) return true;
|
|
161
|
-
}
|
|
162
|
-
return false;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Gets the switch case containing a node
|
|
167
|
-
*/
|
|
168
|
-
function getSwitchCase(node: TSESTree.Node): TSESTree.SwitchCase | null {
|
|
169
|
-
return findAncestor(node, (n): n is TSESTree.SwitchCase =>
|
|
170
|
-
n.type === AST_NODE_TYPES.SwitchCase
|
|
171
|
-
);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Gets the if statement directly containing a node (not ancestors)
|
|
176
|
-
*/
|
|
177
|
-
function getDirectIfStatement(node: TSESTree.Node): TSESTree.IfStatement | null {
|
|
178
|
-
return findAncestor(node, (n): n is TSESTree.IfStatement =>
|
|
179
|
-
n.type === AST_NODE_TYPES.IfStatement
|
|
180
|
-
);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Checks if the if statement's branch containing the release has a return/throw after it
|
|
185
|
-
*/
|
|
186
|
-
function ifBranchHasExitAfterRelease(releaseNode: TSESTree.Node, ifStmt: TSESTree.IfStatement): boolean {
|
|
187
|
-
// Find which branch contains the release
|
|
188
|
-
const releaseRange = releaseNode.range;
|
|
189
|
-
const consequentRange = ifStmt.consequent.range;
|
|
190
|
-
|
|
191
|
-
const inConsequent = releaseRange[0] >= consequentRange[0] && releaseRange[1] <= consequentRange[1];
|
|
192
|
-
|
|
193
|
-
let branchBlock: TSESTree.BlockStatement | null = null;
|
|
194
|
-
|
|
195
|
-
if (inConsequent && ifStmt.consequent.type === AST_NODE_TYPES.BlockStatement) {
|
|
196
|
-
branchBlock = ifStmt.consequent;
|
|
197
|
-
} else if (!inConsequent && ifStmt.alternate?.type === AST_NODE_TYPES.BlockStatement) {
|
|
198
|
-
branchBlock = ifStmt.alternate;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
if (!branchBlock) return false;
|
|
202
|
-
|
|
203
|
-
// Find the release statement index
|
|
204
|
-
const releaseParentStmt = releaseNode.parent?.type === AST_NODE_TYPES.ExpressionStatement
|
|
205
|
-
? releaseNode.parent
|
|
206
|
-
: null;
|
|
207
|
-
|
|
208
|
-
if (!releaseParentStmt) return false;
|
|
209
|
-
|
|
210
|
-
const releaseIdx = branchBlock.body.indexOf(releaseParentStmt as TSESTree.Statement);
|
|
211
|
-
if (releaseIdx === -1) return false;
|
|
212
|
-
|
|
213
|
-
// Check if there's a return/throw after the release
|
|
214
|
-
for (let i = releaseIdx + 1; i < branchBlock.body.length; i++) {
|
|
215
|
-
const stmt = branchBlock.body[i];
|
|
216
|
-
if (stmt.type === AST_NODE_TYPES.ReturnStatement || stmt.type === AST_NODE_TYPES.ThrowStatement) {
|
|
217
|
-
return true;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
return false;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
export const preventDoubleRelease: TSESLint.RuleModule<
|
|
225
|
-
'doubleRelease' | 'doubleReleaseCallback',
|
|
226
|
-
PreventDoubleReleaseOptions
|
|
227
|
-
> = {
|
|
228
|
-
meta: {
|
|
229
|
-
type: 'problem',
|
|
230
|
-
docs: {
|
|
231
|
-
description: 'Prevent releasing a pg client multiple times.',
|
|
232
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/prevent-double-release.md',
|
|
233
|
-
cwe: 'CWE-415',
|
|
234
|
-
cweJustification: 'CWE-415 (Double Free) — semantic equivalent for connection lifecycle: releasing a pooled client twice corrupts the pool\'s internal accounting and can hand the same connection to two callers.',
|
|
235
|
-
confidence: 'medium',
|
|
236
|
-
},
|
|
237
|
-
messages: {
|
|
238
|
-
doubleRelease: formatLLMMessage({
|
|
239
|
-
icon: MessageIcons.QUALITY,
|
|
240
|
-
issueName: 'Double Release',
|
|
241
|
-
description: 'Client release() called multiple times on the same object.',
|
|
242
|
-
severity: 'HIGH',
|
|
243
|
-
effort: 'low',
|
|
244
|
-
fix: 'Ensure client.release() is called exactly once per acquisition, preferably in a finally block.',
|
|
245
|
-
documentationLink: 'https://node-postgres.com/api/client#clientrelease',
|
|
246
|
-
}),
|
|
247
|
-
doubleReleaseCallback: formatLLMMessage({
|
|
248
|
-
icon: MessageIcons.QUALITY,
|
|
249
|
-
issueName: 'Double Release (Callback)',
|
|
250
|
-
description: 'Connection release callback (done) called multiple times.',
|
|
251
|
-
severity: 'HIGH',
|
|
252
|
-
effort: 'low',
|
|
253
|
-
fix: 'Ensure done() or release callback is called exactly once per pool.connect() callback.',
|
|
254
|
-
documentationLink: 'https://node-postgres.com/api/pool#poolconnect',
|
|
255
|
-
}),
|
|
256
|
-
},
|
|
257
|
-
schema: [],
|
|
258
|
-
},
|
|
259
|
-
defaultOptions: [],
|
|
260
|
-
create(context) {
|
|
261
|
-
return {
|
|
262
|
-
VariableDeclarator(node: TSESTree.VariableDeclarator) {
|
|
263
|
-
const declaredVariables = context.sourceCode.getDeclaredVariables(node);
|
|
264
|
-
|
|
265
|
-
for (const variable of declaredVariables) {
|
|
266
|
-
const def = variable.defs[0];
|
|
267
|
-
const isDestructuredRelease =
|
|
268
|
-
def?.type === 'Variable' &&
|
|
269
|
-
def.node.id.type === AST_NODE_TYPES.ObjectPattern &&
|
|
270
|
-
def.name.name === 'release';
|
|
271
|
-
|
|
272
|
-
const releaseCalls: { node: TSESTree.CallExpression; guarded: boolean }[] = [];
|
|
273
|
-
|
|
274
|
-
variable.references.forEach((ref) => {
|
|
275
|
-
const id = ref.identifier;
|
|
276
|
-
|
|
277
|
-
if (isDestructuredRelease) {
|
|
278
|
-
if (
|
|
279
|
-
id.parent?.type === AST_NODE_TYPES.CallExpression &&
|
|
280
|
-
id.parent.callee === id
|
|
281
|
-
) {
|
|
282
|
-
const callNode = id.parent;
|
|
283
|
-
const guarded = isGuardedByCondition(callNode);
|
|
284
|
-
releaseCalls.push({ node: callNode, guarded });
|
|
285
|
-
}
|
|
286
|
-
} else {
|
|
287
|
-
if (
|
|
288
|
-
id.parent?.type === AST_NODE_TYPES.MemberExpression &&
|
|
289
|
-
id.parent.object === id &&
|
|
290
|
-
id.parent.property.type === AST_NODE_TYPES.Identifier &&
|
|
291
|
-
id.parent.property.name === 'release' &&
|
|
292
|
-
id.parent.parent?.type === AST_NODE_TYPES.CallExpression
|
|
293
|
-
) {
|
|
294
|
-
const callNode = id.parent.parent;
|
|
295
|
-
const guarded = isGuardedByCondition(callNode);
|
|
296
|
-
releaseCalls.push({ node: callNode, guarded });
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
if (releaseCalls.length > 1) {
|
|
302
|
-
releaseCalls.sort((a, b) => a.node.range[0] - b.node.range[0]);
|
|
303
|
-
const reported = new Set<TSESTree.Node>();
|
|
304
|
-
|
|
305
|
-
for (let i = 0; i < releaseCalls.length; i++) {
|
|
306
|
-
for (let j = i + 1; j < releaseCalls.length; j++) {
|
|
307
|
-
const callA = releaseCalls[i];
|
|
308
|
-
const callB = releaseCalls[j];
|
|
309
|
-
|
|
310
|
-
// Skip if already reported
|
|
311
|
-
if (reported.has(callB.node)) continue;
|
|
312
|
-
|
|
313
|
-
// Skip if both are guarded
|
|
314
|
-
if (callA.guarded && callB.guarded) continue;
|
|
315
|
-
|
|
316
|
-
// Skip if mutually exclusive branches
|
|
317
|
-
if (areMutuallyExclusiveBranches(callA.node, callB.node)) continue;
|
|
318
|
-
|
|
319
|
-
// Same function scope check
|
|
320
|
-
const funcA = getContainingFunction(callA.node);
|
|
321
|
-
const funcB = getContainingFunction(callB.node);
|
|
322
|
-
if (funcA !== funcB) continue;
|
|
323
|
-
|
|
324
|
-
// Case 1: Try + Catch (not finally) - both paths release
|
|
325
|
-
const tryA = isInTryBlock(callA.node);
|
|
326
|
-
const catchB = isInCatchBlock(callB.node);
|
|
327
|
-
if (tryA && catchB && catchB.parent === tryA) {
|
|
328
|
-
context.report({ node: callB.node, messageId: 'doubleRelease' });
|
|
329
|
-
reported.add(callB.node);
|
|
330
|
-
continue;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
// Case 2: Catch + Try (reversed order in source)
|
|
334
|
-
const catchA = isInCatchBlock(callA.node);
|
|
335
|
-
const tryB = isInTryBlock(callB.node);
|
|
336
|
-
if (catchA && tryB && catchA.parent === tryB) {
|
|
337
|
-
context.report({ node: callB.node, messageId: 'doubleRelease' });
|
|
338
|
-
reported.add(callB.node);
|
|
339
|
-
continue;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
// Case 3: Switch fallthrough
|
|
343
|
-
const switchCaseA = getSwitchCase(callA.node);
|
|
344
|
-
const switchCaseB = getSwitchCase(callB.node);
|
|
345
|
-
if (switchCaseA && switchCaseB && switchCaseA !== switchCaseB) {
|
|
346
|
-
// Check if same switch statement
|
|
347
|
-
const switchA = findAncestor(switchCaseA, (n): n is TSESTree.SwitchStatement =>
|
|
348
|
-
n.type === AST_NODE_TYPES.SwitchStatement
|
|
349
|
-
);
|
|
350
|
-
const switchB = findAncestor(switchCaseB, (n): n is TSESTree.SwitchStatement =>
|
|
351
|
-
n.type === AST_NODE_TYPES.SwitchStatement
|
|
352
|
-
);
|
|
353
|
-
if (switchA === switchB && switchA) {
|
|
354
|
-
// Check if caseA doesn't have break/return/throw (fallthrough)
|
|
355
|
-
if (!caseHasExit(switchCaseA)) {
|
|
356
|
-
context.report({ node: callB.node, messageId: 'doubleRelease' });
|
|
357
|
-
reported.add(callB.node);
|
|
358
|
-
continue;
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
// Case 4: If without else + sequential release
|
|
364
|
-
const ifA = getDirectIfStatement(callA.node);
|
|
365
|
-
if (ifA && !ifA.alternate) {
|
|
366
|
-
// B is after the if statement
|
|
367
|
-
if (callB.node.range[0] > ifA.range[1]) {
|
|
368
|
-
// Check if B is in same block as the if
|
|
369
|
-
const parentBlock = findAncestor(ifA, (n): n is TSESTree.BlockStatement =>
|
|
370
|
-
n.type === AST_NODE_TYPES.BlockStatement
|
|
371
|
-
);
|
|
372
|
-
if (parentBlock) {
|
|
373
|
-
const bParentStmt = callB.node.parent?.type === AST_NODE_TYPES.ExpressionStatement
|
|
374
|
-
? callB.node.parent : null;
|
|
375
|
-
if (bParentStmt && parentBlock.body.includes(bParentStmt as TSESTree.Statement)) {
|
|
376
|
-
// Check if the if branch has exit after release
|
|
377
|
-
if (!ifBranchHasExitAfterRelease(callA.node, ifA)) {
|
|
378
|
-
context.report({ node: callB.node, messageId: 'doubleRelease' });
|
|
379
|
-
reported.add(callB.node);
|
|
380
|
-
continue;
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
// Case 5: Two sequential if statements (neither has else)
|
|
388
|
-
const ifB = getDirectIfStatement(callB.node);
|
|
389
|
-
if (ifA && ifB && ifA !== ifB && !ifA.alternate && !ifB.alternate) {
|
|
390
|
-
// Both are different if statements at same level
|
|
391
|
-
const parentBlockA = ifA.parent;
|
|
392
|
-
const parentBlockB = ifB.parent;
|
|
393
|
-
if (parentBlockA === parentBlockB && parentBlockA?.type === AST_NODE_TYPES.BlockStatement) {
|
|
394
|
-
// Check if ifA doesn't exit after release
|
|
395
|
-
if (!ifBranchHasExitAfterRelease(callA.node, ifA)) {
|
|
396
|
-
context.report({ node: callB.node, messageId: 'doubleRelease' });
|
|
397
|
-
reported.add(callB.node);
|
|
398
|
-
continue;
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
const parentA = callA.node.parent?.parent;
|
|
404
|
-
const parentB = callB.node.parent?.parent;
|
|
405
|
-
|
|
406
|
-
// Case 6: Same Block - direct sequential releases
|
|
407
|
-
if (
|
|
408
|
-
parentA &&
|
|
409
|
-
parentB &&
|
|
410
|
-
parentA === parentB &&
|
|
411
|
-
parentA.type === AST_NODE_TYPES.BlockStatement
|
|
412
|
-
) {
|
|
413
|
-
const block = parentA as TSESTree.BlockStatement;
|
|
414
|
-
const indexA = block.body.indexOf(callA.node.parent as TSESTree.Statement);
|
|
415
|
-
const indexB = block.body.indexOf(callB.node.parent as TSESTree.Statement);
|
|
416
|
-
if (indexB > indexA && !hasExitBetween(block, indexA, indexB)) {
|
|
417
|
-
context.report({ node: callB.node, messageId: 'doubleRelease' });
|
|
418
|
-
reported.add(callB.node);
|
|
419
|
-
continue;
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
// Case 7: Try/Catch + Finally pattern
|
|
424
|
-
const catchAForFinally = isInCatchBlock(callA.node);
|
|
425
|
-
const finallyB = isInFinallyBlock(callB.node);
|
|
426
|
-
if (catchAForFinally && finallyB) {
|
|
427
|
-
const tryOfCatch = catchAForFinally.parent;
|
|
428
|
-
if (tryOfCatch === finallyB) {
|
|
429
|
-
context.report({ node: callB.node, messageId: 'doubleRelease' });
|
|
430
|
-
reported.add(callB.node);
|
|
431
|
-
continue;
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
// Case 8: Try block + Finally pattern
|
|
436
|
-
const tryAForFinally = isInTryBlock(callA.node);
|
|
437
|
-
const finallyB2 = isInFinallyBlock(callB.node);
|
|
438
|
-
if (tryAForFinally && finallyB2 && tryAForFinally === finallyB2) {
|
|
439
|
-
context.report({ node: callB.node, messageId: 'doubleRelease' });
|
|
440
|
-
reported.add(callB.node);
|
|
441
|
-
continue;
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
// Case 9: Finally + After try pattern
|
|
445
|
-
const finallyA = isInFinallyBlock(callA.node);
|
|
446
|
-
if (finallyA) {
|
|
447
|
-
const tryEndPos = finallyA.range[1];
|
|
448
|
-
if (callB.node.range[0] > tryEndPos) {
|
|
449
|
-
context.report({ node: callB.node, messageId: 'doubleRelease' });
|
|
450
|
-
reported.add(callB.node);
|
|
451
|
-
continue;
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
// Case 10: Try block release + after try (with or without finally)
|
|
456
|
-
const tryAWithOrWithout = isInTryBlock(callA.node);
|
|
457
|
-
if (tryAWithOrWithout) {
|
|
458
|
-
// B is after the entire try statement (including any finally)
|
|
459
|
-
const tryEndPos = tryAWithOrWithout.range[1];
|
|
460
|
-
if (callB.node.range[0] > tryEndPos && !isInFinallyBlock(callB.node)) {
|
|
461
|
-
context.report({ node: callB.node, messageId: 'doubleRelease' });
|
|
462
|
-
reported.add(callB.node);
|
|
463
|
-
continue;
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
// Case 11: Early return branch + Finally (without exit)
|
|
468
|
-
const ifStmtA = findAncestor(callA.node, (n): n is TSESTree.IfStatement =>
|
|
469
|
-
n.type === AST_NODE_TYPES.IfStatement
|
|
470
|
-
);
|
|
471
|
-
if (ifStmtA && finallyB) {
|
|
472
|
-
if (!ifBranchHasExitAfterRelease(callA.node, ifStmtA)) {
|
|
473
|
-
context.report({ node: callB.node, messageId: 'doubleRelease' });
|
|
474
|
-
reported.add(callB.node);
|
|
475
|
-
continue;
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
// Case 12: Catch block + after try (no finally, or B is outside finally)
|
|
480
|
-
const catchAForAfter = isInCatchBlock(callA.node);
|
|
481
|
-
if (catchAForAfter) {
|
|
482
|
-
const tryOfCatchA = catchAForAfter.parent as TSESTree.TryStatement;
|
|
483
|
-
const tryEndPos = tryOfCatchA.range[1];
|
|
484
|
-
if (callB.node.range[0] > tryEndPos) {
|
|
485
|
-
context.report({ node: callB.node, messageId: 'doubleRelease' });
|
|
486
|
-
reported.add(callB.node);
|
|
487
|
-
continue;
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
// Case 13: Expression-based release (ternary, short-circuit) + sequential
|
|
492
|
-
// Check if A is in a ternary/conditional expression or logical expression
|
|
493
|
-
const isInExpression = (exprNode: TSESTree.Node): boolean => {
|
|
494
|
-
let current = exprNode.parent;
|
|
495
|
-
while (current) {
|
|
496
|
-
if (current.type === AST_NODE_TYPES.ConditionalExpression) return true;
|
|
497
|
-
if (current.type === AST_NODE_TYPES.LogicalExpression) return true;
|
|
498
|
-
if (current.type === AST_NODE_TYPES.ExpressionStatement) break;
|
|
499
|
-
if (current.type === AST_NODE_TYPES.BlockStatement) break;
|
|
500
|
-
current = current.parent;
|
|
501
|
-
}
|
|
502
|
-
return false;
|
|
503
|
-
};
|
|
504
|
-
|
|
505
|
-
if (isInExpression(callA.node)) {
|
|
506
|
-
// A is in a conditional/logical expression, B comes after
|
|
507
|
-
// Find the containing expression statement for A
|
|
508
|
-
let exprStmtA: TSESTree.Node | null = callA.node;
|
|
509
|
-
while (exprStmtA && exprStmtA.type !== AST_NODE_TYPES.ExpressionStatement) {
|
|
510
|
-
exprStmtA = exprStmtA.parent ?? null;
|
|
511
|
-
}
|
|
512
|
-
if (exprStmtA && callB.node.range[0] > exprStmtA.range[1]) {
|
|
513
|
-
// Check they're in the same block
|
|
514
|
-
const blockA = findAncestor(exprStmtA, (n): n is TSESTree.BlockStatement =>
|
|
515
|
-
n.type === AST_NODE_TYPES.BlockStatement
|
|
516
|
-
);
|
|
517
|
-
const blockB = findAncestor(callB.node, (n): n is TSESTree.BlockStatement =>
|
|
518
|
-
n.type === AST_NODE_TYPES.BlockStatement
|
|
519
|
-
);
|
|
520
|
-
if (blockA && blockA === blockB) {
|
|
521
|
-
context.report({ node: callB.node, messageId: 'doubleRelease' });
|
|
522
|
-
reported.add(callB.node);
|
|
523
|
-
continue;
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
},
|
|
532
|
-
};
|
|
533
|
-
},
|
|
534
|
-
};
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tests for prevent-double-release
|
|
3
|
-
* Prevent releasing a pg client multiple times.
|
|
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 { preventDoubleRelease } 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('prevent-double-release', () => {
|
|
28
|
-
describe('Valid Code', () => {
|
|
29
|
-
ruleTester.run('valid - safe patterns', preventDoubleRelease, {
|
|
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
|
-
// Properly configured connection
|
|
44
|
-
{
|
|
45
|
-
code: `mongoose.connect(uri, { tls: true });`,
|
|
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', preventDoubleRelease, {
|
|
58
|
-
valid: [],
|
|
59
|
-
invalid: [
|
|
60
|
-
// Triggers doubleRelease
|
|
61
|
-
{
|
|
62
|
-
code: `async function test() { const client = await pool.connect(); try { await client.query('SELECT 1'); client.release(); } catch(e) { client.release(); } }`,
|
|
63
|
-
errors: [{ messageId: 'doubleRelease' }],
|
|
64
|
-
},
|
|
65
|
-
// NOTE: doubleReleaseCallback requires complex scope analysis.
|
|
66
|
-
// Run /write-tests prevent-double-release for comprehensive test coverage.
|
|
67
|
-
],
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
});
|
package/src/types/index.ts
DELETED
|
@@ -1,58 +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
|
-
|
|
7
|
-
export type NoUnsafeQueryOptions = [];
|
|
8
|
-
export type NoInsecureSslOptions = [];
|
|
9
|
-
export type NoHardcodedCredentialsOptions = [];
|
|
10
|
-
export type NoInjectionInIdentifierOptions = [];
|
|
11
|
-
export type CheckQueryParamsOptions = [];
|
|
12
|
-
export type NoMissingClientReleaseOptions = [];
|
|
13
|
-
export type NoTransactionOnPoolOptions = [];
|
|
14
|
-
export type NoFloatingQueryOptions = [];
|
|
15
|
-
export type NoUselessClientOptions = [];
|
|
16
|
-
export type EnforceIdleTimeoutOptions = [];
|
|
17
|
-
export type NoSelectAllOptions = [];
|
|
18
|
-
export type PreferPoolQueryOptions = [];
|
|
19
|
-
export type NoBatchInsertLoopOptions = [];
|
|
20
|
-
export type PreferCursorForLargeResultsOptions = [];
|
|
21
|
-
export type NoTopLevelQueryOptions = [];
|
|
22
|
-
export type EnforceCamelcaseColumnsOptions = [];
|
|
23
|
-
export type ConnectOnceOptions = [];
|
|
24
|
-
|
|
25
|
-
// Phase 2
|
|
26
|
-
export type NoUnsafeSearchPathOptions = [];
|
|
27
|
-
export type NoUnsafeCopyFromOptions = [
|
|
28
|
-
{
|
|
29
|
-
/** Allow hardcoded file paths (for admin/migration scripts). Default: false */
|
|
30
|
-
allowHardcodedPaths?: boolean;
|
|
31
|
-
/** List of allowed file path patterns (regex strings) */
|
|
32
|
-
allowedPaths?: string[];
|
|
33
|
-
}?,
|
|
34
|
-
];
|
|
35
|
-
export type PreventDoubleReleaseOptions = [];
|
|
36
|
-
|
|
37
|
-
export interface AllPgRulesOptions {
|
|
38
|
-
'pg/no-unsafe-query': NoUnsafeQueryOptions;
|
|
39
|
-
'pg/no-insecure-ssl': NoInsecureSslOptions;
|
|
40
|
-
'pg/no-hardcoded-credentials': NoHardcodedCredentialsOptions;
|
|
41
|
-
'pg/no-injection-in-identifier': NoInjectionInIdentifierOptions;
|
|
42
|
-
'pg/check-query-params': CheckQueryParamsOptions;
|
|
43
|
-
'pg/no-missing-client-release': NoMissingClientReleaseOptions;
|
|
44
|
-
'pg/no-transaction-on-pool': NoTransactionOnPoolOptions;
|
|
45
|
-
'pg/no-floating-query': NoFloatingQueryOptions;
|
|
46
|
-
'pg/no-useless-client': NoUselessClientOptions;
|
|
47
|
-
'pg/enforce-idle-timeout': EnforceIdleTimeoutOptions;
|
|
48
|
-
'pg/no-select-all': NoSelectAllOptions;
|
|
49
|
-
'pg/prefer-pool-query': PreferPoolQueryOptions;
|
|
50
|
-
'pg/no-batch-insert-loop': NoBatchInsertLoopOptions;
|
|
51
|
-
'pg/prefer-cursor-for-large-results': PreferCursorForLargeResultsOptions;
|
|
52
|
-
'pg/no-top-level-query': NoTopLevelQueryOptions;
|
|
53
|
-
'pg/enforce-camelcase-columns': EnforceCamelcaseColumnsOptions;
|
|
54
|
-
'pg/connect-once': ConnectOnceOptions;
|
|
55
|
-
'pg/no-unsafe-search-path': NoUnsafeSearchPathOptions;
|
|
56
|
-
'pg/no-unsafe-copy-from': NoUnsafeCopyFromOptions;
|
|
57
|
-
'pg/prevent-double-release': PreventDoubleReleaseOptions;
|
|
58
|
-
}
|
|
File without changes
|