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 committed 2026-07-03 19:41:25 +08:00
1 parent abef7973d9
commit 7b93eb2716
2 files changed
+37 -28

No files matched your search

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