@intentrax/sdk 1.7.0 → 1.8.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 +17 -2
- package/cli/intentrax.mjs +45 -1
- package/mcp/README.md +138 -0
- package/mcp/canonical.d.ts +12 -0
- package/mcp/canonical.mjs +70 -0
- package/mcp/cli.d.ts +16 -0
- package/mcp/cli.mjs +214 -0
- package/mcp/client.d.ts +131 -0
- package/mcp/client.mjs +955 -0
- package/mcp/commands.d.ts +113 -0
- package/mcp/commands.mjs +575 -0
- package/mcp/index.d.ts +12 -0
- package/mcp/index.mjs +12 -0
- package/mcp/proxy.d.ts +47 -0
- package/mcp/proxy.mjs +434 -0
- package/mcp/receipt-meta.d.ts +59 -0
- package/mcp/receipt-meta.mjs +178 -0
- package/mcp/tool-manifest.d.ts +74 -0
- package/mcp/tool-manifest.mjs +154 -0
- package/package.json +16 -1
- package/readback/README.md +352 -0
- package/readback/bridge.mjs +71 -0
- package/readback/cli.mjs +150 -0
- package/readback/confirm.mjs +404 -0
- package/readback/index.mjs +12 -0
- package/readback/recipe-worker.mjs +33 -0
- package/readback/recipes/_shared.mjs +126 -0
- package/readback/recipes/email-delivery.mjs +136 -0
- package/readback/recipes/github-issue.mjs +88 -0
- package/readback/recipes/http-json.mjs +109 -0
- package/readback/recipes/sql-row.mjs +96 -0
- package/readback/recipes/stripe-refund.mjs +73 -0
- package/readback/redact.mjs +68 -0
- package/scan/README.md +211 -0
- package/scan/classify.mjs +276 -0
- package/scan/cli.mjs +102 -0
- package/scan/configs.mjs +261 -0
- package/scan/credentials.mjs +121 -0
- package/scan/extract.mjs +323 -0
- package/scan/gates.mjs +94 -0
- package/scan/report.mjs +58 -0
- package/scan/scan.mjs +571 -0
- package/scan/text.mjs +186 -0
package/scan/configs.mjs
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
// Agent-client configuration: MCP server configs (Claude Code .mcp.json and
|
|
2
|
+
// .claude/settings*.json, Cursor .cursor/mcp.json, VS Code .vscode/mcp.json and
|
|
3
|
+
// settings.json, Claude Desktop, Gemini CLI, generic mcp.json), Claude Code
|
|
4
|
+
// permissions and PreToolUse hooks, and Cursor hooks.
|
|
5
|
+
//
|
|
6
|
+
// Credential VALUES in these files are never kept: an env entry becomes
|
|
7
|
+
// { name, refersTo?, inline } and nothing else.
|
|
8
|
+
|
|
9
|
+
import { posix } from "node:path";
|
|
10
|
+
import { parseJsonc, makeLineIndex, tokenize } from "./text.mjs";
|
|
11
|
+
import { isCredentialName, referencedVars } from "./credentials.mjs";
|
|
12
|
+
|
|
13
|
+
const CONFIG_KINDS = [
|
|
14
|
+
{ re: /(^|\/)\.mcp\.json$/, client: "claude-code", up: 0 },
|
|
15
|
+
{ re: /(^|\/)\.claude\/settings(\.local)?\.json$/, client: "claude-code", up: 1, claudeSettings: true },
|
|
16
|
+
{ re: /(^|\/)\.cursor\/mcp\.json$/, client: "cursor", up: 1 },
|
|
17
|
+
{ re: /(^|\/)\.cursor\/hooks\.json$/, client: "cursor", up: 1, cursorHooks: true },
|
|
18
|
+
{ re: /(^|\/)\.vscode\/mcp\.json$/, client: "vscode", up: 1 },
|
|
19
|
+
{ re: /(^|\/)\.vscode\/settings\.json$/, client: "vscode", up: 1 },
|
|
20
|
+
{ re: /(^|\/)\.gemini\/settings\.json$/, client: "gemini-cli", up: 1 },
|
|
21
|
+
{ re: /(^|\/)claude_desktop_config\.json$/, client: "claude-desktop", up: 0 },
|
|
22
|
+
{ re: /(^|\/)mcp\.json$/, client: "mcp-client", up: 0 },
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
export function configKind(relPath) {
|
|
26
|
+
return CONFIG_KINDS.find((k) => k.re.test(relPath)) ?? null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function rootOf(relPath, up) {
|
|
30
|
+
let dir = posix.dirname(relPath);
|
|
31
|
+
for (let i = 0; i < up; i += 1) dir = posix.dirname(dir);
|
|
32
|
+
return dir === "." ? "" : dir;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function serversOf(json) {
|
|
36
|
+
if (!json || typeof json !== "object") return null;
|
|
37
|
+
if (json.mcpServers && typeof json.mcpServers === "object") return json.mcpServers;
|
|
38
|
+
if (json.servers && typeof json.servers === "object") return json.servers;
|
|
39
|
+
if (json.mcp && typeof json.mcp === "object" && json.mcp.servers && typeof json.mcp.servers === "object") return json.mcp.servers;
|
|
40
|
+
if (json["mcp.servers"] && typeof json["mcp.servers"] === "object") return json["mcp.servers"];
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const SECRET_FLAG = /^--?([A-Za-z0-9_-]*(?:token|key|secret|password|passwd|pat|auth|credential)[A-Za-z0-9_-]*)(?:=(.*))?$/i;
|
|
45
|
+
|
|
46
|
+
// Credentials a server receives: env entries, auth headers, URL query
|
|
47
|
+
// parameters, secret-looking CLI flags and docker `-e NAME`.
|
|
48
|
+
function serverCredentials(server, file, line, serverName) {
|
|
49
|
+
const creds = [];
|
|
50
|
+
const add = (c) => creds.push({ file, line, ...c });
|
|
51
|
+
const env = server.env && typeof server.env === "object" ? server.env : {};
|
|
52
|
+
for (const [k, v] of Object.entries(env)) {
|
|
53
|
+
const refs = referencedVars(v);
|
|
54
|
+
if (!isCredentialName(k) && !refs.some(isCredentialName)) continue;
|
|
55
|
+
add({
|
|
56
|
+
name: k,
|
|
57
|
+
refersTo: refs.find((r) => r !== k) ?? null,
|
|
58
|
+
inline: refs.length === 0 && typeof v === "string" && v.length > 0,
|
|
59
|
+
origin: `${serverName} env.${k}`,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const headers = server.headers && typeof server.headers === "object" ? server.headers : {};
|
|
63
|
+
for (const [h, v] of Object.entries(headers)) {
|
|
64
|
+
if (!/authori[sz]ation|token|key|secret|cookie|auth/i.test(h)) continue;
|
|
65
|
+
const refs = referencedVars(v);
|
|
66
|
+
if (refs.length) for (const r of refs) add({ name: r, refersTo: null, inline: false, origin: `${serverName} headers.${h}` });
|
|
67
|
+
else add({ name: `header ${h}`, refersTo: null, inline: true, origin: `${serverName} headers.${h}` });
|
|
68
|
+
}
|
|
69
|
+
if (typeof server.url === "string" || typeof server.serverUrl === "string") {
|
|
70
|
+
const raw = server.url ?? server.serverUrl;
|
|
71
|
+
for (const r of referencedVars(raw)) if (isCredentialName(r)) add({ name: r, refersTo: null, inline: false, origin: `${serverName} url` });
|
|
72
|
+
try {
|
|
73
|
+
const u = new URL(raw.replace(/\$\{[^}]*\}/g, "x"));
|
|
74
|
+
for (const p of u.searchParams.keys()) {
|
|
75
|
+
if (/token|key|secret|auth|password|sig/i.test(p)) add({ name: `url query parameter "${p}"`, refersTo: null, inline: true, origin: `${serverName} url` });
|
|
76
|
+
}
|
|
77
|
+
} catch {
|
|
78
|
+
// not a URL we can parse; nothing to record
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const args = Array.isArray(server.args) ? server.args.map(String) : [];
|
|
82
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
83
|
+
const a = args[i];
|
|
84
|
+
if ((a === "-e" || a === "--env") && args[i + 1]) {
|
|
85
|
+
const [n, ...rest] = args[i + 1].split("=");
|
|
86
|
+
if (isCredentialName(n)) add({ name: n, refersTo: null, inline: rest.length > 0 && referencedVars(rest.join("=")).length === 0, origin: `${serverName} args (${a} ${n})` });
|
|
87
|
+
i += 1;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
const f = SECRET_FLAG.exec(a);
|
|
91
|
+
if (f) {
|
|
92
|
+
const value = f[2] ?? args[i + 1];
|
|
93
|
+
const refs = referencedVars(value);
|
|
94
|
+
if (refs.length) for (const r of refs) add({ name: r, refersTo: null, inline: false, origin: `${serverName} args (--${f[1]})` });
|
|
95
|
+
else add({ name: `flag --${f[1]}`, refersTo: null, inline: value !== undefined, origin: `${serverName} args (--${f[1]})` });
|
|
96
|
+
if (f[2] === undefined) i += 1;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return creds;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Tokens used for domain heuristics (server name, command, package names,
|
|
103
|
+
// URL host). Values after secret flags and env assignments are excluded.
|
|
104
|
+
function domainTokensOf(name, server) {
|
|
105
|
+
const parts = [name, server.command ?? ""];
|
|
106
|
+
const args = Array.isArray(server.args) ? server.args.map(String) : [];
|
|
107
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
108
|
+
const a = args[i];
|
|
109
|
+
if (SECRET_FLAG.test(a)) {
|
|
110
|
+
if (!a.includes("=")) i += 1;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (a === "-e" || a === "--env") {
|
|
114
|
+
i += 1;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (a.includes("=") || /^https?:/i.test(a)) continue;
|
|
118
|
+
parts.push(a);
|
|
119
|
+
}
|
|
120
|
+
const raw = server.url ?? server.serverUrl;
|
|
121
|
+
if (typeof raw === "string") {
|
|
122
|
+
try {
|
|
123
|
+
parts.push(new URL(raw.replace(/\$\{[^}]*\}/g, "x")).hostname);
|
|
124
|
+
} catch {
|
|
125
|
+
// ignore
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return [...new Set(parts.flatMap((p) => tokenize(p)))];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Candidate entry files for a stdio server, relative to the config root.
|
|
132
|
+
function entryCandidates(server) {
|
|
133
|
+
const out = [];
|
|
134
|
+
const tokens = [server.command, ...(Array.isArray(server.args) ? server.args : [])].filter((t) => typeof t === "string");
|
|
135
|
+
let base = typeof server.cwd === "string" ? server.cwd : "";
|
|
136
|
+
for (let i = 0; i < tokens.length; i += 1) {
|
|
137
|
+
const t = tokens[i];
|
|
138
|
+
if ((t === "--directory" || t === "--cwd" || t === "--prefix" || t === "-C") && tokens[i + 1]) {
|
|
139
|
+
base = tokens[i + 1];
|
|
140
|
+
i += 1;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
if (t === "-m" && tokens[i + 1]) {
|
|
144
|
+
const mod = tokens[i + 1].replace(/\./g, "/");
|
|
145
|
+
out.push({ base, path: `${mod}.py` }, { base, path: `${mod}/__main__.py` }, { base, path: `${mod}/server.py` }, { base, path: `${mod}/__init__.py` });
|
|
146
|
+
i += 1;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (/\.(?:m?[jt]sx?|c[jt]s|py)$/i.test(t)) out.push({ base, path: t });
|
|
150
|
+
}
|
|
151
|
+
return out.map((c) => ({ base: clean(c.base), path: clean(c.path) }));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function clean(p) {
|
|
155
|
+
return String(p ?? "")
|
|
156
|
+
.replace(/\$\{(?:workspaceFolder|workspaceRoot|PWD|CLAUDE_PROJECT_DIR|pwd)\}\/?/g, "")
|
|
157
|
+
.replace(/\$(?:PWD|CLAUDE_PROJECT_DIR)\/?/g, "")
|
|
158
|
+
.split("\\")
|
|
159
|
+
.join("/");
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function lineOfKey(text, key) {
|
|
163
|
+
const lineOf = makeLineIndex(text);
|
|
164
|
+
const i = text.indexOf(JSON.stringify(key));
|
|
165
|
+
return i >= 0 ? lineOf(i) : 1;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Parses one config file. Returns { servers, claude, cursor, warning }.
|
|
169
|
+
export function parseConfig(relPath, text) {
|
|
170
|
+
const kind = configKind(relPath);
|
|
171
|
+
if (!kind) return null;
|
|
172
|
+
let json;
|
|
173
|
+
try {
|
|
174
|
+
json = parseJsonc(text);
|
|
175
|
+
} catch {
|
|
176
|
+
return { warning: `could not parse ${relPath} as JSON; MCP servers or hooks in it were not scanned` };
|
|
177
|
+
}
|
|
178
|
+
const root = rootOf(relPath, kind.up);
|
|
179
|
+
const result = { root, servers: [], claude: null, cursor: null };
|
|
180
|
+
|
|
181
|
+
const servers = serversOf(json);
|
|
182
|
+
if (servers) {
|
|
183
|
+
for (const [name, server] of Object.entries(servers)) {
|
|
184
|
+
if (!server || typeof server !== "object") continue;
|
|
185
|
+
const line = lineOfKey(text, name);
|
|
186
|
+
result.servers.push({
|
|
187
|
+
name,
|
|
188
|
+
client: kind.client,
|
|
189
|
+
configFile: relPath,
|
|
190
|
+
line,
|
|
191
|
+
root,
|
|
192
|
+
disabled: server.disabled === true || server.enabled === false,
|
|
193
|
+
transport: typeof (server.url ?? server.serverUrl) === "string" ? "http" : "stdio",
|
|
194
|
+
credentials: serverCredentials(server, relPath, line, name),
|
|
195
|
+
domainTokens: domainTokensOf(name, server),
|
|
196
|
+
entries: entryCandidates(server),
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (kind.claudeSettings) {
|
|
202
|
+
const perms = json.permissions && typeof json.permissions === "object" ? json.permissions : {};
|
|
203
|
+
const list = (v) => (Array.isArray(v) ? v.filter((x) => typeof x === "string") : []);
|
|
204
|
+
const pre = [];
|
|
205
|
+
const hooks = json.hooks && typeof json.hooks === "object" ? json.hooks : {};
|
|
206
|
+
for (const entry of Array.isArray(hooks.PreToolUse) ? hooks.PreToolUse : []) {
|
|
207
|
+
const commands = (Array.isArray(entry?.hooks) ? entry.hooks : []).map((h) => String(h?.command ?? "")).filter(Boolean);
|
|
208
|
+
pre.push({ matcher: typeof entry?.matcher === "string" ? entry.matcher : "", commands, file: relPath, line: lineOfKey(text, "PreToolUse") });
|
|
209
|
+
}
|
|
210
|
+
result.claude = {
|
|
211
|
+
file: relPath,
|
|
212
|
+
allow: list(perms.allow),
|
|
213
|
+
ask: list(perms.ask),
|
|
214
|
+
deny: list(perms.deny),
|
|
215
|
+
defaultMode: typeof perms.defaultMode === "string" ? perms.defaultMode : null,
|
|
216
|
+
enableAllProjectMcpServers: json.enableAllProjectMcpServers === true,
|
|
217
|
+
preToolUse: pre,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (kind.cursorHooks) {
|
|
222
|
+
const hooks = json.hooks && typeof json.hooks === "object" ? json.hooks : {};
|
|
223
|
+
const pick = (k) => (Array.isArray(hooks[k]) ? hooks[k] : []).map((h) => String(h?.command ?? "")).filter(Boolean);
|
|
224
|
+
result.cursor = {
|
|
225
|
+
file: relPath,
|
|
226
|
+
line: lineOfKey(text, "hooks"),
|
|
227
|
+
beforeMCPExecution: pick("beforeMCPExecution"),
|
|
228
|
+
beforeShellExecution: pick("beforeShellExecution"),
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
return result;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Claude Code permission rule match for a tool id such as
|
|
235
|
+
// "mcp__infra__delete_volume" or "Bash".
|
|
236
|
+
export function claudeRuleMatches(rule, toolId) {
|
|
237
|
+
const r = rule.trim();
|
|
238
|
+
if (r === toolId) return true;
|
|
239
|
+
if (toolId.startsWith("mcp__")) {
|
|
240
|
+
const [, server] = toolId.split("__");
|
|
241
|
+
if (r === `mcp__${server}` || r === `mcp__${server}__*` || r === "mcp__*") return true;
|
|
242
|
+
}
|
|
243
|
+
if (toolId === "Bash") {
|
|
244
|
+
// "Bash", "Bash(*)" and "Bash(:*)" allow every command; narrower rules
|
|
245
|
+
// ("Bash(npm test)") cover only some.
|
|
246
|
+
return r === "Bash" || r === "Bash(*)" || r === "Bash(:*)";
|
|
247
|
+
}
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// PreToolUse matcher semantics: empty or "*" matches all; otherwise the
|
|
252
|
+
// matcher is matched as a regular expression against the whole tool name.
|
|
253
|
+
export function claudeMatcherMatches(matcher, toolId) {
|
|
254
|
+
if (!matcher || matcher === "*") return true;
|
|
255
|
+
if (matcher === toolId) return true;
|
|
256
|
+
try {
|
|
257
|
+
return new RegExp(`^(?:${matcher})$`).test(toolId);
|
|
258
|
+
} catch {
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// Credential references and breadth heuristics. The scan records a credential
|
|
2
|
+
// only by its variable or config name and where it was found. Values are never
|
|
3
|
+
// stored, compared against output, or printed: when a config holds a literal
|
|
4
|
+
// value, we record `inline: true` and nothing else.
|
|
5
|
+
|
|
6
|
+
// Names that look like credentials (env vars, config keys, headers, flags).
|
|
7
|
+
export const CREDENTIAL_NAME = /(TOKEN|SECRET|PASSWORD|PASSWD|PASSPHRASE|API[_-]?KEY|ACCESS[_-]?KEY|PRIVATE[_-]?KEY|CREDENTIALS?|AUTH(?!OR)|(^|[_-])PAT$|^PAT$|DSN$|DATABASE[_-]?URL|DB[_-]?URL|CONNECTION[_-]?STRING|KUBECONFIG|SERVICE[_-]?ROLE|[_-]KEY$|^KEY$|COOKIE)/i;
|
|
8
|
+
|
|
9
|
+
export function isCredentialName(name) {
|
|
10
|
+
return CREDENTIAL_NAME.test(String(name ?? ""));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// `${VAR}`, `${env:VAR}`, `$VAR`, `%VAR%`, `{env:VAR}`: returns the referenced
|
|
14
|
+
// variable names. A value with no reference is treated as an inline literal.
|
|
15
|
+
export function referencedVars(value) {
|
|
16
|
+
const out = [];
|
|
17
|
+
const re = /\$\{(?:env:|input:)?([A-Za-z_][A-Za-z0-9_]*)\}|\$([A-Za-z_][A-Za-z0-9_]*)|%([A-Za-z_][A-Za-z0-9_]*)%|\{env:([A-Za-z_][A-Za-z0-9_]*)\}/g;
|
|
18
|
+
let m;
|
|
19
|
+
while ((m = re.exec(String(value ?? "")))) out.push(m[1] ?? m[2] ?? m[3] ?? m[4]);
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const PLATFORMS = [
|
|
24
|
+
["github", /GITHUB|(^|_)GH_/],
|
|
25
|
+
["gitlab", /GITLAB/],
|
|
26
|
+
["stripe", /STRIPE/],
|
|
27
|
+
["aws", /(^|_)AWS(_|$)/],
|
|
28
|
+
["gcp", /GOOGLE|(^|_)GCP(_|$)|GCLOUD/],
|
|
29
|
+
["azure", /AZURE/],
|
|
30
|
+
["cloudflare", /CLOUDFLARE|(^|_)CF_/],
|
|
31
|
+
["supabase", /SUPABASE/],
|
|
32
|
+
["kubernetes", /KUBE/],
|
|
33
|
+
["slack", /SLACK/],
|
|
34
|
+
["database", /DATABASE|POSTGRES|(^|_)PG|MYSQL|MONGO|(^|_)DB(_|$)|REDIS/],
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
export function platformOf(name) {
|
|
38
|
+
const upper = String(name).toUpperCase();
|
|
39
|
+
for (const [p, re] of PLATFORMS) if (re.test(upper)) return p;
|
|
40
|
+
return "generic";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const SCOPED_ALTERNATIVE = {
|
|
44
|
+
github: "a fine-grained personal access token or GitHub App installation token limited to the repositories and permissions this agent needs",
|
|
45
|
+
gitlab: "a project access token limited to one project and the scopes this agent needs",
|
|
46
|
+
stripe: "a restricted key with only the resource permissions this agent needs",
|
|
47
|
+
aws: "an IAM role whose policy allows only the actions and resources this agent needs, with no delete actions unless one tool truly needs them",
|
|
48
|
+
gcp: "a service account with a narrow predefined or custom role on only the resources this agent needs",
|
|
49
|
+
azure: "a managed identity or service principal with a narrow role assignment on one resource group",
|
|
50
|
+
cloudflare: "an API token scoped to the zones and permissions this agent needs (not the global API key)",
|
|
51
|
+
supabase: "the anon key with row-level security, or a database role limited to the tables and statements this agent needs",
|
|
52
|
+
kubernetes: "a service account bound to a namespaced Role with only the verbs this agent needs",
|
|
53
|
+
slack: "a bot token with only the scopes this agent needs",
|
|
54
|
+
database: "a database role limited to the tables and statements this agent needs (and a separate read-only role for reads)",
|
|
55
|
+
generic: "a token limited to the project, resources and actions this agent needs, with no delete or admin rights",
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export function scopedAlternative(name) {
|
|
59
|
+
return SCOPED_ALTERNATIVE[platformOf(name)];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Breadth rules, most specific first. `label` is the phrase used in the finding
|
|
63
|
+
// line ("reachable with an account-wide token").
|
|
64
|
+
const BREADTH_RULES = [
|
|
65
|
+
{ re: /ACCOUNT[_-]?(API[_-]?)?(TOKEN|KEY|SECRET)|ACCOUNT[_-]?WIDE/, label: "an account-wide token", why: 'name contains "ACCOUNT" with TOKEN/KEY' },
|
|
66
|
+
{ re: /(^|_)(ORG|ORGANI[SZ]ATION)[_-]?(ADMIN[_-]?)?(TOKEN|KEY|SECRET)/, label: "an organization-wide token", why: 'name contains "ORG" with TOKEN/KEY' },
|
|
67
|
+
{ re: /GLOBAL[_-]?API[_-]?KEY|^CLOUDFLARE_API_KEY$|^CF_API_KEY$/, label: "an account-wide global API key", why: "global API keys carry every permission of the account" },
|
|
68
|
+
{ re: /SERVICE[_-]?ROLE/, label: "a service-role key", why: "service-role keys bypass row-level security" },
|
|
69
|
+
{ re: /FULL[_-]?ACCESS|ALL[_-]?ACCESS|UNRESTRICTED|GOD[_-]?MODE/, label: "a full-access token", why: 'name says "full/all access"' },
|
|
70
|
+
{ re: /(^|_)(SUPERUSER|SUPER_USER|SUDO)(_|$)/, label: "a superuser credential", why: 'name contains "SUPERUSER"/"SUDO"' },
|
|
71
|
+
{ re: /(^|_)ROOT(_|$)/, label: "a root credential", why: 'name contains "ROOT"' },
|
|
72
|
+
{ re: /(^|_)(ADMIN|ADMINISTRATOR)(_|$)/, label: "an admin token", why: 'name contains "ADMIN"' },
|
|
73
|
+
{ re: /(^|_)OWNER(_|$)/, label: "an owner token", why: 'name contains "OWNER"' },
|
|
74
|
+
{ re: /(^|_)MASTER(_|$)/, label: "a master key", why: 'name contains "MASTER"' },
|
|
75
|
+
{ re: /^(GITHUB_PERSONAL_ACCESS_TOKEN|GITHUB_PAT|GH_PAT|GITLAB_PERSONAL_ACCESS_TOKEN)$/, label: "a personal access token", why: "a personal access token acts as the user; a classic token with repo scope reaches every repository the user can" },
|
|
76
|
+
{ re: /^STRIPE_(SECRET_KEY|API_KEY|SECRET|KEY)$/, label: "a full-access secret key", why: "a standard Stripe secret key has full API access (restricted keys exist)" },
|
|
77
|
+
{ re: /^AWS_(SECRET_ACCESS_KEY|ACCESS_KEY_ID|SECRET_KEY)$/, label: "a long-lived cloud access key", why: "long-lived IAM access keys are often attached to broad policies; their scope is not visible in the repository" },
|
|
78
|
+
{ re: /^(GOOGLE_APPLICATION_CREDENTIALS|GCP_SERVICE_ACCOUNT_KEY|GCLOUD_SERVICE_KEY|GOOGLE_CREDENTIALS)$/, label: "a service-account key", why: "service-account key files are often granted project-wide roles; their scope is not visible in the repository" },
|
|
79
|
+
{ re: /^AZURE_CLIENT_SECRET$/, label: "a service-principal secret", why: "service-principal role assignments are often subscription-wide; their scope is not visible in the repository" },
|
|
80
|
+
{ re: /^(DIGITALOCEAN_(ACCESS_)?TOKEN|DO_TOKEN|HEROKU_API_KEY|VERCEL_TOKEN|NETLIFY_AUTH_TOKEN|FLY_API_TOKEN|LINODE_TOKEN|HCLOUD_TOKEN|RENDER_API_KEY|RAILWAY_API_TOKEN)$/, label: "a platform API token", why: "this platform's API tokens are account- or team-wide unless created with a narrower scope" },
|
|
81
|
+
{ re: /KUBECONFIG/, label: "cluster credentials", why: "kubeconfig credentials are often cluster-admin" },
|
|
82
|
+
];
|
|
83
|
+
|
|
84
|
+
const GITHUB_TOKEN_NAMES = /^(GITHUB_TOKEN|GH_TOKEN)$/;
|
|
85
|
+
const REPO_WIDE_SCOPE_HINT = /scopes?\W{0,40}["'](?:repo|admin:org|delete_repo|workflow|admin:repo_hook)["']|["'](?:delete_repo|admin:org)["']/i;
|
|
86
|
+
|
|
87
|
+
// Returns null when the name gives no breadth signal. `contextText` is the
|
|
88
|
+
// file the credential was referenced from, used only for GitHub scope hints.
|
|
89
|
+
export function credentialBreadth(name, contextText = "") {
|
|
90
|
+
const upper = String(name ?? "").toUpperCase();
|
|
91
|
+
for (const rule of BREADTH_RULES) {
|
|
92
|
+
if (rule.re.test(upper)) return { label: rule.label, heuristic: true, reason: rule.why };
|
|
93
|
+
}
|
|
94
|
+
if (GITHUB_TOKEN_NAMES.test(upper) && REPO_WIDE_SCOPE_HINT.test(contextText)) {
|
|
95
|
+
return { label: "a repo-wide GitHub token", heuristic: true, reason: "the same file mentions repo-wide scopes (repo, admin:org, delete_repo or workflow)" };
|
|
96
|
+
}
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Env reads in source code: process.env.X, os.environ["X"], os.getenv("X") ...
|
|
101
|
+
const ENV_READ = /process\.env\.([A-Za-z_][A-Za-z0-9_]*)|process\.env\[\s*["']([^"']+)["']\s*\]|os\.environ\[\s*["']([^"']+)["']\s*\]|os\.environ\.get\(\s*["']([^"']+)["']|os\.getenv\(\s*["']([^"']+)["']|Deno\.env\.get\(\s*["']([^"']+)["']|Bun\.env\.([A-Za-z_][A-Za-z0-9_]*)|import\.meta\.env\.([A-Za-z_][A-Za-z0-9_]*)/g;
|
|
102
|
+
const ENV_DESTRUCTURE = /\{([^{}]{1,400})\}\s*=\s*(?:process\.env|Bun\.env|import\.meta\.env)\b/g;
|
|
103
|
+
|
|
104
|
+
export function envReadsIn(text, from = 0, to = text.length) {
|
|
105
|
+
const slice = text.slice(from, to);
|
|
106
|
+
const out = [];
|
|
107
|
+
let m;
|
|
108
|
+
ENV_READ.lastIndex = 0;
|
|
109
|
+
while ((m = ENV_READ.exec(slice))) {
|
|
110
|
+
const name = m.slice(1).find((g) => g !== undefined);
|
|
111
|
+
if (name) out.push({ name, index: from + m.index });
|
|
112
|
+
}
|
|
113
|
+
ENV_DESTRUCTURE.lastIndex = 0;
|
|
114
|
+
while ((m = ENV_DESTRUCTURE.exec(slice))) {
|
|
115
|
+
for (const part of m[1].split(",")) {
|
|
116
|
+
const name = part.split(/[:=]/)[0].trim();
|
|
117
|
+
if (/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) out.push({ name, index: from + m.index });
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return out.filter((r) => isCredentialName(r.name));
|
|
121
|
+
}
|