eslint-plugin-postgresql-security 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/CHANGELOG.md +150 -0
  2. package/LICENSE +23 -0
  3. package/README.md +169 -0
  4. package/dist/AGENTS.md +118 -0
  5. package/dist/CHANGELOG.md +150 -0
  6. package/dist/LICENSE +23 -0
  7. package/dist/README.md +169 -0
  8. package/dist/package.json +99 -0
  9. package/dist/src/constants.d.ts +6 -0
  10. package/dist/src/constants.js +13 -0
  11. package/dist/src/index.d.ts +26 -0
  12. package/dist/src/index.js +116 -0
  13. package/dist/src/oxlint.d.ts +20 -0
  14. package/dist/src/oxlint.js +21 -0
  15. package/dist/src/rules/check-query-params/index.d.ts +8 -0
  16. package/dist/src/rules/check-query-params/index.js +90 -0
  17. package/dist/src/rules/no-batch-insert-loop/index.d.ts +8 -0
  18. package/dist/src/rules/no-batch-insert-loop/index.js +90 -0
  19. package/dist/src/rules/no-floating-query/index.d.ts +8 -0
  20. package/dist/src/rules/no-floating-query/index.js +74 -0
  21. package/dist/src/rules/no-hardcoded-credentials/index.d.ts +8 -0
  22. package/dist/src/rules/no-hardcoded-credentials/index.js +86 -0
  23. package/dist/src/rules/no-insecure-ssl/index.d.ts +8 -0
  24. package/dist/src/rules/no-insecure-ssl/index.js +66 -0
  25. package/dist/src/rules/no-missing-client-release/index.d.ts +8 -0
  26. package/dist/src/rules/no-missing-client-release/index.js +85 -0
  27. package/dist/src/rules/no-select-all/index.d.ts +8 -0
  28. package/dist/src/rules/no-select-all/index.js +79 -0
  29. package/dist/src/rules/no-transaction-on-pool/index.d.ts +8 -0
  30. package/dist/src/rules/no-transaction-on-pool/index.js +71 -0
  31. package/dist/src/rules/no-unsafe-copy-from/index.d.ts +10 -0
  32. package/dist/src/rules/no-unsafe-copy-from/index.js +205 -0
  33. package/dist/src/rules/no-unsafe-query/index.d.ts +8 -0
  34. package/dist/src/rules/no-unsafe-query/index.js +132 -0
  35. package/dist/src/rules/no-unsafe-search-path/index.d.ts +8 -0
  36. package/dist/src/rules/no-unsafe-search-path/index.js +144 -0
  37. package/dist/src/rules/prefer-pool-query/index.d.ts +8 -0
  38. package/dist/src/rules/prefer-pool-query/index.js +104 -0
  39. package/dist/src/rules/prevent-double-release/index.d.ts +8 -0
  40. package/dist/src/rules/prevent-double-release/index.js +456 -0
  41. package/dist/src/types/index.d.ts +54 -0
  42. package/dist/src/types/index.js +7 -0
  43. package/package.json +99 -0
  44. package/src/__compatibility__/pg-interface.spec.ts +495 -0
  45. package/src/constants.ts +11 -0
  46. package/src/index.spec.ts +14 -0
  47. package/src/index.test.ts +59 -0
  48. package/src/index.ts +130 -0
  49. package/src/oxlint.spec.ts +70 -0
  50. package/src/oxlint.ts +23 -0
  51. package/src/rules/check-query-params/check-query-params.test.ts +68 -0
  52. package/src/rules/check-query-params/index.spec.ts +117 -0
  53. package/src/rules/check-query-params/index.ts +106 -0
  54. package/src/rules/no-batch-insert-loop/index.spec.ts +105 -0
  55. package/src/rules/no-batch-insert-loop/index.ts +105 -0
  56. package/src/rules/no-batch-insert-loop/no-batch-insert-loop.test.ts +68 -0
  57. package/src/rules/no-floating-query/index.spec.ts +112 -0
  58. package/src/rules/no-floating-query/index.ts +81 -0
  59. package/src/rules/no-floating-query/no-floating-query.test.ts +68 -0
  60. package/src/rules/no-hardcoded-credentials/index.spec.ts +38 -0
  61. package/src/rules/no-hardcoded-credentials/index.ts +93 -0
  62. package/src/rules/no-hardcoded-credentials/no-hardcoded-credentials.test.ts +68 -0
  63. package/src/rules/no-insecure-ssl/index.spec.ts +40 -0
  64. package/src/rules/no-insecure-ssl/index.ts +81 -0
  65. package/src/rules/no-insecure-ssl/no-insecure-ssl.test.ts +68 -0
  66. package/src/rules/no-missing-client-release/coverage-gaps.spec.ts +42 -0
  67. package/src/rules/no-missing-client-release/index.spec.ts +52 -0
  68. package/src/rules/no-missing-client-release/index.ts +100 -0
  69. package/src/rules/no-missing-client-release/no-missing-client-release.test.ts +68 -0
  70. package/src/rules/no-select-all/index.spec.ts +44 -0
  71. package/src/rules/no-select-all/index.ts +92 -0
  72. package/src/rules/no-select-all/no-select-all.test.ts +68 -0
  73. package/src/rules/no-transaction-on-pool/index.spec.ts +36 -0
  74. package/src/rules/no-transaction-on-pool/index.ts +85 -0
  75. package/src/rules/no-transaction-on-pool/no-transaction-on-pool.test.ts +68 -0
  76. package/src/rules/no-unsafe-copy-from/coverage-gaps.spec.ts +76 -0
  77. package/src/rules/no-unsafe-copy-from/index.spec.ts +274 -0
  78. package/src/rules/no-unsafe-copy-from/index.ts +226 -0
  79. package/src/rules/no-unsafe-copy-from/no-unsafe-copy-from.test.ts +78 -0
  80. package/src/rules/no-unsafe-query/coverage-gaps.spec.ts +61 -0
  81. package/src/rules/no-unsafe-query/index.spec.ts +103 -0
  82. package/src/rules/no-unsafe-query/index.ts +149 -0
  83. package/src/rules/no-unsafe-query/no-unsafe-query.test.ts +73 -0
  84. package/src/rules/no-unsafe-search-path/index.spec.ts +46 -0
  85. package/src/rules/no-unsafe-search-path/index.ts +164 -0
  86. package/src/rules/no-unsafe-search-path/no-unsafe-search-path.test.ts +68 -0
  87. package/src/rules/prefer-pool-query/coverage-gaps.spec.ts +76 -0
  88. package/src/rules/prefer-pool-query/index.spec.ts +97 -0
  89. package/src/rules/prefer-pool-query/index.ts +118 -0
  90. package/src/rules/prefer-pool-query/prefer-pool-query.test.ts +68 -0
  91. package/src/rules/prevent-double-release/coverage-gaps.spec.ts +538 -0
  92. package/src/rules/prevent-double-release/index.spec.ts +824 -0
  93. package/src/rules/prevent-double-release/index.ts +534 -0
  94. package/src/rules/prevent-double-release/prevent-double-release.test.ts +70 -0
  95. package/src/types/index.ts +58 -0
