@interlace/eslint-devkit 1.7.0 → 1.9.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.
- package/README.md +8 -0
- package/package.json +1 -2
- package/src/ast/index.js +15 -2
- package/src/index.js +22 -9
- package/src/messaging/index.js +19 -6
- package/src/node/fs.js +34 -2
- package/src/node/index.js +17 -4
- package/src/node/os.js +34 -2
- package/src/node/path.js +34 -2
- package/src/resolver/index.js +18 -5
- package/src/resolver.js +15 -2
- package/src/rule-creation/browser-escape-hatch-rule.d.ts +72 -0
- package/src/rule-creation/browser-escape-hatch-rule.js +100 -0
- package/src/rule-creation/hardcoded-credentials-rule.d.ts +81 -0
- package/src/rule-creation/hardcoded-credentials-rule.js +143 -0
- package/src/rule-creation/index.d.ts +6 -0
- package/src/rule-creation/index.js +24 -5
- package/src/rule-creation/mass-assignment-rule.d.ts +79 -0
- package/src/rule-creation/mass-assignment-rule.js +121 -0
- package/src/rule-creation/raw-identifier-rule.d.ts +127 -0
- package/src/rule-creation/raw-identifier-rule.js +140 -0
- package/src/rule-creation/require-tls-rule.d.ts +113 -0
- package/src/rule-creation/require-tls-rule.js +190 -0
- package/src/rule-creation/sdk-api-key-rule.d.ts +108 -0
- package/src/rule-creation/sdk-api-key-rule.js +121 -0
- package/src/rule-creation/sql-injection-rule.d.ts +4 -3
- package/src/rule-creation/sql-injection-rule.js +96 -5
- package/src/security/index.js +15 -2
- package/src/types/index.js +15 -2
|
@@ -0,0 +1,113 @@
|
|
|
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
|
+
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
|
|
7
|
+
/** Message ids every rule built by this factory reports. */
|
|
8
|
+
export type RequireTlsMessageIds = 'tlsDisabled' | 'certificateValidationDisabled';
|
|
9
|
+
export interface RequireTlsRuleConfig {
|
|
10
|
+
/**
|
|
11
|
+
* The rule's own `meta.type` + `meta.docs`, spelled out by the caller.
|
|
12
|
+
* Same reasoning as `createUnscopedMutationRule`: the metadata auditor
|
|
13
|
+
* statically parses each rule's source file, so metadata hidden inside a
|
|
14
|
+
* factory is invisible to it.
|
|
15
|
+
*/
|
|
16
|
+
readonly meta: {
|
|
17
|
+
readonly type: 'problem';
|
|
18
|
+
readonly docs: {
|
|
19
|
+
readonly description: string;
|
|
20
|
+
readonly url: string;
|
|
21
|
+
readonly cwe: string;
|
|
22
|
+
/**
|
|
23
|
+
* Secondary-mapping note. `cwe` holds one identifier, but this rule
|
|
24
|
+
* reports two weaknesses — `tlsDisabled` is CWE-319 and
|
|
25
|
+
* `certificateValidationDisabled` is CWE-295 — so the second one is
|
|
26
|
+
* recorded here, where `scripts/docs-cwe-coverage.ts` reads it.
|
|
27
|
+
*/
|
|
28
|
+
readonly cweJustification?: string;
|
|
29
|
+
readonly cvss: number;
|
|
30
|
+
readonly confidence: 'high' | 'medium' | 'low';
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Modules whose import means this file talks to the driver, e.g.
|
|
35
|
+
* `['mysql2', 'mysql']`. A file importing none of them is skipped entirely,
|
|
36
|
+
* which is what keeps this rule out of `node-security`'s territory.
|
|
37
|
+
*/
|
|
38
|
+
readonly modules: readonly string[];
|
|
39
|
+
/**
|
|
40
|
+
* Extra keys, on top of the shared defaults, that identify an object as a
|
|
41
|
+
* connection config for this driver — Sequelize's `dialectOptions`,
|
|
42
|
+
* TypeORM's `synchronize`, and so on.
|
|
43
|
+
*/
|
|
44
|
+
readonly connectionKeys?: readonly string[];
|
|
45
|
+
/**
|
|
46
|
+
* URL schemes this driver accepts in a connection string, e.g.
|
|
47
|
+
* `['mysql']`. A string literal starting with one of them is scanned for
|
|
48
|
+
* `sslmode=disable` / `ssl=false`. Omit for drivers that take no URL.
|
|
49
|
+
*/
|
|
50
|
+
readonly urlSchemes?: readonly string[];
|
|
51
|
+
/** Remediation line in the emitted message. */
|
|
52
|
+
readonly fix: string;
|
|
53
|
+
/** Reference link in the emitted message. */
|
|
54
|
+
readonly documentationLink: string;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Is this property value the literal `false` (or another falsy literal)?
|
|
58
|
+
*
|
|
59
|
+
* Only a *readable* literal counts. `ssl: enableSsl` is a variable this rule
|
|
60
|
+
* cannot resolve, and guessing there is how a security rule earns a
|
|
61
|
+
* false-positive reputation — it stays silent instead.
|
|
62
|
+
*
|
|
63
|
+
* `trustServerCertificate` inverts the sense: it is dangerous when TRUE. Callers
|
|
64
|
+
* pass `expect` to say which polarity they mean.
|
|
65
|
+
*/
|
|
66
|
+
export declare function isLiteralBoolean(value: TSESTree.Node, expect: boolean): boolean;
|
|
67
|
+
/** Does this object literal carry at least one connection-identifying key? */
|
|
68
|
+
export declare function looksLikeConnectionConfig(obj: TSESTree.ObjectExpression, connectionKeys: readonly string[]): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* A connection string that explicitly turns TLS off.
|
|
71
|
+
*
|
|
72
|
+
* Anchored on the scheme so a random sentence containing `sslmode=disable`
|
|
73
|
+
* (a doc comment, an error message) is not a finding, and matched with a word
|
|
74
|
+
* boundary so `sslmode=disabled-for-now` — not a real libpq value — does not
|
|
75
|
+
* silently pass either.
|
|
76
|
+
*
|
|
77
|
+
* The fragment is cut off before the scan, because a URL fragment ends the
|
|
78
|
+
* query string. It has to be cut on both sides of the finding:
|
|
79
|
+
*
|
|
80
|
+
* - `?sslmode=disable#frag` — the fragment ends a real parameter, so this is
|
|
81
|
+
* a finding. Terminating on `&` alone made it a silent false negative.
|
|
82
|
+
* - `#?sslmode=disable` — the text sits *inside* the fragment, which no
|
|
83
|
+
* driver parses as a connection option, so this is not a finding.
|
|
84
|
+
*/
|
|
85
|
+
export declare function urlDisablesTls(value: string, schemes: readonly string[]): boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Is this string literal actually being used as a connection string?
|
|
88
|
+
*
|
|
89
|
+
* The object path earns its low false-positive rate by requiring a
|
|
90
|
+
* connection-shaped neighbour before a TLS key counts. The URL path needs the
|
|
91
|
+
* same discipline: a `mysql://…?sslmode=disable` string is only a finding where
|
|
92
|
+
* a driver would read it. Two positions qualify —
|
|
93
|
+
*
|
|
94
|
+
* - a connection-named property: `{ connection: 'postgres://…' }`,
|
|
95
|
+
* `{ url: '…' }`, `{ uri: '…' }`
|
|
96
|
+
* - an argument to a call on a driver binding:
|
|
97
|
+
* `mysql.createConnection('mysql://…')`, `new Sequelize('postgres://…')`
|
|
98
|
+
*
|
|
99
|
+
* Deliberately NOT qualifying: a bare `const dsn = 'mysql://…'`. It is
|
|
100
|
+
* indistinguishable from a fixture, a doc example, or an error-message
|
|
101
|
+
* template, and reporting it is the exact false positive that makes a security
|
|
102
|
+
* plugin the one users switch off. The cost is a false negative when the DSN is
|
|
103
|
+
* built in one statement and passed in another.
|
|
104
|
+
*/
|
|
105
|
+
export declare function inConnectionPosition(node: TSESTree.Literal | TSESTree.TemplateLiteral, connectionKeys: readonly string[], bindings: ReadonlySet<string>): boolean;
|
|
106
|
+
/**
|
|
107
|
+
* Build a CWE-319 rule for the given driver.
|
|
108
|
+
*
|
|
109
|
+
* Reports a config object only when a TLS key is explicitly disabled with a
|
|
110
|
+
* readable literal AND the object reads as a connection config. An unreadable
|
|
111
|
+
* value is a deliberate false negative.
|
|
112
|
+
*/
|
|
113
|
+
export declare function createRequireTlsRule(config: RequireTlsRuleConfig): TSESLint.RuleModule<RequireTlsMessageIds, []>;
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isLiteralBoolean = isLiteralBoolean;
|
|
4
|
+
exports.looksLikeConnectionConfig = looksLikeConnectionConfig;
|
|
5
|
+
exports.urlDisablesTls = urlDisablesTls;
|
|
6
|
+
exports.inConnectionPosition = inConnectionPosition;
|
|
7
|
+
exports.createRequireTlsRule = createRequireTlsRule;
|
|
8
|
+
const ast_node_types_1 = require("../ast-node-types");
|
|
9
|
+
const messaging_1 = require("../messaging");
|
|
10
|
+
const unscoped_mutation_rule_1 = require("./unscoped-mutation-rule");
|
|
11
|
+
const DEFAULT_CONNECTION_KEYS = [
|
|
12
|
+
'host',
|
|
13
|
+
'hostname',
|
|
14
|
+
'port',
|
|
15
|
+
'user',
|
|
16
|
+
'username',
|
|
17
|
+
'password',
|
|
18
|
+
'database',
|
|
19
|
+
'connectionString',
|
|
20
|
+
'socketPath',
|
|
21
|
+
'uri',
|
|
22
|
+
'url',
|
|
23
|
+
'connection',
|
|
24
|
+
'pool',
|
|
25
|
+
'schema',
|
|
26
|
+
];
|
|
27
|
+
const TLS_OFF_KEYS = ['ssl', 'secure', 'tls', 'encrypt', 'useSSL', 'requireSSL'];
|
|
28
|
+
const VERIFY_OFF_KEYS = [
|
|
29
|
+
'rejectUnauthorized',
|
|
30
|
+
'verifyServerCertificate',
|
|
31
|
+
'checkServerIdentity',
|
|
32
|
+
'trustServerCertificate',
|
|
33
|
+
];
|
|
34
|
+
function isLiteralBoolean(value, expect) {
|
|
35
|
+
if (value.type !== ast_node_types_1.AST_NODE_TYPES.Literal)
|
|
36
|
+
return false;
|
|
37
|
+
if (typeof value.value !== 'boolean') {
|
|
38
|
+
return expect === false && !value.value;
|
|
39
|
+
}
|
|
40
|
+
return value.value === expect;
|
|
41
|
+
}
|
|
42
|
+
function looksLikeConnectionConfig(obj, connectionKeys) {
|
|
43
|
+
return obj.properties.some((prop) => {
|
|
44
|
+
const name = (0, unscoped_mutation_rule_1.propertyKeyName)(prop);
|
|
45
|
+
return name !== undefined && connectionKeys.includes(name);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
function urlDisablesTls(value, schemes) {
|
|
49
|
+
if (!schemes.some((scheme) => value.startsWith(`${scheme}://`)))
|
|
50
|
+
return false;
|
|
51
|
+
const query = value.split('#')[0];
|
|
52
|
+
return /[?&](?:sslmode=disable|ssl=(?:false|0))(?:&|$)/i.test(query);
|
|
53
|
+
}
|
|
54
|
+
function inConnectionPosition(node, connectionKeys, bindings) {
|
|
55
|
+
const parent = node.parent;
|
|
56
|
+
if (!parent)
|
|
57
|
+
return false;
|
|
58
|
+
if (parent.type === ast_node_types_1.AST_NODE_TYPES.Property && parent.value === node) {
|
|
59
|
+
const name = (0, unscoped_mutation_rule_1.propertyKeyName)(parent);
|
|
60
|
+
return name !== undefined && connectionKeys.includes(name);
|
|
61
|
+
}
|
|
62
|
+
if (parent.type === ast_node_types_1.AST_NODE_TYPES.CallExpression ||
|
|
63
|
+
parent.type === ast_node_types_1.AST_NODE_TYPES.NewExpression) {
|
|
64
|
+
if (!parent.arguments.includes(node))
|
|
65
|
+
return false;
|
|
66
|
+
return calleeBaseIsDriver(parent.callee, bindings);
|
|
67
|
+
}
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
function calleeBaseIsDriver(callee, bindings) {
|
|
71
|
+
let cursor = callee;
|
|
72
|
+
for (;;) {
|
|
73
|
+
if (cursor.type === ast_node_types_1.AST_NODE_TYPES.MemberExpression) {
|
|
74
|
+
cursor = cursor.object;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (cursor.type === ast_node_types_1.AST_NODE_TYPES.CallExpression) {
|
|
78
|
+
cursor = cursor.callee;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
return cursor.type === ast_node_types_1.AST_NODE_TYPES.Identifier && bindings.has(cursor.name);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function createRequireTlsRule(config) {
|
|
85
|
+
const connectionKeys = [...DEFAULT_CONNECTION_KEYS, ...(config.connectionKeys ?? [])];
|
|
86
|
+
const urlSchemes = config.urlSchemes ?? [];
|
|
87
|
+
return {
|
|
88
|
+
meta: {
|
|
89
|
+
type: config.meta.type,
|
|
90
|
+
docs: { ...config.meta.docs },
|
|
91
|
+
messages: {
|
|
92
|
+
tlsDisabled: (0, messaging_1.formatLLMMessage)({
|
|
93
|
+
icon: messaging_1.MessageIcons.SECURITY,
|
|
94
|
+
issueName: 'Cleartext Database Connection',
|
|
95
|
+
description: 'TLS is explicitly disabled on this database connection. Queries, result rows and the credentials that open the session all cross the network in cleartext.',
|
|
96
|
+
severity: 'HIGH',
|
|
97
|
+
cwe: config.meta.docs.cwe,
|
|
98
|
+
owasp: 'A02:2021',
|
|
99
|
+
compliance: ['SOC2', 'PCI-DSS', 'HIPAA', 'NIST-CSF'],
|
|
100
|
+
effort: 'low',
|
|
101
|
+
fix: config.fix,
|
|
102
|
+
documentationLink: config.documentationLink,
|
|
103
|
+
}),
|
|
104
|
+
certificateValidationDisabled: (0, messaging_1.formatLLMMessage)({
|
|
105
|
+
icon: messaging_1.MessageIcons.SECURITY,
|
|
106
|
+
issueName: 'Unverified Database TLS Peer',
|
|
107
|
+
description: 'Certificate validation is disabled on this database connection. The traffic is encrypted but the server is never authenticated, so it encrypts just as willingly to whoever answered in its place.',
|
|
108
|
+
severity: 'HIGH',
|
|
109
|
+
cwe: config.meta.docs.cwe,
|
|
110
|
+
owasp: 'A02:2021',
|
|
111
|
+
compliance: ['SOC2', 'PCI-DSS', 'HIPAA', 'NIST-CSF'],
|
|
112
|
+
effort: 'medium',
|
|
113
|
+
fix: 'Supply the server CA instead of switching verification off — `ssl: { ca: fs.readFileSync(caPath) }`. If the certificate is self-signed, add that CA to the trust store rather than trusting everything.',
|
|
114
|
+
documentationLink: config.documentationLink,
|
|
115
|
+
}),
|
|
116
|
+
},
|
|
117
|
+
schema: [],
|
|
118
|
+
},
|
|
119
|
+
defaultOptions: [],
|
|
120
|
+
create(context) {
|
|
121
|
+
let bindings = new Set();
|
|
122
|
+
let reported = new WeakSet();
|
|
123
|
+
const report = (prop, messageId) => {
|
|
124
|
+
if (reported.has(prop))
|
|
125
|
+
return;
|
|
126
|
+
reported.add(prop);
|
|
127
|
+
context.report({ node: prop, messageId });
|
|
128
|
+
};
|
|
129
|
+
const reportVerifyOffProperty = (prop, name) => {
|
|
130
|
+
if (!VERIFY_OFF_KEYS.includes(name))
|
|
131
|
+
return;
|
|
132
|
+
const inverted = name === 'trustServerCertificate';
|
|
133
|
+
if (!isLiteralBoolean(prop.value, inverted))
|
|
134
|
+
return;
|
|
135
|
+
report(prop, 'certificateValidationDisabled');
|
|
136
|
+
};
|
|
137
|
+
const scanTlsKeys = (obj) => {
|
|
138
|
+
for (const prop of obj.properties) {
|
|
139
|
+
if (prop.type !== ast_node_types_1.AST_NODE_TYPES.Property)
|
|
140
|
+
continue;
|
|
141
|
+
const name = (0, unscoped_mutation_rule_1.propertyKeyName)(prop);
|
|
142
|
+
if (name === undefined)
|
|
143
|
+
continue;
|
|
144
|
+
if (TLS_OFF_KEYS.includes(name)) {
|
|
145
|
+
if (isLiteralBoolean(prop.value, false)) {
|
|
146
|
+
report(prop, 'tlsDisabled');
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (prop.value.type === ast_node_types_1.AST_NODE_TYPES.ObjectExpression) {
|
|
150
|
+
scanTlsKeys(prop.value);
|
|
151
|
+
}
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
reportVerifyOffProperty(prop, name);
|
|
155
|
+
if (prop.value.type === ast_node_types_1.AST_NODE_TYPES.ObjectExpression &&
|
|
156
|
+
connectionKeys.includes(name)) {
|
|
157
|
+
scanTlsKeys(prop.value);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
const checkObject = (obj) => {
|
|
162
|
+
if (!looksLikeConnectionConfig(obj, connectionKeys))
|
|
163
|
+
return;
|
|
164
|
+
scanTlsKeys(obj);
|
|
165
|
+
};
|
|
166
|
+
return {
|
|
167
|
+
Program(program) {
|
|
168
|
+
bindings = (0, unscoped_mutation_rule_1.driverBindings)(program, config.modules);
|
|
169
|
+
reported = new WeakSet();
|
|
170
|
+
},
|
|
171
|
+
ObjectExpression(node) {
|
|
172
|
+
if (bindings.size === 0)
|
|
173
|
+
return;
|
|
174
|
+
checkObject(node);
|
|
175
|
+
},
|
|
176
|
+
Literal(node) {
|
|
177
|
+
if (bindings.size === 0)
|
|
178
|
+
return;
|
|
179
|
+
if (typeof node.value !== 'string')
|
|
180
|
+
return;
|
|
181
|
+
if (!urlDisablesTls(node.value, urlSchemes))
|
|
182
|
+
return;
|
|
183
|
+
if (!inConnectionPosition(node, connectionKeys, bindings))
|
|
184
|
+
return;
|
|
185
|
+
context.report({ node, messageId: 'tlsDisabled' });
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
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
|
+
* @fileoverview Shared CWE-798 detector — a literal API key in an inference
|
|
8
|
+
* SDK's client construction.
|
|
9
|
+
*
|
|
10
|
+
* ## Why this is not `secure-coding/no-hardcoded-credentials`
|
|
11
|
+
*
|
|
12
|
+
* Measured, not assumed: every rule in `eslint-plugin-secure-coding` reports
|
|
13
|
+
* *nothing* on `new OpenAI({ apiKey: 'sk-proj-…' })`,
|
|
14
|
+
* `new GoogleGenerativeAI('AIza…')` or the Anthropic equivalent. The generic
|
|
15
|
+
* credential rules look for password-shaped names and connection strings; an
|
|
16
|
+
* SDK's client options are a different shape and no rule in the ecosystem
|
|
17
|
+
* covers them.
|
|
18
|
+
*
|
|
19
|
+
* The gate here compares against an exact SDK module specifier, which by the
|
|
20
|
+
* taxonomy contract puts it in that SDK's plugin — not in `secure-coding`.
|
|
21
|
+
* The logic is shared because the three SDKs differ only in the module name,
|
|
22
|
+
* the option names, and the remediation copy.
|
|
23
|
+
*/
|
|
24
|
+
import type { TSESTree } from '@typescript-eslint/utils';
|
|
25
|
+
export interface SdkApiKeyRuleConfig {
|
|
26
|
+
/** Rule id, e.g. `no-hardcoded-api-key`. */
|
|
27
|
+
ruleName: string;
|
|
28
|
+
/** Vendor name for the message, e.g. `OpenAI`. */
|
|
29
|
+
vendor: string;
|
|
30
|
+
/**
|
|
31
|
+
* Module names that open the gate. Matched as the exact specifier or a
|
|
32
|
+
* subpath of it (`openai` matches `openai` and `openai/resources`, but not
|
|
33
|
+
* `openai-edge`, which is a different package with a different client).
|
|
34
|
+
* A bare scope (`@anthropic-ai`) matches every package under it.
|
|
35
|
+
*/
|
|
36
|
+
modules: readonly string[];
|
|
37
|
+
/** Client-option names that hold the credential, e.g. `apiKey`. */
|
|
38
|
+
keyProps: readonly string[];
|
|
39
|
+
/**
|
|
40
|
+
* The corrected construction shown in the fix.
|
|
41
|
+
*
|
|
42
|
+
* A template, not a computed string: `{{prop}}` here is resolved by ESLint
|
|
43
|
+
* from the same report data as the description, so a consumer's RuleTester
|
|
44
|
+
* cases only ever declare `prop`. Passing a pre-computed string instead
|
|
45
|
+
* leaves an unhydrated placeholder in every expected message.
|
|
46
|
+
*
|
|
47
|
+
* SDKs whose key can be positional should keep this static — `{{prop}}`
|
|
48
|
+
* reads as "The first argument" there and would render nonsense inside an
|
|
49
|
+
* options literal.
|
|
50
|
+
*/
|
|
51
|
+
fixTemplate: string;
|
|
52
|
+
/** Rule documentation URL. */
|
|
53
|
+
docsUrl: string;
|
|
54
|
+
/** Vendor documentation link carried in the message. */
|
|
55
|
+
documentationLink: string;
|
|
56
|
+
/**
|
|
57
|
+
* Constructors whose *first positional argument* is the key itself, e.g.
|
|
58
|
+
* `new GoogleGenerativeAI('AIza…')`. Empty for SDKs that only take options.
|
|
59
|
+
*/
|
|
60
|
+
positionalKeyConstructors?: readonly string[];
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* What `{{prop}}` reads as when the credential is a positional argument rather
|
|
64
|
+
* than a named option. Exported so a test can pin the string the
|
|
65
|
+
* description renders, without duplicating the literal.
|
|
66
|
+
*/
|
|
67
|
+
export declare const POSITIONAL_KEY_LABEL = "The first argument";
|
|
68
|
+
/**
|
|
69
|
+
* Does this import specifier belong to one of the configured modules?
|
|
70
|
+
*
|
|
71
|
+
* Exact match or a subpath, never a prefix match on the bare name — `openai`
|
|
72
|
+
* must not open the gate for `openai-edge`.
|
|
73
|
+
*/
|
|
74
|
+
export declare function matchesModule(source: string, modules: readonly string[]): boolean;
|
|
75
|
+
/**
|
|
76
|
+
* `literal` carries the property name that actually held the credential.
|
|
77
|
+
* A bare verdict left the caller guessing, and it guessed the first configured
|
|
78
|
+
* prop — naming the wrong option whenever a later one was the offender.
|
|
79
|
+
*/
|
|
80
|
+
export type KeyVerdict = {
|
|
81
|
+
kind: 'literal';
|
|
82
|
+
prop: string;
|
|
83
|
+
} | {
|
|
84
|
+
kind: 'safe';
|
|
85
|
+
} | {
|
|
86
|
+
kind: 'unreadable';
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Whether the client options literal carries an inline credential.
|
|
90
|
+
*
|
|
91
|
+
* `process.env.OPENAI_API_KEY` and any other non-literal expression are `safe`
|
|
92
|
+
* — reading a key from the environment is the correct pattern. A spread is
|
|
93
|
+
* `unreadable`: the key may be in the spread source, and guessing there would
|
|
94
|
+
* flag correct code.
|
|
95
|
+
*
|
|
96
|
+
* A safe credential prop `continue`s rather than returning, because a config
|
|
97
|
+
* can carry more than one: Anthropic's `{ apiKey, authToken }` meant an
|
|
98
|
+
* environment-read `apiKey` returned early and a hardcoded `authToken` right
|
|
99
|
+
* after it was never inspected. `safe` is only the verdict once every property
|
|
100
|
+
* has been looked at.
|
|
101
|
+
*/
|
|
102
|
+
export declare function readCredential(options: TSESTree.ObjectExpression, keyProps: ReadonlySet<string>): KeyVerdict;
|
|
103
|
+
/**
|
|
104
|
+
* The callee name of a `new X(...)` / `X(...)`, if it is statically known.
|
|
105
|
+
* `new pkg.Client()` counts as `Client` — the namespace is not the identity.
|
|
106
|
+
*/
|
|
107
|
+
export declare function calleeName(node: TSESTree.Node): string | undefined;
|
|
108
|
+
export declare function createSdkApiKeyRule(config: SdkApiKeyRuleConfig): import("./rule-creator").RuleModuleWithName<"hardcodedApiKey", [], unknown>;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.POSITIONAL_KEY_LABEL = void 0;
|
|
4
|
+
exports.matchesModule = matchesModule;
|
|
5
|
+
exports.readCredential = readCredential;
|
|
6
|
+
exports.calleeName = calleeName;
|
|
7
|
+
exports.createSdkApiKeyRule = createSdkApiKeyRule;
|
|
8
|
+
const rule_creator_1 = require("./rule-creator");
|
|
9
|
+
const messaging_1 = require("../messaging");
|
|
10
|
+
exports.POSITIONAL_KEY_LABEL = 'The first argument';
|
|
11
|
+
function matchesModule(source, modules) {
|
|
12
|
+
return modules.some((m) => source === m || source.startsWith(`${m}/`));
|
|
13
|
+
}
|
|
14
|
+
function readCredential(options, keyProps) {
|
|
15
|
+
for (const prop of options.properties) {
|
|
16
|
+
if (prop.type === 'SpreadElement')
|
|
17
|
+
return { kind: 'unreadable' };
|
|
18
|
+
if (prop.computed)
|
|
19
|
+
continue;
|
|
20
|
+
const name = prop.key.type === 'Identifier' ? prop.key.name : String(prop.key.value);
|
|
21
|
+
if (!keyProps.has(name))
|
|
22
|
+
continue;
|
|
23
|
+
if (prop.value.type !== 'Literal')
|
|
24
|
+
continue;
|
|
25
|
+
if (typeof prop.value.value === 'string' && prop.value.value.length > 0) {
|
|
26
|
+
return { kind: 'literal', prop: name };
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return { kind: 'safe' };
|
|
30
|
+
}
|
|
31
|
+
function calleeName(node) {
|
|
32
|
+
if (node.type === 'Identifier')
|
|
33
|
+
return node.name;
|
|
34
|
+
if (node.type === 'MemberExpression' && !node.computed && node.property.type === 'Identifier') {
|
|
35
|
+
return node.property.name;
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
function createSdkApiKeyRule(config) {
|
|
40
|
+
const keyProps = new Set(config.keyProps);
|
|
41
|
+
const positional = new Set(config.positionalKeyConstructors ?? []);
|
|
42
|
+
return (0, rule_creator_1.createRule)({
|
|
43
|
+
name: config.ruleName,
|
|
44
|
+
meta: {
|
|
45
|
+
type: 'problem',
|
|
46
|
+
docs: {
|
|
47
|
+
url: config.docsUrl,
|
|
48
|
+
description: `Forbid a literal API key in the ${config.vendor} client options`,
|
|
49
|
+
cwe: 'CWE-798',
|
|
50
|
+
cvss: 9.1,
|
|
51
|
+
},
|
|
52
|
+
messages: {
|
|
53
|
+
hardcodedApiKey: (0, messaging_1.formatLLMMessage)({
|
|
54
|
+
icon: messaging_1.MessageIcons.SECURITY,
|
|
55
|
+
issueName: `Hardcoded ${config.vendor} Credential`,
|
|
56
|
+
cwe: 'CWE-798',
|
|
57
|
+
owasp: 'A07:2021',
|
|
58
|
+
cvss: 9.1,
|
|
59
|
+
description: '{{prop}} is a string literal, so the credential is committed to source control and readable by anyone with repository access',
|
|
60
|
+
severity: 'CRITICAL',
|
|
61
|
+
compliance: ['SOC2', 'PCI-DSS'],
|
|
62
|
+
fix: `Read it from the environment: ${config.fixTemplate}`,
|
|
63
|
+
documentationLink: config.documentationLink,
|
|
64
|
+
}),
|
|
65
|
+
},
|
|
66
|
+
schema: [],
|
|
67
|
+
},
|
|
68
|
+
defaultOptions: [],
|
|
69
|
+
create(context) {
|
|
70
|
+
let importsSdk = false;
|
|
71
|
+
const candidates = [];
|
|
72
|
+
function inspect(node, args) {
|
|
73
|
+
const first = args[0];
|
|
74
|
+
if (first === undefined)
|
|
75
|
+
return;
|
|
76
|
+
if (first.type === 'Literal') {
|
|
77
|
+
const name = calleeName(node.callee);
|
|
78
|
+
if (name !== undefined &&
|
|
79
|
+
positional.has(name) &&
|
|
80
|
+
typeof first.value === 'string' &&
|
|
81
|
+
first.value.length > 0) {
|
|
82
|
+
candidates.push({ node, prop: exports.POSITIONAL_KEY_LABEL });
|
|
83
|
+
}
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (first.type !== 'ObjectExpression')
|
|
87
|
+
return;
|
|
88
|
+
const verdict = readCredential(first, keyProps);
|
|
89
|
+
if (verdict.kind === 'literal')
|
|
90
|
+
candidates.push({ node, prop: verdict.prop });
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
ImportDeclaration(node) {
|
|
94
|
+
if (matchesModule(String(node.source.value), config.modules))
|
|
95
|
+
importsSdk = true;
|
|
96
|
+
},
|
|
97
|
+
NewExpression(node) {
|
|
98
|
+
inspect(node, node.arguments);
|
|
99
|
+
},
|
|
100
|
+
CallExpression(node) {
|
|
101
|
+
if (node.callee.type === 'Identifier' &&
|
|
102
|
+
node.callee.name === 'require' &&
|
|
103
|
+
node.arguments[0]?.type === 'Literal' &&
|
|
104
|
+
typeof node.arguments[0].value === 'string' &&
|
|
105
|
+
matchesModule(node.arguments[0].value, config.modules)) {
|
|
106
|
+
importsSdk = true;
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
inspect(node, node.arguments);
|
|
110
|
+
},
|
|
111
|
+
'Program:exit'() {
|
|
112
|
+
if (!importsSdk)
|
|
113
|
+
return;
|
|
114
|
+
for (const { node, prop } of candidates) {
|
|
115
|
+
context.report({ node, messageId: 'hardcodedApiKey', data: { prop } });
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
}
|
|
@@ -43,8 +43,9 @@ export interface SqlInjectionRuleConfig {
|
|
|
43
43
|
/**
|
|
44
44
|
* Build a CWE-89 rule for the given sinks and remediation copy.
|
|
45
45
|
*
|
|
46
|
-
* Detects
|
|
47
|
-
* interpolation into a sink,
|
|
48
|
-
* `+=`) that is later passed to a sink
|
|
46
|
+
* Detects four shapes: direct concatenation into a sink, direct
|
|
47
|
+
* interpolation into a sink, a variable tainted by either (including via
|
|
48
|
+
* `+=`) that is later passed to a sink, and either of those passed to a
|
|
49
|
+
* same-file helper that forwards the argument into a sink.
|
|
49
50
|
*/
|
|
50
51
|
export declare function createSqlInjectionRule(config: SqlInjectionRuleConfig): TSESLint.RuleModule<SqlInjectionMessageIds, []>;
|