mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-18 22:14:27 +00:00
* 🔐 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.
92 lines
2.9 KiB
TypeScript
92 lines
2.9 KiB
TypeScript
import { logger } from '@librechat/data-schemas';
|
|
import { ErrorTypes, SystemRoles } from 'librechat-data-provider';
|
|
import type { AppConfig, UserRecord } from '@librechat/data-schemas';
|
|
import type {
|
|
GetAppConfig,
|
|
GetBalanceConfig,
|
|
ProvisionedUser,
|
|
FindUserByFields,
|
|
FindBalanceByUser,
|
|
CreateUserIfAbsent,
|
|
LoginUserResolution,
|
|
} from './provision';
|
|
import { createUserOnce } from './provision';
|
|
|
|
/** Finds the account an LDAP login continues as; one owned by another provider fails the login. */
|
|
export async function findLdapUser({
|
|
findUser,
|
|
ldapId,
|
|
}: {
|
|
findUser: FindUserByFields;
|
|
ldapId: string;
|
|
}): Promise<LoginUserResolution> {
|
|
const user = await findUser({ ldapId });
|
|
if (user && user.provider !== 'ldap') {
|
|
logger.info(`[ldapStrategy] User ${user._id} already exists with provider ${user.provider}`);
|
|
return { user: null, error: ErrorTypes.AUTH_FAILED };
|
|
}
|
|
return { user, error: null };
|
|
}
|
|
|
|
/**
|
|
* Resolves the account an LDAP login continues as, carrying the directory's values: LDAP manages
|
|
* these users' identity, so provider, `ldapId`, email, username and name are overwritten on every
|
|
* login. A found account continues under `appConfig`, its own config. Without an account, the
|
|
* user is created through `createUserOnce` (the deployment's first user as ADMIN), which admits
|
|
* it under that role's config, and an account a concurrent first login created is admitted like a
|
|
* found account and refreshed the same way. Returns the account with the config the login
|
|
* continues under.
|
|
*/
|
|
export async function provisionLdapUser({
|
|
user,
|
|
ldapId,
|
|
email,
|
|
username,
|
|
name,
|
|
appConfig,
|
|
getAppConfig,
|
|
getBalanceConfig,
|
|
findUser,
|
|
countUsers,
|
|
createUserIfAbsent,
|
|
findBalanceByUser,
|
|
}: {
|
|
user: UserRecord | null;
|
|
ldapId: string;
|
|
email: string;
|
|
username: string;
|
|
name: string;
|
|
appConfig: AppConfig;
|
|
getAppConfig: GetAppConfig;
|
|
getBalanceConfig: GetBalanceConfig;
|
|
findUser: FindUserByFields;
|
|
countUsers: () => Promise<number>;
|
|
createUserIfAbsent: CreateUserIfAbsent;
|
|
findBalanceByUser: FindBalanceByUser;
|
|
}): Promise<ProvisionedUser> {
|
|
let account = user;
|
|
let accountConfig = appConfig;
|
|
if (!account) {
|
|
const role = (await countUsers()) === 0 ? SystemRoles.ADMIN : SystemRoles.USER;
|
|
const result = await createUserOnce({
|
|
newUser: { provider: 'ldap', ldapId, username, email, emailVerified: true, name, role },
|
|
email,
|
|
getAppConfig,
|
|
getBalanceConfig,
|
|
strategyName: 'ldapStrategy',
|
|
lookup: () => findLdapUser({ findUser, ldapId }),
|
|
createUserIfAbsent,
|
|
findBalanceByUser,
|
|
});
|
|
if (result.error !== null) return { user: null, error: result.error };
|
|
account = result.user;
|
|
accountConfig = result.appConfig;
|
|
}
|
|
|
|
const directoryValues = { provider: 'ldap', ldapId, email, username, name };
|
|
return {
|
|
user: Object.assign({}, account, directoryValues),
|
|
appConfig: accountConfig,
|
|
error: null,
|
|
};
|
|
}
|