Skip to content

Commit 1d3fea5

Browse files
committed
Merge remote-tracking branch 'origin/naren/fix-NVCARPS-150-semantic-provenance' into pr556-clean
Signed-off-by: Narendran Raghavan <[email protected]>
2 parents aab4713 + 7c9f7f5 commit 1d3fea5

10 files changed

Lines changed: 885 additions & 62 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ jobs:
102102
with:
103103
node-version: ${{ env.NODE_VERSION }}
104104
- run: node --test tests/opencode/skillspector_scan_lib.test.ts
105+
- name: Run Pi extension security tests
106+
run: node --test tests/unit/test_pi_extension.mjs
105107

106108
docker-smoke:
107109
needs: changes

‎extensions/skillspector.ts‎

Lines changed: 105 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
22
import { StringEnum } from "@earendil-works/pi-ai";
33
import { Type, type Static } from "typebox";
4-
import { existsSync } from "node:fs";
5-
import { dirname, isAbsolute, resolve } from "node:path";
4+
import { chmodSync, constants, copyFileSync, existsSync, lstatSync, mkdtempSync, realpathSync, renameSync, rmSync } from "node:fs";
5+
import { tmpdir } from "node:os";
6+
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
67
import { fileURLToPath } from "node:url";
78

89
const scanSchema = Type.Object({
@@ -12,7 +13,7 @@ const scanSchema = Type.Object({
1213
description: "SkillSpector output format. Defaults to terminal.",
1314
}),
1415
),
15-
output: Type.Optional(Type.String({ description: "Optional report output path." })),
16+
output: Type.Optional(Type.String({ description: "Optional report output path within the current workspace." })),
1617
noLlm: Type.Optional(Type.Boolean({ description: "Skip LLM analysis. Defaults to true." })),
1718
provider: Type.Optional(
1819
StringEnum(["openai", "anthropic", "anthropic_proxy", "nv_build", "nv_inference"] as const, {
@@ -57,20 +58,57 @@ function packageRoot(): string {
5758
}
5859

5960
function findSkillSpectorBin(): string {
60-
if (process.env.SKILLSPECTOR_BIN) return process.env.SKILLSPECTOR_BIN;
61-
const localBin = resolve(packageRoot(), ".venv/bin/skillspector");
62-
if (existsSync(localBin)) return localBin;
63-
return "skillspector";
61+
const bundledBin = process.platform === "win32" ? ".venv/Scripts/skillspector.exe" : ".venv/bin/skillspector";
62+
const bin = process.env.SKILLSPECTOR_BIN ?? resolve(packageRoot(), bundledBin);
63+
if (!isAbsolute(bin) || !existsSync(bin)) {
64+
throw new Error("Install SkillSpector in the extension's .venv or set SKILLSPECTOR_BIN to an absolute executable path.");
65+
}
66+
return bin;
6467
}
6568

66-
function buildScanArgs(params: SkillSpectorScanParams, cwd: string): string[] {
69+
function isWithin(root: string, path: string): boolean {
70+
const rel = relative(root, path);
71+
return rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel);
72+
}
73+
74+
function reportOutputPath(cwd: string, value?: string): string | undefined {
75+
if (!value) return undefined;
76+
const output = resolve(cwd, value);
77+
if (output === resolve(cwd) || !isWithin(resolve(cwd), output)) {
78+
throw new Error("Report output must be a file within the current workspace.");
79+
}
80+
const parent = realpathSync(dirname(output));
81+
if (!isWithin(realpathSync(cwd), parent)) {
82+
throw new Error("Report output must be a file within the current workspace.");
83+
}
84+
const destination = join(parent, basename(output));
85+
const existing = lstatSync(destination, { throwIfNoEntry: false });
86+
if (existing && !existing.isFile()) {
87+
throw new Error("Report output must be a regular file, not a symlink or directory.");
88+
}
89+
return destination;
90+
}
91+
92+
function publishReport(source: string, destination: string): void {
93+
const staging = mkdtempSync(join(dirname(destination), ".skillspector-report-"));
94+
try {
95+
const report = join(staging, "report");
96+
copyFileSync(source, report, constants.COPYFILE_EXCL);
97+
chmodSync(report, 0o600);
98+
// Replacing the directory entry never follows an existing hard link.
99+
renameSync(report, destination);
100+
} finally {
101+
rmSync(staging, { recursive: true, force: true });
102+
}
103+
}
104+
105+
function buildScanArgs(params: SkillSpectorScanParams, cwd: string, output?: string): string[] {
67106
const args = ["scan", resolveMaybePath(cwd, params.target) ?? params.target];
68107
args.push("--format", params.format ?? "terminal");
69108

70109
const noLlm = params.noLlm ?? true;
71110
if (noLlm) args.push("--no-llm");
72111

73-
const output = resolveMaybePath(cwd, params.output);
74112
if (output) args.push("--output", output);
75113

76114
const yaraRulesDir = resolveMaybePath(cwd, params.yaraRulesDir);
@@ -93,50 +131,65 @@ export default function (pi: ExtensionAPI) {
93131
parameters: scanSchema,
94132
async execute(_toolCallId, params, signal, onUpdate, ctx) {
95133
const bin = findSkillSpectorBin();
96-
const args = buildScanArgs(params, ctx.cwd);
97-
const env: Record<string, string> = {};
98-
99-
if (params.provider) env.SKILLSPECTOR_PROVIDER = params.provider;
100-
if (params.model) env.SKILLSPECTOR_MODEL = params.model;
101-
102-
onUpdate?.({ content: [{ type: "text", text: `Running ${bin} ${args.slice(0, 2).join(" ")} ...` }] });
103-
104-
const result = await pi.exec(bin, args, {
105-
cwd: ctx.cwd,
106-
env,
107-
signal,
108-
timeout: 120000,
109-
});
110-
111-
const stdout = truncateText(redactSecrets(result.stdout ?? ""));
112-
const stderr = truncateText(redactSecrets(result.stderr ?? ""), 6000);
113-
114-
if (result.code !== 0) {
115-
throw new Error(`SkillSpector failed with exit code ${result.code}.\n${stderr.text}`);
134+
const outputPath = reportOutputPath(ctx.cwd, params.output);
135+
const reportDir = outputPath ? mkdtempSync(join(tmpdir(), "skillspector-report-")) : undefined;
136+
const reportPath = reportDir ? join(reportDir, "report") : undefined;
137+
try {
138+
const args = buildScanArgs(params, ctx.cwd, reportPath);
139+
const env: Record<string, string> = {};
140+
141+
if (params.provider) env.SKILLSPECTOR_PROVIDER = params.provider;
142+
if (params.model) env.SKILLSPECTOR_MODEL = params.model;
143+
144+
onUpdate?.({ content: [{ type: "text", text: `Running ${bin} ${args.slice(0, 2).join(" ")} ...` }] });
145+
146+
const result = await pi.exec(bin, args, {
147+
cwd: ctx.cwd,
148+
env,
149+
signal,
150+
timeout: 120000,
151+
});
152+
153+
const stdout = truncateText(redactSecrets(result.stdout ?? ""));
154+
const stderr = truncateText(redactSecrets(result.stderr ?? ""), 6000);
155+
156+
// Exit 2 can follow a diagnostic report; failures before reporting produce no file.
157+
const failureReport = result.code === 2 && reportPath
158+
? lstatSync(reportPath, { throwIfNoEntry: false }) : undefined;
159+
const hasFailureReport = failureReport?.isFile() && failureReport.size > 0;
160+
if ((result.code === 0 || result.code === 1 || hasFailureReport) && outputPath && reportPath) {
161+
if (reportOutputPath(ctx.cwd, params.output) !== outputPath) {
162+
throw new Error("Report output directory changed during the scan.");
163+
}
164+
publishReport(reportPath, outputPath);
165+
}
166+
if (result.code !== 0) {
167+
throw new Error(`SkillSpector failed with exit code ${result.code}.\n${stderr.text}`);
168+
}
169+
const lines = [
170+
`SkillSpector scan complete: ${params.target}`,
171+
`format: ${params.format ?? "terminal"}`,
172+
`noLlm: ${params.noLlm ?? true}`,
173+
];
174+
if (outputPath) lines.push(`output: ${outputPath}`);
175+
if (stdout.truncated) lines.push("stdout truncated: true");
176+
if (stderr.text.trim()) lines.push(`stderr:\n${stderr.text}`);
177+
if (stdout.text.trim()) lines.push(`stdout:\n${stdout.text}`);
178+
179+
return {
180+
content: [{ type: "text", text: lines.join("\n") }],
181+
details: {
182+
code: result.code,
183+
command: bin,
184+
args,
185+
outputPath,
186+
stdoutTruncated: stdout.truncated,
187+
stderrTruncated: stderr.truncated,
188+
},
189+
};
190+
} finally {
191+
if (reportDir) rmSync(reportDir, { recursive: true, force: true });
116192
}
117-
118-
const outputPath = resolveMaybePath(ctx.cwd, params.output);
119-
const lines = [
120-
`SkillSpector scan complete: ${params.target}`,
121-
`format: ${params.format ?? "terminal"}`,
122-
`noLlm: ${params.noLlm ?? true}`,
123-
];
124-
if (outputPath) lines.push(`output: ${outputPath}`);
125-
if (stdout.truncated) lines.push("stdout truncated: true");
126-
if (stderr.text.trim()) lines.push(`stderr:\n${stderr.text}`);
127-
if (stdout.text.trim()) lines.push(`stdout:\n${stdout.text}`);
128-
129-
return {
130-
content: [{ type: "text", text: lines.join("\n") }],
131-
details: {
132-
code: result.code,
133-
command: bin,
134-
args,
135-
outputPath,
136-
stdoutTruncated: stdout.truncated,
137-
stderrTruncated: stderr.truncated,
138-
},
139-
};
140193
},
141194
});
142195
}

‎src/skillspector/mcp_registry.py‎

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@
77

88
import hashlib
99
import json
10+
import os
1011
import re
1112
from dataclasses import asdict, dataclass
1213
from datetime import UTC, datetime
1314
from itertools import chain
1415
from pathlib import Path
16+
from stat import S_ISREG
1517
from typing import Any, TypedDict
1618

1719
import httpx
1820

1921
REGISTRY_URL = "https://registry.modelcontextprotocol.io/v0/servers"
2022
OFFICIAL_META_KEY = "io.modelcontextprotocol.registry/official"
23+
MAX_REGISTRY_BYTES = 16 * 1024 * 1024
24+
MAX_REGISTRY_DEPTH = 64
25+
MAX_REGISTRY_RECORDS = 10_000
2126
FILE_SHA256_RE = re.compile(r"^[a-f0-9]{64}$")
2227
MUTABLE_VERSION_TAGS = frozenset(
2328
{
@@ -224,6 +229,24 @@ def normalize_server(
224229
def normalize_payload(payload: dict[str, Any], *, source: str) -> list[RegistryServerSnapshot]:
225230
if not isinstance(payload, dict) or not isinstance(payload.get("servers"), list):
226231
raise ValueError(f"MCP Registry payload from {source} must contain a servers list")
232+
# Check the full expansion before constructing any snapshots or findings.
233+
records = len(payload["servers"])
234+
if records <= MAX_REGISTRY_RECORDS:
235+
for entry in payload["servers"]:
236+
record = entry.get("server") if isinstance(entry, dict) else None
237+
if isinstance(record, dict):
238+
records += sum(
239+
len(record[field])
240+
for field in ("packages", "remotes")
241+
if isinstance(record.get(field), list)
242+
)
243+
if records > MAX_REGISTRY_RECORDS:
244+
break
245+
if records > MAX_REGISTRY_RECORDS:
246+
raise ValueError(
247+
f"MCP Registry payload from {source} exceeds {MAX_REGISTRY_RECORDS} records "
248+
"(servers, packages, and remotes)"
249+
)
227250
scanned_at = datetime.now(UTC).isoformat()
228251
return [
229252
normalize_server(entry, source=source, scanned_at=scanned_at)
@@ -344,14 +367,47 @@ def _dict_payload(payload: object, *, source: str) -> dict[str, Any]:
344367
return payload
345368

346369

370+
def _load_local_registry(path: Path) -> dict[str, Any]:
371+
with open(
372+
path,
373+
"rb",
374+
opener=lambda path, flags: os.open(path, flags | getattr(os, "O_NONBLOCK", 0)),
375+
) as source_file:
376+
if not S_ISREG(os.fstat(source_file.fileno()).st_mode):
377+
raise ValueError(f"Registry input must be a regular file: {path}")
378+
raw = source_file.read(MAX_REGISTRY_BYTES + 1)
379+
if len(raw) > MAX_REGISTRY_BYTES:
380+
raise ValueError(f"input exceeds {MAX_REGISTRY_BYTES} bytes")
381+
text = raw.decode("utf-8")
382+
# The JSON decoder owns syntax validation; only bound nesting here, while
383+
# ignoring delimiters in strings, before its recursive parser runs.
384+
depth = 0
385+
in_string = False
386+
escaped = False
387+
for char in text:
388+
if in_string:
389+
if escaped:
390+
escaped = False
391+
elif char == "\\":
392+
escaped = True
393+
elif char == '"':
394+
in_string = False
395+
elif char == '"':
396+
in_string = True
397+
elif char in "[{":
398+
depth += 1
399+
if depth > MAX_REGISTRY_DEPTH:
400+
raise ValueError(f"JSON nesting exceeds {MAX_REGISTRY_DEPTH} levels")
401+
elif char in "]}":
402+
depth -= 1
403+
return _dict_payload(json.loads(text), source=str(path))
404+
405+
347406
def _load_payload(input_path: str) -> dict[str, Any]:
348407
source = input_path
349408
try:
350409
if Path(input_path).is_file():
351-
return _dict_payload(
352-
json.loads(Path(input_path).read_text(encoding="utf-8")),
353-
source=source,
354-
)
410+
return _load_local_registry(Path(input_path))
355411
if input_path.startswith(("http://", "https://")):
356412
if input_path != REGISTRY_URL:
357413
raise ValueError(
@@ -372,7 +428,7 @@ def _load_payload(input_path: str) -> dict[str, Any]:
372428
# assesses the owner's latest record, not the historical tail.
373429
latest = [entry for entry in matches if _official_meta(entry).get("isLatest") is True]
374430
return {"servers": latest or matches}
375-
except (OSError, json.JSONDecodeError, httpx.HTTPError, ValueError) as exc:
431+
except (OSError, httpx.HTTPError, ValueError, RecursionError) as exc:
376432
if isinstance(exc, ValueError) and str(exc).startswith("MCP Registry source"):
377433
raise
378434
raise ValueError(f"MCP Registry source failed: {source}: {exc}") from exc

‎src/skillspector/providers/_agent_cli.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@
7979
# Includes API keys, cloud creds, SSH agent, and SkillSpector's own keys.
8080
_SECRET_ENV_PREFIXES: tuple[str, ...] = (
8181
"ANTHROPIC_API_KEY",
82+
"ANTHROPIC_PROXY_API_KEY",
8283
"OPENAI_API_KEY",
84+
"SKILLSPECTOR_API_KEYS",
85+
"SKILLSPECTOR_COMPAT_API_KEY",
8386
"NVIDIA_INFERENCE_KEY",
8487
"NVIDIA_INFERENCE_METADATA_KEY",
8588
"AWS_",

0 commit comments

Comments
 (0)