Skip to content

Commit 58a8545

Browse files
Merge branch 'main' into yashraj/cache-shell-flag-scope-analysis
2 parents 960be30 + 789f517 commit 58a8545

17 files changed

Lines changed: 1386 additions & 73 deletions

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,9 @@ The top-level shape is (this example shows a full LLM-backed scan; with `--no-ll
835835
- `risk_assessment.severity` ∈ `LOW | MEDIUM | HIGH | CRITICAL`.
836836
- `risk_assessment.recommendation` ∈ `SAFE | CAUTION | DO_NOT_INSTALL`, mapped from severity: `LOW → SAFE`, `MEDIUM → CAUTION`, `HIGH`/`CRITICAL → DO_NOT_INSTALL`.
837837
- `metadata.llm_error` appears only when LLM analysis was requested but unavailable.
838+
- Archive-member components retain their virtual `path` and include `outer_path`, `nested_path`,
839+
and container provenance. Virtual paths identify in-memory members, not on-disk files; see
840+
[JSON component locations](docs/NESTED_ARTIFACT_INSPECTION.md#json-component-locations).
838841
- AE1 findings use **Incomplete referenced artifact analysis**. Their source
839842
location identifies the reference; `evidence` identifies the affected target,
840843
analyzer reasons, and available bounds. Review the target's completeness

‎docs/NESTED_ARTIFACT_INSPECTION.md‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,37 @@ Nested members use a stable virtual path that retains their full provenance:
1111
outer-file!/nested.zip!/scripts/setup.sh
1212
```
1313

14+
## JSON component locations
15+
16+
JSON component rows preserve this virtual path in `path`, matching finding locations and
17+
inspection-ledger entries. Archive members also expose their existing inventory provenance:
18+
19+
```json
20+
{
21+
"path": "bundle.whl!/package/module.py",
22+
"type": "python",
23+
"executable": true,
24+
"outer_path": "bundle.whl",
25+
"nested_path": "package/module.py",
26+
"container_type": "zip",
27+
"container_ancestry": ["zip"],
28+
"container_depth": 1
29+
}
30+
```
31+
32+
`outer_path` names the physical container relative to that component's source root. `nested_path`
33+
retains the full member chain inside it, including any nested `!/` boundaries. Each member keeps
34+
its own component row, executable flag, type, line count, and byte size. Ordinary files do not gain
35+
archive fields merely because their names contain `!/`.
36+
37+
Consumers must distinguish a virtual member from an on-disk file: `executable: true` describes
38+
the member's content, not the existence of its virtual path on disk. Use the source identity and
39+
full `path` to join components with findings; do not collapse members by `outer_path`. When
40+
validating a report, check archive provenance and containment within the corresponding source
41+
root, and retain incomplete-inspection failures. These additive fields do not change existing
42+
consumer validators automatically, prove that every member was inspected, or justify extracting
43+
or executing archive content.
44+
1445
## Security invariants
1546

1647
- Members are read in memory. SkillSpector never extracts, renders, imports, installs, or executes

‎docs/PI_EXTENSION.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ Equivalent CLI:
4848
- `yaraRulesDir`: optional directory of extra YARA rules.
4949
- `verbose`: optional detailed progress.
5050

51+
Inputs inside the session's working directory run without a prompt. Remote targets
52+
and external paths require confirmation; redirected aliases show their resolved
53+
destination too. Print and JSON sessions reject these requests because they cannot
54+
show a dialog. Use TUI or RPC mode, move the skill into the working directory, or
55+
run the CLI directly. Local targets retain the CLI's refusal of symlinked paths.
56+
Missing rule directories fail before scanning, and YARA `include` directives are
57+
disabled: put self-contained rule files in the selected directory.
58+
5159
## LLM-backed analysis
5260

5361
Static scan is default. To use semantic LLM analysis, configure provider credentials in your shell before launching Pi, then call the tool with `noLlm=false` and a provider.

‎extensions/skillspector.ts‎

Lines changed: 87 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
1+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
22
import { StringEnum } from "@earendil-works/pi-ai";
33
import { Type, type Static } from "typebox";
44
import { chmodSync, constants, copyFileSync, existsSync, lstatSync, mkdtempSync, realpathSync, renameSync, rmSync } from "node:fs";
@@ -7,7 +7,7 @@ import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "nod
77
import { fileURLToPath } from "node:url";
88

99
const scanSchema = Type.Object({
10-
target: Type.String({ description: "Path, URL, zip, Git repo, or SKILL.md to scan." }),
10+
target: Type.String({ description: "Path, URL, zip, Git repo, or SKILL.md to scan. External paths and remote targets require user confirmation." }),
1111
format: Type.Optional(
1212
StringEnum(["terminal", "json", "markdown", "sarif"] as const, {
1313
description: "SkillSpector output format. Defaults to terminal.",
@@ -21,22 +21,12 @@ const scanSchema = Type.Object({
2121
}),
2222
),
2323
model: Type.Optional(Type.String({ description: "Optional model override." })),
24-
yaraRulesDir: Type.Optional(Type.String({ description: "Optional extra YARA rules directory." })),
24+
yaraRulesDir: Type.Optional(Type.String({ description: "Optional extra YARA rules directory. External paths require user confirmation." })),
2525
verbose: Type.Optional(Type.Boolean({ description: "Show detailed progress." })),
2626
});
2727

2828
type SkillSpectorScanParams = Static<typeof scanSchema>;
2929

30-
function isLikelyUrl(value: string): boolean {
31-
return /^[a-z][a-z0-9+.-]*:\/\//i.test(value) || /^[\w.-]+\/[\w.-]+(?:\.git)?(?:@.+)?$/i.test(value);
32-
}
33-
34-
function resolveMaybePath(ctxCwd: string, value?: string): string | undefined {
35-
if (!value) return undefined;
36-
if (isLikelyUrl(value)) return value;
37-
return isAbsolute(value) ? value : resolve(ctxCwd, value);
38-
}
39-
4030
function redactSecrets(value: string): string {
4131
return value
4232
.replace(/(sk-ant-[A-Za-z0-9_-]{12,})/g, "[REDACTED_ANTHROPIC_KEY]")
@@ -71,6 +61,85 @@ function isWithin(root: string, path: string): boolean {
7161
return rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel);
7262
}
7363

64+
async function approveScanInputs(
65+
params: SkillSpectorScanParams,
66+
ctx: ExtensionContext,
67+
signal?: AbortSignal,
68+
): Promise<SkillSpectorScanParams> {
69+
signal?.throwIfAborted();
70+
const workspace = realpathSync(ctx.cwd);
71+
const prepared = { ...params };
72+
const localPaths: Array<{ field: "target" | "yaraRulesDir"; input: string; resolved?: string }> = [];
73+
const requests: string[] = [];
74+
const readRequest = (field: string, path: string) =>
75+
`Read external ${field === "target" ? "scan target" : "YARA rules"}: ${JSON.stringify(path)}`;
76+
async function approve(requests: string[]): Promise<void> {
77+
if (!requests.length) return;
78+
signal?.throwIfAborted();
79+
if (!ctx.hasUI) throw new Error("External scan inputs require user confirmation in an interactive or RPC session.");
80+
const approved = await ctx.ui.confirm(
81+
"Allow SkillSpector external access?",
82+
`${requests.join("\n")}\n\nScanned content and matching rule text can appear in the agent conversation.`,
83+
{ signal },
84+
);
85+
signal?.throwIfAborted();
86+
if (!approved) throw new Error("SkillSpector external access was not approved.");
87+
}
88+
for (const field of ["target", "yaraRulesDir"] as const) {
89+
const value = params[field]?.trim();
90+
if (field === "yaraRulesDir" && !value) {
91+
prepared[field] = undefined;
92+
continue;
93+
}
94+
if (!value) throw new Error("A scan target is required.");
95+
// Match the CLI's remote forms. A local owner/repo path is not a URL.
96+
const remote = !isAbsolute(value) && (value.startsWith("https://") || (value.startsWith("git@") && value.endsWith(".git")));
97+
if (remote) {
98+
if (field !== "target") throw new Error("YARA rules must be a local directory.");
99+
prepared[field] = value;
100+
requests.push(`Fetch remote scan target: ${JSON.stringify(value)}`);
101+
} else {
102+
const input = resolve(ctx.cwd, value);
103+
// Ask before resolving external paths, which can probe the host or access
104+
// a Windows network share even when the file is never opened.
105+
if (!isWithin(resolve(ctx.cwd), input)) {
106+
localPaths.push({ field, input });
107+
requests.push(readRequest(field, input));
108+
} else {
109+
let resolved: string;
110+
try {
111+
resolved = realpathSync(input);
112+
} catch {
113+
throw new Error(`Could not resolve ${field === "target" ? "scan target" : "YARA rules directory"}. Check that it exists and is accessible.`);
114+
}
115+
localPaths.push({ field, input, resolved });
116+
if (!isWithin(workspace, resolved)) requests.push(readRequest(field, resolved));
117+
}
118+
}
119+
}
120+
await approve(requests);
121+
const aliasRequests: string[] = [];
122+
for (const path of localPaths) {
123+
try {
124+
path.resolved ??= realpathSync(path.input);
125+
} catch {
126+
throw new Error(`Could not resolve ${path.field === "target" ? "scan target" : "YARA rules directory"}. Check that it exists and is accessible.`);
127+
}
128+
if (!isWithin(resolve(ctx.cwd), path.input) && !isWithin(workspace, path.resolved) && path.resolved !== path.input) {
129+
aliasRequests.push(readRequest(path.field, path.resolved));
130+
}
131+
// Keep the original target path so the CLI can enforce its no-symlink
132+
// input policy. YARA directories are canonicalised by the CLI too.
133+
prepared[path.field] = path.field === "target" ? path.input : path.resolved;
134+
}
135+
await approve(aliasRequests);
136+
signal?.throwIfAborted();
137+
if (realpathSync(ctx.cwd) !== workspace || localPaths.some(({ input, resolved }) => realpathSync(input) !== resolved)) {
138+
throw new Error("Scan input path changed while awaiting confirmation.");
139+
}
140+
return prepared;
141+
}
142+
74143
function reportOutputPath(cwd: string, value?: string): string | undefined {
75144
if (!value) return undefined;
76145
const output = resolve(cwd, value);
@@ -102,17 +171,16 @@ function publishReport(source: string, destination: string): void {
102171
}
103172
}
104173

105-
function buildScanArgs(params: SkillSpectorScanParams, cwd: string, output?: string): string[] {
106-
const args = ["scan", resolveMaybePath(cwd, params.target) ?? params.target];
174+
function buildScanArgs(params: SkillSpectorScanParams, output?: string): string[] {
175+
const args = ["scan", params.target];
107176
args.push("--format", params.format ?? "terminal");
108177

109178
const noLlm = params.noLlm ?? true;
110179
if (noLlm) args.push("--no-llm");
111180

112181
if (output) args.push("--output", output);
113182

114-
const yaraRulesDir = resolveMaybePath(cwd, params.yaraRulesDir);
115-
if (yaraRulesDir) args.push("--yara-rules-dir", yaraRulesDir);
183+
if (params.yaraRulesDir) args.push("--yara-rules-dir", params.yaraRulesDir);
116184

117185
if (params.verbose) args.push("--verbose");
118186
return args;
@@ -132,10 +200,11 @@ export default function (pi: ExtensionAPI) {
132200
async execute(_toolCallId, params, signal, onUpdate, ctx) {
133201
const bin = findSkillSpectorBin();
134202
const outputPath = reportOutputPath(ctx.cwd, params.output);
203+
const prepared = await approveScanInputs(params, ctx, signal);
135204
const reportDir = outputPath ? mkdtempSync(join(tmpdir(), "skillspector-report-")) : undefined;
136205
const reportPath = reportDir ? join(reportDir, "report") : undefined;
137206
try {
138-
const args = buildScanArgs(params, ctx.cwd, reportPath);
207+
const args = buildScanArgs(prepared, reportPath);
139208
const env: Record<string, string> = {};
140209

141210
if (params.provider) env.SKILLSPECTOR_PROVIDER = params.provider;

‎src/skillspector/cli.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
dump_baseline,
8383
effective_findings,
8484
load_baseline,
85+
source_content_key,
8586
)
8687

8788
logger = get_logger(__name__)
@@ -1179,7 +1180,7 @@ def _source_aware_file_cache(
11791180
file_cache: dict[str, str], source_identity: str | None
11801181
) -> dict[str, str]:
11811182
return {
1182-
_transitive_component_key(source_identity, path): content
1183+
(source_content_key(source_identity, path) if source_identity else path): content
11831184
for path, content in file_cache.items()
11841185
}
11851186

@@ -1777,7 +1778,8 @@ def _bounded_cache_update(
17771778
) -> None:
17781779
for path in sorted(values):
17791780
if path in destination:
1780-
destination[path] = values[path]
1781+
# Source-scoped keys are disjoint from root paths. A repeated key
1782+
# refers to the same cached result within this traversal.
17811783
continue
17821784
if len(destination) >= limit:
17831785
traversal.note_truncation(f"{resource} budget {limit} reached")

‎src/skillspector/nodes/analyzers/behavioral_taint_tracking.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
"httpx.patch",
129129
"httpx.get",
130130
"urllib.request.urlopen",
131+
"urllib.request.build_opener.open",
131132
"socket.socket.send",
132133
"socket.socket.sendall",
133134
"socket.socket.sendto",
@@ -303,6 +304,14 @@ def _resolve_sink_name(
303304
``importlib.import_module('subprocess').run(...)`` resolves to ``'subprocess.run'``
304305
and re-enters ``_EXEC_SINKS`` like the statically-imported form would.
305306
"""
307+
if (
308+
isinstance(node.func, ast.Attribute)
309+
and node.func.attr == "open"
310+
and isinstance(node.func.value, ast.Call)
311+
and resolve_call_name_typed(node.func.value, type_map, aliases)
312+
== "urllib.request.build_opener"
313+
):
314+
return "urllib.request.build_opener.open"
306315
name = resolve_call_name_typed(node, type_map, aliases)
307316
if name is None:
308317
name = resolve_dynamic_import_call(node, aliases)

0 commit comments

Comments
 (0)