LibreChat/packages/data-provider/src/twoFactor.ts
Marco Beretta 92432e95d5
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Votes / vote (full suite) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
Frontend Unit Tests / Client build recovery regression (push) Waiting to run
Frontend Unit Tests / Codegraph select (push) Waiting to run
Frontend Unit Tests / Build packages (push) Waiting to run
Frontend Unit Tests / TypeScript type checks (client) (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/client (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 1/2) (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 2/2) (push) Blocked by required conditions
Frontend Unit Tests / Vite build verification (push) Blocked by required conditions
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Waiting to run
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Blocked by required conditions
🔐 feat: Enforce Required Two-Factor Authentication (#14731)
* feat: enforce required two-factor authentication

Require local and LDAP accounts to enroll in TOTP before they can use the
app when ENFORCE_TWO_FACTOR_AUTHENTICATION is set. Sign-in, refresh, bearer
verification, optional share auth and image authorization all route an
unenrolled account into a staged setup flow, and every bearer credential
minted before enrollment or a password reset is retired.

Password reset retirement uses the existing credentialsChangedAt stamp, and
commitPasswordReset clears any staged enrollment in the same write. Passkey
sign-in follows the same enrollment gate as password sign-in.

* test: cover passkey sign-in handing off to required enrollment

* refactor: keep Mongoose user types out of two-factor auth contracts

* fix: withdraw a minted session when its retirement recheck fails

* fix: hold Agent API keys and the reset CLI to the required two-factor policy

* fix: adopt a setup token handed off after the setup screen mounted

* fix: move every tab that receives an enrollment hand-off into setup

* fix: read the enrollment gate's retirement cutoff in system scope

* fix: clear minted session cookies before the fallible session deletion

* fix: adopt a setup token persisted before the setup screen subscribed
2026-10-01 23:39:51 +02:00

70 lines
2.7 KiB
TypeScript

/**
* Hand-off channel for the required-enrollment setup token.
*
* The token is a bearer credential in its own right: it can generate a TOTP secret of the
* holder's choosing and then drive confirm, acknowledge, and finalize into a real session, with
* no victim secret involved. Carrying it in the query string would leave it in browser history,
* in access and reverse-proxy logs, and potentially in a Referer header, so it is handed to the
* setup screen out of band instead.
*
* Session storage survives the hard navigation the response interceptor performs and is scoped to
* the one tab. The in-memory mirror keeps the flow working where storage is blocked, which is why
* persisting reports whether it reached durable storage: a navigation that replaces the document
* would drop the mirror, so the interceptor stays inside the document when only the mirror holds.
*/
const SETUP_TOKEN_STORAGE_KEY = 'two_factor_setup_token';
const SETUP_TOKEN_EVENT = 'librechat:two-factor-setup-token';
interface TwoFactorSetupWindow extends Window {
__librechatTwoFactorSetupToken?: string;
}
/**
* Returns whether the token reached storage that outlives the current document. Callers that were
* about to replace the document need to know, because the in-memory mirror does not survive that.
*/
export function persistTwoFactorSetupToken(tempToken: string): boolean {
const trimmed = tempToken.trim();
(window as TwoFactorSetupWindow).__librechatTwoFactorSetupToken = trimmed;
let durable = true;
try {
window.sessionStorage.setItem(SETUP_TOKEN_STORAGE_KEY, trimmed);
} catch {
// Session storage can be blocked in embedded or private contexts.
durable = false;
}
window.dispatchEvent(new Event(SETUP_TOKEN_EVENT));
return durable;
}
/**
* Notifies a mounted setup screen when a token arrives after it rendered, as when a refresh on the
* setup route itself hands one off without navigating. Returns the unsubscribe function.
*/
export function subscribeTwoFactorSetupToken(listener: () => void): () => void {
window.addEventListener(SETUP_TOKEN_EVENT, listener);
return () => window.removeEventListener(SETUP_TOKEN_EVENT, listener);
}
export function readTwoFactorSetupToken(): string {
try {
const stored = window.sessionStorage.getItem(SETUP_TOKEN_STORAGE_KEY)?.trim();
if (stored) {
return stored;
}
} catch {
// Fall through to the in-memory mirror.
}
return (window as TwoFactorSetupWindow).__librechatTwoFactorSetupToken?.trim() ?? '';
}
export function clearTwoFactorSetupToken(): void {
delete (window as TwoFactorSetupWindow).__librechatTwoFactorSetupToken;
try {
window.sessionStorage.removeItem(SETUP_TOKEN_STORAGE_KEY);
} catch {
// Ignore unavailable storage.
}
}