From e84a27111d17793f3bf676e8beddb2b7ba4348ce Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 24 Nov 2026 08:42:33 -0600 Subject: [PATCH 1/3] IDE Wiring Map: open only a file the wiring graph reports The Wiring Map's "open" handler passed a webview message's file straight to messagefoundry.openSource, which opens any path. A page script names that file, so a compromised page could have the extension open any file the operator's account can read. graphOpenTarget (wiringMapModel.ts, vscode-free) now accepts the request only when the current graph reports that file for an element, compared with fileKey against the workspace root, and hands openSource the graph's own spelling of it. Anything else is refused with a warning and nothing opens. openSource itself is unchanged: its other callers pass a graph-reported tree-row path or the open document. Node-side tests: graph paths (absolute and workspace-relative) open; page-chosen paths, traversal, UNC and malformed requests are refused. BACKLOG #4277 --- ide/src/test/suite/wiring-map-model.test.ts | 65 ++++++++++++++++++++- ide/src/wiringMap.ts | 13 ++++- ide/src/wiringMapModel.ts | 31 ++++++++++ 3 files changed, 106 insertions(+), 3 deletions(-) diff --git a/ide/src/test/suite/wiring-map-model.test.ts b/ide/src/test/suite/wiring-map-model.test.ts index b5237f3c1a..acea304309 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, 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,66 @@ 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), on each path flavour. + const POSIX: 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: null, line: null }], + }; + + test("a file the graph reports opens, at the requested line, under the graph's spelling", () => { + assert.deepStrictEqual(graphOpenTarget(POSIX, "/ws/cfg/a.py", 10, "/ws", false), { file: "/ws/cfg/a.py", line: 10 }); + assert.deepStrictEqual(graphOpenTarget(POSIX, "connections.toml", 12, "/ws", false), { + file: "connections.toml", + line: 12, + }); + // The same file named another way still resolves to the graph's own string. + assert.deepStrictEqual(graphOpenTarget(POSIX, "/ws/connections.toml", 1, "/ws", false), { + file: "connections.toml", + line: 1, + }); + }); + + test("a page-chosen file the graph does not report is refused", () => { + for (const bad of ["/etc/passwd", "/ws/cfg/b.py", "../secrets.env", "cfg/../../etc/passwd", "//host/share/x.py"]) { + assert.strictEqual(graphOpenTarget(POSIX, bad, 1, "/ws", false), undefined, bad); + } + }); + + test("a malformed request, or no graph, is refused", () => { + for (const bad of [undefined, null, 7, "", { file: "/ws/cfg/a.py" }]) { + assert.strictEqual(graphOpenTarget(POSIX, bad, 1, "/ws", false), undefined, String(bad)); + } + assert.strictEqual(graphOpenTarget(undefined, "/ws/cfg/a.py", 1, "/ws", false), 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(POSIX, "/ws/cfg/a.py", ln, "/ws", false), { file: "/ws/cfg/a.py", line: 1 }, String(ln)); + } + }); + + test("Windows: compared case-insensitively against the workspace root, UNC refused", () => { + const WIN: Graph = { + version: 2, + inbound: [{ name: "IB_A", type: "mllp", router: "r", file: String.raw`C:\ws\cfg\a.py`, line: 3 }], + outbound: [{ name: "OB_T", type: "file", file: "connections.toml", line: 12 }], + routers: [], + handlers: [], + }; + const base = String.raw`c:\ws`; + assert.deepStrictEqual(graphOpenTarget(WIN, String.raw`c:\WS\cfg\A.py`, 3, base, true), { + file: String.raw`C:\ws\cfg\a.py`, + line: 3, + }); + assert.deepStrictEqual(graphOpenTarget(WIN, "connections.toml", 12, base, true), { file: "connections.toml", line: 12 }); + for (const bad of [String.raw`\\attacker\share\a.py`, String.raw`C:\Windows\win.ini`, String.raw`..\ws2\connections.toml`]) { + assert.strictEqual(graphOpenTarget(WIN, bad, 1, base, true), undefined, bad); + } + }); +}); diff --git a/ide/src/wiringMap.ts b/ide/src/wiringMap.ts index 697694880b..d0d8ad35a3 100644 --- a/ide/src/wiringMap.ts +++ b/ide/src/wiringMap.ts @@ -12,8 +12,9 @@ // 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 { graphOpenTarget, wiringMapPayload, type MapFocus } from "./wiringMapModel"; import type { GraphProvider } from "./graphTree"; +import { workspaceDir } from "./cli"; import { STARTUP_BANNERS, openChannel, postToWebview } from "./webviewMessaging"; import { wiringMapScript } from "./wiringMapWebview"; @@ -76,7 +77,15 @@ 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). + const target = graphOpenTarget(this.graph.getGraph(), m.file, m.line, workspaceDir() ?? ""); + if (!target) { + void vscode.window.showWarningMessage( + "MessageFoundry: the Wiring Map asked to open a file the wiring graph does not report; it was not opened. Refresh the map and try again.", + ); + return; + } + await vscode.commands.executeCommand("messagefoundry.openSource", target.file, target.line); } 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..31d34f331b 100644 --- a/ide/src/wiringMapModel.ts +++ b/ide/src/wiringMapModel.ts @@ -10,6 +10,7 @@ // incompleteness is visible, never a silently shorter map (AC-3). Tested node-side like graphModel. import { normalize, type ElementKind, type Graph, type NormalGraph } from "./graphModel"; +import { fileKey, IS_WIN32 } from "./pathIdentity"; // The members live in a const so a webview page can be handed the same list its host is typed by // (ASVS 3.5.5, BACKLOG #1123). The type is derived from it, so the two cannot drift. @@ -337,3 +338,33 @@ 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 file it was sent, but a page script is not trusted to name + * one: the file must be one the current graph itself reports for an element, compared as fileKey + * resolved against `base` (the workspace root openSource resolves a relative path against). What + * comes back is the GRAPH's spelling of the file, never the page's, so openSource only ever sees a + * path the CLI printed. The line is only a cursor position; a value that is not a positive safe + * integer falls back to 1. + */ +export function graphOpenTarget( + g: Graph | undefined, + file: unknown, + line: unknown, + base: string, + win32: boolean = IS_WIN32, +): { file: string; line: number } | undefined { + if (!g || typeof file !== "string" || file === "") { + return undefined; + } + const want = fileKey(base, file, win32); + const reported = [...g.inbound, ...g.routers, ...g.handlers, ...g.outbound] + .map((e) => e.file) + .find((f): f is string => typeof f === "string" && f !== "" && fileKey(base, f, win32) === want); + if (reported === undefined) { + return undefined; + } + const ln = typeof line === "number" && Number.isSafeInteger(line) && line >= 1 ? line : 1; + return { file: reported, line: ln }; +} From bf1b53ef35ae9bbac30ca9aaa222d394d0202a5b Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 24 Nov 2026 09:03:15 -0600 Subject: [PATCH 2/3] IDE Wiring Map: match the open request by exact string, surface open failures Review round 1. graphOpenTarget now requires the page's file to EQUAL a file the graph reports, rather than matching a resolved, case-folded key: the page only ever echoes the payload, and an exact match takes no dependency on the workspace root or on fileKey, whose contract says it is not a containment check. The open message's fields are typed unknown, an openSource rejection (a graph file gone since the last refresh) now warns instead of being dropped, and the tests no longer claim a UNC rule the guard does not have. A new test pins that every file the map payload offers is one the guard accepts. BACKLOG #4277 --- ide/src/test/suite/wiring-map-model.test.ts | 79 ++++++++++----------- ide/src/wiringMap.ts | 14 +++- ide/src/wiringMapModel.ts | 24 +++---- 3 files changed, 60 insertions(+), 57 deletions(-) diff --git a/ide/src/test/suite/wiring-map-model.test.ts b/ide/src/test/suite/wiring-map-model.test.ts index acea304309..4caca1033f 100644 --- a/ide/src/test/suite/wiring-map-model.test.ts +++ b/ide/src/test/suite/wiring-map-model.test.ts @@ -250,63 +250,62 @@ suite("wiringMapModel — connection-to-connection references (BACKLOG #5966)", 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), on each path flavour. - const POSIX: Graph = { + // 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: null, line: null }], + 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, under the graph's spelling", () => { - assert.deepStrictEqual(graphOpenTarget(POSIX, "/ws/cfg/a.py", 10, "/ws", false), { file: "/ws/cfg/a.py", line: 10 }); - assert.deepStrictEqual(graphOpenTarget(POSIX, "connections.toml", 12, "/ws", false), { - file: "connections.toml", - line: 12, - }); - // The same file named another way still resolves to the graph's own string. - assert.deepStrictEqual(graphOpenTarget(POSIX, "/ws/connections.toml", 1, "/ws", false), { - file: "connections.toml", - line: 1, - }); + 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("a page-chosen file the graph does not report is refused", () => { - for (const bad of ["/etc/passwd", "/ws/cfg/b.py", "../secrets.env", "cfg/../../etc/passwd", "//host/share/x.py"]) { - assert.strictEqual(graphOpenTarget(POSIX, bad, 1, "/ws", false), undefined, bad); + 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 malformed request, or no graph, is refused", () => { - for (const bad of [undefined, null, 7, "", { file: "/ws/cfg/a.py" }]) { - assert.strictEqual(graphOpenTarget(POSIX, bad, 1, "/ws", false), undefined, String(bad)); + 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); } - assert.strictEqual(graphOpenTarget(undefined, "/ws/cfg/a.py", 1, "/ws", false), 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(POSIX, "/ws/cfg/a.py", ln, "/ws", false), { file: "/ws/cfg/a.py", line: 1 }, String(ln)); + 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("Windows: compared case-insensitively against the workspace root, UNC refused", () => { - const WIN: Graph = { - version: 2, - inbound: [{ name: "IB_A", type: "mllp", router: "r", file: String.raw`C:\ws\cfg\a.py`, line: 3 }], - outbound: [{ name: "OB_T", type: "file", file: "connections.toml", line: 12 }], - routers: [], - handlers: [], - }; - const base = String.raw`c:\ws`; - assert.deepStrictEqual(graphOpenTarget(WIN, String.raw`c:\WS\cfg\A.py`, 3, base, true), { - file: String.raw`C:\ws\cfg\a.py`, - line: 3, - }); - assert.deepStrictEqual(graphOpenTarget(WIN, "connections.toml", 12, base, true), { file: "connections.toml", line: 12 }); - for (const bad of [String.raw`\\attacker\share\a.py`, String.raw`C:\Windows\win.ini`, String.raw`..\ws2\connections.toml`]) { - assert.strictEqual(graphOpenTarget(WIN, bad, 1, base, true), undefined, bad); + test("a location-less element does not make an empty file openable", () => { + const g: Graph = { ...G, 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)); } }); }); diff --git a/ide/src/wiringMap.ts b/ide/src/wiringMap.ts index d0d8ad35a3..70070a5dba 100644 --- a/ide/src/wiringMap.ts +++ b/ide/src/wiringMap.ts @@ -22,7 +22,8 @@ type Incoming = | { command: "ready" } | { command: "setFocus"; kind: ElementKind; name: string } | { command: "refresh" } - | { command: "open"; file: string; line: number } + // Untrusted: the page names these, so graphOpenTarget checks them (BACKLOG #4277). + | { command: "open"; file: unknown; line: unknown } | { command: "reveal"; kind: ElementKind; name: string }; export class WiringMapPanel { @@ -78,14 +79,21 @@ export class WiringMapPanel { await this.graph.refresh(); // fires onDidChangeTreeData -> post() } else if (m.command === "open") { // A page script names the file, so open only a file the current graph reports (BACKLOG #4277). - const target = graphOpenTarget(this.graph.getGraph(), m.file, m.line, workspaceDir() ?? ""); + const target = graphOpenTarget(this.graph.getGraph(), m.file, m.line); if (!target) { void vscode.window.showWarningMessage( "MessageFoundry: the Wiring Map asked to open a file the wiring graph does not report; it was not opened. Refresh the map and try again.", ); return; } - await vscode.commands.executeCommand("messagefoundry.openSource", target.file, target.line); + try { + await vscode.commands.executeCommand("messagefoundry.openSource", target.file, target.line); + } catch (err) { + // A graph file can be gone since the last refresh; say so rather than drop the rejection. + void vscode.window.showWarningMessage( + `MessageFoundry: could not open the source file: ${err instanceof Error ? err.message : String(err)}`, + ); + } } 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 31d34f331b..04179657ea 100644 --- a/ide/src/wiringMapModel.ts +++ b/ide/src/wiringMapModel.ts @@ -341,30 +341,26 @@ export function wiringMapPayload(g: Graph | undefined, focus: MapFocus | null): /** * 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 file it was sent, but a page script is not trusted to name - * one: the file must be one the current graph itself reports for an element, compared as fileKey - * resolved against `base` (the workspace root openSource resolves a relative path against). What - * comes back is the GRAPH's spelling of the file, never the page's, so openSource only ever sees a - * path the CLI printed. The line is only a cursor position; a value that is not a positive safe - * integer falls back to 1. + * 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 a file the current + * graph reports for an element. Exact equality, not a resolved or case-folded key, so nothing about + * the workspace root or the platform's case rules can widen the match; a refresh that respells a + * file only refuses the stale click until the map re-renders. 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 (a UNC share, say) still opens it. */ export function graphOpenTarget( g: Graph | undefined, file: unknown, line: unknown, - base: string, - win32: boolean = IS_WIN32, ): { file: string; line: number } | undefined { if (!g || typeof file !== "string" || file === "") { return undefined; } - const want = fileKey(base, file, win32); - const reported = [...g.inbound, ...g.routers, ...g.handlers, ...g.outbound] - .map((e) => e.file) - .find((f): f is string => typeof f === "string" && f !== "" && fileKey(base, f, win32) === want); - if (reported === undefined) { + const reports = (e: { file?: string | null }): boolean => e.file === file; + if (!(g.inbound.some(reports) || g.routers.some(reports) || g.handlers.some(reports) || g.outbound.some(reports))) { return undefined; } const ln = typeof line === "number" && Number.isSafeInteger(line) && line >= 1 ? line : 1; - return { file: reported, line: ln }; + return { file, line: ln }; } From 37363340baeb19c606ea68a8a3f5c9f4c27de44b Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 24 Nov 2026 09:27:58 -0600 Subject: [PATCH 3/3] IDE Wiring Map: test the open handler's decision, one source for openable files Review round 2. The open handler's decision moves into handleOpenRequest (wiringMapModel.ts, vscode-free, host calls injected), so node-side tests now pin that a refused file runs no command and a graph file reaches messagefoundry.openSource under the graph's spelling. graphOpenTarget reads openable files through collectElements, the same source the payload is built from, so an empty file is excluded by construction. Drops the imports round 1 left unused, and the refusal warning now says when no graph is loaded. A mutation that disables the guard turns 4 of the 22 suite tests red. BACKLOG #4277 --- ide/src/test/suite/wiring-map-model.test.ts | 67 ++++++++++++++++++++- ide/src/wiringMap.ts | 25 +++----- ide/src/wiringMapModel.ts | 61 ++++++++++++++++--- 3 files changed, 122 insertions(+), 31 deletions(-) diff --git a/ide/src/test/suite/wiring-map-model.test.ts b/ide/src/test/suite/wiring-map-model.test.ts index 4caca1033f..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, graphOpenTarget, 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 @@ -298,8 +298,12 @@ suite("wiringMapModel — graphOpenTarget refuses a file the graph did not repor assert.strictEqual(graphOpenTarget(undefined, "/ws/cfg/a.py", 1), undefined); }); - test("a location-less element does not make an empty file openable", () => { - const g: Graph = { ...G, handlers: [{ name: "h", edges: [], file: null, line: null }] }; + 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); }); @@ -309,3 +313,60 @@ suite("wiringMapModel — graphOpenTarget refuses a file the graph did not repor } }); }); + +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 70070a5dba..bf95f437c1 100644 --- a/ide/src/wiringMap.ts +++ b/ide/src/wiringMap.ts @@ -12,9 +12,8 @@ // posted map payload, so config-supplied names are never string-interpolated into markup. import * as vscode from "vscode"; import type { ElementKind } from "./graphModel"; -import { graphOpenTarget, wiringMapPayload, type MapFocus } from "./wiringMapModel"; +import { handleOpenRequest, wiringMapPayload, type MapFocus } from "./wiringMapModel"; import type { GraphProvider } from "./graphTree"; -import { workspaceDir } from "./cli"; import { STARTUP_BANNERS, openChannel, postToWebview } from "./webviewMessaging"; import { wiringMapScript } from "./wiringMapWebview"; @@ -22,7 +21,8 @@ type Incoming = | { command: "ready" } | { command: "setFocus"; kind: ElementKind; name: string } | { command: "refresh" } - // Untrusted: the page names these, so graphOpenTarget checks them (BACKLOG #4277). + // 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 }; @@ -79,21 +79,10 @@ export class WiringMapPanel { await this.graph.refresh(); // fires onDidChangeTreeData -> post() } else if (m.command === "open") { // A page script names the file, so open only a file the current graph reports (BACKLOG #4277). - const target = graphOpenTarget(this.graph.getGraph(), m.file, m.line); - if (!target) { - void vscode.window.showWarningMessage( - "MessageFoundry: the Wiring Map asked to open a file the wiring graph does not report; it was not opened. Refresh the map and try again.", - ); - return; - } - try { - await vscode.commands.executeCommand("messagefoundry.openSource", target.file, target.line); - } catch (err) { - // A graph file can be gone since the last refresh; say so rather than drop the rejection. - void vscode.window.showWarningMessage( - `MessageFoundry: could not open the source file: ${err instanceof Error ? err.message : String(err)}`, - ); - } + 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 04179657ea..31ac7ef166 100644 --- a/ide/src/wiringMapModel.ts +++ b/ide/src/wiringMapModel.ts @@ -10,7 +10,6 @@ // incompleteness is visible, never a silently shorter map (AC-3). Tested node-side like graphModel. import { normalize, type ElementKind, type Graph, type NormalGraph } from "./graphModel"; -import { fileKey, IS_WIN32 } from "./pathIdentity"; // The members live in a const so a webview page can be handed the same list its host is typed by // (ASVS 3.5.5, BACKLOG #1123). The type is derived from it, so the two cannot drift. @@ -342,25 +341,67 @@ export function wiringMapPayload(g: Graph | undefined, focus: MapFocus | null): /** * 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 a file the current - * graph reports for an element. Exact equality, not a resolved or case-folded key, so nothing about - * the workspace root or the platform's case rules can widen the match; a refresh that respells a - * file only refuses the stale click until the map re-renders. 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 (a UNC share, say) still opens it. + * 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" || file === "") { + if (!g || typeof file !== "string") { return undefined; } - const reports = (e: { file?: string | null }): boolean => e.file === file; - if (!(g.inbound.some(reports) || g.routers.some(reports) || g.handlers.some(reports) || g.outbound.some(reports))) { + 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)}`); + } +}