Files
SnowLuma/packages/core/tests/webui-sse-auth-allowlist.test.ts
T
DrSmoothl bb752b13a1 fix(webui): add /api/state/stream to SSE token-query allowlist
Adversarial review caught: EventSource cannot set custom headers, so every
SSE endpoint MUST appear in the TOKEN_QUERY_ALLOWLIST or the auth
middleware at server.ts:404 computes queryToken='' and 401s every connect.

Without this fix the entire phase-3 SSE endpoint is dead on arrival:
clients silently fall back to the 30s REST reconcile and the original
"WebUI must refresh to see new state" bug is unfixed.

Export the allowlist as SSE_TOKEN_QUERY_PATHS so a regression test can
lock the invariant — a missing entry has no error log, the only symptom
is the silent stream failure.
2026-06-24 22:36:40 +08:00

25 lines
1.2 KiB
TypeScript

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);
}
});
});