hermes-agent/apps/desktop/electron/link-title-url.ts
Brooklyn Nicholson 723c50233b fix(desktop): link-title pipeline refuses non-http(s) input before loadURL (#93893)
The hermes:fetchLinkTitle IPC handed whatever string the renderer sent
straight to the title tiers: canonicalTitleCacheKey's catch block
returned the RAW value (so leaked @url: markup became a loadable-looking
cache key), and the value then reached curl and the hidden title
window's loadURL(), surfacing as repeating
`Failed to load URL: @url:… ERR_NAME_NOT_RESOLVED` console noise on
printf-style code snippets.

Two layers, both in a new pure electron/link-title-url.ts module so the
admission rule is testable without Electron: fetchLinkTitle bails on any
input that does not parse as an absolute http(s) URL, and the cache-key
builder collapses unparseable input to '' instead of the raw string.

Co-authored-by: beplee <[email protected]>
2026-09-26 20:29:47 -05:00

50 lines
1.6 KiB
TypeScript

/**
* URL admission for the link-title pipeline.
*
* The renderer's title-fetch path can send ANY href-shaped string to the
* ``hermes:fetchLinkTitle`` IPC; the main process must re-validate before
* anything reaches curl or the hidden title window's ``loadURL()``. A leaked
* directive-shaped string (``@url:`https://…```, #93893) navigates Chromium to
* a non-URL and surfaces as repeating ``ERR_NAME_NOT_RESOLVED`` console noise.
*
* Pure and dependency-free so the admission rule is testable without
* Electron; ``main.ts`` imports both helpers.
*/
/** True only for strings that parse as absolute http(s) URLs. */
export function isFetchableHttpUrl(raw: string): boolean {
let url: URL
try {
url = new URL(raw)
} catch {
return false
}
return url.protocol === 'http:' || url.protocol === 'https:'
}
/**
* Cache key for a fetched title: host + normalized path + search, or '' when
* the input is not a parseable URL (never the raw string — an unparseable
* value must not become a loadable-looking key).
*/
export function canonicalTitleCacheKey(rawUrl: string): string {
const value = String(rawUrl || '').trim()
if (!value) {
return ''
}
try {
const url = new URL(value)
const host = url.hostname.replace(/^www\./i, '').toLowerCase()
const pathname = url.pathname === '/' ? '/' : url.pathname.replace(/\/+$/, '') || '/'
return `${host}${pathname}${url.search || ''}`
} catch {
// Not a parseable URL (e.g. leaked @url: markup): an empty key makes every
// consumer bail out instead of feeding the string downstream (#93893).
return ''
}
}