mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-16 19:36:54 +00:00
* test(desktop): cover access-proxy headers on OAuth login URLs Connections extra headers must apply to /login and OAuth-partition requests, not only an exact remembered WebSocket URL. * fix(desktop): send extra gateway headers on OAuth login Cloudflare Access service tokens never reached the OAuth sign-in window because they were injected only on defaultSession and v1 remote config. Attach them on OAuth partitions and loadURL too. * fix(desktop): strip CR/LF from remote gateway header values The PR this builds on sanitized header values in remote-ws-headers.ts, on the resolution path only. That left the sibling consumers of decryptRemoteHeaders untouched: buildRemoteConnection and both connection-test paths still handed raw values to setHeader. Sanitize on the canonical normalizer instead, where header NAMES are already validated against REMOTE_HEADER_NAME_RE, so every consumer inherits it. normalizeRemoteHeaders only trim()'d, which strips a trailing newline but leaves an EMBEDDED CR/LF intact — the actual request-splitting vector. * refactor(desktop): drop the duplicate header normalizer from remote-ws-headers Header values now arrive sanitized from decryptRemoteHeaders, so the second sanitizeRemoteHeaderValue/sanitizeRemoteHeaderMap pair here was a duplicate of the canonical one in connection-config. The routing test keeps its original job (Connections headers reach /login, longest base URL wins); the CR/LF assertions move to connection-config.test.ts where the stripping now lives. * perf(desktop): memoize decrypted remote header sources onBeforeSendHeaders is now attached to every OAuth partition as well as defaultSession, and headersForRemoteRequest decrypts EVERY registry connection's headers. A safeStorage-encoded value costs a keychain round-trip per read, so that was N decryptions per subresource request where main previously did at most one behind a modeIsRemoteLike gate. Key the collected sources off the connection config and registry cache objects, which both readers already refresh on mtime change. --------- Co-authored-by: xxxigm <[email protected]>
215 lines
8.2 KiB
TypeScript
215 lines
8.2 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest'
|
|
|
|
import {
|
|
applyRemoteRequestHeaders,
|
|
attachRemoteRequestHeaderListener,
|
|
collectRemoteHeaderSources,
|
|
createRegistryGatewayWsUrlHandler,
|
|
createRemoteWsHeaderStore,
|
|
oauthLoginLoadUrlOptions,
|
|
type RegistryGatewayWsConnection,
|
|
resolveRemoteRequestHeaders
|
|
} from './remote-ws-headers'
|
|
|
|
const accessHeaders = {
|
|
'CF-Access-Client-Id': 'client-id',
|
|
'CF-Access-Client-Secret': 'client-secret'
|
|
}
|
|
|
|
function createHarness(connection: RegistryGatewayWsConnection) {
|
|
const store = createRemoteWsHeaderStore()
|
|
const ensureBackend = vi.fn(async () => connection)
|
|
const mintTicket = vi.fn(async () => 'fresh-ticket')
|
|
|
|
const handler = createRegistryGatewayWsUrlHandler({
|
|
ensureBackend,
|
|
mintTicket,
|
|
buildTicketUrl: baseUrl => `${baseUrl.replace(/^https:/, 'wss:')}/api/ws?region=us&ticket=fresh-ticket&profile=old`,
|
|
rememberHeaders: store.remember
|
|
})
|
|
|
|
return { ensureBackend, handler, mintTicket, store }
|
|
}
|
|
|
|
function expectRequestHeaders(
|
|
store: ReturnType<typeof createRemoteWsHeaderStore>,
|
|
url: string,
|
|
expected: Record<string, string> | undefined
|
|
) {
|
|
const callback = vi.fn()
|
|
|
|
applyRemoteRequestHeaders({ url, requestHeaders: { Origin: 'app://hermes' } }, callback, store.headersFor)
|
|
|
|
expect(callback).toHaveBeenCalledOnce()
|
|
expect(callback).toHaveBeenCalledWith(expected ? { requestHeaders: { Origin: 'app://hermes', ...expected } } : {})
|
|
}
|
|
|
|
function expectNoHeadersForNearbyUrls(store: ReturnType<typeof createRemoteWsHeaderStore>, exactUrl: string) {
|
|
const exact = new URL(exactUrl)
|
|
const unscoped = new URL(exact)
|
|
unscoped.searchParams.delete('profile')
|
|
const sibling = new URL(exact)
|
|
sibling.pathname = '/api/ws/sibling'
|
|
const otherProfile = new URL(exact)
|
|
otherProfile.searchParams.set('profile', 'analysis')
|
|
const otherCredential = new URL(exact)
|
|
|
|
if (otherCredential.searchParams.has('ticket')) {
|
|
otherCredential.searchParams.set('ticket', 'other-ticket')
|
|
} else {
|
|
otherCredential.searchParams.set('token', 'other-token')
|
|
}
|
|
|
|
const reordered = new URL(exact)
|
|
const entries = [...reordered.searchParams.entries()].reverse()
|
|
reordered.search = ''
|
|
|
|
for (const [name, value] of entries) {
|
|
reordered.searchParams.append(name, value)
|
|
}
|
|
|
|
for (const url of [unscoped, sibling, otherProfile, otherCredential, reordered]) {
|
|
expect(store.headersFor(url.toString())).toEqual({})
|
|
expectRequestHeaders(store, url.toString(), undefined)
|
|
}
|
|
}
|
|
|
|
describe('registry gateway WebSocket headers', () => {
|
|
it('evicts the least recently accessed exact URL', () => {
|
|
const store = createRemoteWsHeaderStore(2)
|
|
const firstUrl = 'wss://gateway.example/api/ws?token=first&profile=research'
|
|
const secondUrl = 'wss://gateway.example/api/ws?token=second&profile=research'
|
|
const thirdUrl = 'wss://gateway.example/api/ws?token=third&profile=research'
|
|
|
|
store.remember(firstUrl, accessHeaders)
|
|
store.remember(secondUrl, accessHeaders)
|
|
expect(store.headersFor('wss://gateway.example/api/ws?token=missing&profile=research')).toEqual({})
|
|
expect(store.headersFor(firstUrl)).toEqual(accessHeaders)
|
|
|
|
store.remember(thirdUrl, accessHeaders)
|
|
|
|
expect(store.headersFor(firstUrl)).toEqual(accessHeaders)
|
|
expect(store.headersFor(secondUrl)).toEqual({})
|
|
expect(store.headersFor(thirdUrl)).toEqual(accessHeaders)
|
|
})
|
|
|
|
it('updates headers without changing insertion recency', () => {
|
|
const store = createRemoteWsHeaderStore(2)
|
|
const firstUrl = 'wss://gateway.example/api/ws?token=first'
|
|
const secondUrl = 'wss://gateway.example/api/ws?token=second'
|
|
const thirdUrl = 'wss://gateway.example/api/ws?token=third'
|
|
|
|
store.remember(firstUrl, { 'CF-Access-Client-Id': 'old-client-id' })
|
|
store.remember(secondUrl, accessHeaders)
|
|
store.remember(firstUrl, { 'CF-Access-Client-Id': 'updated-client-id' })
|
|
store.remember(thirdUrl, accessHeaders)
|
|
|
|
expect(store.headersFor(firstUrl)).toEqual({})
|
|
expect(store.headersFor(secondUrl)).toEqual(accessHeaders)
|
|
expect(store.headersFor(thirdUrl)).toEqual(accessHeaders)
|
|
})
|
|
|
|
it('token path binds headers to the exact profile scoped URL', async () => {
|
|
const { ensureBackend, handler, mintTicket, store } = createHarness({
|
|
authMode: 'token',
|
|
baseUrl: 'https://gateway.example',
|
|
wsUrl: 'wss://gateway.example/api/ws?token=secret&trace=one&profile=old',
|
|
headers: accessHeaders,
|
|
profile: 'research',
|
|
sharedRemote: true
|
|
})
|
|
|
|
const result = await handler({ connectionId: 'remote-one', profile: 'research' })
|
|
const expectedUrl = 'wss://gateway.example/api/ws?token=secret&trace=one&profile=research'
|
|
|
|
expect(result).toBe(expectedUrl)
|
|
expect(ensureBackend).toHaveBeenCalledWith('remote-one', 'research')
|
|
expect(mintTicket).not.toHaveBeenCalled()
|
|
expect(store.headersFor(result)).toEqual(accessHeaders)
|
|
expectRequestHeaders(store, result, accessHeaders)
|
|
expectNoHeadersForNearbyUrls(store, result)
|
|
})
|
|
|
|
it('OAuth path binds headers to the exact fresh profile scoped URL', async () => {
|
|
const { handler, mintTicket, store } = createHarness({
|
|
authMode: 'oauth',
|
|
baseUrl: 'https://gateway.example',
|
|
wsUrl: 'wss://gateway.example/api/ws?ticket=stale',
|
|
headers: accessHeaders,
|
|
profile: 'research',
|
|
sharedRemote: true
|
|
})
|
|
|
|
const result = await handler({ connectionId: 'cloud-one', profile: 'research' })
|
|
const expectedUrl = 'wss://gateway.example/api/ws?region=us&ticket=fresh-ticket&profile=research'
|
|
|
|
expect(result).toBe(expectedUrl)
|
|
expect(mintTicket).toHaveBeenCalledOnce()
|
|
expect(mintTicket).toHaveBeenCalledWith('https://gateway.example', accessHeaders)
|
|
expect(store.headersFor(result)).toEqual(accessHeaders)
|
|
expectRequestHeaders(store, result, accessHeaders)
|
|
expectNoHeadersForNearbyUrls(store, result)
|
|
})
|
|
|
|
it('sharedRemote false preserves the original URL and exact header behavior', async () => {
|
|
const { handler, store } = createHarness({
|
|
authMode: 'token',
|
|
baseUrl: 'https://gateway.example',
|
|
wsUrl: 'wss://gateway.example/api/ws?trace=one&token=secret',
|
|
headers: accessHeaders,
|
|
profile: 'research',
|
|
sharedRemote: false
|
|
})
|
|
|
|
const result = await handler({ connectionId: 'remote-one', profile: 'research' })
|
|
|
|
expect(result).toBe('wss://gateway.example/api/ws?trace=one&token=secret')
|
|
expect(store.headersFor(result)).toEqual(accessHeaders)
|
|
expectRequestHeaders(store, result, accessHeaders)
|
|
expect(store.headersFor('wss://gateway.example/api/ws?token=secret&trace=one')).toEqual({})
|
|
})
|
|
})
|
|
|
|
describe('OAuth login and registry extra headers', () => {
|
|
it('applies Connections extra headers to /login, not only an exact WebSocket URL', () => {
|
|
const sources = collectRemoteHeaderSources({
|
|
connections: [{ kind: 'local' }, { kind: 'remote', url: 'https://gateway.example', headers: accessHeaders }],
|
|
v1Remote: { url: 'https://other.example', headers: { 'CF-Access-Client-Id': 'v1-only' } }
|
|
})
|
|
|
|
expect(resolveRemoteRequestHeaders('https://gateway.example/login', { sources })).toEqual(accessHeaders)
|
|
expect(resolveRemoteRequestHeaders('https://gateway.example/api/status', { sources })).toEqual(accessHeaders)
|
|
expect(oauthLoginLoadUrlOptions(accessHeaders)).toEqual({
|
|
extraHeaders: 'CF-Access-Client-Id: client-id\nCF-Access-Client-Secret: client-secret'
|
|
})
|
|
expect(resolveRemoteRequestHeaders('https://other.example/login', { sources })).toEqual({
|
|
'CF-Access-Client-Id': 'v1-only'
|
|
})
|
|
})
|
|
|
|
it('injects extra headers on an OAuth partition session, not only defaultSession', () => {
|
|
const listeners = []
|
|
|
|
const oauthSession = {
|
|
webRequest: {
|
|
onBeforeSendHeaders: listener => {
|
|
listeners.push(listener)
|
|
}
|
|
}
|
|
}
|
|
|
|
const sources = collectRemoteHeaderSources({
|
|
connections: [{ kind: 'remote', url: 'https://gateway.example', headers: accessHeaders }]
|
|
})
|
|
|
|
attachRemoteRequestHeaderListener(oauthSession, url => resolveRemoteRequestHeaders(url, { sources }))
|
|
|
|
const callback = vi.fn()
|
|
listeners[0]({ url: 'https://gateway.example/login', requestHeaders: { Origin: 'app://hermes' } }, callback)
|
|
|
|
expect(listeners).toHaveLength(1)
|
|
expect(callback).toHaveBeenCalledWith({
|
|
requestHeaders: { Origin: 'app://hermes', ...accessHeaders }
|
|
})
|
|
})
|
|
})
|