hermes-agent/apps/desktop/electron/link-title-url.test.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

56 lines
2.6 KiB
TypeScript

import assert from 'node:assert/strict'
import { describe, test } from 'vitest'
import { canonicalTitleCacheKey, isFetchableHttpUrl } from './link-title-url'
// #93893: the renderer can send ANY href-shaped string to the
// hermes:fetchLinkTitle IPC; before these guards, an unparseable string
// became its own cache key (canonicalTitleCacheKey returned the raw value)
// and flowed to the hidden title window's loadURL(), producing repeating
// `Failed to load URL: … ERR_NAME_NOT_RESOLVED` console noise.
describe('isFetchableHttpUrl', () => {
test('admits absolute http and https URLs', () => {
assert.equal(isFetchableHttpUrl('https://example.com/docs'), true)
assert.equal(isFetchableHttpUrl('http://example.com'), true)
assert.equal(isFetchableHttpUrl('https://example.com/a/b?x=1'), true)
})
test('rejects leaked directive markup before it can reach loadURL', () => {
assert.equal(isFetchableHttpUrl('@url:`https://oauth2:%[email protected]`'), false)
assert.equal(isFetchableHttpUrl('@url:https://example.com'), false)
})
test('rejects non-http schemes, placeholders, and garbage', () => {
assert.equal(isFetchableHttpUrl('file:///etc/passwd'), false)
assert.equal(isFetchableHttpUrl('mailto:[email protected]'), false)
assert.equal(isFetchableHttpUrl('javascript:alert(1)'), false)
assert.equal(isFetchableHttpUrl('https://example.com'), true) // control
assert.equal(isFetchableHttpUrl('not a url'), false)
assert.equal(isFetchableHttpUrl('printf("hello %s")'), false)
assert.equal(isFetchableHttpUrl(''), false)
})
})
describe('canonicalTitleCacheKey', () => {
test('never returns the raw string for unparseable input', () => {
// The passthrough hole: an unparseable value used to become a
// loadable-looking cache key. It must collapse to '' instead.
const junk = '@url:`https://oauth2:%[email protected]`'
assert.equal(canonicalTitleCacheKey(junk), '')
assert.equal(canonicalTitleCacheKey('not a url'), '')
assert.equal(canonicalTitleCacheKey(''), '')
})
test('builds a host+path+search key, normalizing www and trailing slashes', () => {
assert.equal(canonicalTitleCacheKey('https://www.example.com/docs/'), 'example.com/docs')
assert.equal(canonicalTitleCacheKey('https://example.com'), 'example.com/')
assert.equal(canonicalTitleCacheKey('https://example.com/search?q=hi'), 'example.com/search?q=hi')
// Same page, same key (that is the point of a cache key).
assert.equal(
canonicalTitleCacheKey('http://www.example.com/docs///'),
canonicalTitleCacheKey('https://example.com/docs')
)
})
})