mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-18 14:08:21 +00:00
* perf: Serve JWT Bearer User Lookups From the Auth User Cache * perf: Batch Agent Permission Reads on the File Access Path * perf: Remember a Loaded Google Service Key Across Endpoint Config Reads * perf: Run Independent Startup, Tool, Skill and Erasure Reads Concurrently * fix: Drop JWT User Cache Writes That Race a User Mutation Confirm each cache write with a second read and evict when the user changed, so an in-flight lookup cannot repopulate an entry a credential change just evicted. The cache mode now arrives through the loader dependencies. * refactor: Move Agent Skill Access Reads Into packages/api The chat-completions and Responses controllers call loadSkillAccess instead of orchestrating the three reads in CJS. The preview route spec mocks the batched file access helper the middleware now calls. * test: Cover Agent File Access, JWT Bursts and Remote Skill Reads End to End * fix: Serve a Cached JWT User Only While Its updatedAt Is Current A cache hit now reads the stored updatedAt and deletion fence instead of the fence alone, so a document cached from a read that raced a user mutation is never served, even when the write landed after that mutation's eviction. * refactor: Move Startup Migration Checks Into packages/api * refactor: Inject the Startup Migration Checks Instead of Mongoose Models * fix: Serve Cached OpenID Users Only While Their updatedAt Is Current Moves the freshness check into getCurrentCachedAuthUserDoc and uses it on the OpenID bearer path as well as the local JWT one, so a document cached from a read that raced a credential or role change is never served there either. Fixes berry-13/LibreChat#432 * fix: Compare Authorization Fields on Every Auth Cache Hit updatedAt has millisecond resolution, so the freshness read also compares the token retirement stamps, role, tenant and two-factor state. * fix: Annotate the Auth Freshness Projection for Isolated Declarations * perf: Reuse the OpenID Cache Freshness Read for Deletion Admission A cache hit already proved the deletion fence is clear, so only users read from the database take the separate fence query. * refactor: Keep the OpenID Cache Freshness and Fence Decisions in packages/api * test: Exercise JWT Cache Freshness Against Real User Methods in MongoDB * fix: Stamp the JWT User Lookup So the Cache Write Fence Applies * fix: Reload a Google Service Key File When It Changes or Disappears The availability check remembers a loaded key only while the file's mtime and size are unchanged, and fetches URL sources every time as before. * fix: Recheck a Service Key File After a Permission Change
359 lines
11 KiB
JavaScript
359 lines
11 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',
|
|
loadJwtUser: jest.requireActual('@librechat/api').loadJwtUser,
|
|
getAuthUserDocCacheMode: () => 'off',
|
|
isTokenRetired: jest.requireActual('@librechat/api').isTokenRetired,
|
|
continueAfterBearerRetirement: jest.requireActual('@librechat/api').continueAfterBearerRetirement,
|
|
}));
|
|
|
|
jest.mock('~/models', () => ({
|
|
getUserById: jest.fn(),
|
|
updateUser: jest.fn(),
|
|
}));
|
|
|
|
jest.mock('~/cache/getLogStores', () => 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');
|
|
});
|
|
});
|
|
});
|