diff --git a/packages/webui/src/App.tsx b/packages/webui/src/App.tsx index f1a380b3..588ba12e 100644 --- a/packages/webui/src/App.tsx +++ b/packages/webui/src/App.tsx @@ -20,6 +20,24 @@ export default function App() { ); } +// #194: read a one-shot `?token=` login param and immediately strip +// it from the URL (replaceState) so the credential doesn't linger in the +// address bar / browser history / Referer header. Named `token` to match the +// requested query key; the value is the WebUI login password. +function consumeUrlToken(): string | null { + if (typeof window === 'undefined') return null; + try { + const url = new URL(window.location.href); + const value = url.searchParams.get('token'); + if (!value) return null; + url.searchParams.delete('token'); + window.history.replaceState({}, '', `${url.pathname}${url.search}${url.hash}`); + return value; + } catch { + return null; + } +} + function AuthBoundary() { const [authChecked, setAuthChecked] = useState(false); const [authed, setAuthed] = useState(false); @@ -60,11 +78,28 @@ function AuthBoundary() { useEffect(() => { (async () => { - const ok = await client.status(); + let ok = await client.status(); + // #194: `?token=` in the URL logs in without typing the + // password. Only attempted when no stored token already authed us; the + // value is the WebUI login password (verified by /api/login server-side, + // so a wrong one just falls through to the login page). The param is + // consumed once and stripped from the URL first — a password in the + // address bar leaks via browser history / access logs / the Referer. + let urlPassword: string | undefined; + if (!ok) { + const pw = consumeUrlToken(); + if (pw) { + const result = await client.login(pw); + if (result.ok) { ok = true; urlPassword = pw; } + } + } if (ok) { setAuthed(true); setStatus('已连接'); setMustChange(await client.mustChangePassword()); + // Carry the URL password into the forced change-password gate so it + // needn't re-prompt for the old password (matches the login flow). + if (urlPassword) setLoginPassword(urlPassword); await refreshAgreements(); } setAuthChecked(true); diff --git a/packages/webui/src/lib/api/client.ts b/packages/webui/src/lib/api/client.ts index 7249081d..1219a085 100644 --- a/packages/webui/src/lib/api/client.ts +++ b/packages/webui/src/lib/api/client.ts @@ -37,27 +37,6 @@ function extractErrorMessage(payload: ErrorPayload, fallback: string): string { return payload.message || payload.error || fallback; } -// #194: seed the bearer token from a `?token=…` URL query param so an operator -// can bookmark / share a one-click auto-login link instead of typing the -// password every visit. The token is consumed once and IMMEDIATELY stripped -// from the URL (via replaceState) — a credential left in the address bar leaks -// through browser history, server access logs, and the Referer header. It's -// still validated server-side by the normal /api/status check, so a stale or -// forged value just falls through to the login page. -function consumeUrlToken(): string | null { - if (typeof window === 'undefined') return null; - try { - const url = new URL(window.location.href); - const token = url.searchParams.get('token'); - if (!token) return null; - url.searchParams.delete('token'); - window.history.replaceState({}, '', `${url.pathname}${url.search}${url.hash}`); - return token; - } catch { - return null; - } -} - class HttpApiClient implements ApiClient { private tokenStore: TokenStore; private currentToken: string | null; @@ -77,12 +56,7 @@ class HttpApiClient implements ApiClient { constructor(opts: CreateApiClientOptions = {}) { this.tokenStore = opts.tokenStore ?? localStorageTokenStore(DEFAULT_TOKEN_KEY); - // A `?token=…` link wins over any previously stored token, and is persisted - // so a later reload (URL now stripped) stays signed in. Validation happens - // in the normal status() gate, so an invalid link just shows the login page. - const urlToken = consumeUrlToken(); - this.currentToken = urlToken ?? this.tokenStore.load(); - if (urlToken) this.tokenStore.save(urlToken); + this.currentToken = this.tokenStore.load(); this.onUnauthorized = opts.onUnauthorized; this.processes = {