diff --git a/packages/core/src/webui/server.ts b/packages/core/src/webui/server.ts index df79acef..9a1bef5d 100644 --- a/packages/core/src/webui/server.ts +++ b/packages/core/src/webui/server.ts @@ -83,14 +83,20 @@ const CONSENT_ALLOWLIST = new Set([ '/api/logout', ]); -// Endpoints that may authenticate via `?token=` query parameter. Only the -// SSE log stream is here because EventSource cannot set custom headers; all -// other endpoints MUST use the Authorization header so tokens never leak -// into access logs / Referer / browser history. -const TOKEN_QUERY_ALLOWLIST = new Set([ +// Endpoints that may authenticate via `?token=` query parameter. Reserved +// for SSE streams: EventSource cannot set custom headers, so these MUST +// accept the token via query. All other endpoints use the Authorization +// header so tokens never leak into access logs / Referer / browser history. +// +// Exported so the regression test can lock the invariant — a missing entry +// here silently 401s the EventSource and the whole push channel goes dark, +// which has happened (see /api/state/stream's first cut). +export const SSE_TOKEN_QUERY_PATHS: ReadonlySet = new Set([ '/api/logs/stream', '/api/debug/stream', + '/api/state/stream', ]); +const TOKEN_QUERY_ALLOWLIST = SSE_TOKEN_QUERY_PATHS; // uin = QQ number; 5–12 digits. Used to construct config file paths, // so we MUST refuse anything else (path traversal, NUL bytes, etc.). diff --git a/packages/core/tests/webui-sse-auth-allowlist.test.ts b/packages/core/tests/webui-sse-auth-allowlist.test.ts new file mode 100644 index 00000000..b4889361 --- /dev/null +++ b/packages/core/tests/webui-sse-auth-allowlist.test.ts @@ -0,0 +1,24 @@ +import { describe, expect, it } from 'vitest'; +import { SSE_TOKEN_QUERY_PATHS } from '../src/webui/server'; + +// Regression lock for a class of bug that has bricked an SSE endpoint +// before: EventSource cannot set Authorization headers, so every SSE +// path MUST appear in this allowlist or the auth middleware computes +// `queryToken = ''` and 401s every connect. A missing entry is silent — +// the only symptom is "live updates don't work, refresh fixes it" — so +// keep this list short and verified. +describe('SSE_TOKEN_QUERY_PATHS', () => { + it('contains every SSE stream the WebUI subscribes to', () => { + expect(SSE_TOKEN_QUERY_PATHS.has('/api/logs/stream')).toBe(true); + expect(SSE_TOKEN_QUERY_PATHS.has('/api/debug/stream')).toBe(true); + expect(SSE_TOKEN_QUERY_PATHS.has('/api/state/stream')).toBe(true); + }); + + it('does NOT carry any non-stream endpoint — token-via-query is reserved for SSE', () => { + // The auth middleware accepts `?token=...` for these paths only; any + // bearer-only endpoint listed here would leak tokens via access logs. + for (const path of SSE_TOKEN_QUERY_PATHS) { + expect(path.endsWith('/stream')).toBe(true); + } + }); +});