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,81 @@
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 { TSESLint, AST_NODE_TYPES, TSESTree, formatLLMMessage, MessageIcons } from '@interlace/eslint-devkit';
8
+ import { NoInsecureSslOptions } from '../../types';
9
+
10
+ export const noInsecureSsl: TSESLint.RuleModule<
11
+ 'noInsecureSsl',
12
+ NoInsecureSslOptions
13
+ > = {
14
+ meta: {
15
+ type: 'problem',
16
+ docs: {
17
+ description: 'Prevent the use of insecure SSL configurations (rejectUnauthorized: false).',
18
+ url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/no-insecure-ssl.md',
19
+ cwe: 'CWE-319',
20
+ cvss: 7.5,
21
+ },
22
+ messages: {
23
+ noInsecureSsl: formatLLMMessage({
24
+ icon: MessageIcons.SECURITY,
25
+ issueName: 'Insecure SSL',
26
+ description: 'Insecure SSL configuration detected (rejectUnauthorized: false).',
27
+ severity: 'HIGH',
28
+ cwe: 'CWE-319',
29
+ owasp: 'A05:2021',
30
+ compliance: ['SOC2', 'PCI-DSS', 'HIPAA', 'GDPR'],
31
+ effort: 'low',
32
+ fix: 'Set "rejectUnauthorized: true" or use a valid CA bundle. Do not disable SSL verification in production.',
33
+ documentationLink: 'https://node-postgres.com/features/ssl',
34
+ }),
35
+ },
36
+ schema: [],
37
+ },
38
+ defaultOptions: [],
39
+ create(context) {
40
+ return {
41
+ NewExpression(node) {
42
+ if (node.callee.type !== AST_NODE_TYPES.Identifier || (node.callee.name !== 'Client' && node.callee.name !== 'Pool')) {
43
+ return;
44
+ }
45
+
46
+ const configArg = node.arguments[0];
47
+ if (!configArg || configArg.type !== AST_NODE_TYPES.ObjectExpression) {
48
+ return;
49
+ }
50
+
51
+ const sslProp = configArg.properties.find(
52
+ (prop): prop is TSESTree.Property =>
53
+ prop.type === AST_NODE_TYPES.Property && prop.key.type === AST_NODE_TYPES.Identifier && prop.key.name === 'ssl'
54
+ );
55
+
56
+ if (!sslProp) return;
57
+
58
+ // ssl: { rejectUnauthorized: false }
59
+ if (sslProp.value.type === AST_NODE_TYPES.ObjectExpression) {
60
+ const rejectUnauthorized = sslProp.value.properties.find(
61
+ (prop): prop is TSESTree.Property =>
62
+ prop.type === AST_NODE_TYPES.Property &&
63
+ prop.key.type === AST_NODE_TYPES.Identifier &&
64
+ prop.key.name === 'rejectUnauthorized'
65
+ );
66
+
67
+ if (
68
+ rejectUnauthorized &&
69
+ rejectUnauthorized.value.type === AST_NODE_TYPES.Literal &&
70
+ rejectUnauthorized.value.value === false
71
+ ) {
72
+ context.report({
73
+ node: rejectUnauthorized,
74
+ messageId: 'noInsecureSsl',
75
+ });
76
+ }
77
+ }
78
+ },
79
+ };
80
+ },
81
+ };
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Tests for no-insecure-ssl
3
+ * CWE-319 — Prevent the use of insecure SSL configurations (rejectUnauthorized: false).
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 { noInsecureSsl } 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('no-insecure-ssl', () => {
28
+ describe('Valid Code', () => {
29
+ ruleTester.run('valid - safe patterns', noInsecureSsl, {
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
+ // Static property values are safe
40
+ {
41
+ code: `const config = { key: "static-value" };`,
42
+ },
43
+ // Environment variable usage is safe (padding case 4)
44
+ {
45
+ code: `const safe3 = process.env.DB_HOST;`,
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', noInsecureSsl, {
58
+ valid: [],
59
+ invalid: [
60
+ // Triggers noInsecureSsl: rejectUnauthorized disabled in SSL config
61
+ {
62
+ code: `new Client({ host: 'db.example.com', ssl: { rejectUnauthorized: false } })`,
63
+ errors: [{ messageId: 'noInsecureSsl' }],
64
+ },
65
+ ],
66
+ });
67
+ });
68
+ });
@@ -0,0 +1,42 @@
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
+ /**
8
+ * Layer-2 coverage-gap test for no-missing-client-release.
9
+ *
10
+ * The `!variable` early return is unreachable through the real parser: a
11
+ * VariableDeclarator with an Identifier id always declares exactly one
12
+ * variable. `createWithMockContext` stubs `getDeclaredVariables` to return
13
+ * an empty list, so the guard is exercised directly.
14
+ */
15
+ import { describe, it, expect } from 'vitest';
16
+ import { createWithMockContext } from '@interlace/eslint-devkit';
17
+ import { noMissingClientRelease } from './index';
18
+
19
+ describe('no-missing-client-release — coverage gaps (Layer 2, synthetic AST)', () => {
20
+ it('returns silently when the declarator resolves to no variable', () => {
21
+ const { listeners, reports } = createWithMockContext(
22
+ noMissingClientRelease,
23
+ );
24
+ const declarator = {
25
+ type: 'VariableDeclarator',
26
+ id: { type: 'Identifier', name: 'client' },
27
+ };
28
+ const node = {
29
+ type: 'CallExpression',
30
+ callee: {
31
+ type: 'MemberExpression',
32
+ object: { type: 'Identifier', name: 'pool' },
33
+ property: { type: 'Identifier', name: 'connect' },
34
+ },
35
+ arguments: [],
36
+ parent: declarator,
37
+ };
38
+ const visit = listeners['CallExpression'] as (n: unknown) => void;
39
+ visit(node);
40
+ expect(reports).toHaveLength(0);
41
+ });
42
+ });
@@ -0,0 +1,52 @@
1
+ import { RuleTester } from '@typescript-eslint/rule-tester';
2
+ import * as parser from '@typescript-eslint/parser';
3
+ import { noMissingClientRelease } from './index';
4
+
5
+ const ruleTester = new RuleTester({
6
+ languageOptions: {
7
+ parser,
8
+ },
9
+ });
10
+
11
+ ruleTester.run('no-missing-client-release', noMissingClientRelease, {
12
+ valid: [
13
+ {
14
+ code: "async function f() { await pool.connect(); }",
15
+ name: 'Ignored: No assignment'
16
+ },
17
+ {
18
+ code: "async function f() { const { release } = await pool.connect(); release(); }",
19
+ name: 'Ignored: Destructuring'
20
+ },
21
+ {
22
+ code: "async function f() { const client = await pool.other(); }",
23
+ name: 'Ignored: Not connect'
24
+ },
25
+ `
26
+ async function noAwait() {
27
+ const client = pool.connect();
28
+ client.release();
29
+ }
30
+ `
31
+ ],
32
+ invalid: [
33
+ {
34
+ code: `
35
+ async function query() {
36
+ const client = await pool.connect();
37
+ await client.query('SELECT 1');
38
+ }
39
+ `,
40
+ errors: [{ messageId: 'missingClientRelease' }],
41
+ },
42
+ {
43
+ code: `
44
+ async function query() {
45
+ const c = await pool.connect();
46
+ // Forgot to release c
47
+ }
48
+ `,
49
+ errors: [{ messageId: 'missingClientRelease' }],
50
+ }
51
+ ],
52
+ });
@@ -0,0 +1,100 @@
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 { TSESLint, AST_NODE_TYPES, TSESTree, formatLLMMessage, MessageIcons } from '@interlace/eslint-devkit';
8
+ import { NoMissingClientReleaseOptions } from '../../types';
9
+
10
+ export const noMissingClientRelease: TSESLint.RuleModule<
11
+ 'missingClientRelease',
12
+ NoMissingClientReleaseOptions
13
+ > = {
14
+ meta: {
15
+ type: 'problem',
16
+ docs: {
17
+ description: 'Ensure pg client is released after use to prevent pool exhaustion.',
18
+ url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/no-missing-client-release.md',
19
+ },
20
+ messages: {
21
+ missingClientRelease: formatLLMMessage({
22
+ icon: MessageIcons.PERFORMANCE,
23
+ issueName: 'Missing Client Release',
24
+ description: 'PG client acquired but not released.',
25
+ severity: 'HIGH',
26
+ cwe: 'CWE-404',
27
+ owasp: 'A05:2025',
28
+ effort: 'low',
29
+ fix: 'Ensure "client.release()" is called in a finally block to return the client to the pool.',
30
+ documentationLink: 'https://node-postgres.com/features/pooling#checkout-use-and-return',
31
+ }),
32
+ },
33
+ schema: [],
34
+ },
35
+ defaultOptions: [],
36
+ create(context) {
37
+ return {
38
+ CallExpression(node) {
39
+ // Detect: const client = await pool.connect() or pool.connect().then(client => ...)
40
+ // For simplicity, we focus on await usage in async functions, which is the modern pattern.
41
+
42
+ if (
43
+ node.callee.type !== AST_NODE_TYPES.MemberExpression ||
44
+ node.callee.property.type !== AST_NODE_TYPES.Identifier ||
45
+ node.callee.property.name !== 'connect'
46
+ ) {
47
+ return;
48
+ }
49
+
50
+ // We assume valid code would assign the result: const client = await pool.connect()
51
+ // Parent of CallExpression is AwaitExpression (usually)
52
+ // Parent of AwaitExpression is VariableDeclarator
53
+
54
+ // Check ancestry up to VariableDeclarator
55
+ let currentNode: TSESTree.Node = node;
56
+ if (node.parent?.type === AST_NODE_TYPES.AwaitExpression) {
57
+ currentNode = node.parent;
58
+ }
59
+
60
+ if (currentNode.parent?.type !== AST_NODE_TYPES.VariableDeclarator) {
61
+ return;
62
+ }
63
+
64
+ const declarator = currentNode.parent;
65
+ if (declarator.id.type !== AST_NODE_TYPES.Identifier) {
66
+ // Destructuring not supported for basic check yet
67
+ return;
68
+ }
69
+
70
+ // Variable name is available as declarator.id.name if needed for debugging
71
+
72
+ // Find the scope of the variable
73
+ const variable = context.sourceCode.getDeclaredVariables(declarator)[0];
74
+ if (!variable) return;
75
+
76
+ // Check references to this variable
77
+ // We look for client.release() call
78
+ const references = variable.references;
79
+ const hasReleaseCall = references.some(ref => {
80
+ const id = ref.identifier;
81
+ // Check if identifier is part of member expression 'client.release()' calls
82
+ return (
83
+ id.parent?.type === AST_NODE_TYPES.MemberExpression &&
84
+ id.parent.object === id &&
85
+ id.parent.property.type === AST_NODE_TYPES.Identifier &&
86
+ id.parent.property.name === 'release' &&
87
+ id.parent.parent?.type === AST_NODE_TYPES.CallExpression
88
+ );
89
+ });
90
+
91
+ if (!hasReleaseCall) {
92
+ context.report({
93
+ node: declarator,
94
+ messageId: 'missingClientRelease',
95
+ });
96
+ }
97
+ },
98
+ };
99
+ },
100
+ };
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Tests for no-missing-client-release
3
+ * CWE-404 — Ensure pg client is released after use to prevent pool exhaustion.
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 { noMissingClientRelease } 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('no-missing-client-release', () => {
28
+ describe('Valid Code', () => {
29
+ ruleTester.run('valid - safe patterns', noMissingClientRelease, {
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', noMissingClientRelease, {
58
+ valid: [],
59
+ invalid: [
60
+ // Triggers missingClientRelease
61
+ {
62
+ code: `async function test() { const client = await pool.connect(); const res = await client.query('SELECT 1'); return res; }`,
63
+ errors: [{ messageId: 'missingClientRelease' }],
64
+ },
65
+ ],
66
+ });
67
+ });
68
+ });
@@ -0,0 +1,44 @@
1
+ import { RuleTester } from '@typescript-eslint/rule-tester';
2
+ import * as parser from '@typescript-eslint/parser';
3
+ import { noSelectAll } from './index';
4
+
5
+ const ruleTester = new RuleTester({
6
+ languageOptions: {
7
+ parser,
8
+ },
9
+ });
10
+
11
+ ruleTester.run('no-select-all', noSelectAll, {
12
+ valid: [
13
+ // Ignored cases (coverage)
14
+ "client.query(dynamicSql)", // Dynamic query string
15
+ "client.other('SELECT *')", // Not .query method
16
+ "client.query('DELETE * FROM users')", // * but no SELECT
17
+ "client.query('SELECT a, b FROM users')", // Valid SELECT without *
18
+ "other.method('SELECT *')", // Not query method
19
+ "pool.query('SELECT COUNT( * ) FROM users')",
20
+ // Line 40: empty args
21
+ "client.query()",
22
+ // Line 44: non-string literal arg
23
+ "client.query(123)",
24
+ // Line 61: comma followed by * but no SELECT keyword (inner check fails)
25
+ "client.query('INSERT INTO foo VALUES (1, *)')",
26
+ // SELECT * FROM UNNEST is idiomatic for bulk inserts
27
+ "pool.query('INSERT INTO users SELECT * FROM unnest($1::int[], $2::text[])', [ids, names])",
28
+ "pool.query('INSERT INTO logs SELECT * FROM UNNEST($1::text[])', [messages])",
29
+ ],
30
+ invalid: [
31
+ {
32
+ code: "client.query('SELECT * FROM users')",
33
+ errors: [{ messageId: 'noSelectAll' }],
34
+ },
35
+ {
36
+ code: "pool.query('select * from users')",
37
+ errors: [{ messageId: 'noSelectAll' }],
38
+ },
39
+ {
40
+ code: "client.query('SELECT a, b, * FROM table')", // Rare but possible in some SQL dialects or logic
41
+ errors: [{ messageId: 'noSelectAll' }],
42
+ },
43
+ ],
44
+ });
@@ -0,0 +1,92 @@
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 { TSESLint, AST_NODE_TYPES, formatLLMMessage, MessageIcons } from '@interlace/eslint-devkit';
8
+ import { NoSelectAllOptions } from '../../types';
9
+
10
+ // Pre-compiled regex patterns for performance
11
+ const COUNT_STAR_REGEX = /COUNT\s*\(\s*\*\s*\)/g;
12
+ const SELECT_STAR_REGEX = /\bSELECT\s+\*/;
13
+ const COMMA_STAR_REGEX = /,\s*\*/;
14
+ const SELECT_KEYWORD_REGEX = /\bSELECT\b/;
15
+ const UNNEST_PATTERN_REGEX = /\bSELECT\s+\*\s+FROM\s+UNNEST\s*\(/;
16
+
17
+ export const noSelectAll: TSESLint.RuleModule<
18
+ 'noSelectAll',
19
+ NoSelectAllOptions
20
+ > = {
21
+ meta: {
22
+ type: 'problem',
23
+ docs: {
24
+ description: 'Prevent using * in SELECT statements (implicit columns).',
25
+ url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/no-select-all.md',
26
+ cwe: 'CWE-1049',
27
+ cvss: 5,
28
+ },
29
+ messages: {
30
+ noSelectAll: formatLLMMessage({
31
+ icon: MessageIcons.PERFORMANCE,
32
+ issueName: 'Select All',
33
+ description: 'Avoid using "SELECT *" which fetches all columns.',
34
+ severity: 'MEDIUM',
35
+ cwe: 'CWE-1049',
36
+ effort: 'low',
37
+ fix: 'Explicitly list the columns you need (e.g., SELECT id, name FROM ...).',
38
+ documentationLink: 'https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_SELECT_.2A',
39
+ }),
40
+ },
41
+ schema: [],
42
+ },
43
+ defaultOptions: [],
44
+ create(context) {
45
+ return {
46
+ CallExpression(node) {
47
+ if (
48
+ node.callee.type !== AST_NODE_TYPES.MemberExpression ||
49
+ node.callee.property.type !== AST_NODE_TYPES.Identifier ||
50
+ node.callee.property.name !== 'query'
51
+ ) {
52
+ return;
53
+ }
54
+
55
+ const args = node.arguments;
56
+ if (args.length === 0) return;
57
+
58
+ const queryArg = args[0];
59
+
60
+ if (queryArg.type !== AST_NODE_TYPES.Literal || typeof queryArg.value !== 'string') {
61
+ return;
62
+ }
63
+
64
+ const queryText = queryArg.value;
65
+ const upperQuery = queryText.toUpperCase();
66
+
67
+ // Exclude count(*) from the check by removing it temporarily
68
+ const cleanedQuery = upperQuery.replace(COUNT_STAR_REGEX, 'COUNT_ALL');
69
+
70
+ // Check for "SELECT *"
71
+ // 1. SELECT followed immediately by * (with spaces) -> SELECT *
72
+ // 2. Comma followed by * -> SELECT a, *
73
+ // BUT exclude SELECT * FROM UNNEST which is idiomatic for bulk inserts
74
+
75
+ if (SELECT_STAR_REGEX.test(cleanedQuery) || COMMA_STAR_REGEX.test(cleanedQuery)) {
76
+ // Ensure it is actually a SELECT query
77
+ if (SELECT_KEYWORD_REGEX.test(cleanedQuery)) {
78
+ // Exclude SELECT * FROM UNNEST (idiomatic bulk insert pattern)
79
+ if (UNNEST_PATTERN_REGEX.test(cleanedQuery)) {
80
+ return;
81
+ }
82
+ context.report({
83
+ node: queryArg,
84
+ messageId: 'noSelectAll',
85
+ });
86
+ }
87
+ }
88
+ },
89
+ };
90
+ },
91
+ };
92
+
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Tests for no-select-all
3
+ * CWE-1049 — Prevent using * in SELECT statements (implicit columns).
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 { noSelectAll } 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('no-select-all', () => {
28
+ describe('Valid Code', () => {
29
+ ruleTester.run('valid - safe patterns', noSelectAll, {
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
+ // Environment variable usage is safe (padding case 4)
44
+ {
45
+ code: `const safe3 = process.env.DB_HOST;`,
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', noSelectAll, {
58
+ valid: [],
59
+ invalid: [
60
+ // Triggers noSelectAll
61
+ {
62
+ code: `client.query('SELECT * FROM users')`,
63
+ errors: [{ messageId: 'noSelectAll' }],
64
+ },
65
+ ],
66
+ });
67
+ });
68
+ });
@@ -0,0 +1,36 @@
1
+ import { RuleTester } from '@typescript-eslint/rule-tester';
2
+ import * as parser from '@typescript-eslint/parser';
3
+ import { noTransactionOnPool } from './index';
4
+
5
+ const ruleTester = new RuleTester({
6
+ languageOptions: {
7
+ parser,
8
+ },
9
+ });
10
+
11
+ ruleTester.run('no-transaction-on-pool', noTransactionOnPool, {
12
+ valid: [
13
+ // Ignored cases (coverage)
14
+ "pool.otherMethod('BEGIN')",
15
+ "pool.query(dynamicVar)", // Not literal
16
+ "client.query()", // Empty args
17
+ "pool.query()", // Empty args (on pool)
18
+ "client.query('COMMIT')",
19
+ "pool.query('SELECT 1')",
20
+ "customClient.query('BEGIN')",
21
+ ],
22
+ invalid: [
23
+ {
24
+ code: "pool.query('BEGIN')",
25
+ errors: [{ messageId: 'noTransactionOnPool' }],
26
+ },
27
+ {
28
+ code: "myPool.query('COMMIT')",
29
+ errors: [{ messageId: 'noTransactionOnPool' }],
30
+ },
31
+ {
32
+ code: "pool.query('ROLLBACK')",
33
+ errors: [{ messageId: 'noTransactionOnPool' }],
34
+ },
35
+ ],
36
+ });