LibreChat/api/strategies/jwtStrategy.spec.js
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

355 lines
10 KiB
JavaScript

const { SystemRoles } = require('librechat-data-provider');
let capturedVerifyCallback;
let capturedStrategyOptions;
const mockRunAsSystem = jest.fn((callback) => callback());
jest.mock('passport-jwt', () => ({
Strategy: jest.fn((opts, verifyCallback) => {
capturedStrategyOptions = opts;
capturedVerifyCallback = verifyCallback;
return { name: 'jwt' };
}),
ExtractJwt: {
fromAuthHeaderAsBearerToken: jest.fn(() => 'mock-extractor'),
},
}));
jest.mock('@librechat/data-schemas', () => ({
logger: { info: jest.fn(), warn: jest.fn(), debug: jest.fn(), error: jest.fn() },
runAsSystem: mockRunAsSystem,
}));
jest.mock('@librechat/api', () => ({
AGENT_TRIGGER_SCOPE: 'agent_trigger',
isTokenRetired: jest.requireActual('@librechat/api').isTokenRetired,
continueAfterBearerRetirement: jest.requireActual('@librechat/api').continueAfterBearerRetirement,
}));
jest.mock('~/models', () => ({
getUserById: jest.fn(),
updateUser: jest.fn(),
}));
const jwtLogin = require('./jwtStrategy');
const { getUserById, updateUser } = require('~/models');
function request(overrides = {}) {
return {
method: 'GET',
originalUrl: '/api/user',
headers: {},
...overrides,
};
}
function invokeVerify(payload, req = request()) {
return new Promise((resolve, reject) => {
capturedVerifyCallback(req, payload, (err, user, info) => {
if (err) {
return reject(err);
}
resolve({ user, info });
});
});
}
describe('jwtStrategy', () => {
beforeEach(() => {
jest.clearAllMocks();
updateUser.mockResolvedValue({});
jwtLogin();
});
it('coerces missing idOnTheSource to null for local users', async () => {
getUserById.mockResolvedValue({
_id: { toString: () => 'user-1' },
role: SystemRoles.USER,
});
const { user } = await invokeVerify({ id: 'user-1' });
expect(user.id).toBe('user-1');
expect(user.idOnTheSource).toBeNull();
expect(capturedStrategyOptions.passReqToCallback).toBe(true);
});
it('runs globally scoped authentication reads and writes in system context', async () => {
getUserById.mockResolvedValue({
_id: { toString: () => 'user-system' },
});
const { user } = await invokeVerify({ id: 'user-system' });
expect(user).toMatchObject({ id: 'user-system', role: SystemRoles.USER });
expect(mockRunAsSystem).toHaveBeenCalledTimes(2);
expect(getUserById).toHaveBeenCalledWith(
'user-system',
'-password -__v -totpSecret -backupCodes +agentTriggerDeletionStartedAt',
);
expect(updateUser).toHaveBeenCalledWith('user-system', { role: SystemRoles.USER });
});
it('preserves a stored idOnTheSource for federated users', async () => {
getUserById.mockResolvedValue({
_id: { toString: () => 'user-2' },
role: SystemRoles.USER,
idOnTheSource: 'entra-oid-123',
});
const { user } = await invokeVerify({ id: 'user-2' });
expect(user.idOnTheSource).toBe('entra-oid-123');
});
it('returns false when no user is found', async () => {
getUserById.mockResolvedValue(null);
const { user } = await invokeVerify({ id: 'missing' });
expect(user).toBe(false);
});
it('accepts a scoped trigger token only on an exact marked admission route', async () => {
getUserById.mockResolvedValue({
_id: { toString: () => 'user-3' },
role: SystemRoles.USER,
});
const { user } = await invokeVerify(
{ id: 'user-3', scope: 'agent_trigger' },
request({
method: 'POST',
originalUrl: '/trusted/base/api/agents/chat/agents?source=delivery',
headers: { 'x-lc-agent-trigger': '1' },
}),
);
expect(user.id).toBe('user-3');
});
it.each([
['an unrelated route', request({ method: 'POST', originalUrl: '/api/user' })],
[
'a missing transport marker',
request({ method: 'POST', originalUrl: '/api/agents/chat/agents' }),
],
[
'a non-POST request',
request({
method: 'GET',
originalUrl: '/api/agents/chat/steer/deliver',
headers: { 'x-lc-agent-trigger': '1' },
}),
],
])('rejects a scoped trigger token on %s', async (_label, req) => {
const { user, info } = await invokeVerify({ id: 'user-3', scope: 'agent_trigger' }, req);
expect(user).toBe(false);
expect(info).toEqual({ message: 'Agent trigger token is not valid for this endpoint' });
expect(getUserById).not.toHaveBeenCalled();
});
it('rejects ordinary and trigger-scoped tokens while account deletion is fenced', async () => {
getUserById.mockResolvedValue({
_id: { toString: () => 'user-3' },
role: SystemRoles.USER,
agentTriggerDeletionStartedAt: new Date('2026-08-17T12:00:00.000Z'),
});
const ordinary = await invokeVerify({ id: 'user-3' });
const trigger = await invokeVerify(
{ id: 'user-3', scope: 'agent_trigger' },
request({
method: 'POST',
originalUrl: '/api/agents/chat/steer/deliver',
headers: { 'x-lc-agent-trigger': '1' },
}),
);
expect(ordinary).toEqual({
user: false,
info: {
message: 'Account deletion is in progress',
code: 'ACCOUNT_DELETION_IN_PROGRESS',
},
});
expect(trigger).toEqual({
user: false,
info: {
message: 'Account deletion is in progress',
code: 'ACCOUNT_DELETION_IN_PROGRESS',
},
});
expect(getUserById).toHaveBeenCalledWith(
'user-3',
'-password -__v -totpSecret -backupCodes +agentTriggerDeletionStartedAt',
);
});
describe('credentialsChangedAt revocation', () => {
/** Reset landed 500ms into second 1700000010 */
const credentialsChangedAt = new Date(1700000010500);
const mockUserWithStamp = (stamp) => {
getUserById.mockResolvedValue({
_id: { toString: () => 'user-reset' },
role: SystemRoles.USER,
credentialsChangedAt: stamp,
});
};
it('rejects an access token issued before the password reset', async () => {
mockUserWithStamp(credentialsChangedAt);
const { user } = await invokeVerify({ id: 'user-reset', iat: 1700000009 });
expect(user).toBe(false);
});
it('accepts an access token issued after the password reset', async () => {
mockUserWithStamp(credentialsChangedAt);
const { user } = await invokeVerify({ id: 'user-reset', iat: 1700000011 });
expect(user.id).toBe('user-reset');
});
it('rejects a token whose issuing second started before the reset instant', async () => {
mockUserWithStamp(credentialsChangedAt);
const { user } = await invokeVerify({ id: 'user-reset', iat: 1700000010 });
expect(user).toBe(false);
});
it('rejects a token issued in the same second as a reset stamped on the second boundary', async () => {
mockUserWithStamp(new Date(1700000010000));
const { user } = await invokeVerify({ id: 'user-reset', iat: 1700000010 });
expect(user).toBe(false);
});
it('accepts a token dated after a boundary reset to the millisecond', async () => {
mockUserWithStamp(new Date(1700000010000));
const { user } = await invokeVerify({
id: 'user-reset',
iat: 1700000010,
issuedAtMs: 1700000010001,
});
expect(user.id).toBe('user-reset');
});
it('accepts a stale token when the user never changed credentials', async () => {
getUserById.mockResolvedValue({
_id: { toString: () => 'user-1' },
role: SystemRoles.USER,
});
const { user } = await invokeVerify({ id: 'user-1', iat: 1 });
expect(user.id).toBe('user-1');
});
it('rejects a token without iat once credentials have changed', async () => {
mockUserWithStamp(credentialsChangedAt);
const { user } = await invokeVerify({ id: 'user-reset' });
expect(user).toBe(false);
});
it('honors a stamp deserialized as a string', async () => {
mockUserWithStamp(credentialsChangedAt.toISOString());
const { user } = await invokeVerify({ id: 'user-reset', iat: 1700000009 });
expect(user).toBe(false);
});
});
describe('two-factor enrollment cutoff', () => {
const enrolledAt = new Date('2026-01-01T00:00:10.500Z');
const enrolledSecond = Math.floor(enrolledAt.getTime() / 1000);
const mockEnrolledUser = () =>
getUserById.mockResolvedValue({
_id: { toString: () => 'user-3' },
role: SystemRoles.USER,
twoFactorEnabled: true,
twoFactorEnrolledAt: enrolledAt,
});
it('refuses an access token minted before enrollment', async () => {
mockEnrolledUser();
const { user } = await invokeVerify({ id: 'user-3', iat: enrolledSecond - 1 });
expect(user).toBe(false);
});
it('refuses an access token minted before a password reset', async () => {
getUserById.mockResolvedValue({
_id: { toString: () => 'user-3' },
role: SystemRoles.USER,
twoFactorEnrolledAt: null,
credentialsChangedAt: enrolledAt,
});
const { user } = await invokeVerify({ id: 'user-3', iat: enrolledSecond - 1 });
expect(user).toBe(false);
});
it('accepts an access token minted after a password reset', async () => {
getUserById.mockResolvedValue({
_id: { toString: () => 'user-3' },
role: SystemRoles.USER,
twoFactorEnrolledAt: null,
credentialsChangedAt: enrolledAt,
});
const { user } = await invokeVerify({ id: 'user-3', iat: enrolledSecond + 60 });
expect(user.id).toBe('user-3');
});
it('accepts the session minted within the enrolling second', async () => {
mockEnrolledUser();
const { user } = await invokeVerify({ id: 'user-3', iat: enrolledSecond });
expect(user.id).toBe('user-3');
});
it('accepts an access token minted after enrollment', async () => {
mockEnrolledUser();
const { user } = await invokeVerify({ id: 'user-3', iat: enrolledSecond + 60 });
expect(user.id).toBe('user-3');
});
it('refuses an undatable token once the account is enrolled', async () => {
mockEnrolledUser();
const { user } = await invokeVerify({ id: 'user-3' });
expect(user).toBe(false);
});
it('leaves accounts that never enrolled untouched', async () => {
getUserById.mockResolvedValue({
_id: { toString: () => 'user-4' },
role: SystemRoles.USER,
twoFactorEnrolledAt: null,
});
const { user } = await invokeVerify({ id: 'user-4', iat: 0 });
expect(user.id).toBe('user-4');
});
});
});