LibreChat/client/src/utils/redirect.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

163 lines
5.5 KiB
TypeScript

import { readTwoFactorSetupToken } from 'librechat-data-provider';
export const REDIRECT_PARAM = 'redirect_to';
export const SESSION_KEY = 'post_login_redirect_to';
/** Matches `/login` as a full path segment, with optional basename prefix (e.g. `/librechat/login/2fa`) */
const LOGIN_PATH_RE = /(?:^|\/)login(?:\/|$)/;
/**
* The URL parser removes every ASCII tab and newline before resolving, so a target
* like `/\t/evil.com` would clear the prefix checks below and then resolve to
* `//evil.com`. Reject C0 controls outright rather than trying to match the parser.
*/
function hasControlChar(url: string): boolean {
for (let i = 0; i < url.length; i++) {
if (url.charCodeAt(i) <= 0x1f) {
return true;
}
}
return false;
}
/** Confirms the target resolves to this origin, which is what the browser will actually navigate to */
function resolvesToSameOrigin(url: string): boolean {
const origin = typeof window === 'undefined' ? undefined : window.location?.origin;
if (origin == null || origin === 'null') {
return false;
}
try {
return new URL(url, origin).origin === origin;
} catch {
return false;
}
}
interface PostLoginRedirectWindow extends Window {
__librechatPostLoginRedirect?: string;
}
/**
* Session storage is blocked outright in embedded and private contexts, where it throws on access
* rather than returning null. The destination is a convenience, never a credential, so a blocked
* store must never take down the sign-in that carries it: access degrades to an in-memory mirror,
* which still reaches the hand-offs that stay inside the document.
*
* Storage stays authoritative whenever it answers, and the mirror is written only where storage
* refused. Mirroring every write would let a destination that storage has since dropped come back
* from the dead.
*/
const readStoredRedirect = (): string | null => {
try {
return window.sessionStorage.getItem(SESSION_KEY);
} catch {
return (window as PostLoginRedirectWindow).__librechatPostLoginRedirect ?? null;
}
};
const writeStoredRedirect = (value: string): void => {
try {
window.sessionStorage.setItem(SESSION_KEY, value);
delete (window as PostLoginRedirectWindow).__librechatPostLoginRedirect;
} catch {
(window as PostLoginRedirectWindow).__librechatPostLoginRedirect = value;
}
};
const dropStoredRedirect = (): void => {
delete (window as PostLoginRedirectWindow).__librechatPostLoginRedirect;
try {
window.sessionStorage.removeItem(SESSION_KEY);
} catch {
// Ignore unavailable storage.
}
};
/** Suffix rather than whole path, so a deployment served under a basename still matches. */
const SETUP_PATH_SUFFIX = '/login/2fa/setup';
/**
* The mandatory enrollment screen holding a live setup token is a destination in its own right, so
* it is exempt from the post-login redirects that would otherwise send an authenticated but
* unenrolled user on to the app.
*
* The router matches case-insensitively and ignores a trailing slash, so recognising the route by
* its exact spelling would strand anyone whose URL differs only in those: the screen renders, this
* reports it unprotected, and the redirect pulls them off a live enrollment. Normalize the same way
* the router does before comparing.
*/
export function isRequiredTwoFactorSetupRoute(): boolean {
const pathname = window.location.pathname.toLowerCase().replace(/\/+$/, '');
return pathname.endsWith(SETUP_PATH_SUFFIX) && !!readTwoFactorSetupToken();
}
/** Validates that a redirect target is a safe relative path (not an absolute or protocol-relative URL) */
export function isSafeRedirect(url: string): boolean {
if (hasControlChar(url)) {
return false;
}
if (!url.startsWith('/') || url.startsWith('//') || url.includes('\\')) {
return false;
}
const path = url.split('?')[0].split('#')[0];
if (LOGIN_PATH_RE.test(path)) {
return false;
}
return resolvesToSameOrigin(url);
}
/**
* Resolves the post-login redirect from URL params and sessionStorage,
* cleans up both sources, and returns the validated target (or null).
*/
export function getPostLoginRedirect(searchParams: URLSearchParams): string | null {
const target = peekPostLoginRedirect(searchParams);
dropStoredRedirect();
return target;
}
/** Drops a pending destination, so a later sign-in starts from a clean slate. */
export function clearPostLoginRedirect(): void {
dropStoredRedirect();
}
export function peekPostLoginRedirect(searchParams: URLSearchParams): string | null {
const urlRedirect = searchParams.get(REDIRECT_PARAM);
const storedRedirect = readStoredRedirect();
const target = urlRedirect ?? storedRedirect;
if (target == null || !isSafeRedirect(target)) {
return null;
}
return target;
}
/**
* Prefixes an app-relative path with the deployment's `<base href>` so installs served
* from a subdirectory navigate inside the router instead of to the host root. Reads the
* same source the router uses for its basename, keeping full-page navigations aligned
* with router navigations.
*/
export function withBasePath(path: string): string {
if (typeof document === 'undefined') {
return path;
}
const href = document.querySelector('base')?.getAttribute('href');
if (href == null || href === '') {
return path;
}
try {
const base = new URL(href, window.location.origin).pathname.replace(/\/+$/, '');
return base ? `${base}${path}` : path;
} catch {
return path;
}
}
export function persistRedirectToSession(value: string): void {
if (isSafeRedirect(value)) {
writeStoredRedirect(value);
}
}