hermes-agent/apps/desktop/electron/app-installer-file.ts
ethernet faffafa4dd fix(desktop): validate the configured App Installer feed base and refuse descriptor redirects
resolveDesktopFeedBaseUrl returned `updates.desktop_feed_base_url` /
HERMES_DESKTOP_FEED_BASE_URL verbatim, so the non-channel Windows arm
downloaded `<base>/win32/<channel>/<channel>.appinstaller` from a plain-http
or credentialed base, followed redirects, opened the file and quit — while
the mac arm and the channel arm already ran channelPublicBase(). One
resolver (updater/feed-config.ts::resolveFeedBaseUrl) now applies the same
origin rule to both sources, and stageAppInstallerFile fetches with
redirect:'error' so the descriptor comes from the validated origin itself.
2026-09-21 18:44:30 -04:00

59 lines
1.6 KiB
TypeScript

import * as fs from 'node:fs/promises'
import * as path from 'node:path'
/** Download the descriptor before teardown. Windows verifies the referenced package. */
export async function stageAppInstallerFile(
url: string,
directory: string,
fetchFile: typeof fetch = fetch
): Promise<string> {
// No redirects: the descriptor must come from the validated feed origin
// itself, not from wherever that origin's operator (or a hijacked hop)
// points next. Windows verifies the referenced package, not this file.
const response = await fetchFile(url, { signal: AbortSignal.timeout(30_000), redirect: 'error' })
if (!response.ok || !response.body) {
throw new Error(`App Installer descriptor download failed: HTTP ${response.status}`)
}
const chunks: Uint8Array[] = []
let size = 0
const reader = response.body.getReader()
try {
for (;;) {
const { value, done } = await reader.read()
if (done) {
break
}
size += value.byteLength
if (size > 1024 * 1024) {
throw new Error('App Installer descriptor exceeds 1 MiB')
}
chunks.push(value)
}
} finally {
await reader.cancel()
}
if (size === 0) {
throw new Error('App Installer descriptor is empty')
}
await fs.mkdir(directory, { recursive: true })
const target = path.join(directory, 'update.appinstaller')
const temporary = `${target}.tmp`
try {
await fs.writeFile(temporary, Buffer.concat(chunks))
await fs.rename(temporary, target)
} finally {
await fs.rm(temporary, { force: true })
}
return target
}