coldpin 0.0.0-stage → 1.5.1
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/ACCESSIBILITY.md +17 -0
- package/CHANGELOG.md +97 -0
- package/CONTRIBUTING.md +36 -0
- package/LICENSE +98 -0
- package/MAINTENANCE.md +33 -0
- package/README.md +142 -2
- package/SECURITY.md +34 -0
- package/TESTING.md +26 -0
- package/agents/coldpin-auditor.md +47 -0
- package/agents/coldpin-compliance.md +38 -0
- package/agents/coldpin-confusion-auditor.md +38 -0
- package/agents/coldpin-discoverer.md +37 -0
- package/agents/coldpin-forensics.md +38 -0
- package/agents/coldpin-inspector.md +37 -0
- package/agents/coldpin-manifest-auditor.md +38 -0
- package/assets/icon.svg +7 -0
- package/catalog/mcp-schema-pin.json +14 -0
- package/catalog/plugin.oms.sig +19 -0
- package/catalog/sigma/npx-suspicious-child-process-linux.yml +54 -0
- package/catalog/sigma/npx-suspicious-child-process.yml +49 -0
- package/catalog/triage-rules.json +104 -0
- package/docs/assets/readme-header.svg +21 -0
- package/docs/designs/SITEMAP.md +14 -0
- package/docs/designs/coldpin.md +68 -0
- package/docs/fact-ledger.md +36 -0
- package/docs/faq.md +31 -0
- package/docs/getting_started.md +62 -0
- package/docs/harnesses.md +45 -0
- package/docs/privacy.md +27 -0
- package/docs/verification/skill-inspector-self-audit.md +33 -0
- package/examples/.coldpin.policy.example.json +27 -0
- package/examples/.npmrc.example +8 -0
- package/examples/mcp_config.private-registry.example.json +42 -0
- package/hooks.json +47 -0
- package/lib/analysis/parity-and-bisect.mjs +1371 -0
- package/lib/analysis/static-triage.mjs +4572 -0
- package/lib/cache/sqlite-store.mjs +251 -0
- package/lib/cli.mjs +427 -0
- package/lib/compliance/audit-log.mjs +357 -0
- package/lib/config/policy.mjs +780 -0
- package/lib/core/path-confinement.mjs +289 -0
- package/lib/core/workflows.mjs +1205 -0
- package/lib/discovery/slopsquat-guard.mjs +289 -0
- package/lib/hook.mjs +1038 -0
- package/lib/intel/registry-osv.mjs +1681 -0
- package/lib/mcp/report-store.mjs +175 -0
- package/lib/mcp/server.mjs +1387 -0
- package/lib/mcp/stdio-transport.mjs +289 -0
- package/lib/mcp/tool-catalog.mjs +273 -0
- package/lib/mcp/types.mjs +95 -0
- package/lib/mcp.mjs +19 -0
- package/lib/quarantine/safe-unpack.mjs +568 -0
- package/lib/types.mjs +383 -0
- package/lib/verdict/engine-and-formatters.mjs +895 -0
- package/mcp_config.json +21 -0
- package/package.json +71 -3
- package/plugin.json +54 -0
- package/rules/AGENTS.md +18 -0
- package/rules/coldpin-incident-checklist.md +18 -0
- package/rules/coldpin-manifest-guard.md +14 -0
- package/server.json +38 -0
- package/skills/coldpin/BENCHMARK.md +166 -0
- package/skills/coldpin/SKILL.md +110 -0
- package/skills/coldpin/TEST.md +13 -0
- package/skills/coldpin/agents/openai.yaml +6 -0
- package/skills/coldpin/assets/icon.svg +7 -0
- package/skills/coldpin/assets/runtime-contract.yaml +105 -0
- package/skills/coldpin/evals/evals.json +60 -0
- package/skills/coldpin/references/aspect-compliance-audit.md +27 -0
- package/skills/coldpin/references/aspect-discovery.md +26 -0
- package/skills/coldpin/references/aspect-forensics-rea.md +26 -0
- package/skills/coldpin/references/aspect-inspection.md +26 -0
- package/skills/coldpin/references/aspect-manifest-confusion.md +26 -0
- package/skills/coldpin/references/enterprise-policy-schema.md +65 -0
- package/skills/coldpin/references/evidence-card-spec.md +27 -0
- package/skills/coldpin/references/rea-integration-playbook.md +34 -0
- package/skills/coldpin/references/threat-taxonomy.md +35 -0
- package/skills/coldpin/skill-card.md +62 -0
- package/skills/coldpin/skill.oms.sig +19 -0
- package/skills/coldpin/test_scenarios.yaml +61 -0
- package/skills/coldpin-audit/SKILL.md +39 -0
- package/skills/coldpin-incident/SKILL.md +37 -0
- package/src/analysis/parity-and-bisect.ts +1368 -0
- package/src/analysis/static-triage.ts +4569 -0
- package/src/cache/sqlite-store.ts +248 -0
- package/src/cli.ts +424 -0
- package/src/compliance/audit-log.ts +354 -0
- package/src/config/policy.ts +777 -0
- package/src/core/path-confinement.ts +286 -0
- package/src/core/workflows.ts +1202 -0
- package/src/discovery/slopsquat-guard.ts +286 -0
- package/src/hook.ts +1035 -0
- package/src/intel/registry-osv.ts +1678 -0
- package/src/mcp/report-store.ts +172 -0
- package/src/mcp/server.ts +1384 -0
- package/src/mcp/stdio-transport.ts +286 -0
- package/src/mcp/tool-catalog.ts +270 -0
- package/src/mcp/types.ts +92 -0
- package/src/mcp.ts +16 -0
- package/src/quarantine/safe-unpack.ts +565 -0
- package/src/types.ts +380 -0
- package/src/verdict/engine-and-formatters.ts +892 -0
|
@@ -0,0 +1,1205 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import {
|
|
3
|
+
extractCommitDirectDependencies,
|
|
4
|
+
verifyTarballVsGitParity,
|
|
5
|
+
walkGitHistoryForCleanAnchor,
|
|
6
|
+
} from "../analysis/parity-and-bisect.mjs";
|
|
7
|
+
import { LIFECYCLE_HOOKS, RULESET_SHA256, triageUnpackedEntries } from "../analysis/static-triage.mjs";
|
|
8
|
+
import { buildFactCacheKey, } from "../cache/sqlite-store.mjs";
|
|
9
|
+
import {
|
|
10
|
+
appendAuditEventAsync,
|
|
11
|
+
MAX_AUDIT_LOG_BYTES,
|
|
12
|
+
verifyAuditLogContent,
|
|
13
|
+
} from "../compliance/audit-log.mjs";
|
|
14
|
+
import { routeRegistry, } from "../config/policy.mjs";
|
|
15
|
+
import { evaluateSlopsquatAndCooldown } from "../discovery/slopsquat-guard.mjs";
|
|
16
|
+
import {
|
|
17
|
+
auditDependencyConfusion,
|
|
18
|
+
discoverPackages,
|
|
19
|
+
fetchTarballBuffer,
|
|
20
|
+
fetchVersionMetadata,
|
|
21
|
+
inspectNpmAttestationBundle,
|
|
22
|
+
queryOsvAdvisories,
|
|
23
|
+
|
|
24
|
+
} from "../intel/registry-osv.mjs";
|
|
25
|
+
import { unpackTarballInMemory } from "../quarantine/safe-unpack.mjs";
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
import { assembleInspectionReport, verdictToExitCode } from "../verdict/engine-and-formatters.mjs";
|
|
43
|
+
import { openReadCappedFile, redactHomePath, resolveConfinedPath } from "./path-confinement.mjs";
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
export function parsePkgSpec(spec ) {
|
|
77
|
+
const trimmed = spec.trim();
|
|
78
|
+
const atIdx = trimmed.lastIndexOf("@");
|
|
79
|
+
if (atIdx > 0) {
|
|
80
|
+
return { name: trimmed.slice(0, atIdx), version: trimmed.slice(atIdx + 1) };
|
|
81
|
+
}
|
|
82
|
+
return { name: trimmed };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function emitTelemetry(ctx , event ) {
|
|
86
|
+
if (ctx.onTelemetry) {
|
|
87
|
+
ctx.onTelemetry({ request_id: ctx.requestId, ...event });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export async function runDiscover(
|
|
92
|
+
ctx ,
|
|
93
|
+
input
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
,
|
|
100
|
+
) {
|
|
101
|
+
const limit = input.limit ?? 10;
|
|
102
|
+
const res = await discoverPackages({
|
|
103
|
+
query: input.query,
|
|
104
|
+
org: input.org,
|
|
105
|
+
creator: input.creator,
|
|
106
|
+
category: input.category,
|
|
107
|
+
limit,
|
|
108
|
+
policy: ctx.policy,
|
|
109
|
+
npmrc: ctx.npmrc,
|
|
110
|
+
cache: ctx.cache,
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
if (ctx.auditLogPath) {
|
|
114
|
+
await appendAuditEventAsync(ctx.auditLogPath, {
|
|
115
|
+
request_id: ctx.requestId,
|
|
116
|
+
invoker_surface: ctx.invokerSurface,
|
|
117
|
+
...(ctx.mcpClientName ? { mcp_client_name: ctx.mcpClientName } : {}),
|
|
118
|
+
...(ctx.invokerSurface === "mcp" ? { tool_name: "discover_packages" } : {}),
|
|
119
|
+
action: "discover",
|
|
120
|
+
target: input.query || input.org || "*",
|
|
121
|
+
verdict: res.blocked_candidates.length > 0 && res.candidates.length === 0 ? "BLOCKED" : "OK",
|
|
122
|
+
policy_sha256: ctx.policy_sha256,
|
|
123
|
+
evidence_ids: [],
|
|
124
|
+
...(ctx.overrideTicketId ? { override_ticket_id: ctx.overrideTicketId } : {}),
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
emitTelemetry(ctx, {
|
|
129
|
+
stage: "discover_complete",
|
|
130
|
+
candidates: res.candidates.length,
|
|
131
|
+
blocked: res.blocked_candidates.length,
|
|
132
|
+
route: res.registry_route,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
const exitCode = res.blocked_candidates.length > 0 && res.candidates.length === 0 ? 2 : 0;
|
|
136
|
+
return { ok: true, report: res, exitCode };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export async function runInspect(
|
|
140
|
+
ctx ,
|
|
141
|
+
input
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
,
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
{
|
|
162
|
+
// LLM offline flag may only tighten false -> true
|
|
163
|
+
const policy =
|
|
164
|
+
input.offline && !ctx.policy.offline_mode ? { ...ctx.policy, offline_mode: true } : ctx.policy;
|
|
165
|
+
|
|
166
|
+
let resolvedTarballPath ;
|
|
167
|
+
if (input.tarballPath) {
|
|
168
|
+
if (ctx.enforceRootConfinement) {
|
|
169
|
+
const confined = resolveConfinedPath(input.tarballPath, ctx.workspaceRoots, {
|
|
170
|
+
requireRegularFile: true,
|
|
171
|
+
rejectSymlink: true,
|
|
172
|
+
label: "tarball_path",
|
|
173
|
+
});
|
|
174
|
+
if (!confined.ok) {
|
|
175
|
+
return { ok: false, error: confined.error, exitCode: 1, isOperationalError: true };
|
|
176
|
+
}
|
|
177
|
+
resolvedTarballPath = confined.canonicalPath;
|
|
178
|
+
} else {
|
|
179
|
+
resolvedTarballPath = resolve(input.tarballPath);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
let resolvedGitDir ;
|
|
184
|
+
if (input.localGitDir) {
|
|
185
|
+
if (ctx.enforceRootConfinement) {
|
|
186
|
+
const confined = resolveConfinedPath(input.localGitDir, ctx.workspaceRoots, {
|
|
187
|
+
requireDirectory: true,
|
|
188
|
+
label: "git_dir",
|
|
189
|
+
});
|
|
190
|
+
if (!confined.ok) {
|
|
191
|
+
return { ok: false, error: confined.error, exitCode: 1, isOperationalError: true };
|
|
192
|
+
}
|
|
193
|
+
resolvedGitDir = confined.canonicalPath;
|
|
194
|
+
} else {
|
|
195
|
+
resolvedGitDir = resolve(input.localGitDir);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const targetArg = input.packageSpec;
|
|
200
|
+
let tarballBuffer ;
|
|
201
|
+
let expectedIntegrity ;
|
|
202
|
+
let pkgName = targetArg ? parsePkgSpec(targetArg).name : "local-tarball";
|
|
203
|
+
let pkgVersion = targetArg ? (parsePkgSpec(targetArg).version ?? "0.0.0") : "0.0.0";
|
|
204
|
+
let orgOrScope = pkgName.startsWith("@") ? pkgName.slice(0, pkgName.indexOf("/")) : "unscoped";
|
|
205
|
+
let creator = "unknown";
|
|
206
|
+
let summary = "";
|
|
207
|
+
let license = "UNKNOWN";
|
|
208
|
+
let categoryTags = [];
|
|
209
|
+
let publishedAt ;
|
|
210
|
+
let provenanceAttested = false;
|
|
211
|
+
let provenanceStatus = "none";
|
|
212
|
+
let provenanceUrl ;
|
|
213
|
+
let provenanceInspection ;
|
|
214
|
+
let registryAuthToken ;
|
|
215
|
+
let registryAllowedOrigin ;
|
|
216
|
+
let gitHead ;
|
|
217
|
+
let repoUrl ;
|
|
218
|
+
let binNames = [];
|
|
219
|
+
const registryObservations = [];
|
|
220
|
+
const registryUnknowns = [];
|
|
221
|
+
let advisories = [];
|
|
222
|
+
let routeKind = "public";
|
|
223
|
+
let transitiveChecked = false;
|
|
224
|
+
|
|
225
|
+
if (resolvedTarballPath) {
|
|
226
|
+
try {
|
|
227
|
+
// Allow reading slightly above policy cap? No: enforce policy.max_compressed_bytes at FD read boundary in MCP;
|
|
228
|
+
// in CLI unpackTarballInMemory emits TARBALL_SIZE_LIMIT_EXCEEDED if > max_compressed_bytes, so cap FD at max(policy cap * 2, 50 MiB) for CLI and exact policy cap in MCP.
|
|
229
|
+
const readCap = ctx.enforceRootConfinement
|
|
230
|
+
? policy.max_compressed_bytes
|
|
231
|
+
: Math.max(policy.max_compressed_bytes * 2, 50 * 1024 * 1024);
|
|
232
|
+
tarballBuffer = await openReadCappedFile(resolvedTarballPath, readCap, ctx.enforceRootConfinement);
|
|
233
|
+
} catch (err) {
|
|
234
|
+
return {
|
|
235
|
+
ok: false,
|
|
236
|
+
error: `Failed reading tarball: ${err instanceof Error ? err.message : String(err)}`,
|
|
237
|
+
exitCode: 1,
|
|
238
|
+
isOperationalError: true,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
if (policy.offline_mode) {
|
|
242
|
+
registryUnknowns.push("OFFLINE_INTEL_SKIPPED: local tarball inspected in offline mode.");
|
|
243
|
+
}
|
|
244
|
+
} else {
|
|
245
|
+
if (!targetArg) {
|
|
246
|
+
return {
|
|
247
|
+
ok: false,
|
|
248
|
+
error: "inspect requires <pkg[@ver]> or --tarball <file.tgz>",
|
|
249
|
+
exitCode: 1,
|
|
250
|
+
isOperationalError: true,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
const spec = parsePkgSpec(targetArg);
|
|
254
|
+
pkgName = spec.name;
|
|
255
|
+
const routing = routeRegistry(pkgName, policy, ctx.npmrc);
|
|
256
|
+
if (
|
|
257
|
+
routing.route === "blocked_typo" ||
|
|
258
|
+
routing.route === "blocked_unconfigured" ||
|
|
259
|
+
routing.route === "blocked_secret"
|
|
260
|
+
) {
|
|
261
|
+
return { ok: false, error: `BLOCKED: ${routing.reason}`, exitCode: 3, isOperationalError: false };
|
|
262
|
+
}
|
|
263
|
+
routeKind = routing.route;
|
|
264
|
+
if (policy.offline_mode) {
|
|
265
|
+
return {
|
|
266
|
+
ok: false,
|
|
267
|
+
error: "Offline mode requires a local tarball_path (--tarball <file.tgz>).",
|
|
268
|
+
exitCode: 2,
|
|
269
|
+
isOperationalError: true,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const metaRes = await fetchVersionMetadata({
|
|
274
|
+
package_name: pkgName,
|
|
275
|
+
version: spec.version,
|
|
276
|
+
registry_url: routing.registry_url,
|
|
277
|
+
auth_token: routing.auth_token,
|
|
278
|
+
signal: ctx.signal,
|
|
279
|
+
});
|
|
280
|
+
if (!metaRes.ok || !metaRes.meta) {
|
|
281
|
+
return {
|
|
282
|
+
ok: false,
|
|
283
|
+
error: `Error fetching metadata for ${pkgName}: ${metaRes.error}`,
|
|
284
|
+
exitCode: 2,
|
|
285
|
+
isOperationalError: true,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
const meta = metaRes.meta;
|
|
289
|
+
pkgName = meta.name;
|
|
290
|
+
pkgVersion = meta.version;
|
|
291
|
+
orgOrScope = pkgName.startsWith("@") ? pkgName.slice(0, pkgName.indexOf("/")) : "unscoped";
|
|
292
|
+
creator = meta.creator;
|
|
293
|
+
summary = meta.description;
|
|
294
|
+
license = meta.license;
|
|
295
|
+
categoryTags = meta.keywords;
|
|
296
|
+
publishedAt = meta.published_at;
|
|
297
|
+
provenanceAttested = meta.provenance_attested;
|
|
298
|
+
provenanceStatus = meta.provenance_status;
|
|
299
|
+
provenanceUrl = meta.provenance_url;
|
|
300
|
+
gitHead = meta.git_head;
|
|
301
|
+
repoUrl = meta.repository_url;
|
|
302
|
+
expectedIntegrity = meta.integrity;
|
|
303
|
+
binNames = meta.bin_names;
|
|
304
|
+
|
|
305
|
+
registryObservations.push(
|
|
306
|
+
...evaluateSlopsquatAndCooldown({
|
|
307
|
+
package_name: pkgName,
|
|
308
|
+
created_at_iso: meta.created_at,
|
|
309
|
+
version_published_at_iso: meta.published_at,
|
|
310
|
+
has_lifecycle_scripts: Object.keys(meta.scripts).some((k) => LIFECYCLE_HOOKS.has(k.toLowerCase())),
|
|
311
|
+
policy,
|
|
312
|
+
}),
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
const directDepsEntries = Object.entries({ ...meta.dependencies, ...meta.optional_dependencies }).slice(0, 50);
|
|
316
|
+
const osvTargets = [{ name: pkgName, version: pkgVersion }];
|
|
317
|
+
let hasFloatingDirectDeps = false;
|
|
318
|
+
for (const [depName, depRange] of directDepsEntries) {
|
|
319
|
+
const trimmed = depRange.trim();
|
|
320
|
+
const isExact = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(trimmed);
|
|
321
|
+
if (isExact) {
|
|
322
|
+
osvTargets.push({ name: depName, version: trimmed });
|
|
323
|
+
} else {
|
|
324
|
+
hasFloatingDirectDeps = true;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if (hasFloatingDirectDeps) {
|
|
328
|
+
registryUnknowns.push(
|
|
329
|
+
"TRANSITIVE_FLOATING_RANGES_UNRESOLVED: declared dependencies include floating semver ranges that stock npx resolves live at runtime.",
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const osvRes = await queryOsvAdvisories({
|
|
334
|
+
packages: osvTargets,
|
|
335
|
+
policy,
|
|
336
|
+
cache: ctx.cache,
|
|
337
|
+
route: routeKind,
|
|
338
|
+
});
|
|
339
|
+
advisories = osvRes.advisories;
|
|
340
|
+
registryUnknowns.push(...osvRes.unknowns);
|
|
341
|
+
transitiveChecked = !policy.offline_mode && !hasFloatingDirectDeps && osvRes.unknowns.length === 0;
|
|
342
|
+
|
|
343
|
+
let allowedAuthOrigin ;
|
|
344
|
+
try {
|
|
345
|
+
allowedAuthOrigin = new URL(routing.registry_url).origin;
|
|
346
|
+
} catch {
|
|
347
|
+
allowedAuthOrigin = undefined;
|
|
348
|
+
}
|
|
349
|
+
registryAuthToken = routing.auth_token;
|
|
350
|
+
registryAllowedOrigin = allowedAuthOrigin;
|
|
351
|
+
const dl = await fetchTarballBuffer(
|
|
352
|
+
meta.tarball_url,
|
|
353
|
+
policy.max_compressed_bytes,
|
|
354
|
+
routing.auth_token,
|
|
355
|
+
20000,
|
|
356
|
+
allowedAuthOrigin,
|
|
357
|
+
ctx.signal,
|
|
358
|
+
);
|
|
359
|
+
if (!dl.ok || !dl.buffer) {
|
|
360
|
+
return {
|
|
361
|
+
ok: false,
|
|
362
|
+
error: `Error downloading tarball: ${dl.error}`,
|
|
363
|
+
exitCode: 2,
|
|
364
|
+
isOperationalError: true,
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
tarballBuffer = dl.buffer;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const unpack = await unpackTarballInMemory(tarballBuffer, policy, expectedIntegrity);
|
|
371
|
+
|
|
372
|
+
const triageCacheKey = buildFactCacheKey({
|
|
373
|
+
content_sha: unpack.tarball_sha512,
|
|
374
|
+
ruleset_sha256: RULESET_SHA256,
|
|
375
|
+
options: { max_single_file_bytes: policy.max_single_file_bytes },
|
|
376
|
+
});
|
|
377
|
+
let triage ;
|
|
378
|
+
let triageCacheHit = false;
|
|
379
|
+
const cachedTriage =
|
|
380
|
+
unpack.ok && !unpack.halted_by_cap ? ctx.cache.getArtifactFacts (triageCacheKey) : null;
|
|
381
|
+
if (cachedTriage) {
|
|
382
|
+
triage = cachedTriage;
|
|
383
|
+
triageCacheHit = true;
|
|
384
|
+
} else {
|
|
385
|
+
triage = triageUnpackedEntries(unpack.entries, policy);
|
|
386
|
+
if (unpack.ok && !unpack.halted_by_cap) {
|
|
387
|
+
ctx.cache.putArtifactFacts({
|
|
388
|
+
cache_key: triageCacheKey,
|
|
389
|
+
content_sha: unpack.tarball_sha512,
|
|
390
|
+
ruleset_sha256: RULESET_SHA256,
|
|
391
|
+
facts: triage,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
emitTelemetry(ctx, {
|
|
397
|
+
stage: "inspect_triage",
|
|
398
|
+
target: pkgName,
|
|
399
|
+
cache_hit: triageCacheHit,
|
|
400
|
+
files_inspected: triage.files_inspected,
|
|
401
|
+
observations: triage.observations.length,
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
if (triage.manifest && resolvedTarballPath) {
|
|
405
|
+
pkgName = triage.manifest.name ?? pkgName;
|
|
406
|
+
pkgVersion = triage.manifest.version ?? pkgVersion;
|
|
407
|
+
orgOrScope = pkgName.startsWith("@") ? pkgName.slice(0, pkgName.indexOf("/")) : "unscoped";
|
|
408
|
+
creator =
|
|
409
|
+
typeof triage.manifest.author === "string"
|
|
410
|
+
? triage.manifest.author
|
|
411
|
+
: triage.manifest.author?.name ?? creator;
|
|
412
|
+
summary = triage.manifest.description ?? summary;
|
|
413
|
+
license =
|
|
414
|
+
typeof triage.manifest.license === "string"
|
|
415
|
+
? triage.manifest.license
|
|
416
|
+
: triage.manifest.license?.type ?? license;
|
|
417
|
+
categoryTags = Array.isArray(triage.manifest.keywords) ? triage.manifest.keywords : [];
|
|
418
|
+
gitHead = triage.manifest.gitHead ?? gitHead;
|
|
419
|
+
if (typeof triage.manifest.bin === "string") binNames = [pkgName];
|
|
420
|
+
else if (triage.manifest.bin && typeof triage.manifest.bin === "object") {
|
|
421
|
+
binNames = Object.keys(triage.manifest.bin);
|
|
422
|
+
}
|
|
423
|
+
registryObservations.push(
|
|
424
|
+
...evaluateSlopsquatAndCooldown({
|
|
425
|
+
package_name: pkgName,
|
|
426
|
+
has_lifecycle_scripts: Object.keys(triage.manifest.scripts ?? {}).some((k) =>
|
|
427
|
+
LIFECYCLE_HOOKS.has(k.toLowerCase()),
|
|
428
|
+
),
|
|
429
|
+
policy,
|
|
430
|
+
}),
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
if (!resolvedTarballPath && !policy.offline_mode && provenanceUrl) {
|
|
435
|
+
provenanceInspection = await inspectNpmAttestationBundle({
|
|
436
|
+
attestationUrl: provenanceUrl,
|
|
437
|
+
expectedTarballSha512: unpack.tarball_sha512,
|
|
438
|
+
expectedPackageName: pkgName,
|
|
439
|
+
expectedPackageVersion: pkgVersion,
|
|
440
|
+
route: routeKind,
|
|
441
|
+
authToken: registryAuthToken,
|
|
442
|
+
allowedAuthOrigin: registryAllowedOrigin,
|
|
443
|
+
});
|
|
444
|
+
provenanceStatus = provenanceInspection.status;
|
|
445
|
+
provenanceAttested = provenanceInspection.provenance_attested;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
const parity = await verifyTarballVsGitParity({
|
|
449
|
+
tarballManifest: triage.manifest,
|
|
450
|
+
tarballEntries: unpack.entries,
|
|
451
|
+
provenanceAttested,
|
|
452
|
+
provenanceStatus,
|
|
453
|
+
provenanceUrl,
|
|
454
|
+
provenanceInspection,
|
|
455
|
+
gitHead,
|
|
456
|
+
repositoryUrl: repoUrl,
|
|
457
|
+
localGitDir: resolvedGitDir,
|
|
458
|
+
subdir: input.subdir,
|
|
459
|
+
signal: ctx.signal,
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
const lockfileCwd = ctx.enforceRootConfinement ? ctx.workspaceRoots[0] : ctx.cwd;
|
|
463
|
+
const report = assembleInspectionReport({
|
|
464
|
+
package_name: pkgName,
|
|
465
|
+
version: pkgVersion,
|
|
466
|
+
org_or_scope: orgOrScope,
|
|
467
|
+
creator,
|
|
468
|
+
category_tags: categoryTags,
|
|
469
|
+
summary,
|
|
470
|
+
license,
|
|
471
|
+
published_at: publishedAt,
|
|
472
|
+
bin_names: binNames,
|
|
473
|
+
unpack,
|
|
474
|
+
triage,
|
|
475
|
+
parity,
|
|
476
|
+
advisories,
|
|
477
|
+
registryObservations,
|
|
478
|
+
registryUnknowns,
|
|
479
|
+
policy,
|
|
480
|
+
host_capabilities: ctx.host_capabilities,
|
|
481
|
+
cwd: lockfileCwd,
|
|
482
|
+
transitive_checked: transitiveChecked,
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
if (ctx.auditLogPath) {
|
|
486
|
+
await appendAuditEventAsync(ctx.auditLogPath, {
|
|
487
|
+
request_id: ctx.requestId,
|
|
488
|
+
invoker_surface: ctx.invokerSurface,
|
|
489
|
+
...(ctx.mcpClientName ? { mcp_client_name: ctx.mcpClientName } : {}),
|
|
490
|
+
...(ctx.invokerSurface === "mcp" ? { tool_name: "inspect_package_artifact" } : {}),
|
|
491
|
+
action: "inspect",
|
|
492
|
+
target: report.title.package_name,
|
|
493
|
+
version: report.title.version,
|
|
494
|
+
commit_sha: report.title.commit_sha,
|
|
495
|
+
tarball_sha512: report.pinned_execution.tarball_sha512,
|
|
496
|
+
verdict: report.verdict,
|
|
497
|
+
policy_sha256: ctx.policy_sha256,
|
|
498
|
+
evidence_ids: report.evidence.map((e) => e.evidence_id),
|
|
499
|
+
...(ctx.overrideTicketId ? { override_ticket_id: ctx.overrideTicketId } : {}),
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
return {
|
|
504
|
+
ok: true,
|
|
505
|
+
report,
|
|
506
|
+
dependencies: triage.manifest?.dependencies,
|
|
507
|
+
exitCode: verdictToExitCode(report.verdict),
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
export async function runResolveAnchor(
|
|
512
|
+
ctx ,
|
|
513
|
+
input
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
,
|
|
520
|
+
)
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
{
|
|
524
|
+
if (!input.packageName && !input.localGitDir) {
|
|
525
|
+
return {
|
|
526
|
+
ok: false,
|
|
527
|
+
error: "resolve-anchor requires <pkg> and/or --git-dir <path>",
|
|
528
|
+
exitCode: 1,
|
|
529
|
+
isOperationalError: true,
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
const effectivePolicy = { ...ctx.policy };
|
|
534
|
+
if (input.window !== undefined) {
|
|
535
|
+
effectivePolicy.bisect_max_commits = ctx.enforceRootConfinement
|
|
536
|
+
? Math.min(input.window, ctx.policy.bisect_max_commits)
|
|
537
|
+
: input.window;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
let resolvedGitDir ;
|
|
541
|
+
if (input.localGitDir) {
|
|
542
|
+
if (ctx.enforceRootConfinement) {
|
|
543
|
+
const confined = resolveConfinedPath(input.localGitDir, ctx.workspaceRoots, {
|
|
544
|
+
requireDirectory: true,
|
|
545
|
+
label: "git_dir",
|
|
546
|
+
});
|
|
547
|
+
if (!confined.ok) {
|
|
548
|
+
return { ok: false, error: confined.error, exitCode: 1, isOperationalError: true };
|
|
549
|
+
}
|
|
550
|
+
resolvedGitDir = confined.canonicalPath;
|
|
551
|
+
} else {
|
|
552
|
+
resolvedGitDir = resolve(input.localGitDir);
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
const pkgName = input.packageName ? parsePkgSpec(input.packageName).name : "local-repo";
|
|
557
|
+
let repoTarget = resolvedGitDir ?? input.repoUrl;
|
|
558
|
+
const isLocalDir = Boolean(resolvedGitDir);
|
|
559
|
+
const releaseMap = new Map
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
();
|
|
563
|
+
const releaseDepsByCommit = new Map
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
();
|
|
567
|
+
const residualAdvisoriesByCommit = new Map ();
|
|
568
|
+
const compromisedVersions = new Set ();
|
|
569
|
+
let binName ;
|
|
570
|
+
let categoryKeywords = [];
|
|
571
|
+
let routeKind = "public";
|
|
572
|
+
let matchedPrivateScope = false;
|
|
573
|
+
|
|
574
|
+
if (!effectivePolicy.offline_mode && input.packageName) {
|
|
575
|
+
const routing = routeRegistry(pkgName, effectivePolicy, ctx.npmrc);
|
|
576
|
+
if (
|
|
577
|
+
routing.route === "blocked_typo" ||
|
|
578
|
+
routing.route === "blocked_unconfigured" ||
|
|
579
|
+
routing.route === "blocked_secret"
|
|
580
|
+
) {
|
|
581
|
+
return { ok: false, error: `BLOCKED: ${routing.reason}`, exitCode: 3, isOperationalError: false };
|
|
582
|
+
}
|
|
583
|
+
routeKind = routing.route;
|
|
584
|
+
matchedPrivateScope = Boolean(routing.matched_scope_or_prefix);
|
|
585
|
+
|
|
586
|
+
const metaRes = await fetchVersionMetadata({
|
|
587
|
+
package_name: pkgName,
|
|
588
|
+
registry_url: routing.registry_url,
|
|
589
|
+
auth_token: routing.auth_token,
|
|
590
|
+
signal: ctx.signal,
|
|
591
|
+
});
|
|
592
|
+
if (metaRes.ok && metaRes.packument) {
|
|
593
|
+
binName = metaRes.packument.latest_meta.bin_names[0];
|
|
594
|
+
categoryKeywords = metaRes.packument.latest_meta.keywords;
|
|
595
|
+
const releaseOsvTargets = [];
|
|
596
|
+
const depOwnerCommits = new Map ();
|
|
597
|
+
|
|
598
|
+
for (const v of metaRes.packument.versions_desc.slice(0, 25)) {
|
|
599
|
+
releaseOsvTargets.push({ name: pkgName, version: v.version });
|
|
600
|
+
if (v.git_head) {
|
|
601
|
+
releaseMap.set(v.git_head, {
|
|
602
|
+
version: v.version,
|
|
603
|
+
tarball_sha512: v.integrity,
|
|
604
|
+
provenance_attested: v.provenance_attested,
|
|
605
|
+
provenance_status: v.provenance_status,
|
|
606
|
+
});
|
|
607
|
+
const exactDeps = [];
|
|
608
|
+
let hasFloating = false;
|
|
609
|
+
for (const [depName, depRange] of Object.entries({ ...v.dependencies, ...v.optional_dependencies }).slice(
|
|
610
|
+
0,
|
|
611
|
+
30,
|
|
612
|
+
)) {
|
|
613
|
+
const trimmed = depRange.trim();
|
|
614
|
+
if (/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(trimmed)) {
|
|
615
|
+
exactDeps.push({ name: depName, version: trimmed });
|
|
616
|
+
const depKey = `${depName}@${trimmed}`;
|
|
617
|
+
const owners = depOwnerCommits.get(depKey) ?? [];
|
|
618
|
+
owners.push(v.git_head);
|
|
619
|
+
depOwnerCommits.set(depKey, owners);
|
|
620
|
+
} else {
|
|
621
|
+
hasFloating = true;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
releaseDepsByCommit.set(v.git_head, { exactDeps, hasFloating });
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
const seenTargets = new Set(releaseOsvTargets.map((t) => `${t.name}@${t.version}`));
|
|
629
|
+
for (const depKey of Array.from(depOwnerCommits.keys()).slice(0, 60)) {
|
|
630
|
+
if (!seenTargets.has(depKey)) {
|
|
631
|
+
const atIdx = depKey.lastIndexOf("@");
|
|
632
|
+
if (atIdx > 0) {
|
|
633
|
+
releaseOsvTargets.push({ name: depKey.slice(0, atIdx), version: depKey.slice(atIdx + 1) });
|
|
634
|
+
seenTargets.add(depKey);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
const relOsv = await queryOsvAdvisories({
|
|
640
|
+
packages: releaseOsvTargets,
|
|
641
|
+
policy: effectivePolicy,
|
|
642
|
+
cache: ctx.cache,
|
|
643
|
+
route: routeKind,
|
|
644
|
+
});
|
|
645
|
+
for (const adv of relOsv.advisories) {
|
|
646
|
+
if (adv.package_name === pkgName) {
|
|
647
|
+
if (adv.is_malicious && adv.affected_version) {
|
|
648
|
+
compromisedVersions.add(adv.affected_version);
|
|
649
|
+
}
|
|
650
|
+
if (adv.affected_version) {
|
|
651
|
+
for (const [sha, rel] of releaseMap.entries()) {
|
|
652
|
+
if (rel.version === adv.affected_version) {
|
|
653
|
+
const list = residualAdvisoriesByCommit.get(sha) ?? [];
|
|
654
|
+
list.push(adv);
|
|
655
|
+
residualAdvisoriesByCommit.set(sha, list);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
} else if (adv.affected_version) {
|
|
660
|
+
const owners = depOwnerCommits.get(`${adv.package_name}@${adv.affected_version}`) ?? [];
|
|
661
|
+
for (const sha of owners) {
|
|
662
|
+
const list = residualAdvisoriesByCommit.get(sha) ?? [];
|
|
663
|
+
list.push(adv);
|
|
664
|
+
residualAdvisoriesByCommit.set(sha, list);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
if (!repoTarget && metaRes.packument.latest_meta.repository_url) {
|
|
670
|
+
repoTarget = metaRes.packument.latest_meta.repository_url
|
|
671
|
+
.replace(/^git\+/, "")
|
|
672
|
+
.replace(/^git:\/\//, "https://");
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
if (!repoTarget) {
|
|
678
|
+
return {
|
|
679
|
+
ok: false,
|
|
680
|
+
error: "Could not resolve repository URL; supply git_dir or repo_url.",
|
|
681
|
+
exitCode: 2,
|
|
682
|
+
isOperationalError: true,
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
if (isLocalDir && !binName) {
|
|
687
|
+
const localMeta = await extractCommitDirectDependencies(repoTarget, "HEAD", input.subdir, ctx.signal);
|
|
688
|
+
binName = localMeta.binName;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
let decision = await walkGitHistoryForCleanAnchor({
|
|
692
|
+
package_name: pkgName,
|
|
693
|
+
repoUrlOrLocalPath: repoTarget,
|
|
694
|
+
isLocalDir,
|
|
695
|
+
subdir: input.subdir,
|
|
696
|
+
binName,
|
|
697
|
+
policy: effectivePolicy,
|
|
698
|
+
cache: ctx.cache,
|
|
699
|
+
releaseTarballByCommit: releaseMap,
|
|
700
|
+
residualAdvisoriesByCommit,
|
|
701
|
+
compromisedVersions,
|
|
702
|
+
signal: ctx.signal,
|
|
703
|
+
});
|
|
704
|
+
|
|
705
|
+
const selectedAnchorSha = decision.safe_anchor?.commit_sha;
|
|
706
|
+
if (selectedAnchorSha) {
|
|
707
|
+
const relDeps = releaseDepsByCommit.get(selectedAnchorSha);
|
|
708
|
+
const localDeps = isLocalDir
|
|
709
|
+
? await extractCommitDirectDependencies(repoTarget, selectedAnchorSha, input.subdir, ctx.signal)
|
|
710
|
+
: null;
|
|
711
|
+
if (relDeps?.hasFloating || localDeps?.deps.some((d) => d.isFloating)) {
|
|
712
|
+
decision.unknowns.push(
|
|
713
|
+
`TRANSITIVE_FLOATING_RANGES_UNRESOLVED: safe anchor commit ${selectedAnchorSha.slice(0, 10)} declares floating dependency ranges that stock npx resolves live.`,
|
|
714
|
+
);
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
if (
|
|
719
|
+
!effectivePolicy.offline_mode &&
|
|
720
|
+
!matchedPrivateScope &&
|
|
721
|
+
decision.alternatives.length === 0 &&
|
|
722
|
+
decision.recommended_action !== "USE_PINNED_LATEST" &&
|
|
723
|
+
decision.recommended_action !== "ROLLBACK_TO_SAFE_ANCHOR"
|
|
724
|
+
) {
|
|
725
|
+
const altQuery = categoryKeywords[0] ?? pkgName.replace(/^@[^/]+\//, "").split(/[-_]/)[0] ?? "";
|
|
726
|
+
const compromisedOrg = pkgName.startsWith("@") ? pkgName.slice(0, pkgName.indexOf("/")).toLowerCase() : "";
|
|
727
|
+
if (altQuery.length >= 3) {
|
|
728
|
+
const altDiscovery = await discoverPackages({
|
|
729
|
+
query: altQuery,
|
|
730
|
+
category: categoryKeywords[0],
|
|
731
|
+
limit: 6,
|
|
732
|
+
policy: effectivePolicy,
|
|
733
|
+
npmrc: ctx.npmrc,
|
|
734
|
+
cache: ctx.cache,
|
|
735
|
+
});
|
|
736
|
+
const vettedAlts = altDiscovery.candidates
|
|
737
|
+
.filter(
|
|
738
|
+
(c) =>
|
|
739
|
+
c.package_name !== pkgName &&
|
|
740
|
+
(!compromisedOrg || c.org_or_scope.toLowerCase() !== compromisedOrg) &&
|
|
741
|
+
c.verdict_preview !== "MALICIOUS_INDICATORS_FOUND" &&
|
|
742
|
+
c.squat_warnings.length === 0,
|
|
743
|
+
)
|
|
744
|
+
.slice(0, 3)
|
|
745
|
+
.map((c) => ({
|
|
746
|
+
package_name: c.package_name,
|
|
747
|
+
version: c.version,
|
|
748
|
+
org_or_scope: c.org_or_scope,
|
|
749
|
+
verdict: c.verdict_preview,
|
|
750
|
+
pinned_command: null,
|
|
751
|
+
reason: `Candidate alternative discovered via '${altQuery}'; run 'inspect ${c.package_name}@${c.version}' before execution.`,
|
|
752
|
+
}));
|
|
753
|
+
if (vettedAlts.length > 0) {
|
|
754
|
+
decision = {
|
|
755
|
+
...decision,
|
|
756
|
+
alternatives: vettedAlts,
|
|
757
|
+
recommended_action: "SWITCH_TO_ALTERNATIVE",
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
if (ctx.auditLogPath) {
|
|
764
|
+
await appendAuditEventAsync(ctx.auditLogPath, {
|
|
765
|
+
request_id: ctx.requestId,
|
|
766
|
+
invoker_surface: ctx.invokerSurface,
|
|
767
|
+
...(ctx.mcpClientName ? { mcp_client_name: ctx.mcpClientName } : {}),
|
|
768
|
+
...(ctx.invokerSurface === "mcp" ? { tool_name: "resolve_safe_anchor" } : {}),
|
|
769
|
+
action: "resolve-anchor",
|
|
770
|
+
target: decision.package_name,
|
|
771
|
+
version: decision.safe_anchor?.version,
|
|
772
|
+
commit_sha: decision.safe_anchor?.commit_sha,
|
|
773
|
+
tarball_sha512: decision.safe_anchor?.tarball_sha512,
|
|
774
|
+
verdict: decision.recommended_action,
|
|
775
|
+
policy_sha256: ctx.policy_sha256,
|
|
776
|
+
evidence_ids: decision.evidence.map((e) => e.evidence_id),
|
|
777
|
+
...(ctx.overrideTicketId ? { override_ticket_id: ctx.overrideTicketId } : {}),
|
|
778
|
+
});
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
const exitCode =
|
|
782
|
+
decision.recommended_action === "USE_PINNED_LATEST" ||
|
|
783
|
+
decision.recommended_action === "ROLLBACK_TO_SAFE_ANCHOR"
|
|
784
|
+
? 0
|
|
785
|
+
: decision.recommended_action === "BLOCK_NO_SAFE_PATH"
|
|
786
|
+
? 3
|
|
787
|
+
: 2;
|
|
788
|
+
|
|
789
|
+
return { ok: true, decision, exitCode };
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
export async function runVerifyConfusion(
|
|
793
|
+
ctx ,
|
|
794
|
+
input
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
,
|
|
799
|
+
)
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
{
|
|
803
|
+
let resolvedManifestPath ;
|
|
804
|
+
let effectiveCwd = ctx.cwd ?? ctx.workspaceRoots[0] ?? "";
|
|
805
|
+
|
|
806
|
+
if (ctx.enforceRootConfinement) {
|
|
807
|
+
const rawTarget = input.manifestPath ?? (ctx.workspaceRoots[0] ? resolve(ctx.workspaceRoots[0], "package.json") : "");
|
|
808
|
+
if (!rawTarget) {
|
|
809
|
+
return {
|
|
810
|
+
ok: false,
|
|
811
|
+
error: "Local manifest_path access denied: no authorized workspace roots configured.",
|
|
812
|
+
exitCode: 1,
|
|
813
|
+
isOperationalError: true,
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
const confined = resolveConfinedPath(rawTarget, ctx.workspaceRoots, {
|
|
817
|
+
requireRegularFile: true,
|
|
818
|
+
rejectSymlink: true,
|
|
819
|
+
label: "manifest_path",
|
|
820
|
+
});
|
|
821
|
+
if (!confined.ok) {
|
|
822
|
+
return { ok: false, error: confined.error, exitCode: 1, isOperationalError: true };
|
|
823
|
+
}
|
|
824
|
+
resolvedManifestPath = confined.canonicalPath;
|
|
825
|
+
effectiveCwd = ctx.workspaceRoots[0] ;
|
|
826
|
+
} else {
|
|
827
|
+
resolvedManifestPath = input.manifestPath;
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
const checkPublicEffective = Boolean(input.checkPublic && input.allowPublicProbe);
|
|
831
|
+
const report = await auditDependencyConfusion({
|
|
832
|
+
cwd: effectiveCwd,
|
|
833
|
+
manifestPath: resolvedManifestPath,
|
|
834
|
+
policy: ctx.policy,
|
|
835
|
+
checkPublicClaimability: checkPublicEffective,
|
|
836
|
+
redactHomePaths: ctx.invokerSurface === "mcp",
|
|
837
|
+
});
|
|
838
|
+
|
|
839
|
+
if (input.checkPublic && !input.allowPublicProbe) {
|
|
840
|
+
report.unknowns.push(
|
|
841
|
+
"PUBLIC_CONFUSION_PROBE_SUPPRESSED: check_public requires operator env COLDPIN_ALLOW_PUBLIC_CONFUSION_PROBE=1.",
|
|
842
|
+
);
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
if (ctx.auditLogPath) {
|
|
846
|
+
await appendAuditEventAsync(ctx.auditLogPath, {
|
|
847
|
+
request_id: ctx.requestId,
|
|
848
|
+
invoker_surface: ctx.invokerSurface,
|
|
849
|
+
...(ctx.mcpClientName ? { mcp_client_name: ctx.mcpClientName } : {}),
|
|
850
|
+
...(ctx.invokerSurface === "mcp" ? { tool_name: "verify_confusion" } : {}),
|
|
851
|
+
action: "verify-confusion",
|
|
852
|
+
target: report.manifest_path,
|
|
853
|
+
verdict: report.passed ? "PASSED" : "CONFUSION_RISK_FOUND",
|
|
854
|
+
policy_sha256: ctx.policy_sha256,
|
|
855
|
+
evidence_ids: [],
|
|
856
|
+
...(ctx.overrideTicketId ? { override_ticket_id: ctx.overrideTicketId } : {}),
|
|
857
|
+
});
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
return { ok: true, report, exitCode: report.passed ? 0 : 3 };
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
export async function runAuditVerify(
|
|
864
|
+
ctx ,
|
|
865
|
+
input ,
|
|
866
|
+
)
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
{
|
|
874
|
+
let targetPath ;
|
|
875
|
+
if (ctx.enforceRootConfinement) {
|
|
876
|
+
const confined = resolveConfinedPath(input.auditLogPath, ctx.workspaceRoots, {
|
|
877
|
+
requireRegularFile: true,
|
|
878
|
+
rejectSymlink: true,
|
|
879
|
+
label: "audit_log_path",
|
|
880
|
+
});
|
|
881
|
+
if (!confined.ok) {
|
|
882
|
+
return { ok: false, error: confined.error, exitCode: 1, isOperationalError: true };
|
|
883
|
+
}
|
|
884
|
+
targetPath = confined.canonicalPath;
|
|
885
|
+
} else {
|
|
886
|
+
targetPath = resolve(input.auditLogPath);
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
let rawContent ;
|
|
890
|
+
try {
|
|
891
|
+
const buf = await openReadCappedFile(targetPath, MAX_AUDIT_LOG_BYTES, ctx.enforceRootConfinement);
|
|
892
|
+
rawContent = buf.toString("utf8");
|
|
893
|
+
} catch (err) {
|
|
894
|
+
return {
|
|
895
|
+
ok: false,
|
|
896
|
+
error: `Audit log unreadable or exceeds size cap: ${err instanceof Error ? err.message : String(err)}`,
|
|
897
|
+
exitCode: 3,
|
|
898
|
+
isOperationalError: true,
|
|
899
|
+
};
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
const result = verifyAuditLogContent(rawContent);
|
|
903
|
+
return { ok: true, result, exitCode: result.valid ? 0 : 3 };
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
const MAX_BATCH_DEPS_CAP = 50;
|
|
907
|
+
const MAX_BATCH_MANIFEST_BYTES = 2 * 1024 * 1024;
|
|
908
|
+
|
|
909
|
+
/**
|
|
910
|
+
* Deterministic concurrent batch dependency triage (`v1.4.0`, `audit_manifest_batch`):
|
|
911
|
+
* Screens up to 50 direct dependencies from a root-confined `package.json` locally at $0/mo
|
|
912
|
+
* using private-scope routing, 7-primitive squat screening, exact-range checks, and batched
|
|
913
|
+
* OSV/OpenSSF `MAL-*` lookups (reusing Split-Plane SQLite cache & SingleFlight).
|
|
914
|
+
*/
|
|
915
|
+
export async function runAuditManifestBatch(
|
|
916
|
+
ctx ,
|
|
917
|
+
input
|
|
918
|
+
|
|
919
|
+
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
,
|
|
923
|
+
)
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
{
|
|
927
|
+
const policy =
|
|
928
|
+
input.offline && !ctx.policy.offline_mode ? { ...ctx.policy, offline_mode: true } : ctx.policy;
|
|
929
|
+
|
|
930
|
+
let resolvedManifestPath ;
|
|
931
|
+
if (ctx.enforceRootConfinement) {
|
|
932
|
+
const rawTarget = input.manifestPath ?? (ctx.workspaceRoots[0] ? resolve(ctx.workspaceRoots[0], "package.json") : "");
|
|
933
|
+
if (!rawTarget) {
|
|
934
|
+
return {
|
|
935
|
+
ok: false,
|
|
936
|
+
error: "Local manifest_path access denied: no authorized workspace roots configured.",
|
|
937
|
+
exitCode: 1,
|
|
938
|
+
isOperationalError: true,
|
|
939
|
+
};
|
|
940
|
+
}
|
|
941
|
+
const confined = resolveConfinedPath(rawTarget, ctx.workspaceRoots, {
|
|
942
|
+
requireRegularFile: true,
|
|
943
|
+
rejectSymlink: true,
|
|
944
|
+
label: "manifest_path",
|
|
945
|
+
});
|
|
946
|
+
if (!confined.ok) {
|
|
947
|
+
return { ok: false, error: confined.error, exitCode: 1, isOperationalError: true };
|
|
948
|
+
}
|
|
949
|
+
resolvedManifestPath = confined.canonicalPath;
|
|
950
|
+
} else {
|
|
951
|
+
resolvedManifestPath = resolve(input.manifestPath ?? resolve(ctx.cwd ?? process.cwd(), "package.json"));
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
let pkg ;
|
|
955
|
+
try {
|
|
956
|
+
const rawBuf = await openReadCappedFile(resolvedManifestPath, MAX_BATCH_MANIFEST_BYTES, ctx.enforceRootConfinement);
|
|
957
|
+
pkg = JSON.parse(rawBuf.toString("utf8")) ;
|
|
958
|
+
} catch (err) {
|
|
959
|
+
return {
|
|
960
|
+
ok: false,
|
|
961
|
+
error: `Failed reading manifest: ${err instanceof Error ? err.message : String(err)}`,
|
|
962
|
+
exitCode: 1,
|
|
963
|
+
isOperationalError: true,
|
|
964
|
+
};
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
const combined = {
|
|
968
|
+
...(pkg.dependencies ?? {}),
|
|
969
|
+
...(pkg.optionalDependencies ?? {}),
|
|
970
|
+
...(input.includeDev ? (pkg.devDependencies ?? {}) : {}),
|
|
971
|
+
};
|
|
972
|
+
const allEntries = Object.entries(combined).sort(([a], [b]) => a.localeCompare(b));
|
|
973
|
+
const cap = Math.min(Math.max(Math.floor(input.maxDeps ?? MAX_BATCH_DEPS_CAP), 1), MAX_BATCH_DEPS_CAP);
|
|
974
|
+
const selected = allEntries.slice(0, cap);
|
|
975
|
+
const truncatedAtCap = allEntries.length > cap;
|
|
976
|
+
|
|
977
|
+
const unknowns = [];
|
|
978
|
+
if (truncatedAtCap) {
|
|
979
|
+
unknowns.push(`BATCH_CAP_REACHED: evaluated first ${cap} of ${allEntries.length} declared dependencies.`);
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
const exactVerRe = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/;
|
|
983
|
+
const publicOsvTargets = [];
|
|
984
|
+
const privateOsvTargets = [];
|
|
985
|
+
|
|
986
|
+
|
|
987
|
+
|
|
988
|
+
|
|
989
|
+
|
|
990
|
+
|
|
991
|
+
|
|
992
|
+
|
|
993
|
+
|
|
994
|
+
|
|
995
|
+
const items = [];
|
|
996
|
+
for (const [depName, rawRange] of selected) {
|
|
997
|
+
const range = typeof rawRange === "string" ? rawRange.trim() : "";
|
|
998
|
+
const exactVer = exactVerRe.test(range) ? range : undefined;
|
|
999
|
+
const routing = routeRegistry(depName, policy, ctx.npmrc);
|
|
1000
|
+
const reasons = [];
|
|
1001
|
+
let severityRank = 0;
|
|
1002
|
+
|
|
1003
|
+
if (
|
|
1004
|
+
routing.route === "blocked_typo" ||
|
|
1005
|
+
routing.route === "blocked_unconfigured" ||
|
|
1006
|
+
routing.route === "blocked_secret"
|
|
1007
|
+
) {
|
|
1008
|
+
severityRank = 2;
|
|
1009
|
+
reasons.push(routing.reason);
|
|
1010
|
+
} else {
|
|
1011
|
+
const scope = depName.startsWith("@") ? depName.slice(0, depName.indexOf("/")).toLowerCase() : "unscoped";
|
|
1012
|
+
if (policy.denied_orgs.includes(scope)) {
|
|
1013
|
+
severityRank = 2;
|
|
1014
|
+
reasons.push(`Org/scope '${scope}' is denied by policy.`);
|
|
1015
|
+
} else if (policy.allowed_orgs.length > 0 && !policy.allowed_orgs.includes(scope)) {
|
|
1016
|
+
severityRank = Math.max(severityRank, 1);
|
|
1017
|
+
reasons.push(`Org/scope '${scope}' is not on policy allowed_orgs list.`);
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
const squatObs = evaluateSlopsquatAndCooldown({
|
|
1021
|
+
package_name: depName,
|
|
1022
|
+
has_lifecycle_scripts: false,
|
|
1023
|
+
policy,
|
|
1024
|
+
});
|
|
1025
|
+
for (const obs of squatObs) {
|
|
1026
|
+
if (obs.severity === "CRITICAL") severityRank = 2;
|
|
1027
|
+
else if (obs.severity === "HIGH" || obs.severity === "MEDIUM") severityRank = Math.max(severityRank, 1);
|
|
1028
|
+
reasons.push(`${obs.code}: ${obs.detail}`);
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
if (severityRank < 2) {
|
|
1032
|
+
if (routing.route === "public") {
|
|
1033
|
+
publicOsvTargets.push({ name: depName, ...(exactVer ? { version: exactVer } : {}) });
|
|
1034
|
+
} else if (routing.route === "private") {
|
|
1035
|
+
privateOsvTargets.push({ name: depName, ...(exactVer ? { version: exactVer } : {}) });
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
items.push({ name: depName, range, exactVer, route: routing.route, reasons, severityRank });
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
// Batch OSV queries separately by route (private scopes NEVER leak to public api.osv.dev)
|
|
1044
|
+
const advisoriesByPkg = new Map ();
|
|
1045
|
+
const recordAdvisories = (advs ) => {
|
|
1046
|
+
for (const adv of advs) {
|
|
1047
|
+
const list = advisoriesByPkg.get(adv.package_name) ?? [];
|
|
1048
|
+
list.push(adv);
|
|
1049
|
+
advisoriesByPkg.set(adv.package_name, list);
|
|
1050
|
+
}
|
|
1051
|
+
};
|
|
1052
|
+
|
|
1053
|
+
let pubOsvUnavailable = policy.offline_mode;
|
|
1054
|
+
let privOsvUnavailable = policy.offline_mode;
|
|
1055
|
+
|
|
1056
|
+
if (publicOsvTargets.length > 0) {
|
|
1057
|
+
const pubOsv = await queryOsvAdvisories({
|
|
1058
|
+
packages: publicOsvTargets,
|
|
1059
|
+
policy,
|
|
1060
|
+
cache: ctx.cache,
|
|
1061
|
+
route: "public",
|
|
1062
|
+
});
|
|
1063
|
+
recordAdvisories(pubOsv.advisories);
|
|
1064
|
+
unknowns.push(...pubOsv.unknowns);
|
|
1065
|
+
if (pubOsv.unknowns.some((u) => u.startsWith("OSV_API_") || u.startsWith("OFFLINE_INTEL_"))) {
|
|
1066
|
+
pubOsvUnavailable = true;
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
if (privateOsvTargets.length > 0) {
|
|
1070
|
+
const privOsv = await queryOsvAdvisories({
|
|
1071
|
+
packages: privateOsvTargets,
|
|
1072
|
+
policy,
|
|
1073
|
+
cache: ctx.cache,
|
|
1074
|
+
route: "private",
|
|
1075
|
+
});
|
|
1076
|
+
recordAdvisories(privOsv.advisories);
|
|
1077
|
+
unknowns.push(...privOsv.unknowns);
|
|
1078
|
+
if (
|
|
1079
|
+
privOsv.unknowns.some(
|
|
1080
|
+
(u) => u.startsWith("PRIVATE_SCOPE_OSV_") || u.startsWith("OSV_API_") || u.startsWith("OFFLINE_INTEL_"),
|
|
1081
|
+
)
|
|
1082
|
+
) {
|
|
1083
|
+
privOsvUnavailable = true;
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
const flagged = [];
|
|
1088
|
+
const counts = {
|
|
1089
|
+
clean_screened: 0,
|
|
1090
|
+
flagged_suspicious: 0,
|
|
1091
|
+
malicious_or_blocked: 0,
|
|
1092
|
+
unverifiable_offline: 0,
|
|
1093
|
+
};
|
|
1094
|
+
|
|
1095
|
+
for (const item of items) {
|
|
1096
|
+
const advs = advisoriesByPkg.get(item.name) ?? [];
|
|
1097
|
+
const advisoryIds = [];
|
|
1098
|
+
for (const adv of advs) {
|
|
1099
|
+
advisoryIds.push(adv.id);
|
|
1100
|
+
if (adv.is_malicious) {
|
|
1101
|
+
item.severityRank = 2;
|
|
1102
|
+
item.reasons.push(`OSV_MALICIOUS (${adv.id}): ${adv.summary}`);
|
|
1103
|
+
} else if (
|
|
1104
|
+
(policy.block_on_kev && adv.kev) ||
|
|
1105
|
+
(policy.block_on_critical_cve && adv.severity === "CRITICAL")
|
|
1106
|
+
) {
|
|
1107
|
+
item.severityRank = 2;
|
|
1108
|
+
item.reasons.push(`OSV_BLOCKING_ADVISORY (${adv.id}, ${adv.severity}${adv.kev ? "/KEV" : ""}): ${adv.summary}`);
|
|
1109
|
+
} else if (policy.block_on_high_cve && adv.severity === "HIGH") {
|
|
1110
|
+
item.severityRank = Math.max(item.severityRank, 1);
|
|
1111
|
+
item.reasons.push(`OSV_HIGH_ADVISORY (${adv.id}): ${adv.summary}`);
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
let status ;
|
|
1116
|
+
if (item.severityRank >= 2) {
|
|
1117
|
+
status = "MALICIOUS_OR_BLOCKED";
|
|
1118
|
+
counts.malicious_or_blocked++;
|
|
1119
|
+
flagged.push({
|
|
1120
|
+
package_name: item.name,
|
|
1121
|
+
declared_range: item.range,
|
|
1122
|
+
route: item.route,
|
|
1123
|
+
status,
|
|
1124
|
+
reasons: item.reasons,
|
|
1125
|
+
advisory_ids: advisoryIds,
|
|
1126
|
+
});
|
|
1127
|
+
} else if (item.severityRank === 1) {
|
|
1128
|
+
status = "FLAGGED_SUSPICIOUS";
|
|
1129
|
+
counts.flagged_suspicious++;
|
|
1130
|
+
flagged.push({
|
|
1131
|
+
package_name: item.name,
|
|
1132
|
+
declared_range: item.range,
|
|
1133
|
+
route: item.route,
|
|
1134
|
+
status,
|
|
1135
|
+
reasons: item.reasons,
|
|
1136
|
+
advisory_ids: advisoryIds,
|
|
1137
|
+
});
|
|
1138
|
+
} else if ((item.route === "public" && pubOsvUnavailable) || (item.route === "private" && privOsvUnavailable)) {
|
|
1139
|
+
status = "UNVERIFIABLE_OFFLINE";
|
|
1140
|
+
counts.unverifiable_offline++;
|
|
1141
|
+
} else {
|
|
1142
|
+
status = "CLEAN_SCREENED";
|
|
1143
|
+
counts.clean_screened++;
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
// Prioritize MALICIOUS_OR_BLOCKED ahead of FLAGGED_SUSPICIOUS so compact summaries always surface critical findings first
|
|
1148
|
+
flagged.sort((a, b) => {
|
|
1149
|
+
if (a.status !== b.status) return a.status === "MALICIOUS_OR_BLOCKED" ? -1 : 1;
|
|
1150
|
+
return a.package_name.localeCompare(b.package_name);
|
|
1151
|
+
});
|
|
1152
|
+
|
|
1153
|
+
const displayManifestPath = ctx.invokerSurface === "mcp" ? redactHomePath(resolvedManifestPath) : resolvedManifestPath;
|
|
1154
|
+
const report = {
|
|
1155
|
+
schema_version: "1",
|
|
1156
|
+
manifest_path: displayManifestPath,
|
|
1157
|
+
total_declared: allEntries.length,
|
|
1158
|
+
evaluated_count: selected.length,
|
|
1159
|
+
truncated_at_cap: truncatedAtCap,
|
|
1160
|
+
offline_mode: policy.offline_mode,
|
|
1161
|
+
summary_counts: counts,
|
|
1162
|
+
flagged,
|
|
1163
|
+
unknowns: Array.from(new Set(unknowns)),
|
|
1164
|
+
};
|
|
1165
|
+
|
|
1166
|
+
if (ctx.auditLogPath) {
|
|
1167
|
+
await appendAuditEventAsync(ctx.auditLogPath, {
|
|
1168
|
+
request_id: ctx.requestId,
|
|
1169
|
+
invoker_surface: ctx.invokerSurface,
|
|
1170
|
+
...(ctx.mcpClientName ? { mcp_client_name: ctx.mcpClientName } : {}),
|
|
1171
|
+
...(ctx.invokerSurface === "mcp" ? { tool_name: "audit_manifest_batch" } : {}),
|
|
1172
|
+
action: "audit-manifest-batch",
|
|
1173
|
+
target: displayManifestPath,
|
|
1174
|
+
verdict:
|
|
1175
|
+
counts.malicious_or_blocked > 0
|
|
1176
|
+
? "MALICIOUS_INDICATORS_FOUND"
|
|
1177
|
+
: counts.flagged_suspicious > 0
|
|
1178
|
+
? "QUARANTINE_SUSPICIOUS"
|
|
1179
|
+
: counts.unverifiable_offline > 0
|
|
1180
|
+
? "UNVERIFIABLE_UNKNOWN"
|
|
1181
|
+
: "VERIFIED_CLEAN_WITHIN_SCOPE",
|
|
1182
|
+
policy_sha256: ctx.policy_sha256,
|
|
1183
|
+
evidence_ids: [],
|
|
1184
|
+
...(ctx.overrideTicketId ? { override_ticket_id: ctx.overrideTicketId } : {}),
|
|
1185
|
+
});
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
emitTelemetry(ctx, {
|
|
1189
|
+
stage: "audit_manifest_batch_complete",
|
|
1190
|
+
evaluated: selected.length,
|
|
1191
|
+
malicious_or_blocked: counts.malicious_or_blocked,
|
|
1192
|
+
flagged_suspicious: counts.flagged_suspicious,
|
|
1193
|
+
});
|
|
1194
|
+
|
|
1195
|
+
const exitCode =
|
|
1196
|
+
counts.malicious_or_blocked > 0
|
|
1197
|
+
? 3
|
|
1198
|
+
: counts.flagged_suspicious > 0 || counts.unverifiable_offline > 0
|
|
1199
|
+
? 2
|
|
1200
|
+
: 0;
|
|
1201
|
+
return { ok: true, report, exitCode };
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
|
|
1205
|
+
//# sourceURL=core/workflows.ts
|