eslint-plugin-postgresql-security 1.5.0 → 1.5.2

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 (113) hide show
  1. package/README.md +5 -5
  2. package/package.json +9 -23
  3. package/src/constants.js +1 -0
  4. package/src/index.d.ts +17 -0
  5. package/src/index.js +1 -0
  6. package/src/oxlint.d.ts +17 -0
  7. package/src/oxlint.js +1 -0
  8. package/src/rules/check-query-params/index.js +1 -0
  9. package/src/rules/no-batch-insert-loop/index.js +1 -0
  10. package/src/rules/no-floating-query/index.js +1 -0
  11. package/src/rules/no-hardcoded-credentials/index.js +1 -0
  12. package/src/rules/no-insecure-ssl/index.js +1 -0
  13. package/src/rules/no-missing-client-release/index.js +1 -0
  14. package/src/rules/no-select-all/index.js +1 -0
  15. package/src/rules/no-transaction-on-pool/index.js +1 -0
  16. package/src/rules/no-unsafe-copy-from/index.js +1 -0
  17. package/src/rules/no-unsafe-query/index.js +1 -0
  18. package/src/rules/no-unsafe-search-path/index.js +1 -0
  19. package/src/rules/prefer-pool-query/index.js +1 -0
  20. package/src/rules/prevent-double-release/index.js +1 -0
  21. package/src/types/index.js +1 -0
  22. package/CHANGELOG.md +0 -150
  23. package/dist/AGENTS.md +0 -118
  24. package/dist/CHANGELOG.md +0 -150
  25. package/dist/LICENSE +0 -23
  26. package/dist/README.md +0 -169
  27. package/dist/package.json +0 -99
  28. package/dist/src/constants.d.ts +0 -6
  29. package/dist/src/constants.js +0 -13
  30. package/dist/src/index.d.ts +0 -26
  31. package/dist/src/index.js +0 -116
  32. package/dist/src/oxlint.d.ts +0 -20
  33. package/dist/src/oxlint.js +0 -21
  34. package/dist/src/rules/check-query-params/index.d.ts +0 -8
  35. package/dist/src/rules/check-query-params/index.js +0 -90
  36. package/dist/src/rules/no-batch-insert-loop/index.d.ts +0 -8
  37. package/dist/src/rules/no-batch-insert-loop/index.js +0 -90
  38. package/dist/src/rules/no-floating-query/index.d.ts +0 -8
  39. package/dist/src/rules/no-floating-query/index.js +0 -74
  40. package/dist/src/rules/no-hardcoded-credentials/index.d.ts +0 -8
  41. package/dist/src/rules/no-hardcoded-credentials/index.js +0 -86
  42. package/dist/src/rules/no-insecure-ssl/index.d.ts +0 -8
  43. package/dist/src/rules/no-insecure-ssl/index.js +0 -66
  44. package/dist/src/rules/no-missing-client-release/index.d.ts +0 -8
  45. package/dist/src/rules/no-missing-client-release/index.js +0 -85
  46. package/dist/src/rules/no-select-all/index.d.ts +0 -8
  47. package/dist/src/rules/no-select-all/index.js +0 -79
  48. package/dist/src/rules/no-transaction-on-pool/index.d.ts +0 -8
  49. package/dist/src/rules/no-transaction-on-pool/index.js +0 -71
  50. package/dist/src/rules/no-unsafe-copy-from/index.d.ts +0 -10
  51. package/dist/src/rules/no-unsafe-copy-from/index.js +0 -205
  52. package/dist/src/rules/no-unsafe-query/index.d.ts +0 -8
  53. package/dist/src/rules/no-unsafe-query/index.js +0 -132
  54. package/dist/src/rules/no-unsafe-search-path/index.d.ts +0 -8
  55. package/dist/src/rules/no-unsafe-search-path/index.js +0 -144
  56. package/dist/src/rules/prefer-pool-query/index.d.ts +0 -8
  57. package/dist/src/rules/prefer-pool-query/index.js +0 -104
  58. package/dist/src/rules/prevent-double-release/index.d.ts +0 -8
  59. package/dist/src/rules/prevent-double-release/index.js +0 -456
  60. package/dist/src/types/index.js +0 -7
  61. package/src/__compatibility__/pg-interface.spec.ts +0 -495
  62. package/src/constants.ts +0 -11
  63. package/src/index.spec.ts +0 -14
  64. package/src/index.test.ts +0 -59
  65. package/src/index.ts +0 -130
  66. package/src/oxlint.spec.ts +0 -70
  67. package/src/oxlint.ts +0 -23
  68. package/src/rules/check-query-params/check-query-params.test.ts +0 -68
  69. package/src/rules/check-query-params/index.spec.ts +0 -117
  70. package/src/rules/check-query-params/index.ts +0 -106
  71. package/src/rules/no-batch-insert-loop/index.spec.ts +0 -105
  72. package/src/rules/no-batch-insert-loop/index.ts +0 -105
  73. package/src/rules/no-batch-insert-loop/no-batch-insert-loop.test.ts +0 -68
  74. package/src/rules/no-floating-query/index.spec.ts +0 -112
  75. package/src/rules/no-floating-query/index.ts +0 -81
  76. package/src/rules/no-floating-query/no-floating-query.test.ts +0 -68
  77. package/src/rules/no-hardcoded-credentials/index.spec.ts +0 -38
  78. package/src/rules/no-hardcoded-credentials/index.ts +0 -93
  79. package/src/rules/no-hardcoded-credentials/no-hardcoded-credentials.test.ts +0 -68
  80. package/src/rules/no-insecure-ssl/index.spec.ts +0 -40
  81. package/src/rules/no-insecure-ssl/index.ts +0 -81
  82. package/src/rules/no-insecure-ssl/no-insecure-ssl.test.ts +0 -68
  83. package/src/rules/no-missing-client-release/coverage-gaps.spec.ts +0 -42
  84. package/src/rules/no-missing-client-release/index.spec.ts +0 -52
  85. package/src/rules/no-missing-client-release/index.ts +0 -100
  86. package/src/rules/no-missing-client-release/no-missing-client-release.test.ts +0 -68
  87. package/src/rules/no-select-all/index.spec.ts +0 -44
  88. package/src/rules/no-select-all/index.ts +0 -92
  89. package/src/rules/no-select-all/no-select-all.test.ts +0 -68
  90. package/src/rules/no-transaction-on-pool/index.spec.ts +0 -36
  91. package/src/rules/no-transaction-on-pool/index.ts +0 -85
  92. package/src/rules/no-transaction-on-pool/no-transaction-on-pool.test.ts +0 -68
  93. package/src/rules/no-unsafe-copy-from/coverage-gaps.spec.ts +0 -76
  94. package/src/rules/no-unsafe-copy-from/index.spec.ts +0 -274
  95. package/src/rules/no-unsafe-copy-from/index.ts +0 -226
  96. package/src/rules/no-unsafe-copy-from/no-unsafe-copy-from.test.ts +0 -78
  97. package/src/rules/no-unsafe-query/coverage-gaps.spec.ts +0 -61
  98. package/src/rules/no-unsafe-query/index.spec.ts +0 -103
  99. package/src/rules/no-unsafe-query/index.ts +0 -149
  100. package/src/rules/no-unsafe-query/no-unsafe-query.test.ts +0 -73
  101. package/src/rules/no-unsafe-search-path/index.spec.ts +0 -46
  102. package/src/rules/no-unsafe-search-path/index.ts +0 -164
  103. package/src/rules/no-unsafe-search-path/no-unsafe-search-path.test.ts +0 -68
  104. package/src/rules/prefer-pool-query/coverage-gaps.spec.ts +0 -76
  105. package/src/rules/prefer-pool-query/index.spec.ts +0 -97
  106. package/src/rules/prefer-pool-query/index.ts +0 -118
  107. package/src/rules/prefer-pool-query/prefer-pool-query.test.ts +0 -68
  108. package/src/rules/prevent-double-release/coverage-gaps.spec.ts +0 -538
  109. package/src/rules/prevent-double-release/index.spec.ts +0 -824
  110. package/src/rules/prevent-double-release/index.ts +0 -534
  111. package/src/rules/prevent-double-release/prevent-double-release.test.ts +0 -70
  112. package/src/types/index.ts +0 -58
  113. /package/{dist/src → src}/types/index.d.ts +0 -0
