Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

fix: fall back to `_id` for user id in `createSafeUser` (#14083)

matt burnett61e1633b840d
diff --git a/packages/api/src/utils/env.spec.ts b/packages/api/src/utils/env.spec.ts
index ccfe42c93d..9c4d7bff2b 100644
--- a/packages/api/src/utils/env.spec.ts
+++ b/packages/api/src/utils/env.spec.ts
@@ -2,7 +2,13 @@ import { Types } from 'mongoose';
import { TokenExchangeMethodEnum } from 'librechat-data-provider';
import type { MCPOptions } from 'librechat-data-provider';
import type { IUser } from '@librechat/data-schemas';
-import { resolveHeaders, resolveNestedObject, processMCPEnv, encodeHeaderValue } from './env';
+import {
+ resolveNestedObject,
+ encodeHeaderValue,
+ resolveHeaders,
+ createSafeUser,
+ processMCPEnv,
+} from './env';
function isStdioOptions(options: MCPOptions): options is Extract<MCPOptions, { type?: 'stdio' }> {
return !options.type || options.type === 'stdio';
@@ -2124,3 +2130,62 @@ describe('processMCPEnv', () => {
});
});
});
+
+describe('createSafeUser', () => {
+ it('returns an empty object for null/undefined users', () => {
+ expect(createSafeUser(null)).toEqual({});
+ expect(createSafeUser(undefined)).toEqual({});
+ });
+
+ it('falls back to _id (ObjectId) when the virtual id is absent', () => {
+ const objectId = new Types.ObjectId();
+ const user = { _id: objectId, email: '[email protected]' } as unknown as IUser;
+
+ const safeUser = createSafeUser(user);
+
+ expect(safeUser.id).toBe(objectId.toString());
+ expect(safeUser.email).toBe('[email protected]');
+ });
+
+ it('falls back to a string _id when the virtual id is absent', () => {
+ const user = { _id: 'string-id-123', email: '[email protected]' } as unknown as IUser;
+
+ const safeUser = createSafeUser(user);
+
+ expect(safeUser.id).toBe('string-id-123');
+ });
+
+ it('leaves a truthy id untouched and does not use _id', () => {
+ const objectId = new Types.ObjectId();
+ const user = { _id: objectId, id: 'real-id', email: '[email protected]' } as unknown as IUser;
+
+ const safeUser = createSafeUser(user);
+
+ expect(safeUser.id).toBe('real-id');
+ expect(safeUser.id).not.toBe(objectId.toString());
+ });
+
+ it('replaces a falsy (empty-string) id with _id', () => {
+ const objectId = new Types.ObjectId();
+ const user = { _id: objectId, id: '', email: '[email protected]' } as unknown as IUser;
+
+ const safeUser = createSafeUser(user);
+
+ expect(safeUser.id).toBe(objectId.toString());
+ });
+
+ it('leaves id undefined when _id is absent', () => {
+ const user = { email: '[email protected]' } as unknown as IUser;
+
+ const safeUser = createSafeUser(user);
+
+ expect(safeUser.id).toBeUndefined();
+ });
+
+ it('leaves id undefined when _id is nullish and does not throw', () => {
+ const user = { _id: null, email: '[email protected]' } as unknown as IUser;
+
+ expect(() => createSafeUser(user)).not.toThrow();
+ expect(createSafeUser(user).id).toBeUndefined();
+ });
+});
diff --git a/packages/api/src/utils/env.ts b/packages/api/src/utils/env.ts
index 3349a66ecc..20de5e36e1 100644
--- a/packages/api/src/utils/env.ts
+++ b/packages/api/src/utils/env.ts
@@ -104,6 +104,13 @@ export function createSafeUser(
}
}
+ // Fall back to `_id` when the mongoose virtual `id` is absent (e.g. lean/plain
+ // user objects), so `{{LIBRECHAT_USER_ID}}` placeholders still resolve.
+ if (!safeUser.id && '_id' in user) {
+ const _id = (user as unknown as { _id: { toString?: () => string } | string })._id;
+ safeUser.id = typeof _id === 'string' ? _id : _id?.toString?.();
+ }
+
if ('federatedTokens' in user) {
safeUser.federatedTokens = user.federatedTokens;
}