fix(extension): tighten graphPanel path and line-number bounds

Two follow-up review fixes on the interactive-graph webview surface:

- `loadGraphHtml(projectId)` now resolves the projectId-derived path
  and verifies the result stays inside `GRAPH_DIR`. The
  `socraticode.openInteractiveGraph` command accepts an arbitrary
  argument from any caller (palette, sidebar, other extensions), so a
  value like `../../etc/passwd` would otherwise escape the cache
  directory via `path.join`. Suspicious projectIds are now rejected
  with a log entry and the function returns `undefined`.
- `handleWebviewMessage` now opens the document first, then clamps
  the requested line number against the document's actual `lineCount`
  before constructing a `Range`. The previous `m.line > 0` check
  prevented negatives but allowed absurdly large values (e.g.
  `Number.MAX_SAFE_INTEGER`) that would build a Range past the end of
  the file. Selection is set on the resolved editor.

Lint, typecheck, manifest tests and build all clean.
This commit is contained in:
Giancarlo Erra
2026-05-04 00:52:32 +01:00
parent 562a946053
commit c2d012fe4c
+34 -12
View File
@@ -58,7 +58,7 @@ export async function openInteractiveGraph(projectId?: string): Promise<void> {
currentPanel = undefined;
});
currentPanel.webview.onDidReceiveMessage((msg: unknown) => {
handleWebviewMessage(msg);
void handleWebviewMessage(msg);
});
currentPanel.webview.html = wrapHtml(html, currentPanel.webview);
}
@@ -66,7 +66,17 @@ export async function openInteractiveGraph(projectId?: string): Promise<void> {
async function loadGraphHtml(projectId?: string): Promise<string | undefined> {
let target: string | undefined;
if (projectId) {
target = path.join(GRAPH_DIR, `${projectId}.html`);
// The `socraticode.openInteractiveGraph` command accepts a projectId
// argument from any caller (palette, sidebar, other extensions). A
// value like `../../etc/passwd` would escape GRAPH_DIR via path.join.
// Resolve and confirm the result stays inside GRAPH_DIR.
const candidate = path.resolve(GRAPH_DIR, `${projectId}.html`);
const dirResolved = path.resolve(GRAPH_DIR);
if (candidate !== dirResolved && !candidate.startsWith(dirResolved + path.sep)) {
log(`Graph panel: rejecting suspicious projectId: ${projectId}`);
return undefined;
}
target = candidate;
} else {
// Pick the most recently modified graph file.
try {
@@ -185,7 +195,7 @@ function wrapHtml(html: string, webview: vscode.Webview): string {
return `<!DOCTYPE html><html><head>${cspMeta}${bridge}</head><body>${html}</body></html>`;
}
function handleWebviewMessage(msg: unknown): void {
async function handleWebviewMessage(msg: unknown): Promise<void> {
if (typeof msg !== "object" || msg === null) return;
const m = msg as { type?: string; path?: string; line?: number };
if (m.type !== "openFile" || typeof m.path !== "string") return;
@@ -212,13 +222,25 @@ function handleWebviewMessage(msg: unknown): void {
}
const uri = vscode.Uri.joinPath(ws.uri, ...normalised.split("/"));
const lineIndex =
typeof m.line === "number" && Number.isInteger(m.line) && m.line > 0 ? m.line - 1 : undefined;
const selection =
lineIndex !== undefined ? new vscode.Range(lineIndex, 0, lineIndex, 0) : undefined;
void vscode.window
.showTextDocument(uri, selection ? { selection } : {})
.then(undefined, (err) =>
log(`Graph panel: failed to open ${normalised}: ${(err as Error).message}`),
);
// Open the document first, then clamp the requested line number against
// the actual document length. A malformed message with a huge `m.line`
// (e.g. Number.MAX_SAFE_INTEGER) would otherwise build a Range far past
// the end of the file. We only check `m.line > 0` and `Number.isInteger`
// here because the upper bound depends on what we just opened.
let editor: vscode.TextEditor;
try {
editor = await vscode.window.showTextDocument(uri);
} catch (err) {
log(`Graph panel: failed to open ${normalised}: ${(err as Error).message}`);
return;
}
if (typeof m.line === "number" && Number.isInteger(m.line) && m.line > 0) {
const lastLine = Math.max(0, editor.document.lineCount - 1);
const lineIndex = Math.min(m.line - 1, lastLine);
const range = new vscode.Range(lineIndex, 0, lineIndex, 0);
editor.selection = new vscode.Selection(range.start, range.end);
editor.revealRange(range);
}
}