The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): open bare local paths instead of "Invalid external URL"
Brooklyn Nicholsonb5a5ef7c1683
diff --git a/apps/desktop/electron/external-open.test.ts b/apps/desktop/electron/external-open.test.tsindex 53acd2eb916..6f190d04759 100644--- a/apps/desktop/electron/external-open.test.ts+++ b/apps/desktop/electron/external-open.test.ts@@ -17,6 +17,7 @@ function makeDeps(overrides: Partial<ExternalOpenDeps> = {}) {const calls = {opened: [] as string[],fileOpened: [] as string[],+ localOpened: [] as string[],notified: [] as Array<[string, string]>,logged: [] as string[]}@@ -32,6 +33,11 @@ function makeDeps(overrides: Partial<ExternalOpenDeps> = {}) {openFile: async raw => {calls.fileOpened.push(raw)},+ openLocalPath: async raw => {+ calls.localOpened.push(raw)++ return true+ },notifyFailure: (url, message) => calls.notified.push([url, message]),log: line => calls.logged.push(line),...overrides@@ -93,6 +99,64 @@ test('dispatches file:// URLs to openFile', async () => {assert.deepEqual(calls.fileOpened, ['file:///C:/x.html'])})+test('opens bare local paths through openLocalPath instead of rejecting them', async () => {+ const { deps, calls } = makeDeps()++ // Every shape `new URL()` cannot express as a web/file URL: a Windows drive+ // letter parses as the bogus `c:` scheme, POSIX/UNC/`~` make the parser+ // throw. All previously landed in the "Invalid external URL" reject.+ for (const raw of ['C:\\Users\\x\\a.md', 'C:/Work/report.html', '/tmp/report.pdf', '~/logs/desktop.log', '\\\\server\\share\\a.md']) {+ const result = await openExternalUrl(raw, deps)++ assert.deepEqual(result, { ok: true }, `expected ${raw} to open as a local path`)+ }++ assert.deepEqual(calls.localOpened, [+ 'C:\\Users\\x\\a.md',+ 'C:/Work/report.html',+ '/tmp/report.pdf',+ '~/logs/desktop.log',+ '\\\\server\\share\\a.md'+ ])+ assert.equal(calls.fileOpened.length, 0)+ assert.equal(calls.opened.length, 0)+})++test('resolves invalid, with the path logged, when openLocalPath cannot resolve the path', async () => {+ const { deps, calls } = makeDeps({+ openLocalPath: async () => false+ })++ const result = await openExternalUrl('C:\\Users\\x\\a.md', deps)++ assert.deepEqual(result, { ok: false, reason: 'invalid' })+ assert.ok(calls.logged.some(line => line.includes('openPath resolve rejected') && line.includes('C:\\Users\\x\\a.md')))+ assert.equal(calls.notified.length, 0)+})++test('notifies failed when openLocalPath throws', async () => {+ const { deps, calls } = makeDeps({+ openLocalPath: async () => {+ throw new Error('stat failed')+ }+ })++ const result = await openExternalUrl('/tmp/report.pdf', deps)++ assert.deepEqual(result, { ok: false, reason: 'failed', message: 'stat failed' })+ assert.deepEqual(calls.notified, [['/tmp/report.pdf', 'stat failed']])+})++test('opens protocol-relative URLs as https, never as local paths', async () => {+ const { deps, calls } = makeDeps()++ const result = await openExternalUrl('//cdn.example.com/img.png', deps)++ assert.deepEqual(result, { ok: true })+ assert.deepEqual(calls.opened, ['https://cdn.example.com/img.png'])+ assert.equal(calls.localOpened.length, 0)+})+test('wsl: spawns cmd.exe and resolves ok on the happy path', async () => {const spawned: string[] = []const proc = new EventEmitter() as unknown as ChildProcessdiff --git a/apps/desktop/electron/external-open.ts b/apps/desktop/electron/external-open.tsindex 25894f7157b..8e063185941 100644--- a/apps/desktop/electron/external-open.ts+++ b/apps/desktop/electron/external-open.ts@@ -15,6 +15,8 @@import type { ChildProcess, SpawnOptions } from 'node:child_process'+import { absolutizeProtocolRelativeUrl, looksLikeLocalFilesystemPath } from './local-filesystem-path'+export type ExternalOpenResult ={ ok: true } | { ok: false; reason: 'invalid' } | { ok: false; reason: 'failed'; message: string }@@ -53,6 +55,15 @@ export interface ExternalOpenDeps {spawn: (cmd: string, args: readonly string[], opts: SpawnOptions) => ChildProcessopenExternal: (url: string) => Promise<void>openFile: (rawUrl: string) => Promise<void>+ /**+ * Open a BARE local filesystem path (POSIX `/…`, `~/…`, Windows drive/UNC)+ * that is not a URL. The impl resolves it through the same audited+ * `resolveRequestedPathForIpc` the `file:` route uses, then `shell.openPath`,+ * with the same reveal-in-folder fallback and missing-file reporting as the+ * file route. Resolves false when the path could not be resolved at all+ * (rejected syntax, blocked device path) so the caller logs and reports it.+ */+ openLocalPath: (rawPath: string) => Promise<boolean>notifyFailure: (url: string, message: string) => voidlog: (line: string) => void}@@ -70,12 +81,35 @@ export function externalOpenErrorMessage(error: unknown): string {* so fire-and-forget callers can `void` the result safely.*/export async function openExternalUrl(rawUrl: string, deps: ExternalOpenDeps): Promise<ExternalOpenResult> {- const raw = String(rawUrl || '').trim()+ const raw = absolutizeProtocolRelativeUrl(String(rawUrl || '').trim())if (!raw) {return { ok: false, reason: 'invalid' }}+ // Bare local paths (POSIX `/…`, `~/…`, Windows drive/UNC) are not valid+ // absolute URLs: `new URL()` either throws (POSIX, `~`, UNC) or mis-parses a+ // drive letter as a bogus `c:` scheme, which the web allowlist below would+ // reject as "Invalid external URL". Route them through the audited file+ // resolver instead (hermes-agent 80946, 84361).+ if (looksLikeLocalFilesystemPath(raw)) {+ let opened: boolean++ try {+ opened = await deps.openLocalPath(raw)+ } catch (error) {+ return failOpen(deps, raw, error)+ }++ if (!opened) {+ deps.log(`[file] openPath resolve rejected: path=${raw}`)++ return { ok: false, reason: 'invalid' }+ }++ return { ok: true }+ }+let parsed: URLtry {diff --git a/apps/desktop/electron/local-filesystem-path.test.ts b/apps/desktop/electron/local-filesystem-path.test.tsnew file mode 100644index 00000000000..84c567317ea--- /dev/null+++ b/apps/desktop/electron/local-filesystem-path.test.ts@@ -0,0 +1,37 @@+import assert from 'node:assert/strict'++import { test } from 'vitest'++import { absolutizeProtocolRelativeUrl, looksLikeLocalFilesystemPath } from './local-filesystem-path'++test('recognizes every bare local filesystem path shape', () => {+ for (const value of [+ '/tmp/report.pdf',+ '///tmp/foo',+ '~/logs/desktop.log',+ 'C:\\Users\\x\\a.md',+ 'C:/Work/report.html',+ '\\\\server\\share\\a.md'+ ]) {+ assert.equal(looksLikeLocalFilesystemPath(value), true, `expected ${value} to be a local path`)+ }+})++test('does not claim URLs, relative names, or scheme-ful input', () => {+ for (const value of [+ '',+ 'hermes-support-slack.png',+ 'https://example.com',+ 'file:///tmp/report.pdf',+ 'mailto:[email protected]',+ '//cdn.example.com/img.png'+ ]) {+ assert.equal(looksLikeLocalFilesystemPath(value), false, `expected ${value} NOT to be a local path`)+ }+})++test('absolutizes protocol-relative URLs as https and leaves everything else alone', () => {+ assert.equal(absolutizeProtocolRelativeUrl('//cdn.example.com/img.png'), 'https://cdn.example.com/img.png')+ assert.equal(absolutizeProtocolRelativeUrl('///tmp/foo'), '///tmp/foo')+ assert.equal(absolutizeProtocolRelativeUrl('https://example.com'), 'https://example.com')+})diff --git a/apps/desktop/electron/local-filesystem-path.ts b/apps/desktop/electron/local-filesystem-path.tsnew file mode 100644index 00000000000..aa233fef9cc--- /dev/null+++ b/apps/desktop/electron/local-filesystem-path.ts@@ -0,0 +1,36 @@+// Pure path/URL predicates for the main-process opener. Kept out of+// external-open.ts so `classifyOpenTarget`-style decisions stay testable+// without electron, and so protocol-relative `//host/path` can never silently+// fall through to shell.openPath (it is a URL, not a filesystem path).+//+// A bare local path here is one that `new URL()` either rejects outright+// (POSIX `/…`, `~/…`, UNC `\\server\…`) or mis-parses as a bogus scheme+// (`C:\…` parses as protocol `c:`, which the web allowlist must never see).+// Chat/media links and artifact values arrive at the opener carrying exactly+// these shapes when the renderer hands a raw path through instead of a+// canonical `file://` URL (hermes-agent 80946, 84361).++const LOCAL_FILESYSTEM_PATH_RE = /^(?:\/(?!\/)|\/\/\/|~\/|[a-zA-Z]:[\\/]|\\\\)/+const PROTOCOL_RELATIVE_RE = /^\/\/[^/\s]/++/**+ * POSIX `/…`, `///…`, `~/…`, Windows drive (`C:\…`, `C:/…`) and UNC+ * (`\\server\…`) paths. NOT `//host/…` — that is a protocol-relative URL.+ */+export function looksLikeLocalFilesystemPath(value: string): boolean {+ return LOCAL_FILESYSTEM_PATH_RE.test(value)+}++/**+ * `//cdn.example.com/img.png` is a URL, not a filesystem path. `new URL()`+ * rejects it without a base; open it as `https:` so it reaches the system+ * browser instead of being treated as (or falling through to) a local path.+ * `///tmp/foo` stays a POSIX path.+ */+export function absolutizeProtocolRelativeUrl(value: string): string {+ if (PROTOCOL_RELATIVE_RE.test(value)) {+ return `https:${value}`+ }++ return value+}diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex d97cfa4f52c..50410d66833 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -2133,6 +2133,7 @@ const EXTERNAL_OPEN_DEPS: ExternalOpenDeps = {spawn: (cmd, args, opts) => spawn(cmd, args, opts),openExternal: url => shell.openExternal(url),openFile: openExternalFile,+ openLocalPath: openLocalFilesystemPath,notifyFailure: broadcastOpenFailed,log: rememberLog}@@ -2228,6 +2229,59 @@ async function openExternalFile(rawUrl: string) {}}+// The `hermes:openExternal` route for a BARE local filesystem path — a chat+// media link, a markdown href, or an artifacts-panel value carrying+// `C:\…`, `~/…`, `/…` or a UNC path instead of a `file://` URL. `new URL()`+// cannot express those (hermes-agent 80946): resolve through the same audited+// `resolveRequestedPathForIpc` the file route uses, then OPEN with the OS+// handler and fall back to reveal-in-folder, mirroring openExternalFile's+// missing-file guard so a dead path reports "File not found" instead of a+// silent no-op. Resolves false only when the path could not be resolved at+// all; open failures are logged (with the path, so "Failed to open path" is+// diagnosable) and still count as handled.+async function openLocalFilesystemPath(rawPath: string): Promise<boolean> {+ let localPath: string++ try {+ localPath = resolveRequestedPathForIpc(String(rawPath || ''), { purpose: 'Open external file' })+ } catch {+ return false+ }++ try {+ assertExistingPathForOpen(localPath, 'Open external file')+ } catch (error) {+ if (reportPreOpenStatFailure(error, rawPath, GUARD_REPORT_DEPS)) {+ return true+ }+ }++ try {+ const errorMessage = await shell.openPath(localPath)++ if (!errorMessage) {+ return true+ }++ // Include the path so "Failed to open path" is diagnosable (hermes-agent+ // 84361), then reveal: on Windows archive artifacts have no usable+ // association, and the reveal never re-opens so it can't loop (#53170).+ rememberLog(`[file] openPath failed: ${errorMessage}; path=${localPath}; revealing in folder instead`)++ try {+ shell.showItemInFolder(localPath)+ } catch (revealError) {+ rememberLog(`[file] showItemInFolder failed: ${revealError instanceof Error ? revealError.message : String(revealError)}; path=${localPath}`)+ }++ return true+ } catch (error) {+ rememberLog(`[file] openPath rejected: ${error instanceof Error ? error.message : String(error)}; path=${localPath}`)++ return true+ }+}+// An open failure is surfaced to the renderer as a modal carrying the URL, so// a dead system-browser click (e.g. no https handler registered on Linux) is// never silent. `code` tags the failure class (e.g. 'missing-file') so the