A desktop coding agent with free access to frontier models.
TypeScript
fix: stricter unc host checks
ogsamrataefdd7aed36b
diff --git a/apps/app/src/react-app/domains/session/sync/attachment-file-part.ts b/apps/app/src/react-app/domains/session/sync/attachment-file-part.tsindex 6a4713ca..3af14c6a 100644--- a/apps/app/src/react-app/domains/session/sync/attachment-file-part.ts+++ b/apps/app/src/react-app/domains/session/sync/attachment-file-part.ts@@ -2,7 +2,7 @@ import type { FilePartInput, TextPartInput } from "@opencode-ai/sdk/v2/client";import type { ComposerAttachment } from "../../../../app/types";import { compressImageFile } from "./image-compression";-import { isWindowsWorkspaceRoot, joinWorkspaceRelativePath, toFileUrl } from "./prompt-file-parts";+import { isUsableUncPath, isWindowsWorkspaceRoot, joinWorkspaceRelativePath, toFileUrl } from "./prompt-file-parts";type AttachmentKind = "image" | "file";@@ -362,7 +362,10 @@ export async function composerAttachmentsToWorkspaceFileParts(input: {if (input.attachments.length === 0) return null;const workspaceRoot = input.workspaceRoot.trim();- if (!workspaceRoot) {+ // A device-path (`\\?\`, `\\.\`) or localhost UNC root has no file URL the+ // engine can read back, so it is as unusable as no root.+ const uncRoot = /^(\\\\|\/\/)/.test(workspaceRoot) && isWindowsWorkspaceRoot(workspaceRoot);+ if (!workspaceRoot || (uncRoot && !isUsableUncPath(workspaceRoot))) {throw new Error("Workspace path is unavailable; attachments could not be copied for tool access.");}diff --git a/apps/app/src/react-app/domains/session/sync/prompt-file-parts.ts b/apps/app/src/react-app/domains/session/sync/prompt-file-parts.tsindex 4bd251ea..3bf91f28 100644--- a/apps/app/src/react-app/domains/session/sync/prompt-file-parts.ts+++ b/apps/app/src/react-app/domains/session/sync/prompt-file-parts.ts@@ -74,20 +74,45 @@ export function toFileUrl(path: string, windows = false) {// `file:////server` has an empty host, which Windows cannot turn back into a// path. Elsewhere a host is invalid, so `//x` keeps the empty-host form.const unc = windows ? normalized.match(/^\/\/([^/]+)(\/.*)$/) : null;- if (unc && isUncHost(unc[1] ?? "")) return `file://${unc[1]}${encodeFilePath(unc[2] ?? "")}`;+ if (unc) {+ const url = `file://${unc[1] ?? ""}${encodeFilePath(unc[2] ?? "")}`;+ return isUncHost(unc[1] ?? "") && parsedHost(url) === (unc[1] ?? "").toLowerCase() ? url : "";+ }return `file://${encodeFilePath(normalized)}`;}-/** A plain UNC server name; device paths (`\\?\`, `\\.\`) and odd hosts are not. */+function parsedHost(url: string) {+ try {+ return new URL(url).hostname;+ } catch {+ return "";+ }+}++/**+ * A plain UNC server name. Device paths (`\\?\`, `\\.\`), `localhost` (the URL+ * parser drops it, leaving no host) and odd hosts are not.+ */function isUncHost(host: string) {- return /^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(host);+ return /^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(host) && host.toLowerCase() !== "localhost";+}++/** A Windows UNC root or path the engine can open (`\\server\share\...`). */+export function isUsableUncPath(path: string) {+ return toFileUrl(path, true) !== "";+}++function windowsPlatform() {+ return typeof navigator !== "undefined" && /^win/i.test(navigator.platform ?? "");}/** A workspace on Windows: a drive (`C:\\`) or UNC (`\\\\server\\share`) root. */export function isWindowsWorkspaceRoot(workspaceRoot: string) {const root = workspaceRoot.trim();- if (!root) return typeof navigator !== "undefined" && /^win/i.test(navigator.platform ?? "");- return /^[A-Za-z]:([\\/]|$)/.test(root) || /^(\\\\|\/\/)[^\\/]/.test(root);+ if (!root) return windowsPlatform();+ if (/^[A-Za-z]:([\\/]|$)/.test(root) || /^\\\\[^\\/]/.test(root)) return true;+ // `//x` is a valid POSIX root too; it is a UNC share only on Windows.+ return /^\/\/[^\\/]/.test(root) && windowsPlatform();}/**@@ -103,7 +128,7 @@ export function promptFileUrl(path: string, workspaceRoot: string) {const drive = /^[A-Za-z]:\//.test(normalized);if (isWindowsWorkspaceRoot(workspaceRoot)) {const unc = normalized.match(/^\/\/([^/]+)\/[^/]/);- if (!drive && !(unc && isUncHost(unc[1] ?? ""))) return "";+ if (!drive && !unc) return "";return toFileUrl(normalized, true);}if (!drive && !normalized.startsWith("/")) return "";diff --git a/apps/app/tests/attachment-file-part.test.ts b/apps/app/tests/attachment-file-part.test.tsindex 580b3cc4..47a44c78 100644--- a/apps/app/tests/attachment-file-part.test.ts+++ b/apps/app/tests/attachment-file-part.test.ts@@ -1,3 +1,4 @@+import { fileURLToPath } from "node:url";import { describe, expect, test } from "bun:test";import { attachmentNoteToUIParts } from "../src/react-app/domains/session/sync/usechat-adapter";@@ -408,6 +409,36 @@ describe("composer attachment file parts", () => {});});+ test("a POSIX // workspace root keeps a URL the engine can read", async () => {+ const { endpoint } = uploadRecorder("server-workspace-42");+ const file = new File(["<a/>"], "sitemap.xml", { type: "text/xml" });++ const parts = await composerAttachmentsToWorkspaceFileParts({+ attachments: [attachmentFor(file)],+ endpoint,+ sessionId: "ses_xml",+ workspaceRoot: "//srv/share/proj",+ createId: () => "nonce-xml",+ });++ const url = filePartUrl(parts, 0);+ expect(fileURLToPath(url, { windows: false })).toBe("//srv/share/proj/.opencode/omnirush/inbox/chat-attachments/ses_xml/nonce-xml-sitemap.xml");+ });++ test("device-path and localhost UNC roots are refused as unavailable", async () => {+ for (const workspaceRoot of ["\\\\?\\C:\\proj", "\\\\.\\pipe\\proj", "\\\\localhost\\share\\proj"]) {+ const { endpoint } = uploadRecorder("server-workspace-42");+ const file = new File(["<a/>"], "sitemap.xml", { type: "text/xml" });+ await expect(composerAttachmentsToWorkspaceFileParts({+ attachments: [attachmentFor(file)],+ endpoint,+ sessionId: "ses_xml",+ workspaceRoot,+ createId: () => "nonce-xml",+ })).rejects.toThrow("Workspace path is unavailable");+ }+ });+test("workspace binary attachments upload for tool access without a model-facing file part", async () => {const { endpoint, calls } = uploadRecorder("server-workspace-42");const file = new File([PPTX_BYTES], "recording.zip", { type: "application/zip" });diff --git a/apps/app/tests/prompt-file-url.test.ts b/apps/app/tests/prompt-file-url.test.tsindex 3b9d9994..6c24cf69 100644--- a/apps/app/tests/prompt-file-url.test.ts+++ b/apps/app/tests/prompt-file-url.test.ts@@ -29,7 +29,8 @@ describe("Windows workspace roots", () => {expect(isWindowsWorkspaceRoot("C:\\Users\\omar\\code")).toBe(true);expect(isWindowsWorkspaceRoot("d:/work")).toBe(true);expect(isWindowsWorkspaceRoot("\\\\server\\share\\proj")).toBe(true);- expect(isWindowsWorkspaceRoot("//server/share/proj")).toBe(true);+ // `//x` is a POSIX path too; only a Windows platform makes it a share.+ expect(isWindowsWorkspaceRoot("//server/share/proj")).toBe(false);expect(isWindowsWorkspaceRoot("/home/omar/code")).toBe(false);expect(isWindowsWorkspaceRoot("/Users/omar/code")).toBe(false);});@@ -76,6 +77,15 @@ describe("prompt file URLs", () => {expect(promptFileUrl("\\\\my host\\share\\a.ts", root)).toBe("");expect(toFileUrl("//?/C:/x", true)).not.toStartWith("file://?");});++ test("UNC hosts the URL parser would rewrite or drop are not sent", () => {+ const root = "C:\\Users\\omar\\code";+ for (const host of ["localhost", "LOCALHOST", "nas.01", "192.168.1", "0x7f.1"]) {+ expect(promptFileUrl(`\\\\${host}\\share\\a.ts`, root)).toBe("");+ }+ const ok = promptFileUrl("\\\\192.168.1.20\\share\\a.ts", root);+ expect(windowsPath(ok)).toBe("\\\\192.168.1.20\\share\\a.ts");+ });});describe("Windows first-line paths", () => {