fix(webui): treat ?token= URL param as the login password (#194)

abef797 misread the request and seeded the value as a bearer session token
(which the operator would have to dig out of localStorage). The intent is
simpler: `?token=<你的WebUI登录密码>` — the value is the plain WebUI login
password, so the link is trivially constructable from something the operator
already knows.

- Revert the client-constructor bearer seeding from abef797.
- AuthBoundary: when no stored token already authed us and the URL carries
  ?token=, run it through client.login() (the normal password path). A correct
  password lands on the home page; a wrong one falls through to the login page
  (server-verified, no bypass). The password is carried into the forced
  change-password gate, matching the interactive login flow.
- The param is consumed once and stripped from the URL (replaceState) before
  the login call — a password in the address bar leaks via history / logs /
  Referer.

typecheck 12 包干净。
This commit is contained in:
DrSmoothl
2026-07-03 19:41:25 +08:00
parent abef7973d9
commit 7b93eb2716
2 changed files with 37 additions and 28 deletions
+36 -1
View File
@@ -20,6 +20,24 @@ export default function App() {
);
}
// #194: read a one-shot `?token=<password>` 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=<password>` 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);
+1 -27
View File
@@ -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 = {