mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-25 17:18:11 +00:00
fix(desktop): prevent cross-session leak in background queue drain
A background queue drain (fromQueue: true) whose runtime binding was reaped by the gateway fires with sessionId=null. The expression options?.sessionId ?? activeSessionIdRef.current falls back to whichever runtime id the foreground happens to hold, landing the queued prompt in the session the user is currently viewing instead of the session that owns the queue entry — a cross-session message leak. Guard the fallback: only inherit the foreground runtime when the drain targets the current view (no storedSessionId, or it matches the foreground). A background drain (storedSessionId differs) is left with sessionId=null so the existing session.resume path rebinds the correct runtime before prompt.submit fires. Includes a regression test: "a fromQueue drain with null runtime id does NOT land in the foreground session (cross-session leak guard)". All 53 existing tests pass.
This commit is contained in:
parent
95a566b1e7
commit
062d261955
3 changed files with 70 additions and 1 deletions
|
|
@ -1162,6 +1162,62 @@ describe('usePromptActions submit / queue drain semantics', () => {
|
|||
expect($busy.get()).toBe(false)
|
||||
})
|
||||
|
||||
it('a fromQueue drain with null runtime id does NOT land in the foreground session (cross-session leak guard)', async () => {
|
||||
// The cross-session leak: a background drain fires with sessionId=null
|
||||
// (the stored session's runtime was reaped by the gateway). Without the
|
||||
// guard, `null ?? activeSessionIdRef.current` falls back to whichever
|
||||
// runtime id the foreground happens to hold — landing the queued prompt
|
||||
// in the chat the user is currently viewing, NOT the session that owns
|
||||
// the queue entry. The drain must instead go through session.resume to
|
||||
// rebind the correct runtime before submitting.
|
||||
const requestGateway = vi.fn(
|
||||
async (method: string) =>
|
||||
(method === 'session.resume' ? { session_id: 'rt-session-a-rebound' } : {}) as never
|
||||
)
|
||||
|
||||
let handle: HarnessHandle | null = null
|
||||
render(
|
||||
<Harness
|
||||
activeSessionId={'rt-foreground'}
|
||||
storedSessionId={'rt-foreground'}
|
||||
getRuntimeIdForStoredSession={() => null}
|
||||
onReady={h => (handle = h)}
|
||||
refreshSessions={async () => undefined}
|
||||
requestGateway={requestGateway}
|
||||
/>
|
||||
)
|
||||
|
||||
// Background drain: sessionId=null (binding reaped), storedSessionId
|
||||
// points to a DIFFERENT session than the foreground.
|
||||
const accepted = await handle!.submitText('queued for background session', {
|
||||
fromQueue: true,
|
||||
sessionId: null,
|
||||
storedSessionId: 'stored-session-a'
|
||||
})
|
||||
|
||||
expect(accepted).toBe(true)
|
||||
// Must resume the correct stored session to get the right runtime id.
|
||||
expect(requestGateway).toHaveBeenCalledWith('session.resume', {
|
||||
session_id: 'stored-session-a',
|
||||
source: 'desktop'
|
||||
})
|
||||
// The prompt must land in the resumed session, NOT the foreground.
|
||||
expect(requestGateway).toHaveBeenCalledWith(
|
||||
'prompt.submit',
|
||||
{
|
||||
session_id: 'rt-session-a-rebound',
|
||||
text: 'queued for background session'
|
||||
},
|
||||
1_800_000
|
||||
)
|
||||
// The invariant: the foreground runtime never receives the prompt.
|
||||
expect(
|
||||
requestGateway.mock.calls.every(
|
||||
([method, params]) => method !== 'prompt.submit' || params?.session_id !== 'rt-foreground'
|
||||
)
|
||||
).toBe(true)
|
||||
})
|
||||
|
||||
it('a rejected fromQueue drain returns false (entry stays queued) and a later retry sends it', async () => {
|
||||
// A stale-session 404 must not strand the queued entry: submitPrompt returns
|
||||
// false on failure so the composer keeps it, and the edge-independent
|
||||
|
|
|
|||
|
|
@ -167,7 +167,19 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
|
|||
const targetStartedInCurrentView =
|
||||
!targetStoredSessionId || targetStoredSessionId === selectedStoredSessionIdRef.current
|
||||
|
||||
let sessionId: null | string = options?.sessionId ?? activeSessionIdRef.current
|
||||
// A queued/background drain whose runtime binding was reaped must NOT
|
||||
// inherit the foreground runtime id when its storedSessionId targets a
|
||||
// different session — that would land the queued prompt in whichever
|
||||
// session the user happens to be viewing (cross-session leak). When the
|
||||
// drain is for the current view (no storedSessionId, or it matches the
|
||||
// foreground), the foreground runtime is correct and must be kept.
|
||||
const isBackgroundQueueDrain = Boolean(
|
||||
options?.fromQueue &&
|
||||
options?.storedSessionId &&
|
||||
options.storedSessionId !== selectedStoredSessionIdRef.current
|
||||
)
|
||||
|
||||
let sessionId: null | string = options?.sessionId ?? (isBackgroundQueueDrain ? null : activeSessionIdRef.current)
|
||||
|
||||
// Pin the foreground session context for the whole async submit pipeline.
|
||||
// Without this, a fast session switch during session.resume / file.attach
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
theone139344
|
||||
Loading…
Add table
Add a link
Reference in a new issue