LibreChat/api/strategies/process.test.js
Danny Avila 0abbf3313d
🔐 fix: Apply Base Config Overrides Before Sign-In and Refuse Script Policy Links (#17351)
* 🔐 fix: Apply Base Config Overrides Before Sign-In and Refuse Script Policy Links

Sign-in decisions made before the user is known now read librechat.yaml plus the `__base__`
override of the request's tenant, or of the deployment outside a tenant, and fail closed
(`resolvePreAuthAppConfig`). Registration, the first phase of password reset and the first
email-domain check of the LDAP, OpenID, SAML and social strategies use it. The second check
applies the account's role overrides with or without a tenant, and a new account is checked
against the role it will be created with before anything is inserted. OpenID checks again when
the admin claim or role sync changes the role. Password reset issues no link when the
account's config cannot be read.

The signed-out `GET /api/config` serves Base `registration.socialLogins` and the pre-login
interface fields (`resolvePreLoginAppConfig`), falling back to librechat.yaml when overrides
cannot be read. `turnstile` becomes a base-only section, so Turnstile comes only from
librechat.yaml.

Config reads that merge documents stay in one tenant partition (`tenantPartition`, Mongoose
binding `tenantPartitionFilter`): the active tenant's documents, or the tenantless ones outside
a tenant. The config store's Mongoose binding reads, writes, lists and restores role documents,
`__base__` included, in that partition, and its `listActive` draws every document from it;
group and user documents stay addressed by id. Only a `list` with `acrossTenants` reads every
partition, which `listConfigTombstones` uses so the startup MCP tombstone report still names
each tenant's documents. The tenant-isolation conformance suite gains single-partition cases.

Privacy policy and terms of service `externalUrl` must be http(s), `mailto:`, `tel:`, relative
or blank (`isSafeLinkUrl`). Admin writes of another scheme are refused, a librechat.yaml link is
dropped with a warning at load, a stored one is dropped at merge, and every payload and the
client remove one that reaches them. The admin config meta reports
`features.preAuthBaseOverrides: true`.

* 🧪 test: Layer a Required-Field Override Through interface.currency Instead of Turnstile

Turnstile is set in librechat.yaml only now, so profile overrides of it are skipped and the
layering scenario could no longer see a role's Turnstile options completed by a user's site key.
The scenario now layers `interface.currency`, whose `code` and `rate` are both required: the role
stores only the rate, the user only the code, and /api/config serves both.

* 🛂 fix: Admit a First OpenID Login Under Its Claimed Role Before Creating the Account

The admin-role claim and role sync are now decided before a first-login OpenID account is created, and createUserOnce admits the email under that role's config, read in parallel with the USER role's, before inserting anything. A login the claimed role refuses creates no account and no start balance. Admitted logins are unchanged: the account is still created as USER (now explicitly), moved into the claimed role, re-checked and audited as before, with the claims decided once. An account a concurrent first login created gets the claims decided again for its own role and tenant.
2026-10-22 22:49:54 +02:00

455 lines
14 KiB
JavaScript

const { FileSources, ErrorTypes } = require('librechat-data-provider');
const { handleExistingUser, createSocialUser } = require('./process');
jest.mock('~/server/services/Files/strategies', () => ({
getStrategyFunctions: jest.fn(),
}));
jest.mock('~/server/services/Files/images/avatar', () => ({
resizeAvatar: jest.fn(),
}));
jest.mock('~/models', () => ({
updateUser: jest.fn(),
createUserIfAbsent: jest.fn(),
getUserById: jest.fn(),
findBalanceByUser: jest.fn(),
}));
jest.mock('~/server/services/Config', () => ({
getAppConfig: jest.fn().mockResolvedValue({}),
}));
jest.mock('@librechat/api', () => ({
provisionSocialUser: jest.requireActual('@librechat/api').provisionSocialUser,
getBalanceConfig: jest.fn(() => ({
enabled: false,
})),
}));
const { getStrategyFunctions } = require('~/server/services/Files/strategies');
const { resizeAvatar } = require('~/server/services/Files/images/avatar');
const { updateUser, getUserById, createUserIfAbsent } = require('~/models');
describe('handleExistingUser', () => {
beforeEach(() => {
jest.clearAllMocks();
process.env.CDN_PROVIDER = FileSources.local;
});
it('should not process the avatar when the provider supplies no avatarUrl (local storage)', async () => {
const oldUser = {
_id: 'user123',
avatar: null,
};
await handleExistingUser(oldUser, null);
expect(resizeAvatar).not.toHaveBeenCalled();
expect(updateUser).not.toHaveBeenCalled();
});
it('should not process the avatar when the provider supplies no avatarUrl (non-local storage)', async () => {
process.env.CDN_PROVIDER = FileSources.s3;
const oldUser = {
_id: 'user123',
avatar: null,
};
await handleExistingUser(oldUser, undefined);
expect(resizeAvatar).not.toHaveBeenCalled();
expect(updateUser).not.toHaveBeenCalled();
});
it('should still update the email when the provider supplies no avatarUrl', async () => {
process.env.CDN_PROVIDER = FileSources.s3;
const oldUser = {
_id: 'user123',
avatar: null,
email: 'old@example.com',
};
await handleExistingUser(oldUser, null, undefined, 'new@example.com');
expect(resizeAvatar).not.toHaveBeenCalled();
expect(updateUser).toHaveBeenCalledWith('user123', { email: 'new@example.com' });
});
it('should handle null avatar without throwing error', async () => {
const oldUser = {
_id: 'user123',
avatar: null,
};
const avatarUrl = 'https://example.com/avatar.png';
await handleExistingUser(oldUser, avatarUrl);
expect(updateUser).toHaveBeenCalledWith('user123', { avatar: avatarUrl });
});
it('should handle undefined avatar without throwing error', async () => {
const oldUser = {
_id: 'user123',
// avatar is undefined
};
const avatarUrl = 'https://example.com/avatar.png';
await handleExistingUser(oldUser, avatarUrl);
expect(updateUser).toHaveBeenCalledWith('user123', { avatar: avatarUrl });
});
it('should not update avatar if it has manual=true flag', async () => {
const oldUser = {
_id: 'user123',
avatar: 'https://example.com/avatar.png?manual=true',
};
const avatarUrl = 'https://example.com/new-avatar.png';
await handleExistingUser(oldUser, avatarUrl);
expect(updateUser).not.toHaveBeenCalled();
});
it('should update avatar for local storage when avatar has no manual flag', async () => {
const oldUser = {
_id: 'user123',
avatar: 'https://example.com/old-avatar.png',
};
const avatarUrl = 'https://example.com/new-avatar.png';
await handleExistingUser(oldUser, avatarUrl);
expect(updateUser).toHaveBeenCalledWith('user123', { avatar: avatarUrl });
});
it('should process avatar for non-local storage', async () => {
process.env.CDN_PROVIDER = 's3';
const mockProcessAvatar = jest.fn().mockResolvedValue('processed-avatar-url');
getStrategyFunctions.mockReturnValue({ processAvatar: mockProcessAvatar });
resizeAvatar.mockResolvedValue(Buffer.from('resized-image'));
const oldUser = {
_id: 'user123',
avatar: null,
};
const avatarUrl = 'https://example.com/avatar.png';
await handleExistingUser(oldUser, avatarUrl);
expect(resizeAvatar).toHaveBeenCalledWith({
userId: 'user123',
input: avatarUrl,
});
expect(mockProcessAvatar).toHaveBeenCalledWith({
buffer: Buffer.from('resized-image'),
userId: 'user123',
manual: 'false',
});
expect(updateUser).toHaveBeenCalledWith('user123', { avatar: 'processed-avatar-url' });
});
it('should not update if avatar already has manual flag in non-local storage', async () => {
process.env.CDN_PROVIDER = 's3';
const oldUser = {
_id: 'user123',
avatar: 'https://cdn.example.com/avatar.png?manual=true',
};
const avatarUrl = 'https://example.com/new-avatar.png';
await handleExistingUser(oldUser, avatarUrl);
expect(resizeAvatar).not.toHaveBeenCalled();
expect(updateUser).not.toHaveBeenCalled();
});
it('should handle avatar with query parameters but without manual flag', async () => {
const oldUser = {
_id: 'user123',
avatar: 'https://example.com/avatar.png?size=large&format=webp',
};
const avatarUrl = 'https://example.com/new-avatar.png';
await handleExistingUser(oldUser, avatarUrl);
expect(updateUser).toHaveBeenCalledWith('user123', { avatar: avatarUrl });
});
it('should handle empty string avatar', async () => {
const oldUser = {
_id: 'user123',
avatar: '',
};
const avatarUrl = 'https://example.com/avatar.png';
await handleExistingUser(oldUser, avatarUrl);
expect(updateUser).toHaveBeenCalledWith('user123', { avatar: avatarUrl });
});
it('should handle avatar with manual=false parameter', async () => {
const oldUser = {
_id: 'user123',
avatar: 'https://example.com/avatar.png?manual=false',
};
const avatarUrl = 'https://example.com/new-avatar.png';
await handleExistingUser(oldUser, avatarUrl);
expect(updateUser).toHaveBeenCalledWith('user123', { avatar: avatarUrl });
});
it('should handle oldUser being null gracefully', async () => {
const avatarUrl = 'https://example.com/avatar.png';
// This should throw an error when trying to access oldUser._id
await expect(handleExistingUser(null, avatarUrl)).rejects.toThrow();
});
it('should update email when it has changed', async () => {
const oldUser = {
_id: 'user123',
email: 'old@example.com',
avatar: 'https://example.com/avatar.png?manual=true',
};
const avatarUrl = 'https://example.com/avatar.png';
const newEmail = 'new@example.com';
await handleExistingUser(oldUser, avatarUrl, {}, newEmail);
expect(updateUser).toHaveBeenCalledWith('user123', { email: 'new@example.com' });
});
it('should update both avatar and email when both have changed', async () => {
const oldUser = {
_id: 'user123',
email: 'old@example.com',
avatar: null,
};
const avatarUrl = 'https://example.com/new-avatar.png';
const newEmail = 'new@example.com';
await handleExistingUser(oldUser, avatarUrl, {}, newEmail);
expect(updateUser).toHaveBeenCalledWith('user123', {
avatar: avatarUrl,
email: 'new@example.com',
});
});
it('should not update email when it has not changed', async () => {
const oldUser = {
_id: 'user123',
email: 'same@example.com',
avatar: 'https://example.com/avatar.png?manual=true',
};
const avatarUrl = 'https://example.com/avatar.png';
const sameEmail = 'same@example.com';
await handleExistingUser(oldUser, avatarUrl, {}, sameEmail);
expect(updateUser).not.toHaveBeenCalled();
});
it('should trim email before comparison and update', async () => {
const oldUser = {
_id: 'user123',
email: 'test@example.com',
avatar: 'https://example.com/avatar.png?manual=true',
};
const avatarUrl = 'https://example.com/avatar.png';
const newEmailWithSpaces = ' newemail@example.com ';
await handleExistingUser(oldUser, avatarUrl, {}, newEmailWithSpaces);
expect(updateUser).toHaveBeenCalledWith('user123', { email: 'newemail@example.com' });
});
it('should not update when email parameter is not provided', async () => {
const oldUser = {
_id: 'user123',
email: 'test@example.com',
avatar: 'https://example.com/avatar.png?manual=true',
};
const avatarUrl = 'https://example.com/avatar.png';
await handleExistingUser(oldUser, avatarUrl, {});
expect(updateUser).not.toHaveBeenCalled();
});
});
describe('createSocialUser', () => {
beforeEach(() => {
jest.clearAllMocks();
process.env.CDN_PROVIDER = FileSources.s3;
createUserIfAbsent.mockResolvedValue({ ok: true, value: { _id: 'newUser123' } });
getUserById.mockResolvedValue({ _id: 'newUser123' });
});
it('should not process the avatar when the provider supplies no avatarUrl', async () => {
await createSocialUser({
email: 'user@privaterelay.appleid.com',
avatarUrl: null,
provider: 'apple',
providerKey: 'appleId',
providerId: 'apple-sub-123',
username: 'user',
name: 'User',
emailVerified: true,
lookup: {
findUser: jest.fn(),
provider: 'apple',
providerId: 'apple-sub-123',
email: 'user@privaterelay.appleid.com',
},
});
expect(resizeAvatar).not.toHaveBeenCalled();
expect(updateUser).not.toHaveBeenCalled();
expect(getUserById).toHaveBeenCalledWith('newUser123');
});
describe('under the config of the role the account is created with', () => {
const { getAppConfig } = require('~/server/services/Config');
const params = {
email: 'user@example.com',
avatarUrl: 'https://example.com/avatar.png',
provider: 'google',
providerKey: 'googleId',
providerId: 'google-sub-123',
username: 'user',
name: 'User',
emailVerified: true,
lookup: {
findUser: jest.fn(),
provider: 'google',
providerId: 'google-sub-123',
email: 'user@example.com',
},
};
afterEach(() => {
getAppConfig.mockResolvedValue({});
});
it("refuses a domain the USER role's list excludes before creating the account", async () => {
getAppConfig.mockImplementation(async (options) =>
options?.role === 'USER' ? { registration: { allowedDomains: ['other.example'] } } : {},
);
const login = createSocialUser(params);
await expect(login).rejects.toMatchObject({
code: ErrorTypes.AUTH_FAILED,
message: 'Email domain not allowed',
});
expect(createUserIfAbsent).not.toHaveBeenCalled();
});
it("stores the new account's avatar with that config's file strategy", async () => {
process.env.CDN_PROVIDER = FileSources.local;
const processAvatar = jest.fn().mockResolvedValue('https://cdn.example/avatar.png');
getStrategyFunctions.mockReturnValue({ processAvatar });
resizeAvatar.mockResolvedValue(Buffer.from('avatar'));
getAppConfig.mockImplementation(async (options) =>
options?.role === 'USER' ? { fileStrategy: FileSources.s3 } : {},
);
await createSocialUser(params);
expect(getStrategyFunctions).toHaveBeenCalledWith(FileSources.s3);
expect(updateUser).toHaveBeenCalledWith('newUser123', {
avatar: 'https://cdn.example/avatar.png',
});
});
});
describe('concurrent first login', () => {
const params = {
email: 'user@example.com',
avatarUrl: null,
provider: 'google',
providerKey: 'googleId',
providerId: 'google-sub-123',
username: 'user',
name: 'User',
emailVerified: true,
};
beforeEach(() => {
createUserIfAbsent.mockResolvedValue({ ok: false, error: { code: 'user_exists' } });
});
it('handles the account the other request created as an existing user', async () => {
const winner = { _id: 'winner-id', provider: 'google', email: 'old@example.com' };
const findUser = jest.fn(async (query) =>
query.googleId === params.providerId ? winner : null,
);
const user = await createSocialUser({
...params,
lookup: {
findUser,
provider: 'google',
providerId: params.providerId,
email: params.email,
},
});
expect(user).toBe(winner);
expect(updateUser).toHaveBeenCalledWith('winner-id', { email: 'user@example.com' });
expect(getUserById).not.toHaveBeenCalled();
});
it("fails the login when the recovered tenant account's policy rejects the email", async () => {
const { getAppConfig } = require('~/server/services/Config');
getAppConfig.mockImplementation(async (options) =>
options?.tenantId ? { registration: { allowedDomains: ['other.example'] } } : {},
);
const winner = { _id: 'tenant-id', provider: 'google', email: params.email, tenantId: 't' };
const findUser = jest.fn(async (query) =>
query.googleId === params.providerId ? winner : null,
);
const login = createSocialUser({
...params,
lookup: {
findUser,
provider: 'google',
providerId: params.providerId,
email: params.email,
},
});
await expect(login).rejects.toMatchObject({
code: ErrorTypes.AUTH_FAILED,
message: 'Email domain not allowed',
});
expect(updateUser).not.toHaveBeenCalled();
getAppConfig.mockResolvedValue({});
});
it('fails the login when another provider took the email in the meantime', async () => {
const localUser = { _id: 'local-id', provider: 'local', email: params.email };
const findUser = jest.fn(async (query) => (query.email === params.email ? localUser : null));
const login = createSocialUser({
...params,
lookup: {
findUser,
provider: 'google',
providerId: params.providerId,
email: params.email,
},
});
await expect(login).rejects.toMatchObject({ code: ErrorTypes.AUTH_FAILED });
expect(updateUser).not.toHaveBeenCalled();
});
});
});