@@ -0,0 +1,456 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) 2025 Ofri Peretz
4
+ * Licensed under the MIT License. Use of this source code is governed by the
5
+ * MIT license that can be found in the LICENSE file.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.preventDoubleRelease = void 0;
9
+ const eslint_devkit_1 = require("@interlace/eslint-devkit");
10
+ /**
11
+ * Finds the nearest ancestor of a given type.
12
+ */
13
+ function findAncestor(node, predicate) {
14
+ let current = node?.parent;
15
+ while (current) {
16
+ if (predicate(current))
17
+ return current;
18
+ current = current.parent;
19
+ }
20
+ return null;
21
+ }
22
+ /**
23
+ * Checks if node is inside a TryStatement's catch block
24
+ */
25
+ function isInCatchBlock(node) {
26
+ return findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.CatchClause);
27
+ }
28
+ /**
29
+ * Checks if node is inside a TryStatement's finally block
30
+ */
31
+ function isInFinallyBlock(node) {
32
+ const tryStmt = findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.TryStatement);
33
+ if (!tryStmt?.finalizer)
34
+ return null;
35
+ const nodeRange = node.range;
36
+ const finalizerRange = tryStmt.finalizer.range;
37
+ if (nodeRange[0] >= finalizerRange[0] && nodeRange[1] <= finalizerRange[1]) {
38
+ return tryStmt;
39
+ }
40
+ return null;
41
+ }
42
+ /**
43
+ * Checks if node is inside a TryStatement's try block
44
+ */
45
+ function isInTryBlock(node) {
46
+ const tryStmt = findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.TryStatement);
47
+ if (!tryStmt)
48
+ return null;
49
+ const nodeRange = node.range;
50
+ const blockRange = tryStmt.block.range;
51
+ if (nodeRange[0] >= blockRange[0] && nodeRange[1] <= blockRange[1]) {
52
+ return tryStmt;
53
+ }
54
+ return null;
55
+ }
56
+ /**
57
+ * Gets the containing function for a node
58
+ */
59
+ function getContainingFunction(node) {
60
+ return findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.FunctionDeclaration ||
61
+ n.type === eslint_devkit_1.AST_NODE_TYPES.FunctionExpression ||
62
+ n.type === eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression);
63
+ }
64
+ /**
65
+ * Checks if node is guarded by a conditional check (e.g., if (!released))
66
+ */
67
+ function isGuardedByCondition(node) {
68
+ const ifStmt = findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement);
69
+ if (!ifStmt)
70
+ return false;
71
+ const conditionText = ifStmt.test;
72
+ // Check for patterns like: !released, released === false, !client.released
73
+ if (conditionText.type === eslint_devkit_1.AST_NODE_TYPES.UnaryExpression && conditionText.operator === '!') {
74
+ const arg = conditionText.argument;
75
+ if (arg.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
76
+ const name = arg.name.toLowerCase();
77
+ return name.includes('released') || name.includes('done') || name.includes('closed');
78
+ }
79
+ if (arg.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression && arg.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
80
+ const name = arg.property.name.toLowerCase();
81
+ return name.includes('released') || name.includes('done') || name.includes('closed');
82
+ }
83
+ }
84
+ return false;
85
+ }
86
+ /**
87
+ * Checks if a node is in an else-if/else branch that's mutually exclusive from another
88
+ */
89
+ function areMutuallyExclusiveBranches(nodeA, nodeB) {
90
+ const ifA = findAncestor(nodeA, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement);
91
+ const ifB = findAncestor(nodeB, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement);
92
+ if (!ifA || !ifB)
93
+ return false;
94
+ // Check if both are in the same if/else chain
95
+ if (ifA === ifB) {
96
+ const nodeARange = nodeA.range;
97
+ const nodeBRange = nodeB.range;
98
+ const consequentRange = ifA.consequent.range;
99
+ const alternateRange = ifA.alternate?.range;
100
+ if (!alternateRange)
101
+ return false;
102
+ const aInConsequent = nodeARange[0] >= consequentRange[0] && nodeARange[1] <= consequentRange[1];
103
+ const bInConsequent = nodeBRange[0] >= consequentRange[0] && nodeBRange[1] <= consequentRange[1];
104
+ const aInAlternate = nodeARange[0] >= alternateRange[0] && nodeARange[1] <= alternateRange[1];
105
+ const bInAlternate = nodeBRange[0] >= alternateRange[0] && nodeBRange[1] <= alternateRange[1];
106
+ return (aInConsequent && bInAlternate) || (aInAlternate && bInConsequent);
107
+ }
108
+ return false;
109
+ }
110
+ /**
111
+ * Checks if there's a return/throw statement between two positions in a block
112
+ */
113
+ function hasExitBetween(block, startIdx, endIdx) {
114
+ for (let k = startIdx + 1; k < endIdx; k++) {
115
+ const stmt = block.body[k];
116
+ if (stmt.type === eslint_devkit_1.AST_NODE_TYPES.ReturnStatement)
117
+ return true;
118
+ if (stmt.type === eslint_devkit_1.AST_NODE_TYPES.ThrowStatement)
119
+ return true;
120
+ }
121
+ return false;
122
+ }
123
+ /**
124
+ * Checks if a switch case has a break/return/throw
125
+ */
126
+ function caseHasExit(switchCase) {
127
+ for (const stmt of switchCase.consequent) {
128
+ if (stmt.type === eslint_devkit_1.AST_NODE_TYPES.ReturnStatement)
129
+ return true;
130
+ if (stmt.type === eslint_devkit_1.AST_NODE_TYPES.ThrowStatement)
131
+ return true;
132
+ if (stmt.type === eslint_devkit_1.AST_NODE_TYPES.BreakStatement)
133
+ return true;
134
+ }
135
+ return false;
136
+ }
137
+ /**
138
+ * Gets the switch case containing a node
139
+ */
140
+ function getSwitchCase(node) {
141
+ return findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.SwitchCase);
142
+ }
143
+ /**
144
+ * Gets the if statement directly containing a node (not ancestors)
145
+ */
146
+ function getDirectIfStatement(node) {
147
+ return findAncestor(node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement);
148
+ }
149
+ /**
150
+ * Checks if the if statement's branch containing the release has a return/throw after it
151
+ */
152
+ function ifBranchHasExitAfterRelease(releaseNode, ifStmt) {
153
+ // Find which branch contains the release
154
+ const releaseRange = releaseNode.range;
155
+ const consequentRange = ifStmt.consequent.range;
156
+ const inConsequent = releaseRange[0] >= consequentRange[0] && releaseRange[1] <= consequentRange[1];
157
+ let branchBlock = null;
158
+ if (inConsequent && ifStmt.consequent.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement) {
159
+ branchBlock = ifStmt.consequent;
160
+ }
161
+ else if (!inConsequent && ifStmt.alternate?.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement) {
162
+ branchBlock = ifStmt.alternate;
163
+ }
164
+ if (!branchBlock)
165
+ return false;
166
+ // Find the release statement index
167
+ const releaseParentStmt = releaseNode.parent?.type === eslint_devkit_1.AST_NODE_TYPES.ExpressionStatement
168
+ ? releaseNode.parent
169
+ : null;
170
+ if (!releaseParentStmt)
171
+ return false;
172
+ const releaseIdx = branchBlock.body.indexOf(releaseParentStmt);
173
+ if (releaseIdx === -1)
174
+ return false;
175
+ // Check if there's a return/throw after the release
176
+ for (let i = releaseIdx + 1; i < branchBlock.body.length; i++) {
177
+ const stmt = branchBlock.body[i];
178
+ if (stmt.type === eslint_devkit_1.AST_NODE_TYPES.ReturnStatement || stmt.type === eslint_devkit_1.AST_NODE_TYPES.ThrowStatement) {
179
+ return true;
180
+ }
181
+ }
182
+ return false;
183
+ }
184
+ exports.preventDoubleRelease = {
185
+ meta: {
186
+ type: 'problem',
187
+ docs: {
188
+ description: 'Prevent releasing a pg client multiple times.',
189
+ url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/prevent-double-release.md',
190
+ cwe: 'CWE-415',
191
+ 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.',
192
+ confidence: 'medium',
193
+ },
194
+ messages: {
195
+ doubleRelease: (0, eslint_devkit_1.formatLLMMessage)({
196
+ icon: eslint_devkit_1.MessageIcons.QUALITY,
197
+ issueName: 'Double Release',
198
+ description: 'Client release() called multiple times on the same object.',
199
+ severity: 'HIGH',
200
+ effort: 'low',
201
+ fix: 'Ensure client.release() is called exactly once per acquisition, preferably in a finally block.',
202
+ documentationLink: 'https://node-postgres.com/api/client#clientrelease',
203
+ }),
204
+ doubleReleaseCallback: (0, eslint_devkit_1.formatLLMMessage)({
205
+ icon: eslint_devkit_1.MessageIcons.QUALITY,
206
+ issueName: 'Double Release (Callback)',
207
+ description: 'Connection release callback (done) called multiple times.',
208
+ severity: 'HIGH',
209
+ effort: 'low',
210
+ fix: 'Ensure done() or release callback is called exactly once per pool.connect() callback.',
211
+ documentationLink: 'https://node-postgres.com/api/pool#poolconnect',
212
+ }),
213
+ },
214
+ schema: [],
215
+ },
216
+ defaultOptions: [],
217
+ create(context) {
218
+ return {
219
+ VariableDeclarator(node) {
220
+ const declaredVariables = context.sourceCode.getDeclaredVariables(node);
221
+ for (const variable of declaredVariables) {
222
+ const def = variable.defs[0];
223
+ const isDestructuredRelease = def?.type === 'Variable' &&
224
+ def.node.id.type === eslint_devkit_1.AST_NODE_TYPES.ObjectPattern &&
225
+ def.name.name === 'release';
226
+ const releaseCalls = [];
227
+ variable.references.forEach((ref) => {
228
+ const id = ref.identifier;
229
+ if (isDestructuredRelease) {
230
+ if (id.parent?.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression &&
231
+ id.parent.callee === id) {
232
+ const callNode = id.parent;
233
+ const guarded = isGuardedByCondition(callNode);
234
+ releaseCalls.push({ node: callNode, guarded });
235
+ }
236
+ }
237
+ else {
238
+ if (id.parent?.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
239
+ id.parent.object === id &&
240
+ id.parent.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
241
+ id.parent.property.name === 'release' &&
242
+ id.parent.parent?.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression) {
243
+ const callNode = id.parent.parent;
244
+ const guarded = isGuardedByCondition(callNode);
245
+ releaseCalls.push({ node: callNode, guarded });
246
+ }
247
+ }
248
+ });
249
+ if (releaseCalls.length > 1) {
250
+ releaseCalls.sort((a, b) => a.node.range[0] - b.node.range[0]);
251
+ const reported = new Set();
252
+ for (let i = 0; i < releaseCalls.length; i++) {
253
+ for (let j = i + 1; j < releaseCalls.length; j++) {
254
+ const callA = releaseCalls[i];
255
+ const callB = releaseCalls[j];
256
+ // Skip if already reported
257
+ if (reported.has(callB.node))
258
+ continue;
259
+ // Skip if both are guarded
260
+ if (callA.guarded && callB.guarded)
261
+ continue;
262
+ // Skip if mutually exclusive branches
263
+ if (areMutuallyExclusiveBranches(callA.node, callB.node))
264
+ continue;
265
+ // Same function scope check
266
+ const funcA = getContainingFunction(callA.node);
267
+ const funcB = getContainingFunction(callB.node);
268
+ if (funcA !== funcB)
269
+ continue;
270
+ // Case 1: Try + Catch (not finally) - both paths release
271
+ const tryA = isInTryBlock(callA.node);
272
+ const catchB = isInCatchBlock(callB.node);
273
+ if (tryA && catchB && catchB.parent === tryA) {
274
+ context.report({ node: callB.node, messageId: 'doubleRelease' });
275
+ reported.add(callB.node);
276
+ continue;
277
+ }
278
+ // Case 2: Catch + Try (reversed order in source)
279
+ const catchA = isInCatchBlock(callA.node);
280
+ const tryB = isInTryBlock(callB.node);
281
+ if (catchA && tryB && catchA.parent === tryB) {
282
+ context.report({ node: callB.node, messageId: 'doubleRelease' });
283
+ reported.add(callB.node);
284
+ continue;
285
+ }
286
+ // Case 3: Switch fallthrough
287
+ const switchCaseA = getSwitchCase(callA.node);
288
+ const switchCaseB = getSwitchCase(callB.node);
289
+ if (switchCaseA && switchCaseB && switchCaseA !== switchCaseB) {
290
+ // Check if same switch statement
291
+ const switchA = findAncestor(switchCaseA, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.SwitchStatement);
292
+ const switchB = findAncestor(switchCaseB, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.SwitchStatement);
293
+ if (switchA === switchB && switchA) {
294
+ // Check if caseA doesn't have break/return/throw (fallthrough)
295
+ if (!caseHasExit(switchCaseA)) {
296
+ context.report({ node: callB.node, messageId: 'doubleRelease' });
297
+ reported.add(callB.node);
298
+ continue;
299
+ }
300
+ }
301
+ }
302
+ // Case 4: If without else + sequential release
303
+ const ifA = getDirectIfStatement(callA.node);
304
+ if (ifA && !ifA.alternate) {
305
+ // B is after the if statement
306
+ if (callB.node.range[0] > ifA.range[1]) {
307
+ // Check if B is in same block as the if
308
+ const parentBlock = findAncestor(ifA, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement);
309
+ if (parentBlock) {
310
+ const bParentStmt = callB.node.parent?.type === eslint_devkit_1.AST_NODE_TYPES.ExpressionStatement
311
+ ? callB.node.parent : null;
312
+ if (bParentStmt && parentBlock.body.includes(bParentStmt)) {
313
+ // Check if the if branch has exit after release
314
+ if (!ifBranchHasExitAfterRelease(callA.node, ifA)) {
315
+ context.report({ node: callB.node, messageId: 'doubleRelease' });
316
+ reported.add(callB.node);
317
+ continue;
318
+ }
319
+ }
320
+ }
321
+ }
322
+ }
323
+ // Case 5: Two sequential if statements (neither has else)
324
+ const ifB = getDirectIfStatement(callB.node);
325
+ if (ifA && ifB && ifA !== ifB && !ifA.alternate && !ifB.alternate) {
326
+ // Both are different if statements at same level
327
+ const parentBlockA = ifA.parent;
328
+ const parentBlockB = ifB.parent;
329
+ if (parentBlockA === parentBlockB && parentBlockA?.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement) {
330
+ // Check if ifA doesn't exit after release
331
+ if (!ifBranchHasExitAfterRelease(callA.node, ifA)) {
332
+ context.report({ node: callB.node, messageId: 'doubleRelease' });
333
+ reported.add(callB.node);
334
+ continue;
335
+ }
336
+ }
337
+ }
338
+ const parentA = callA.node.parent?.parent;
339
+ const parentB = callB.node.parent?.parent;
340
+ // Case 6: Same Block - direct sequential releases
341
+ if (parentA &&
342
+ parentB &&
343
+ parentA === parentB &&
344
+ parentA.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement) {
345
+ const block = parentA;
346
+ const indexA = block.body.indexOf(callA.node.parent);
347
+ const indexB = block.body.indexOf(callB.node.parent);
348
+ if (indexB > indexA && !hasExitBetween(block, indexA, indexB)) {
349
+ context.report({ node: callB.node, messageId: 'doubleRelease' });
350
+ reported.add(callB.node);
351
+ continue;
352
+ }
353
+ }
354
+ // Case 7: Try/Catch + Finally pattern
355
+ const catchAForFinally = isInCatchBlock(callA.node);
356
+ const finallyB = isInFinallyBlock(callB.node);
357
+ if (catchAForFinally && finallyB) {
358
+ const tryOfCatch = catchAForFinally.parent;
359
+ if (tryOfCatch === finallyB) {
360
+ context.report({ node: callB.node, messageId: 'doubleRelease' });
361
+ reported.add(callB.node);
362
+ continue;
363
+ }
364
+ }
365
+ // Case 8: Try block + Finally pattern
366
+ const tryAForFinally = isInTryBlock(callA.node);
367
+ const finallyB2 = isInFinallyBlock(callB.node);
368
+ if (tryAForFinally && finallyB2 && tryAForFinally === finallyB2) {
369
+ context.report({ node: callB.node, messageId: 'doubleRelease' });
370
+ reported.add(callB.node);
371
+ continue;
372
+ }
373
+ // Case 9: Finally + After try pattern
374
+ const finallyA = isInFinallyBlock(callA.node);
375
+ if (finallyA) {
376
+ const tryEndPos = finallyA.range[1];
377
+ if (callB.node.range[0] > tryEndPos) {
378
+ context.report({ node: callB.node, messageId: 'doubleRelease' });
379
+ reported.add(callB.node);
380
+ continue;
381
+ }
382
+ }
383
+ // Case 10: Try block release + after try (with or without finally)
384
+ const tryAWithOrWithout = isInTryBlock(callA.node);
385
+ if (tryAWithOrWithout) {
386
+ // B is after the entire try statement (including any finally)
387
+ const tryEndPos = tryAWithOrWithout.range[1];
388
+ if (callB.node.range[0] > tryEndPos && !isInFinallyBlock(callB.node)) {
389
+ context.report({ node: callB.node, messageId: 'doubleRelease' });
390
+ reported.add(callB.node);
391
+ continue;
392
+ }
393
+ }
394
+ // Case 11: Early return branch + Finally (without exit)
395
+ const ifStmtA = findAncestor(callA.node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.IfStatement);
396
+ if (ifStmtA && finallyB) {
397
+ if (!ifBranchHasExitAfterRelease(callA.node, ifStmtA)) {
398
+ context.report({ node: callB.node, messageId: 'doubleRelease' });
399
+ reported.add(callB.node);
400
+ continue;
401
+ }
402
+ }
403
+ // Case 12: Catch block + after try (no finally, or B is outside finally)
404
+ const catchAForAfter = isInCatchBlock(callA.node);
405
+ if (catchAForAfter) {
406
+ const tryOfCatchA = catchAForAfter.parent;
407
+ const tryEndPos = tryOfCatchA.range[1];
408
+ if (callB.node.range[0] > tryEndPos) {
409
+ context.report({ node: callB.node, messageId: 'doubleRelease' });
410
+ reported.add(callB.node);
411
+ continue;
412
+ }
413
+ }
414
+ // Case 13: Expression-based release (ternary, short-circuit) + sequential
415
+ // Check if A is in a ternary/conditional expression or logical expression
416
+ const isInExpression = (exprNode) => {
417
+ let current = exprNode.parent;
418
+ while (current) {
419
+ if (current.type === eslint_devkit_1.AST_NODE_TYPES.ConditionalExpression)
420
+ return true;
421
+ if (current.type === eslint_devkit_1.AST_NODE_TYPES.LogicalExpression)
422
+ return true;
423
+ if (current.type === eslint_devkit_1.AST_NODE_TYPES.ExpressionStatement)
424
+ break;
425
+ if (current.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement)
426
+ break;
427
+ current = current.parent;
428
+ }
429
+ return false;
430
+ };
431
+ if (isInExpression(callA.node)) {
432
+ // A is in a conditional/logical expression, B comes after
433
+ // Find the containing expression statement for A
434
+ let exprStmtA = callA.node;
435
+ while (exprStmtA && exprStmtA.type !== eslint_devkit_1.AST_NODE_TYPES.ExpressionStatement) {
436
+ exprStmtA = exprStmtA.parent ?? null;
437
+ }
438
+ if (exprStmtA && callB.node.range[0] > exprStmtA.range[1]) {
439
+ // Check they're in the same block
440
+ const blockA = findAncestor(exprStmtA, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement);
441
+ const blockB = findAncestor(callB.node, (n) => n.type === eslint_devkit_1.AST_NODE_TYPES.BlockStatement);
442
+ if (blockA && blockA === blockB) {
443
+ context.report({ node: callB.node, messageId: 'doubleRelease' });
444
+ reported.add(callB.node);
445
+ continue;
446
+ }
447
+ }
448
+ }
449
+ }
450
+ }
451
+ }
452
+ }
453
+ },
454
+ };
455
+ },
456
+ };
@@ -0,0 +1,54 @@
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
+ export type NoUnsafeQueryOptions = [];
7
+ export type NoInsecureSslOptions = [];
8
+ export type NoHardcodedCredentialsOptions = [];
9
+ export type NoInjectionInIdentifierOptions = [];
10
+ export type CheckQueryParamsOptions = [];
11
+ export type NoMissingClientReleaseOptions = [];
12
+ export type NoTransactionOnPoolOptions = [];
13
+ export type NoFloatingQueryOptions = [];
14
+ export type NoUselessClientOptions = [];
15
+ export type EnforceIdleTimeoutOptions = [];
16
+ export type NoSelectAllOptions = [];
17
+ export type PreferPoolQueryOptions = [];
18
+ export type NoBatchInsertLoopOptions = [];
19
+ export type PreferCursorForLargeResultsOptions = [];
20
+ export type NoTopLevelQueryOptions = [];
21
+ export type EnforceCamelcaseColumnsOptions = [];
22
+ export type ConnectOnceOptions = [];
23
+ export type NoUnsafeSearchPathOptions = [];
24
+ export type NoUnsafeCopyFromOptions = [
25
+ {
26
+ /** Allow hardcoded file paths (for admin/migration scripts). Default: false */
27
+ allowHardcodedPaths?: boolean;
28
+ /** List of allowed file path patterns (regex strings) */
29
+ allowedPaths?: string[];
30
+ }?
31
+ ];
32
+ export type PreventDoubleReleaseOptions = [];
33
+ export interface AllPgRulesOptions {
34
+ 'pg/no-unsafe-query': NoUnsafeQueryOptions;
35
+ 'pg/no-insecure-ssl': NoInsecureSslOptions;
36
+ 'pg/no-hardcoded-credentials': NoHardcodedCredentialsOptions;
37
+ 'pg/no-injection-in-identifier': NoInjectionInIdentifierOptions;
38
+ 'pg/check-query-params': CheckQueryParamsOptions;
39
+ 'pg/no-missing-client-release': NoMissingClientReleaseOptions;
40
+ 'pg/no-transaction-on-pool': NoTransactionOnPoolOptions;
41
+ 'pg/no-floating-query': NoFloatingQueryOptions;
42
+ 'pg/no-useless-client': NoUselessClientOptions;
43
+ 'pg/enforce-idle-timeout': EnforceIdleTimeoutOptions;
44
+ 'pg/no-select-all': NoSelectAllOptions;
45
+ 'pg/prefer-pool-query': PreferPoolQueryOptions;
46
+ 'pg/no-batch-insert-loop': NoBatchInsertLoopOptions;
47
+ 'pg/prefer-cursor-for-large-results': PreferCursorForLargeResultsOptions;
48
+ 'pg/no-top-level-query': NoTopLevelQueryOptions;
49
+ 'pg/enforce-camelcase-columns': EnforceCamelcaseColumnsOptions;
50
+ 'pg/connect-once': ConnectOnceOptions;
51
+ 'pg/no-unsafe-search-path': NoUnsafeSearchPathOptions;
52
+ 'pg/no-unsafe-copy-from': NoUnsafeCopyFromOptions;
53
+ 'pg/prevent-double-release': PreventDoubleReleaseOptions;
54
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) 2025 Ofri Peretz
4
+ * Licensed under the MIT License. Use of this source code is governed by the
5
+ * MIT license that can be found in the LICENSE file.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,99 @@
1
+ {
2
+ "name": "eslint-plugin-postgresql-security",
3
+ "version": "1.5.0",
4
+ "description": "ESLint plugin for the pg PostgreSQL driver \u2014 detects SQL injection, unreleased clients, floating queries, unsafe search_path, and insecure SSL.",
5
+ "type": "commonjs",
6
+ "main": "./dist/src/index.js",
7
+ "types": "./dist/src/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/src/index.d.ts",
11
+ "default": "./dist/src/index.js"
12
+ },
13
+ "./oxlint": {
14
+ "types": "./src/oxlint.d.ts",
15
+ "default": "./src/oxlint.js"
16
+ }
17
+ },
18
+ "author": "Ofri Peretz <ofriperetzdev@gmail.com>",
19
+ "license": "MIT",
20
+ "homepage": "https://eslint.interlace.tools/docs/security/plugin-postgresql-security?utm_source=npm&utm_medium=referral&utm_campaign=eslint-plugin-postgresql-security",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/ofri-peretz/eslint",
24
+ "directory": "packages/eslint-plugin-postgresql-security"
25
+ },
26
+ "bugs": {
27
+ "url": "https://github.com/ofri-peretz/eslint/issues"
28
+ },
29
+ "publishConfig": {
30
+ "access": "public"
31
+ },
32
+ "files": [
33
+ "src/",
34
+ "dist/",
35
+ "README.md",
36
+ "LICENSE",
37
+ "CHANGELOG.md"
38
+ ],
39
+ "keywords": [
40
+ "eslint",
41
+ "eslintplugin",
42
+ "eslint-plugin",
43
+ "static-analysis",
44
+ "linting",
45
+ "code-quality",
46
+ "security",
47
+ "sast",
48
+ "appsec",
49
+ "vulnerability",
50
+ "owasp",
51
+ "cwe",
52
+ "interlace-security",
53
+ "pg",
54
+ "postgres",
55
+ "postgresql",
56
+ "node-postgres",
57
+ "sql",
58
+ "sql-injection",
59
+ "cwe-89",
60
+ "database",
61
+ "database-security",
62
+ "connection-pool",
63
+ "best-practices",
64
+ "llm-optimized",
65
+ "ai-assistant",
66
+ "typescript"
67
+ ],
68
+ "engines": {
69
+ "node": ">=18.0.0"
70
+ },
71
+ "peerDependencies": {
72
+ "eslint": "^8.0.0 || ^9.0.0 || ^10.0.0",
73
+ "pg": "^6.0.0 || ^7.0.0 || ^8.0.0"
74
+ },
75
+ "peerDependenciesMeta": {
76
+ "pg": {
77
+ "optional": true
78
+ }
79
+ },
80
+ "dependencies": {
81
+ "tslib": "^2.3.0",
82
+ "@interlace/eslint-devkit": "^1.4.3"
83
+ },
84
+ "devDependencies": {
85
+ "@typescript-eslint/parser": "^8.46.2",
86
+ "@typescript-eslint/rule-tester": "^8.46.2",
87
+ "pg": "^8.22.0"
88
+ },
89
+ "scripts": {
90
+ "build": "tsx ../../scripts/build-package.ts",
91
+ "test": "vitest run",
92
+ "test:coverage": "vitest run --coverage",
93
+ "typecheck": "tsc -p tsconfig.lib.json --noEmit"
94
+ },
95
+ "funding": {
96
+ "type": "github",
97
+ "url": "https://github.com/ofri-peretz/eslint"
98
+ }
99
+ }