diff --git a/ide/src/test/suite/wiring-map-model.test.ts b/ide/src/test/suite/wiring-map-model.test.ts index b5237f3c1a..2cdc261864 100644 --- a/ide/src/test/suite/wiring-map-model.test.ts +++ b/ide/src/test/suite/wiring-map-model.test.ts @@ -3,7 +3,7 @@ import * as assert from "assert"; import type { Graph } from "../../graphModel"; -import { buildWiringMap, NODE_CAP, type MapNode } from "../../wiringMapModel"; +import { buildWiringMap, graphOpenTarget, handleOpenRequest, NODE_CAP, type MapNode } from "../../wiringMapModel"; // Pure (vscode-free) view-model for the Wiring Map panel (ADR 0091 D3), exercised node-side like // graph-model.test.ts. Covers: focus-BFS hop membership (1 vs 2), the four-column assignment @@ -247,3 +247,126 @@ suite("wiringMapModel — connection-to-connection references (BACKLOG #5966)", assert.deepStrictEqual(realNames(map.columns[0]), ["IB_LOOP"]); }); }); + +suite("wiringMapModel — graphOpenTarget refuses a file the graph did not report (BACKLOG #4277)", () => { + // One code-authored element (absolute path) and one data-authored connection (workspace-relative + // path, as the CLI reports a connections.toml entry). + const G: Graph = { + version: 2, + inbound: [{ name: "IB_A", type: "mllp", router: "r", file: "/ws/cfg/a.py", line: 3 }], + outbound: [{ name: "OB_T", type: "file", file: "connections.toml", line: 12 }], + routers: [{ name: "r", edges: [], file: "/ws/cfg/a.py", line: 10 }], + handlers: [{ name: "h", edges: [], file: String.raw`C:\ws\cfg\h.py`, line: 4 }], + }; + + test("a file the graph reports opens at the requested line", () => { + assert.deepStrictEqual(graphOpenTarget(G, "/ws/cfg/a.py", 10), { file: "/ws/cfg/a.py", line: 10 }); + assert.deepStrictEqual(graphOpenTarget(G, "connections.toml", 12), { file: "connections.toml", line: 12 }); + assert.deepStrictEqual(graphOpenTarget(G, String.raw`C:\ws\cfg\h.py`, 4), { file: String.raw`C:\ws\cfg\h.py`, line: 4 }); + }); + + test("every file the map payload offers to open is one the guard accepts", () => { + const offered = buildWiringMap(G, null) + .columns.flat() + .flatMap((n) => (n.open ? [n.open] : [])); + assert.ok(offered.length > 0); + for (const o of offered) { + assert.deepStrictEqual(graphOpenTarget(G, o.file, o.line), o); + } + }); + + test("a page-chosen file the graph does not report is refused, however close to a reported one", () => { + const bad = [ + "/etc/passwd", + "/ws/cfg/b.py", + "../secrets.env", + "/ws/cfg/../cfg/a.py", + "/ws/connections.toml", + "/WS/cfg/a.py", + String.raw`c:\ws\cfg\h.py`, + String.raw`\\attacker\share\a.py`, + ]; + for (const f of bad) { + assert.strictEqual(graphOpenTarget(G, f, 1), undefined, f); + } + }); + + test("a malformed request, or no graph, is refused", () => { + for (const f of [undefined, null, 7, "", { file: "/ws/cfg/a.py" }, ["/ws/cfg/a.py"]]) { + assert.strictEqual(graphOpenTarget(G, f, 1), undefined, String(f)); + } + assert.strictEqual(graphOpenTarget(undefined, "/ws/cfg/a.py", 1), undefined); + }); + + test("an element reporting no file, or an empty one, does not make an empty file openable", () => { + const g: Graph = { + ...G, + routers: [{ name: "r", edges: [], file: "", line: 2 }], + handlers: [{ name: "h", edges: [], file: null, line: null }], + }; + assert.strictEqual(graphOpenTarget(g, "", 1), undefined); + }); + + test("a line that is not a positive safe integer falls back to 1", () => { + for (const ln of [0, -4, 1.5, Number.NaN, Number.POSITIVE_INFINITY, "9", undefined]) { + assert.deepStrictEqual(graphOpenTarget(G, "/ws/cfg/a.py", ln), { file: "/ws/cfg/a.py", line: 1 }, String(ln)); + } + }); +}); + +suite("wiringMapModel — handleOpenRequest is what the Wiring Map's open handler runs (BACKLOG #4277)", () => { + const G: Graph = { + version: 2, + inbound: [{ name: "IB_A", type: "mllp", router: "r", file: "/ws/cfg/a.py", line: 3 }], + outbound: [], + routers: [{ name: "r", edges: [], file: "/ws/cfg/a.py", line: 10 }], + handlers: [], + }; + + function recorder(fail?: Error): { calls: unknown[][]; warnings: string[]; host: Parameters[3] } { + const calls: unknown[][] = []; + const warnings: string[] = []; + return { + calls, + warnings, + host: { + execute: (command: string, ...args: unknown[]) => { + calls.push([command, ...args]); + return fail ? Promise.reject(fail) : Promise.resolve(undefined); + }, + warn: (message: string) => { + warnings.push(message); + }, + }, + }; + } + + test("a graph file opens through messagefoundry.openSource", async () => { + const r = recorder(); + await handleOpenRequest(G, "/ws/cfg/a.py", 10, r.host); + assert.deepStrictEqual(r.calls, [["messagefoundry.openSource", "/ws/cfg/a.py", 10]]); + assert.deepStrictEqual(r.warnings, []); + }); + + test("a page-chosen file runs no command and warns", async () => { + const r = recorder(); + await handleOpenRequest(G, "/etc/passwd", 1, r.host); + assert.deepStrictEqual(r.calls, []); + assert.strictEqual(r.warnings.length, 1); + assert.ok(!r.warnings[0].includes("/etc/passwd"), "the page's path is not echoed"); + }); + + test("with no graph loaded, nothing opens and the warning says so", async () => { + const r = recorder(); + await handleOpenRequest(undefined, "/ws/cfg/a.py", 1, r.host); + assert.deepStrictEqual(r.calls, []); + assert.match(r.warnings[0], /no wiring graph is loaded/); + }); + + test("a failed open warns instead of rejecting", async () => { + const r = recorder(new Error("file not found")); + await handleOpenRequest(G, "/ws/cfg/a.py", 3, r.host); + assert.strictEqual(r.calls.length, 1); + assert.match(r.warnings[0], /file not found/); + }); +}); diff --git a/ide/src/wiringMap.ts b/ide/src/wiringMap.ts index 697694880b..bf95f437c1 100644 --- a/ide/src/wiringMap.ts +++ b/ide/src/wiringMap.ts @@ -12,7 +12,7 @@ // posted map payload, so config-supplied names are never string-interpolated into markup. import * as vscode from "vscode"; import type { ElementKind } from "./graphModel"; -import { wiringMapPayload, type MapFocus } from "./wiringMapModel"; +import { handleOpenRequest, wiringMapPayload, type MapFocus } from "./wiringMapModel"; import type { GraphProvider } from "./graphTree"; import { STARTUP_BANNERS, openChannel, postToWebview } from "./webviewMessaging"; import { wiringMapScript } from "./wiringMapWebview"; @@ -21,7 +21,9 @@ type Incoming = | { command: "ready" } | { command: "setFocus"; kind: ElementKind; name: string } | { command: "refresh" } - | { command: "open"; file: string; line: number } + // Typed unknown because the file reaches openSource as a path: handleOpenRequest checks it + // against the graph (BACKLOG #4277). Host-side shape checks on the other variants are #2418. + | { command: "open"; file: unknown; line: unknown } | { command: "reveal"; kind: ElementKind; name: string }; export class WiringMapPanel { @@ -76,7 +78,11 @@ export class WiringMapPanel { } else if (m.command === "refresh") { await this.graph.refresh(); // fires onDidChangeTreeData -> post() } else if (m.command === "open") { - await vscode.commands.executeCommand("messagefoundry.openSource", m.file, m.line); + // A page script names the file, so open only a file the current graph reports (BACKLOG #4277). + await handleOpenRequest(this.graph.getGraph(), m.file, m.line, { + execute: (command, ...args) => vscode.commands.executeCommand(command, ...args), + warn: (message) => void vscode.window.showWarningMessage(message), + }); } else if (m.command === "reveal") { await vscode.commands.executeCommand("messagefoundry.revealElement", m.kind, m.name); } diff --git a/ide/src/wiringMapModel.ts b/ide/src/wiringMapModel.ts index a82b807d2b..31ac7ef166 100644 --- a/ide/src/wiringMapModel.ts +++ b/ide/src/wiringMapModel.ts @@ -337,3 +337,71 @@ export function wiringMapPayload(g: Graph | undefined, focus: MapFocus | null): } return { type: "map", map, focus, names }; } + +/** + * The source location a Wiring Map "open" request may open, or undefined to refuse it (ASVS 5.3.2, + * BACKLOG #4277). The page posts back a node's `open.file` copied unchanged from the payload it was + * sent, but a page script is not trusted to name a file: the string must EQUAL the file of an + * element the current graph reports, read through the same collectElements the payload is built + * from, so what the map offers and what this accepts share one source. Exact equality, not a + * resolved or case-folded key, so neither the workspace root nor the platform's case rules can widen + * the match. The line is only a cursor position; a value that is not a positive safe integer falls + * back to 1. This checks WHO named the file, not WHERE it is: a graph that reports a file outside + * the workspace (on a UNC share, say) still opens it. + */ +export function graphOpenTarget( + g: Graph | undefined, + file: unknown, + line: unknown, +): { file: string; line: number } | undefined { + if (!g || typeof file !== "string") { + return undefined; + } + let reported = false; + for (const e of collectElements(normalize(g)).values()) { + if (e.open?.file === file) { + reported = true; + break; + } + } + if (!reported) { + return undefined; + } + const ln = typeof line === "number" && Number.isSafeInteger(line) && line >= 1 ? line : 1; + return { file, line: ln }; +} + +/** What the Wiring Map host needs to act on an "open" request; injected so the decision is tested node-side. */ +export interface OpenRequestHost { + /** Runs a VS Code command; the host passes vscode.commands.executeCommand. */ + execute(command: string, ...args: unknown[]): PromiseLike; + /** Tells the operator something went wrong; the host passes a warning toast. */ + warn(message: string): void; +} + +/** + * Act on a Wiring Map "open" request (BACKLOG #4277): open the file through messagefoundry.openSource + * only when graphOpenTarget accepts it, passing the graph's file, never the page's; otherwise warn and + * open nothing. A failed open (a file gone since the last refresh) warns rather than being dropped. + */ +export async function handleOpenRequest( + g: Graph | undefined, + file: unknown, + line: unknown, + host: OpenRequestHost, +): Promise { + const target = graphOpenTarget(g, file, line); + if (!target) { + host.warn( + g + ? "MessageFoundry: the Wiring Map asked to open a file the wiring graph does not report; it was not opened." + : "MessageFoundry: no wiring graph is loaded, so the Wiring Map cannot open a file.", + ); + return; + } + try { + await host.execute("messagefoundry.openSource", target.file, target.line); + } catch (err) { + host.warn(`MessageFoundry: could not open the source file: ${err instanceof Error ? err.message : String(err)}`); + } +}