@@ -1,105 +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 { TSESLint, AST_NODE_TYPES, formatLLMMessage, MessageIcons } from '@interlace/eslint-devkit';
8
- import { NoBatchInsertLoopOptions } from '../../types';
9
-
10
- export const noBatchInsertLoop: TSESLint.RuleModule<
11
- 'noBatchInsertLoop',
12
- NoBatchInsertLoopOptions
13
- > = {
14
- meta: {
15
- type: 'problem',
16
- docs: {
17
- description: 'Prevent executing database queries within loops (N+1 problem).',
18
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/no-batch-insert-loop.md',
19
- cwe: 'CWE-1049',
20
- cvss: 7.5,
21
- },
22
- messages: {
23
- noBatchInsertLoop: formatLLMMessage({
24
- icon: MessageIcons.PERFORMANCE,
25
- issueName: 'N+1 Query Detection',
26
- description: 'Database query loop detected.',
27
- severity: 'HIGH',
28
- cwe: 'CWE-1049',
29
- effort: 'medium',
30
- fix: 'Batch queries using arrays and "UNNEST" or a single batched INSERT.',
31
- documentationLink: 'https://use-the-index-luke.com/sql/joins/nested-loops-join-n1-problem',
32
- }),
33
- },
34
- schema: [],
35
- },
36
- defaultOptions: [],
37
- create(context) {
38
- return {
39
- CallExpression(node) {
40
- if (
41
- node.callee.type !== AST_NODE_TYPES.MemberExpression ||
42
- node.callee.property.type !== AST_NODE_TYPES.Identifier ||
43
- node.callee.property.name !== 'query'
44
- ) {
45
- return;
46
- }
47
-
48
- const args = node.arguments;
49
- if (args.length > 0 && args[0].type === AST_NODE_TYPES.Literal && typeof args[0].value === 'string') {
50
- const query = args[0].value.toUpperCase();
51
- if (!query.includes('INSERT') && !query.includes('UPDATE')) {
52
- return; // Only care about mutations usually, SELECT N+1 is also bad but maybe 'no-loop-query' is separate
53
- }
54
- }
55
-
56
- let ancestor: any = node.parent;
57
- while (ancestor) {
58
- if (
59
- ancestor.type === AST_NODE_TYPES.ForStatement ||
60
- ancestor.type === AST_NODE_TYPES.ForOfStatement ||
61
- ancestor.type === AST_NODE_TYPES.ForInStatement ||
62
- ancestor.type === AST_NODE_TYPES.WhileStatement ||
63
- ancestor.type === AST_NODE_TYPES.DoWhileStatement
64
- ) {
65
- context.report({
66
- node,
67
- messageId: 'noBatchInsertLoop',
68
- });
69
- break;
70
- }
71
-
72
- // Check for function expressions that are callbacks to array methods
73
- if (
74
- (ancestor.type === AST_NODE_TYPES.FunctionExpression || ancestor.type === AST_NODE_TYPES.ArrowFunctionExpression)
75
- ) {
76
- if (ancestor.parent?.type === AST_NODE_TYPES.CallExpression &&
77
- ancestor.parent.callee.type === AST_NODE_TYPES.MemberExpression &&
78
- ancestor.parent.callee.property.type === AST_NODE_TYPES.Identifier &&
79
- ['map', 'forEach', 'reduce', 'filter'].includes(ancestor.parent.callee.property.name)
80
- ) {
81
- context.report({
82
- node,
83
- messageId: 'noBatchInsertLoop',
84
- });
85
- break;
86
- }
87
- // Stop traversal at function boundary unless it's an IIFE?
88
- // Actually if we are in a function, we stop unless that function IS the loop body callback.
89
- // If we are in `function foo() { while(...) { query } }` -> we hit WhileStatement first.
90
- // If we are in `users.map(u => query(u))` -> query -> ArrowFunc -> CallExpression(map).
91
-
92
- // So if we hit a function, we must check if it's the specific loop callback.
93
- // If not, we stop (don't flag loop outside of function definition, that's weird).
94
- if (ancestor.parent?.type !== AST_NODE_TYPES.CallExpression) {
95
- // It's a regular function definition, stop going up (don't blame caller's loop).
96
- break;
97
- }
98
- }
99
-
100
- ancestor = ancestor.parent;
101
- }
102
- },
103
- };
104
- },
105
- };
@@ -1,68 +0,0 @@
1
- /**
2
- * Tests for no-batch-insert-loop
3
- * CWE-1049 — Prevent executing database queries within loops (N+1 problem).
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 { noBatchInsertLoop } 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-batch-insert-loop', () => {
28
- describe('Valid Code', () => {
29
- ruleTester.run('valid - safe patterns', noBatchInsertLoop, {
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', noBatchInsertLoop, {
58
- valid: [],
59
- invalid: [
60
- // Triggers noBatchInsertLoop
61
- {
62
- code: `for (const item of items) { client.query('INSERT INTO t(a) VALUES($1)', [item]); }`,
63
- errors: [{ messageId: 'noBatchInsertLoop' }],
64
- },
65
- ],
66
- });
67
- });
68
- });
@@ -1,112 +0,0 @@
1
- import { RuleTester } from '@typescript-eslint/rule-tester';
2
- import * as parser from '@typescript-eslint/parser';
3
- import { noFloatingQuery } from './index';
4
-
5
- const ruleTester = new RuleTester({
6
- languageOptions: {
7
- parser,
8
- parserOptions: {
9
- ecmaVersion: 2022,
10
- sourceType: 'module',
11
- },
12
- },
13
- });
14
-
15
- ruleTester.run('no-floating-query', noFloatingQuery, {
16
- valid: [
17
- // Await
18
- "await client.query('SELECT 1')",
19
- "async function foo() { await client.query('SELECT 1'); }",
20
-
21
- // Return
22
- "return client.query('SELECT 1')",
23
- "function foo() { return client.query('SELECT 1'); }",
24
-
25
- // Assignment
26
- "const p = client.query('SELECT 1')",
27
- "let p; p = client.query('SELECT 1')",
28
-
29
- // Promise Methods & Chaining
30
- "client.query('SELECT 1').then(res => console.log(res))",
31
- "client.query('SELECT 1').catch(err => console.error(err))",
32
- "client.query('SELECT 1').finally(() => cleanup())",
33
- "Promise.all([client.query('SELECT 1')])",
34
- "Promise.allSettled([client.query('SELECT 1')])",
35
- "Promise.race([client.query('SELECT 1')])",
36
-
37
- // Arrow Function Implicit Return
38
- "const foo = () => client.query('SELECT 1')",
39
- "items.map(i => client.query('INSERT', [i]))",
40
-
41
- // Void Operator (Explicit Ignore)
42
- "void client.query('SELECT 1')",
43
-
44
- // Logical Expressions (Short-circuit)
45
- "isValid && client.query('UPDATE ...')",
46
- "isValid || client.query('LOG ...')",
47
-
48
- // Conditional (Ternary)
49
- "isValid ? client.query('COMMIT') : client.query('ROLLBACK')",
50
-
51
- // Sequence Expression
52
- "(other(), client.query('SELECT 1'))",
53
-
54
- // Object/Array Definition
55
- "const obj = { p: client.query('SELECT 1') }",
56
- "const arr = [client.query('SELECT 1')]",
57
-
58
- // Template Literal
59
- "`Result: ${client.query('SELECT 1')}`",
60
-
61
- // Argument Passing
62
- "doSomething(client.query('SELECT 1'))",
63
- ],
64
- invalid: [
65
- {
66
- code: "client.query('SELECT 1');",
67
- errors: [{ messageId: 'noFloatingQuery' }],
68
- },
69
- {
70
- code: `
71
- async function foo() {
72
- pool.query('INSERT ...');
73
- }
74
- `,
75
- errors: [{ messageId: 'noFloatingQuery' }],
76
- },
77
- {
78
- code: "if (true) { client.query('SELECT 1'); }",
79
- errors: [{ messageId: 'noFloatingQuery' }],
80
- },
81
- {
82
- code: "try { client.query('SELECT 1'); } catch (e) {}",
83
- errors: [{ messageId: 'noFloatingQuery' }],
84
- },
85
- {
86
- code: "for (let i=0; i<10; i++) { client.query('INSERT ...'); }",
87
- errors: [{ messageId: 'noFloatingQuery' }],
88
- },
89
- {
90
- code: "while (true) { client.query('SELECT 1'); break; }",
91
- errors: [{ messageId: 'noFloatingQuery' }],
92
- },
93
- {
94
- code: "switch (x) { case 1: client.query('SELECT 1'); break; }",
95
- errors: [{ messageId: 'noFloatingQuery' }],
96
- },
97
- {
98
- // Parentheses (checking if rule handles parens or if parser collapses them)
99
- // Standard parser often handles this transparently, but good to test.
100
- code: "(client.query('SELECT 1'));",
101
- errors: [{ messageId: 'noFloatingQuery' }],
102
- },
103
- {
104
- // Multiple statements
105
- code: `
106
- client.query('SELECT 1');
107
- client.query('SELECT 2');
108
- `,
109
- errors: [{ messageId: 'noFloatingQuery' }, { messageId: 'noFloatingQuery' }],
110
- }
111
- ],
112
- });
@@ -1,81 +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 { TSESLint, AST_NODE_TYPES, formatLLMMessage, MessageIcons } from '@interlace/eslint-devkit';
8
- import { NoFloatingQueryOptions } from '../../types';
9
-
10
- export const noFloatingQuery: TSESLint.RuleModule<
11
- 'noFloatingQuery',
12
- NoFloatingQueryOptions
13
- > = {
14
- meta: {
15
- type: 'problem',
16
- docs: {
17
- description: 'Ensure all queries are awaited or returned to prevent unhandled promise rejections.',
18
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/no-floating-query.md',
19
- cwe: 'CWE-391',
20
- cvss: 5.3,
21
- },
22
- messages: {
23
- noFloatingQuery: formatLLMMessage({
24
- icon: MessageIcons.WARNING,
25
- issueName: 'Floating Query',
26
- description: 'Promise-returning query is neither awaited nor returned.',
27
- severity: 'HIGH',
28
- cwe: 'CWE-391',
29
- effort: 'low',
30
- fix: 'Add "await" or "return" to handle the query promise.',
31
- documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise',
32
- }),
33
- },
34
- schema: [],
35
- },
36
- defaultOptions: [],
37
- create(context) {
38
- return {
39
- CallExpression(node) {
40
- if (
41
- node.callee.type !== AST_NODE_TYPES.MemberExpression ||
42
- node.callee.property.type !== AST_NODE_TYPES.Identifier ||
43
- node.callee.property.name !== 'query'
44
- ) {
45
- return;
46
- }
47
-
48
- // Check parent node to see if the promise is handled
49
- const parent = node.parent;
50
-
51
- // Allowed parents:
52
- // 1. AwaitExpression: await client.query(...)
53
- if (parent?.type === AST_NODE_TYPES.AwaitExpression) return;
54
-
55
- // 2. ReturnStatement: return client.query(...)
56
- if (parent?.type === AST_NODE_TYPES.ReturnStatement) return;
57
-
58
- // 3. ArrowFunctionExpression (implicit return): () => client.query(...)
59
- if (parent?.type === AST_NODE_TYPES.ArrowFunctionExpression) return;
60
-
61
- // 4. VariableDeclarator: const p = client.query(...) -> Assumed handled if assigned
62
- if (parent?.type === AST_NODE_TYPES.VariableDeclarator) return;
63
-
64
- // 5. MemberExpression: client.query(...).then(...) or .catch(...)
65
- if (parent?.type === AST_NODE_TYPES.MemberExpression) return; // check chains?
66
-
67
- // 6. AssignmentExpression: p = client.query(...)
68
- if (parent?.type === AST_NODE_TYPES.AssignmentExpression) return;
69
-
70
- // If specific to typical void usage:
71
- // ExpressionStatement: client.query(...); -> This is bad!
72
- if (parent?.type === AST_NODE_TYPES.ExpressionStatement) {
73
- context.report({
74
- node,
75
- messageId: 'noFloatingQuery',
76
- });
77
- }
78
- },
79
- };
80
- },
81
- };
@@ -1,68 +0,0 @@
1
- /**
2
- * Tests for no-floating-query
3
- * CWE-391 — Ensure all queries are awaited or returned to prevent unhandled promise rejections.
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 { noFloatingQuery } 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-floating-query', () => {
28
- describe('Valid Code', () => {
29
- ruleTester.run('valid - safe patterns', noFloatingQuery, {
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', noFloatingQuery, {
58
- valid: [],
59
- invalid: [
60
- // Triggers noFloatingQuery
61
- {
62
- code: `client.query('SELECT 1')`,
63
- errors: [{ messageId: 'noFloatingQuery' }],
64
- },
65
- ],
66
- });
67
- });
68
- });
@@ -1,38 +0,0 @@
1
- import { RuleTester } from '@typescript-eslint/rule-tester';
2
- import * as parser from '@typescript-eslint/parser';
3
- import { noHardcodedCredentials } from './index';
4
-
5
- const ruleTester = new RuleTester({
6
- languageOptions: {
7
- parser,
8
- },
9
- });
10
-
11
- ruleTester.run('no-hardcoded-credentials', noHardcodedCredentials, {
12
- valid: [
13
- "new Client({ password: process.env.DB_PASSWORD })",
14
- "new Pool({ connectionString: process.env.DATABASE_URL })",
15
- "new Client()", // No config, assumes env vars or defaults
16
- // Ignored NewExpressions (coverage)
17
- "new OtherClass({ password: '123' })",
18
- "new Client({ ...config })", // Spread element (not Property)
19
- "new Client({ ['password']: 'secret' })", // Computed key (ignored by rule for now, but covers line 57)
20
- "new Client('some-random-string')", // String but not a protocol match
21
- "new Client(123)", // Non-string arg
22
- "new Pool({ user: 'postgres', database: 'mydb' })", // No password
23
- ],
24
- invalid: [
25
- {
26
- code: "new Client({ password: 'mysecretpassword' })",
27
- errors: [{ messageId: 'noHardcodedCredentials' }],
28
- },
29
- {
30
- code: "new Pool({ connectionString: 'postgres://user:pass@localhost:5432/db' })",
31
- errors: [{ messageId: 'noHardcodedCredentials' }],
32
- },
33
- {
34
- code: "new Client('postgres://user:pass@localhost:5432/db')",
35
- errors: [{ messageId: 'noHardcodedCredentials' }],
36
- },
37
- ],
38
- });
@@ -1,93 +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 { TSESLint, AST_NODE_TYPES, formatLLMMessage, MessageIcons } from '@interlace/eslint-devkit';
8
- import { NoHardcodedCredentialsOptions } from '../../types';
9
- import { PG_PROTOCOLS } from '../../constants';
10
-
11
- export const noHardcodedCredentials: TSESLint.RuleModule<
12
- 'noHardcodedCredentials',
13
- NoHardcodedCredentialsOptions
14
- > = {
15
- meta: {
16
- type: 'problem',
17
- docs: {
18
- description: 'Detect hardcoded credentials in pg Client or Pool initialization.',
19
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-postgresql-security/docs/rules/no-hardcoded-credentials.md',
20
- cwe: 'CWE-798',
21
- cvss: 9.8,
22
- },
23
- messages: {
24
- noHardcodedCredentials: formatLLMMessage({
25
- icon: MessageIcons.SECURITY,
26
- issueName: 'Hardcoded Credentials',
27
- description: 'Hardcoded credentials detected in database connection.',
28
- severity: 'CRITICAL',
29
- cwe: 'CWE-798',
30
- owasp: 'A07:2021',
31
- compliance: ['SOC2', 'PCI-DSS', 'ISO27001', 'NIST-CSF'],
32
- effort: 'low',
33
- fix: 'Use environment variables (process.env.DB_PASSWORD) instead of hardcoding secrets.',
34
- documentationLink: 'https://owasp.org/www-community/vulnerabilities/Use_of_hard-coded_password',
35
- }),
36
- },
37
- schema: [],
38
- },
39
- defaultOptions: [],
40
- create(context) {
41
- return {
42
- NewExpression(node) {
43
- if (node.callee.type !== AST_NODE_TYPES.Identifier || !['Client', 'Pool'].includes(node.callee.name)) {
44
- return;
45
- }
46
-
47
- if (node.arguments.length === 0) return;
48
- const configArg = node.arguments[0];
49
-
50
- // Case 1: Connection string literal 'postgres://user:pass@localhost:5432/db'
51
- if (configArg.type === AST_NODE_TYPES.Literal && typeof configArg.value === 'string') {
52
- if (PG_PROTOCOLS.some(protocol => (configArg.value as string).includes(protocol))) {
53
- context.report({
54
- node: configArg,
55
- messageId: 'noHardcodedCredentials',
56
- });
57
- return; // Return after reporting for Case 1
58
- }
59
- }
60
-
61
- // Case 2: new Client({ password: 'secret', connectionString: '...' })
62
- if (configArg.type === AST_NODE_TYPES.ObjectExpression) {
63
- for (const prop of configArg.properties) {
64
- if (prop.type !== AST_NODE_TYPES.Property || prop.key.type !== AST_NODE_TYPES.Identifier) {
65
- continue;
66
- }
67
-
68
- // Check for 'password' field
69
- if (prop.key.name === 'password') {
70
- if (prop.value.type === AST_NODE_TYPES.Literal) {
71
- context.report({
72
- node: prop.value,
73
- messageId: 'noHardcodedCredentials',
74
- });
75
- }
76
- continue;
77
- }
78
-
79
- // Check for 'connectionString' field
80
- if (prop.key.name === 'connectionString') {
81
- if (prop.value.type === AST_NODE_TYPES.Literal) {
82
- context.report({
83
- node: prop.value,
84
- messageId: 'noHardcodedCredentials',
85
- });
86
- }
87
- }
88
- }
89
- }
90
- },
91
- };
92
- },
93
- };
@@ -1,68 +0,0 @@
1
- /**
2
- * Tests for no-hardcoded-credentials
3
- * CWE-798 — Detect hardcoded credentials in pg Client or Pool initialization.
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 { noHardcodedCredentials } 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-hardcoded-credentials', () => {
28
- describe('Valid Code', () => {
29
- ruleTester.run('valid - safe patterns', noHardcodedCredentials, {
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
- // 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', noHardcodedCredentials, {
58
- valid: [],
59
- invalid: [
60
- // Triggers noHardcodedCredentials
61
- {
62
- code: `new Client({ host: 'localhost', password: 'secret123' })`,
63
- errors: [{ messageId: 'noHardcodedCredentials' }],
64
- },
65
- ],
66
- });
67
- });
68
- });
@@ -1,40 +0,0 @@
1
- import { RuleTester } from '@typescript-eslint/rule-tester';
2
- import * as parser from '@typescript-eslint/parser';
3
- import { noInsecureSsl } from './index';
4
-
5
- const ruleTester = new RuleTester({
6
- languageOptions: {
7
- parser,
8
- },
9
- });
10
-
11
- ruleTester.run('no-insecure-ssl', noInsecureSsl, {
12
- valid: [
13
- // Default (secure)
14
- "new Client()",
15
- "new Pool({ user: 'postgres' })",
16
- // Explicitly secure
17
- "new Client({ ssl: { ca: '...' } })",
18
- "new obj.Client({ ssl: { rejectUnauthorized: false } })", // MemberExpression callee (ignored)
19
- // String connection string (would require different parsing, rule targets config object)
20
- "new Client('postgres://user:pass@host/db')",
21
- // Line 51: ssl object without rejectUnauthorized property (else branch)
22
- "new Client({ ssl: { minVersion: 'TLSv1.2' } })",
23
- // Line 51: ssl.rejectUnauthorized = true (explicit secure)
24
- "new Client({ ssl: { rejectUnauthorized: true } })",
25
- // Line 51: ssl = true (non-object, else branch for ObjectExpression check)
26
- "new Client({ ssl: true })",
27
- // Line 51: ssl = false (also non-object)
28
- "new Client({ ssl: false })",
29
- ],
30
- invalid: [
31
- {
32
- code: "new Client({ ssl: { rejectUnauthorized: false } })",
33
- errors: [{ messageId: 'noInsecureSsl' }],
34
- },
35
- {
36
- code: "new Pool({ ssl: { rejectUnauthorized: false } })",
37
- errors: [{ messageId: 'noInsecureSsl' }],
38
- },
39
- ],
40
- });