eslint-plugin-postgresql-security 1.5.1 → 1.5.3

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.
@@ -1,381 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.preventDoubleRelease = void 0;
4
- const eslint_devkit_1 = require("@interlace/eslint-devkit");
5
- function findAncestor(node, predicate) {
6
- let current = node?.parent;
7
- while (current) {
8
- if (predicate(current))
9
- return current;
10
- current = current.parent;
11
- }
12
- return null;
13
- }
14
- function isInCatchBlock(node) {
15
- return findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.CatchClause);
16
- }
17
- function isInFinallyBlock(node) {
18
- const tryStmt = findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.TryStatement);
19
- if (!tryStmt?.finalizer)
20
- return null;
21
- const nodeRange = node.range;
22
- const finalizerRange = tryStmt.finalizer.range;
23
- if (nodeRange[0] >= finalizerRange[0] && nodeRange[1] <= finalizerRange[1]) {
24
- return tryStmt;
25
- }
26
- return null;
27
- }
28
- function isInTryBlock(node) {
29
- const tryStmt = findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.TryStatement);
30
- if (!tryStmt)
31
- return null;
32
- const nodeRange = node.range;
33
- const blockRange = tryStmt.block.range;
34
- if (nodeRange[0] >= blockRange[0] && nodeRange[1] <= blockRange[1]) {
35
- return tryStmt;
36
- }
37
- return null;
38
- }
39
- function getContainingFunction(node) {
40
- return findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.FunctionDeclaration ||
41
- n.type === eslint_devkit_1.AST_NODE_TYPES.FunctionExpression ||
42
- n.type === eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression);
43
- }
44
- function isGuardedByCondition(node) {
45
- const ifStmt = findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement);
46
- if (!ifStmt)
47
- return false;
48
- const conditionText = ifStmt.test;
49
- if (conditionText.type === eslint_devkit_1.AST_NODE_TYPES.UnaryExpression && conditionText.operator === '!') {
50
- const arg = conditionText.argument;
51
- if (arg.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
52
- const name = arg.name.toLowerCase();
53
- return name.includes('released') || name.includes('done') || name.includes('closed');
54
- }
55
- if (arg.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression && arg.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
56
- const name = arg.property.name.toLowerCase();
57
- return name.includes('released') || name.includes('done') || name.includes('closed');
58
- }
59
- }
60
- return false;
61
- }
62
- function areMutuallyExclusiveBranches(nodeA, nodeB) {
63
- const ifA = findAncestor(nodeA, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement);
64
- const ifB = findAncestor(nodeB, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement);
65
- if (!ifA || !ifB)
66
- return false;
67
- if (ifA === ifB) {
68
- const nodeARange = nodeA.range;
69
- const nodeBRange = nodeB.range;
70
- const consequentRange = ifA.consequent.range;
71
- const alternateRange = ifA.alternate?.range;
72
- if (!alternateRange)
73
- return false;
74
- const aInConsequent = nodeARange[0] >= consequentRange[0] && nodeARange[1] <= consequentRange[1];
75
- const bInConsequent = nodeBRange[0] >= consequentRange[0] && nodeBRange[1] <= consequentRange[1];
76
- const aInAlternate = nodeARange[0] >= alternateRange[0] && nodeARange[1] <= alternateRange[1];
77
- const bInAlternate = nodeBRange[0] >= alternateRange[0] && nodeBRange[1] <= alternateRange[1];
78
- return (aInConsequent && bInAlternate) || (aInAlternate && bInConsequent);
79
- }
80
- return false;
81
- }
82
- function hasExitBetween(block, startIdx, endIdx) {
83
- for (let k = startIdx + 1; k < endIdx; k++) {
84
- const stmt = block.body[k];
85
- if (stmt.type === eslint_devkit_1.AST_NODE_TYPES.ReturnStatement)
86
- return true;
87
- if (stmt.type === eslint_devkit_1.AST_NODE_TYPES.ThrowStatement)
88
- return true;
89
- }
90
- return false;
91
- }
92
- function caseHasExit(switchCase) {
93
- for (const stmt of switchCase.consequent) {
94
- if (stmt.type === eslint_devkit_1.AST_NODE_TYPES.ReturnStatement)
95
- return true;
96
- if (stmt.type === eslint_devkit_1.AST_NODE_TYPES.ThrowStatement)
97
- return true;
98
- if (stmt.type === eslint_devkit_1.AST_NODE_TYPES.BreakStatement)
99
- return true;
100
- }
101
- return false;
102
- }
103
- function getSwitchCase(node) {
104
- return findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.SwitchCase);
105
- }
106
- function getDirectIfStatement(node) {
107
- return findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement);
108
- }
109
- function ifBranchHasExitAfterRelease(releaseNode, ifStmt) {
110
- const releaseRange = releaseNode.range;
111
- const consequentRange = ifStmt.consequent.range;
112
- const inConsequent = releaseRange[0] >= consequentRange[0] && releaseRange[1] <= consequentRange[1];
113
- let branchBlock = null;
114
- if (inConsequent && ifStmt.consequent.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement) {
115
- branchBlock = ifStmt.consequent;
116
- }
117
- else if (!inConsequent && ifStmt.alternate?.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement) {
118
- branchBlock = ifStmt.alternate;
119
- }
120
- if (!branchBlock)
121
- return false;
122
- const releaseParentStmt = releaseNode.parent?.type === eslint_devkit_1.AST_NODE_TYPES.ExpressionStatement
123
- ? releaseNode.parent
124
- : null;
125
- if (!releaseParentStmt)
126
- return false;
127
- const releaseIdx = branchBlock.body.indexOf(releaseParentStmt);
128
- if (releaseIdx === -1)
129
- return false;
130
- for (let i = releaseIdx + 1; i < branchBlock.body.length; i++) {
131
- const stmt = branchBlock.body[i];
132
- if (stmt.type === eslint_devkit_1.AST_NODE_TYPES.ReturnStatement || stmt.type === eslint_devkit_1.AST_NODE_TYPES.ThrowStatement) {
133
- return true;
134
- }
135
- }
136
- return false;
137
- }
138
- exports.preventDoubleRelease = {
139
- meta: {
140
- type: 'problem',
141
- docs: {
142
- description: 'Prevent releasing a pg client multiple times.',
143
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/prevent-double-release.md',
144
- cwe: 'CWE-415',
145
- 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.',
146
- confidence: 'medium',
147
- },
148
- messages: {
149
- doubleRelease: (0, eslint_devkit_1.formatLLMMessage)({
150
- icon: eslint_devkit_1.MessageIcons.QUALITY,
151
- issueName: 'Double Release',
152
- description: 'Client release() called multiple times on the same object.',
153
- severity: 'HIGH',
154
- effort: 'low',
155
- fix: 'Ensure client.release() is called exactly once per acquisition, preferably in a finally block.',
156
- documentationLink: 'https://node-postgres.com/api/client#clientrelease',
157
- }),
158
- doubleReleaseCallback: (0, eslint_devkit_1.formatLLMMessage)({
159
- icon: eslint_devkit_1.MessageIcons.QUALITY,
160
- issueName: 'Double Release (Callback)',
161
- description: 'Connection release callback (done) called multiple times.',
162
- severity: 'HIGH',
163
- effort: 'low',
164
- fix: 'Ensure done() or release callback is called exactly once per pool.connect() callback.',
165
- documentationLink: 'https://node-postgres.com/api/pool#poolconnect',
166
- }),
167
- },
168
- schema: [],
169
- },
170
- defaultOptions: [],
171
- create(context) {
172
- return {
173
- VariableDeclarator(node) {
174
- const declaredVariables = context.sourceCode.getDeclaredVariables(node);
175
- for (const variable of declaredVariables) {
176
- const def = variable.defs[0];
177
- const isDestructuredRelease = def?.type === 'Variable' &&
178
- def.node.id.type === eslint_devkit_1.AST_NODE_TYPES.ObjectPattern &&
179
- def.name.name === 'release';
180
- const releaseCalls = [];
181
- variable.references.forEach((ref) => {
182
- const id = ref.identifier;
183
- if (isDestructuredRelease) {
184
- if (id.parent?.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression &&
185
- id.parent.callee === id) {
186
- const callNode = id.parent;
187
- const guarded = isGuardedByCondition(callNode);
188
- releaseCalls.push({ node: callNode, guarded });
189
- }
190
- }
191
- else {
192
- if (id.parent?.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
193
- id.parent.object === id &&
194
- id.parent.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
195
- id.parent.property.name === 'release' &&
196
- id.parent.parent?.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
197
- const callNode = id.parent.parent;
198
- const guarded = isGuardedByCondition(callNode);
199
- releaseCalls.push({ node: callNode, guarded });
200
- }
201
- }
202
- });
203
- if (releaseCalls.length > 1) {
204
- releaseCalls.sort((a, b) => a.node.range[0] - b.node.range[0]);
205
- const reported = new Set();
206
- for (let i = 0; i < releaseCalls.length; i++) {
207
- for (let j = i + 1; j < releaseCalls.length; j++) {
208
- const callA = releaseCalls[i];
209
- const callB = releaseCalls[j];
210
- if (reported.has(callB.node))
211
- continue;
212
- if (callA.guarded && callB.guarded)
213
- continue;
214
- if (areMutuallyExclusiveBranches(callA.node, callB.node))
215
- continue;
216
- const funcA = getContainingFunction(callA.node);
217
- const funcB = getContainingFunction(callB.node);
218
- if (funcA !== funcB)
219
- continue;
220
- const tryA = isInTryBlock(callA.node);
221
- const catchB = isInCatchBlock(callB.node);
222
- if (tryA && catchB && catchB.parent === tryA) {
223
- context.report({ node: callB.node, messageId: 'doubleRelease' });
224
- reported.add(callB.node);
225
- continue;
226
- }
227
- const catchA = isInCatchBlock(callA.node);
228
- const tryB = isInTryBlock(callB.node);
229
- if (catchA && tryB && catchA.parent === tryB) {
230
- context.report({ node: callB.node, messageId: 'doubleRelease' });
231
- reported.add(callB.node);
232
- continue;
233
- }
234
- const switchCaseA = getSwitchCase(callA.node);
235
- const switchCaseB = getSwitchCase(callB.node);
236
- if (switchCaseA && switchCaseB && switchCaseA !== switchCaseB) {
237
- const switchA = findAncestor(switchCaseA, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.SwitchStatement);
238
- const switchB = findAncestor(switchCaseB, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.SwitchStatement);
239
- if (switchA === switchB && switchA) {
240
- if (!caseHasExit(switchCaseA)) {
241
- context.report({ node: callB.node, messageId: 'doubleRelease' });
242
- reported.add(callB.node);
243
- continue;
244
- }
245
- }
246
- }
247
- const ifA = getDirectIfStatement(callA.node);
248
- if (ifA && !ifA.alternate) {
249
- if (callB.node.range[0] > ifA.range[1]) {
250
- const parentBlock = findAncestor(ifA, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement);
251
- if (parentBlock) {
252
- const bParentStmt = callB.node.parent?.type === eslint_devkit_1.AST_NODE_TYPES.ExpressionStatement
253
- ? callB.node.parent : null;
254
- if (bParentStmt && parentBlock.body.includes(bParentStmt)) {
255
- if (!ifBranchHasExitAfterRelease(callA.node, ifA)) {
256
- context.report({ node: callB.node, messageId: 'doubleRelease' });
257
- reported.add(callB.node);
258
- continue;
259
- }
260
- }
261
- }
262
- }
263
- }
264
- const ifB = getDirectIfStatement(callB.node);
265
- if (ifA && ifB && ifA !== ifB && !ifA.alternate && !ifB.alternate) {
266
- const parentBlockA = ifA.parent;
267
- const parentBlockB = ifB.parent;
268
- if (parentBlockA === parentBlockB && parentBlockA?.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement) {
269
- if (!ifBranchHasExitAfterRelease(callA.node, ifA)) {
270
- context.report({ node: callB.node, messageId: 'doubleRelease' });
271
- reported.add(callB.node);
272
- continue;
273
- }
274
- }
275
- }
276
- const parentA = callA.node.parent?.parent;
277
- const parentB = callB.node.parent?.parent;
278
- if (parentA &&
279
- parentB &&
280
- parentA === parentB &&
281
- parentA.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement) {
282
- const block = parentA;
283
- const indexA = block.body.indexOf(callA.node.parent);
284
- const indexB = block.body.indexOf(callB.node.parent);
285
- if (indexB > indexA && !hasExitBetween(block, indexA, indexB)) {
286
- context.report({ node: callB.node, messageId: 'doubleRelease' });
287
- reported.add(callB.node);
288
- continue;
289
- }
290
- }
291
- const catchAForFinally = isInCatchBlock(callA.node);
292
- const finallyB = isInFinallyBlock(callB.node);
293
- if (catchAForFinally && finallyB) {
294
- const tryOfCatch = catchAForFinally.parent;
295
- if (tryOfCatch === finallyB) {
296
- context.report({ node: callB.node, messageId: 'doubleRelease' });
297
- reported.add(callB.node);
298
- continue;
299
- }
300
- }
301
- const tryAForFinally = isInTryBlock(callA.node);
302
- const finallyB2 = isInFinallyBlock(callB.node);
303
- if (tryAForFinally && finallyB2 && tryAForFinally === finallyB2) {
304
- context.report({ node: callB.node, messageId: 'doubleRelease' });
305
- reported.add(callB.node);
306
- continue;
307
- }
308
- const finallyA = isInFinallyBlock(callA.node);
309
- if (finallyA) {
310
- const tryEndPos = finallyA.range[1];
311
- if (callB.node.range[0] > tryEndPos) {
312
- context.report({ node: callB.node, messageId: 'doubleRelease' });
313
- reported.add(callB.node);
314
- continue;
315
- }
316
- }
317
- const tryAWithOrWithout = isInTryBlock(callA.node);
318
- if (tryAWithOrWithout) {
319
- const tryEndPos = tryAWithOrWithout.range[1];
320
- if (callB.node.range[0] > tryEndPos && !isInFinallyBlock(callB.node)) {
321
- context.report({ node: callB.node, messageId: 'doubleRelease' });
322
- reported.add(callB.node);
323
- continue;
324
- }
325
- }
326
- const ifStmtA = findAncestor(callA.node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement);
327
- if (ifStmtA && finallyB) {
328
- if (!ifBranchHasExitAfterRelease(callA.node, ifStmtA)) {
329
- context.report({ node: callB.node, messageId: 'doubleRelease' });
330
- reported.add(callB.node);
331
- continue;
332
- }
333
- }
334
- const catchAForAfter = isInCatchBlock(callA.node);
335
- if (catchAForAfter) {
336
- const tryOfCatchA = catchAForAfter.parent;
337
- const tryEndPos = tryOfCatchA.range[1];
338
- if (callB.node.range[0] > tryEndPos) {
339
- context.report({ node: callB.node, messageId: 'doubleRelease' });
340
- reported.add(callB.node);
341
- continue;
342
- }
343
- }
344
- const isInExpression = (exprNode) => {
345
- let current = exprNode.parent;
346
- while (current) {
347
- if (current.type === eslint_devkit_1.AST_NODE_TYPES.ConditionalExpression)
348
- return true;
349
- if (current.type === eslint_devkit_1.AST_NODE_TYPES.LogicalExpression)
350
- return true;
351
- if (current.type === eslint_devkit_1.AST_NODE_TYPES.ExpressionStatement)
352
- break;
353
- if (current.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement)
354
- break;
355
- current = current.parent;
356
- }
357
- return false;
358
- };
359
- if (isInExpression(callA.node)) {
360
- let exprStmtA = callA.node;
361
- while (exprStmtA && exprStmtA.type !== eslint_devkit_1.AST_NODE_TYPES.ExpressionStatement) {
362
- exprStmtA = exprStmtA.parent ?? null;
363
- }
364
- if (exprStmtA && callB.node.range[0] > exprStmtA.range[1]) {
365
- const blockA = findAncestor(exprStmtA, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement);
366
- const blockB = findAncestor(callB.node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement);
367
- if (blockA && blockA === blockB) {
368
- context.report({ node: callB.node, messageId: 'doubleRelease' });
369
- reported.add(callB.node);
370
- continue;
371
- }
372
- }
373
- }
374
- }
375
- }
376
- }
377
- }
378
- },
379
- };
380
- },
381
- };
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.preventDoubleRelease=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");function findAncestor(node,predicate){let current=node?.parent;while(current){if(predicate(current))return current;current=current.parent}return null}function isInCatchBlock(node){return findAncestor(node,n=>n.type===eslint_devkit_1.AST_NODE_TYPES.CatchClause)}function isInFinallyBlock(node){const tryStmt=findAncestor(node,n=>n.type===eslint_devkit_1.AST_NODE_TYPES.TryStatement);if(!tryStmt?.finalizer)return null;const nodeRange=node.range;const finalizerRange=tryStmt.finalizer.range;if(nodeRange[0]>=finalizerRange[0]&&nodeRange[1]<=finalizerRange[1]){return tryStmt}return null}function isInTryBlock(node){const tryStmt=findAncestor(node,n=>n.type===eslint_devkit_1.AST_NODE_TYPES.TryStatement);if(!tryStmt)return null;const nodeRange=node.range;const blockRange=tryStmt.block.range;if(nodeRange[0]>=blockRange[0]&&nodeRange[1]<=blockRange[1]){return tryStmt}return null}function getContainingFunction(node){return findAncestor(node,n=>n.type===eslint_devkit_1.AST_NODE_TYPES.FunctionDeclaration||n.type===eslint_devkit_1.AST_NODE_TYPES.FunctionExpression||n.type===eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression)}function isGuardedByCondition(node){const ifStmt=findAncestor(node,n=>n.type===eslint_devkit_1.AST_NODE_TYPES.IfStatement);if(!ifStmt)return false;const conditionText=ifStmt.test;if(conditionText.type===eslint_devkit_1.AST_NODE_TYPES.UnaryExpression&&conditionText.operator==="!"){const arg=conditionText.argument;if(arg.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const name=arg.name.toLowerCase();return name.includes("released")||name.includes("done")||name.includes("closed")}if(arg.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&arg.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const name=arg.property.name.toLowerCase();return name.includes("released")||name.includes("done")||name.includes("closed")}}return false}function areMutuallyExclusiveBranches(nodeA,nodeB){const ifA=findAncestor(nodeA,n=>n.type===eslint_devkit_1.AST_NODE_TYPES.IfStatement);const ifB=findAncestor(nodeB,n=>n.type===eslint_devkit_1.AST_NODE_TYPES.IfStatement);if(!ifA||!ifB)return false;if(ifA===ifB){const nodeARange=nodeA.range;const nodeBRange=nodeB.range;const consequentRange=ifA.consequent.range;const alternateRange=ifA.alternate?.range;if(!alternateRange)return false;const aInConsequent=nodeARange[0]>=consequentRange[0]&&nodeARange[1]<=consequentRange[1];const bInConsequent=nodeBRange[0]>=consequentRange[0]&&nodeBRange[1]<=consequentRange[1];const aInAlternate=nodeARange[0]>=alternateRange[0]&&nodeARange[1]<=alternateRange[1];const bInAlternate=nodeBRange[0]>=alternateRange[0]&&nodeBRange[1]<=alternateRange[1];return aInConsequent&&bInAlternate||aInAlternate&&bInConsequent}return false}function hasExitBetween(block,startIdx,endIdx){for(let k=startIdx+1;k<endIdx;k++){const stmt=block.body[k];if(stmt.type===eslint_devkit_1.AST_NODE_TYPES.ReturnStatement)return true;if(stmt.type===eslint_devkit_1.AST_NODE_TYPES.ThrowStatement)return true}return false}function caseHasExit(switchCase){for(const stmt of switchCase.consequent){if(stmt.type===eslint_devkit_1.AST_NODE_TYPES.ReturnStatement)return true;if(stmt.type===eslint_devkit_1.AST_NODE_TYPES.ThrowStatement)return true;if(stmt.type===eslint_devkit_1.AST_NODE_TYPES.BreakStatement)return true}return false}function getSwitchCase(node){return findAncestor(node,n=>n.type===eslint_devkit_1.AST_NODE_TYPES.SwitchCase)}function getDirectIfStatement(node){return findAncestor(node,n=>n.type===eslint_devkit_1.AST_NODE_TYPES.IfStatement)}function ifBranchHasExitAfterRelease(releaseNode,ifStmt){const releaseRange=releaseNode.range;const consequentRange=ifStmt.consequent.range;const inConsequent=releaseRange[0]>=consequentRange[0]&&releaseRange[1]<=consequentRange[1];let branchBlock=null;if(inConsequent&&ifStmt.consequent.type===eslint_devkit_1.AST_NODE_TYPES.BlockStatement){branchBlock=ifStmt.consequent}else if(!inConsequent&&ifStmt.alternate?.type===eslint_devkit_1.AST_NODE_TYPES.BlockStatement){branchBlock=ifStmt.alternate}if(!branchBlock)return false;const releaseParentStmt=releaseNode.parent?.type===eslint_devkit_1.AST_NODE_TYPES.ExpressionStatement?releaseNode.parent:null;if(!releaseParentStmt)return false;const releaseIdx=branchBlock.body.indexOf(releaseParentStmt);if(releaseIdx===-1)return false;for(let i=releaseIdx+1;i<branchBlock.body.length;i++){const stmt=branchBlock.body[i];if(stmt.type===eslint_devkit_1.AST_NODE_TYPES.ReturnStatement||stmt.type===eslint_devkit_1.AST_NODE_TYPES.ThrowStatement){return true}}return false}exports.preventDoubleRelease={meta:{type:"problem",docs:{description:"Prevent releasing a pg client multiple times.",url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/prevent-double-release.md",cwe:"CWE-415",cweJustification:"CWE-415 (Double Free) \u2014 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.",confidence:"medium"},messages:{doubleRelease:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.QUALITY,issueName:"Double Release",description:"Client release() called multiple times on the same object.",severity:"HIGH",effort:"low",fix:"Ensure client.release() is called exactly once per acquisition, preferably in a finally block.",documentationLink:"https://node-postgres.com/api/client#clientrelease"}),doubleReleaseCallback:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.QUALITY,issueName:"Double Release (Callback)",description:"Connection release callback (done) called multiple times.",severity:"HIGH",effort:"low",fix:"Ensure done() or release callback is called exactly once per pool.connect() callback.",documentationLink:"https://node-postgres.com/api/pool#poolconnect"})},schema:[]},defaultOptions:[],create(context){return{VariableDeclarator(node){const declaredVariables=context.sourceCode.getDeclaredVariables(node);for(const variable of declaredVariables){const def=variable.defs[0];const isDestructuredRelease=def?.type==="Variable"&&def.node.id.type===eslint_devkit_1.AST_NODE_TYPES.ObjectPattern&&def.name.name==="release";const releaseCalls=[];variable.references.forEach(ref=>{const id=ref.identifier;if(isDestructuredRelease){if(id.parent?.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression&&id.parent.callee===id){const callNode=id.parent;const guarded=isGuardedByCondition(callNode);releaseCalls.push({node:callNode,guarded})}}else{if(id.parent?.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&id.parent.object===id&&id.parent.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&id.parent.property.name==="release"&&id.parent.parent?.type===eslint_devkit_1.AST_NODE_TYPES.CallExpression){const callNode=id.parent.parent;const guarded=isGuardedByCondition(callNode);releaseCalls.push({node:callNode,guarded})}}});if(releaseCalls.length>1){releaseCalls.sort((a,b)=>a.node.range[0]-b.node.range[0]);const reported=new Set;for(let i=0;i<releaseCalls.length;i++){for(let j=i+1;j<releaseCalls.length;j++){const callA=releaseCalls[i];const callB=releaseCalls[j];if(reported.has(callB.node))continue;if(callA.guarded&&callB.guarded)continue;if(areMutuallyExclusiveBranches(callA.node,callB.node))continue;const funcA=getContainingFunction(callA.node);const funcB=getContainingFunction(callB.node);if(funcA!==funcB)continue;const tryA=isInTryBlock(callA.node);const catchB=isInCatchBlock(callB.node);if(tryA&&catchB&&catchB.parent===tryA){context.report({node:callB.node,messageId:"doubleRelease"});reported.add(callB.node);continue}const catchA=isInCatchBlock(callA.node);const tryB=isInTryBlock(callB.node);if(catchA&&tryB&&catchA.parent===tryB){context.report({node:callB.node,messageId:"doubleRelease"});reported.add(callB.node);continue}const switchCaseA=getSwitchCase(callA.node);const switchCaseB=getSwitchCase(callB.node);if(switchCaseA&&switchCaseB&&switchCaseA!==switchCaseB){const switchA=findAncestor(switchCaseA,n=>n.type===eslint_devkit_1.AST_NODE_TYPES.SwitchStatement);const switchB=findAncestor(switchCaseB,n=>n.type===eslint_devkit_1.AST_NODE_TYPES.SwitchStatement);if(switchA===switchB&&switchA){if(!caseHasExit(switchCaseA)){context.report({node:callB.node,messageId:"doubleRelease"});reported.add(callB.node);continue}}}const ifA=getDirectIfStatement(callA.node);if(ifA&&!ifA.alternate){if(callB.node.range[0]>ifA.range[1]){const parentBlock=findAncestor(ifA,n=>n.type===eslint_devkit_1.AST_NODE_TYPES.BlockStatement);if(parentBlock){const bParentStmt=callB.node.parent?.type===eslint_devkit_1.AST_NODE_TYPES.ExpressionStatement?callB.node.parent:null;if(bParentStmt&&parentBlock.body.includes(bParentStmt)){if(!ifBranchHasExitAfterRelease(callA.node,ifA)){context.report({node:callB.node,messageId:"doubleRelease"});reported.add(callB.node);continue}}}}}const ifB=getDirectIfStatement(callB.node);if(ifA&&ifB&&ifA!==ifB&&!ifA.alternate&&!ifB.alternate){const parentBlockA=ifA.parent;const parentBlockB=ifB.parent;if(parentBlockA===parentBlockB&&parentBlockA?.type===eslint_devkit_1.AST_NODE_TYPES.BlockStatement){if(!ifBranchHasExitAfterRelease(callA.node,ifA)){context.report({node:callB.node,messageId:"doubleRelease"});reported.add(callB.node);continue}}}const parentA=callA.node.parent?.parent;const parentB=callB.node.parent?.parent;if(parentA&&parentB&&parentA===parentB&&parentA.type===eslint_devkit_1.AST_NODE_TYPES.BlockStatement){const block=parentA;const indexA=block.body.indexOf(callA.node.parent);const indexB=block.body.indexOf(callB.node.parent);if(indexB>indexA&&!hasExitBetween(block,indexA,indexB)){context.report({node:callB.node,messageId:"doubleRelease"});reported.add(callB.node);continue}}const catchAForFinally=isInCatchBlock(callA.node);const finallyB=isInFinallyBlock(callB.node);if(catchAForFinally&&finallyB){const tryOfCatch=catchAForFinally.parent;if(tryOfCatch===finallyB){context.report({node:callB.node,messageId:"doubleRelease"});reported.add(callB.node);continue}}const tryAForFinally=isInTryBlock(callA.node);const finallyB2=isInFinallyBlock(callB.node);if(tryAForFinally&&finallyB2&&tryAForFinally===finallyB2){context.report({node:callB.node,messageId:"doubleRelease"});reported.add(callB.node);continue}const finallyA=isInFinallyBlock(callA.node);if(finallyA){const tryEndPos=finallyA.range[1];if(callB.node.range[0]>tryEndPos){context.report({node:callB.node,messageId:"doubleRelease"});reported.add(callB.node);continue}}const tryAWithOrWithout=isInTryBlock(callA.node);if(tryAWithOrWithout){const tryEndPos=tryAWithOrWithout.range[1];if(callB.node.range[0]>tryEndPos&&!isInFinallyBlock(callB.node)){context.report({node:callB.node,messageId:"doubleRelease"});reported.add(callB.node);continue}}const ifStmtA=findAncestor(callA.node,n=>n.type===eslint_devkit_1.AST_NODE_TYPES.IfStatement);if(ifStmtA&&finallyB){if(!ifBranchHasExitAfterRelease(callA.node,ifStmtA)){context.report({node:callB.node,messageId:"doubleRelease"});reported.add(callB.node);continue}}const catchAForAfter=isInCatchBlock(callA.node);if(catchAForAfter){const tryOfCatchA=catchAForAfter.parent;const tryEndPos=tryOfCatchA.range[1];if(callB.node.range[0]>tryEndPos){context.report({node:callB.node,messageId:"doubleRelease"});reported.add(callB.node);continue}}const isInExpression=exprNode=>{let current=exprNode.parent;while(current){if(current.type===eslint_devkit_1.AST_NODE_TYPES.ConditionalExpression)return true;if(current.type===eslint_devkit_1.AST_NODE_TYPES.LogicalExpression)return true;if(current.type===eslint_devkit_1.AST_NODE_TYPES.ExpressionStatement)break;if(current.type===eslint_devkit_1.AST_NODE_TYPES.BlockStatement)break;current=current.parent}return false};if(isInExpression(callA.node)){let exprStmtA=callA.node;while(exprStmtA&&exprStmtA.type!==eslint_devkit_1.AST_NODE_TYPES.ExpressionStatement){exprStmtA=exprStmtA.parent??null}if(exprStmtA&&callB.node.range[0]>exprStmtA.range[1]){const blockA=findAncestor(exprStmtA,n=>n.type===eslint_devkit_1.AST_NODE_TYPES.BlockStatement);const blockB=findAncestor(callB.node,n=>n.type===eslint_devkit_1.AST_NODE_TYPES.BlockStatement);if(blockA&&blockA===blockB){context.report({node:callB.node,messageId:"doubleRelease"});reported.add(callB.node);continue}}}}}}}}}}};
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});