mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-16 13:00:55 +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.
122 lines
4.6 KiB
TypeScript
122 lines
4.6 KiB
TypeScript
import { isSafeLinkUrl } from 'librechat-data-provider';
|
|
import type { TTermsOfService } from 'librechat-data-provider';
|
|
import type { AppConfig } from '@librechat/data-schemas';
|
|
|
|
type InterfaceConfig = NonNullable<AppConfig['interfaceConfig']>;
|
|
|
|
type PreLoginInterface = Pick<
|
|
InterfaceConfig,
|
|
'privacyPolicy' | 'termsOfService' | 'buildInfo' | 'theme'
|
|
>;
|
|
|
|
type PolicyLinks = Pick<InterfaceConfig, 'privacyPolicy' | 'termsOfService'>;
|
|
|
|
const POLICY_LINK_FIELDS = ['privacyPolicy', 'termsOfService'] as const;
|
|
|
|
type YamlNode = { [key: string]: YamlValue };
|
|
type YamlValue = string | number | boolean | null | undefined | YamlNode | YamlValue[];
|
|
|
|
export interface ConfigPolicyLinkCheck {
|
|
/** The config to validate and return: the input itself unless a link had to be dropped. */
|
|
config: unknown;
|
|
/** The `externalUrl` paths that were dropped. */
|
|
dropped: string[];
|
|
}
|
|
|
|
function isYamlNode(value: unknown): value is YamlNode {
|
|
return value != null && typeof value === 'object' && !Array.isArray(value);
|
|
}
|
|
|
|
/** The policy link without its `externalUrl` when `isSafeLinkUrl` refuses it. */
|
|
function withSafeExternalUrl<T extends Pick<TTermsOfService, 'externalUrl'>>(
|
|
link: T | undefined,
|
|
): T | undefined {
|
|
if (!link) {
|
|
return link;
|
|
}
|
|
const externalUrl: unknown = link.externalUrl;
|
|
if (externalUrl == null || (typeof externalUrl === 'string' && isSafeLinkUrl(externalUrl))) {
|
|
return link;
|
|
}
|
|
return { ...link, externalUrl: undefined };
|
|
}
|
|
|
|
/**
|
|
* The interface config as a client may render it: a privacy policy or terms of service
|
|
* `externalUrl` that `isSafeLinkUrl` refuses (`javascript:`, `data:` and the like) is removed, so
|
|
* it never becomes an `href`. Admin writes already refuse such a value, the YAML loader drops one
|
|
* from librechat.yaml, and the merge drops one stored before that; this keeps every payload that
|
|
* carries the links (signed out, signed in, shared link) safe on its own, whatever key or path
|
|
* the value arrived by. Returns the same object when nothing is removed.
|
|
*/
|
|
export function withSafePolicyLinks<T extends PolicyLinks | null | undefined>(
|
|
interfaceConfig: T,
|
|
): T {
|
|
if (!interfaceConfig) {
|
|
return interfaceConfig;
|
|
}
|
|
const privacyPolicy = withSafeExternalUrl(interfaceConfig.privacyPolicy);
|
|
const termsOfService = withSafeExternalUrl(interfaceConfig.termsOfService);
|
|
if (
|
|
privacyPolicy === interfaceConfig.privacyPolicy &&
|
|
termsOfService === interfaceConfig.termsOfService
|
|
) {
|
|
return interfaceConfig;
|
|
}
|
|
return { ...interfaceConfig, privacyPolicy, termsOfService };
|
|
}
|
|
|
|
/**
|
|
* Drops a librechat.yaml policy link `externalUrl` that `isSafeLinkUrl` refuses, before the
|
|
* config is validated, so the deployment publishes no link for it and the rest of the config
|
|
* still loads, as an invalid `interface.theme` does. Admin writes of such a value are refused
|
|
* instead, and every payload removes one that reaches it another way. A config whose links are
|
|
* all safe is returned as the same object.
|
|
*/
|
|
export function checkConfigPolicyLinks(config: unknown): ConfigPolicyLinkCheck {
|
|
if (!isYamlNode(config) || !isYamlNode(config.interface)) {
|
|
return { config, dropped: [] };
|
|
}
|
|
const interfaceConfig = config.interface;
|
|
const dropped: string[] = [];
|
|
const checked: YamlNode = { ...interfaceConfig };
|
|
for (const field of POLICY_LINK_FIELDS) {
|
|
const link = interfaceConfig[field];
|
|
if (!isYamlNode(link) || typeof link.externalUrl !== 'string') {
|
|
continue;
|
|
}
|
|
if (isSafeLinkUrl(link.externalUrl)) {
|
|
continue;
|
|
}
|
|
const { externalUrl: _dropped, ...rest } = link;
|
|
checked[field] = rest;
|
|
dropped.push(`interface.${field}.externalUrl`);
|
|
}
|
|
if (dropped.length === 0) {
|
|
return { config, dropped };
|
|
}
|
|
return { config: { ...config, interface: checked }, dropped };
|
|
}
|
|
|
|
/**
|
|
* Selects the interface fields a signed-out client may see: the legal links the
|
|
* login page renders (safe links only), a disabled build-info flag,
|
|
* and the deployment theme so the login page paints it instead of switching after
|
|
* sign-in. Returns `undefined` when none of them is configured, so the payload
|
|
* omits `interface` entirely.
|
|
*/
|
|
export function buildPreLoginInterface(
|
|
interfaceConfig?: AppConfig['interfaceConfig'],
|
|
): PreLoginInterface | undefined {
|
|
if (!interfaceConfig) {
|
|
return undefined;
|
|
}
|
|
const { privacyPolicy, termsOfService, buildInfo, theme } = withSafePolicyLinks(interfaceConfig);
|
|
const selected: PreLoginInterface = {
|
|
...(privacyPolicy && { privacyPolicy }),
|
|
...(termsOfService && { termsOfService }),
|
|
...(buildInfo === false && { buildInfo }),
|
|
...(theme && { theme }),
|
|
};
|
|
return Object.keys(selected).length > 0 ? selected : undefined;
|
|
}
|