LibreChat/packages/data-provider/specs/api-endpoints.spec.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

132 lines
4.6 KiB
TypeScript

/**
* @jest-environment jsdom
*/
import {
agentQueuedTurn,
buildLoginRedirectUrl,
getSharedLinks,
agentQueuedTurnsByConversation,
enableTwoFactorSetup,
confirmTwoFactorSetup,
acknowledgeTwoFactorSetup,
finalizeTwoFactorSetup,
} from '../src/api-endpoints';
describe('buildLoginRedirectUrl', () => {
afterEach(() => {
window.history.replaceState({}, '', '/');
});
it('builds a login URL from explicit args', () => {
const result = buildLoginRedirectUrl('/c/new', '?q=hello', '');
expect(result).toBe('/login?redirect_to=%2Fc%2Fnew%3Fq%3Dhello');
});
it('encodes complex paths with query and hash', () => {
const result = buildLoginRedirectUrl('/c/new', '?q=hello&submit=true', '#section');
expect(result).toContain('redirect_to=');
const encoded = result.split('redirect_to=')[1];
expect(decodeURIComponent(encoded)).toBe('/c/new?q=hello&submit=true#section');
});
it('falls back to window.location when no args provided', () => {
window.history.replaceState({}, '', '/c/abc123?model=gpt-4#msg-5');
const result = buildLoginRedirectUrl();
const encoded = result.split('redirect_to=')[1];
expect(decodeURIComponent(encoded)).toBe('/c/abc123?model=gpt-4#msg-5');
});
it('returns plain /login when all location parts are empty (root)', () => {
window.history.replaceState({}, '', '/');
const result = buildLoginRedirectUrl();
expect(result).toBe('/login');
});
it('returns plain /login when pathname is /login (prevents recursive redirect)', () => {
const result = buildLoginRedirectUrl('/login', '?redirect_to=%2Fc%2Fnew', '');
expect(result).toBe('/login');
});
it('returns plain /login when window.location is already /login', () => {
window.history.replaceState({}, '', '/login?redirect_to=%2Fc%2Fabc');
const result = buildLoginRedirectUrl();
expect(result).toBe('/login');
});
it('returns plain /login for /login sub-paths', () => {
const result = buildLoginRedirectUrl('/login/2fa', '', '');
expect(result).toBe('/login');
});
it('returns plain /login for basename-prefixed /login (e.g. /librechat/login)', () => {
window.history.replaceState({}, '', '/librechat/login?redirect_to=%2Fc%2Fabc');
const result = buildLoginRedirectUrl();
expect(result).toBe('/login');
});
it('returns plain /login for basename-prefixed /login sub-paths', () => {
const result = buildLoginRedirectUrl('/librechat/login/2fa', '', '');
expect(result).toBe('/login');
});
it('returns plain /login for root path (no pointless redirect_to=/)', () => {
const result = buildLoginRedirectUrl('/', '', '');
expect(result).toBe('/login');
expect(result).not.toContain('redirect_to');
});
it('does NOT match paths where "login" is a substring of a segment', () => {
const result = buildLoginRedirectUrl('/c/loginhistory', '', '');
expect(result).toContain('redirect_to=');
expect(decodeURIComponent(result.split('redirect_to=')[1])).toBe('/c/loginhistory');
});
});
describe('getSharedLinks', () => {
it('encodes search and cursor values exactly once', () => {
const result = getSharedLinks(
25,
'createdAt',
'desc',
'100% ready & waiting',
'2030-01-01T00:00:00.000Z',
);
expect(result).toBe(
'/api/share?pageSize=25&sortBy=createdAt&sortDirection=desc&search=100%25%20ready%20%26%20waiting&cursor=2030-01-01T00%3A00%3A00.000Z',
);
});
});
describe('agent queued turns', () => {
it('encodes conversation identity exactly once', () => {
expect(agentQueuedTurnsByConversation('conversation/a b')).toBe(
'/api/agents/chat/queued-turns?conversationId=conversation%2Fa%20b',
);
});
it('encodes deduplicated known request identities as repeated bounded query values', () => {
expect(
agentQueuedTurnsByConversation('conversation/one', [
'request/one',
'request,two',
'request/one',
]),
).toBe(
'/api/agents/chat/queued-turns?conversationId=conversation%2Fone&clientRequestIds=request%2Fone&clientRequestIds=request%2Ctwo',
);
});
it('encodes queued-turn identity as one path segment', () => {
expect(agentQueuedTurn('turn/a b')).toBe('/api/agents/chat/queued-turns/turn%2Fa%20b');
});
});
describe('required two-factor setup endpoints', () => {
it('uses the purpose-specific setup paths', () => {
expect(enableTwoFactorSetup()).toBe('/api/auth/2fa/setup');
expect(confirmTwoFactorSetup()).toBe('/api/auth/2fa/setup/confirm');
expect(acknowledgeTwoFactorSetup()).toBe('/api/auth/2fa/setup/acknowledge');
expect(finalizeTwoFactorSetup()).toBe('/api/auth/2fa/setup/finalize');
});
});