mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-16 19:36:54 +00:00
The `hermes:openExternal` route only accepted URL-parseable input, so every
bare local filesystem path a chat media link, a markdown href, or an
artifacts-panel value carried was a dead click: `new URL('C:\Users\x\a.md')`
parses a Windows drive letter as the bogus `c:` scheme (rejected by the web
allowlist as "Invalid external URL"), and POSIX `/...`, `~/...`, UNC and
relative paths make the parser throw into the same reject. Protocol-relative
`//cdn.example.com/img.png` URLs were also dead — the parser rejects them
without a base, and they are a URL, not a path.
- electron/local-filesystem-path.ts (new, from #84542): pure predicates —
`looksLikeLocalFilesystemPath` (POSIX, `~/`, drive, UNC; never `//host`)
and `absolutizeProtocolRelativeUrl` (`//host/...` opens as https).
- external-open.ts: a bare-path branch ahead of `new URL()` routes local
path shapes through a new `openLocalPath` dep; an unresolved path logs the
path and answers `invalid`, a thrown open reports through the existing
failure modal.
- main.ts: `openLocalFilesystemPath` resolves through the same audited
`resolveRequestedPathForIpc` as the `file:` route, runs the existing
missing-file pre-open stat guard, then `shell.openPath` with a
reveal-in-folder fallback — the reveal never re-opens, so archive artifacts
with no usable association can't loop (#53170). Open failures log the path
so "Failed to open path" is diagnosable.
Fixes https://github.com/NousResearch/hermes-agent/issues/80946
Co-authored-by: 686f6c61 <[email protected]>
Co-authored-by: Uttkarsh Tiwari <[email protected]>
36 lines
1.5 KiB
TypeScript
36 lines
1.5 KiB
TypeScript
// 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
|
|
}
|