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: Guard Null Avatar URLs in Social Login (#15747)

ncchinh5020dbd23036
diff --git a/api/strategies/process.js b/api/strategies/process.js
index c1e0ad0bbc..a911a3b447 100644
--- a/api/strategies/process.js
+++ b/api/strategies/process.js
@@ -29,9 +29,9 @@ const handleExistingUser = async (oldUser, avatarUrl, appConfig, email) => {
const hasManualFlag =
typeof oldUser?.avatar === 'string' && oldUser.avatar.includes('?manual=true');
- if (isLocal && (!oldUser?.avatar || !hasManualFlag)) {
+ if (avatarUrl && isLocal && (!oldUser?.avatar || !hasManualFlag)) {
updatedAvatar = avatarUrl;
- } else if (!isLocal && (!oldUser?.avatar || !hasManualFlag)) {
+ } else if (avatarUrl && !isLocal && (!oldUser?.avatar || !hasManualFlag)) {
const userId = oldUser._id;
const resizedBuffer = await resizeAvatar({
userId,
@@ -102,7 +102,7 @@ const createSocialUser = async ({
const fileStrategy = appConfig?.fileStrategy ?? process.env.CDN_PROVIDER;
const isLocal = fileStrategy === FileSources.local;
- if (!isLocal) {
+ if (avatarUrl && !isLocal) {
const resizedBuffer = await resizeAvatar({
userId: newUserId,
input: avatarUrl,
diff --git a/api/strategies/process.test.js b/api/strategies/process.test.js
index ab5fdb651f..dd953d67dd 100644
--- a/api/strategies/process.test.js
+++ b/api/strategies/process.test.js
@@ -1,5 +1,5 @@
const { FileSources } = require('librechat-data-provider');
-const { handleExistingUser } = require('./process');
+const { handleExistingUser, createSocialUser } = require('./process');
jest.mock('~/server/services/Files/strategies', () => ({
getStrategyFunctions: jest.fn(),
@@ -27,7 +27,7 @@ jest.mock('@librechat/api', () => ({
const { getStrategyFunctions } = require('~/server/services/Files/strategies');
const { resizeAvatar } = require('~/server/services/Files/images/avatar');
-const { updateUser } = require('~/models');
+const { updateUser, createUser, getUserById } = require('~/models');
describe('handleExistingUser', () => {
beforeEach(() => {
@@ -35,6 +35,45 @@ describe('handleExistingUser', () => {
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: '[email protected]',
+ };
+
+ await handleExistingUser(oldUser, null, undefined, '[email protected]');
+
+ expect(resizeAvatar).not.toHaveBeenCalled();
+ expect(updateUser).toHaveBeenCalledWith('user123', { email: '[email protected]' });
+ });
+
it('should handle null avatar without throwing error', async () => {
const oldUser = {
_id: 'user123',
@@ -240,3 +279,29 @@ describe('handleExistingUser', () => {
expect(updateUser).not.toHaveBeenCalled();
});
});
+
+describe('createSocialUser', () => {
+ beforeEach(() => {
+ jest.clearAllMocks();
+ process.env.CDN_PROVIDER = FileSources.s3;
+ createUser.mockResolvedValue('newUser123');
+ getUserById.mockResolvedValue({ _id: 'newUser123' });
+ });
+
+ it('should not process the avatar when the provider supplies no avatarUrl', async () => {
+ await createSocialUser({
+ email: '[email protected]',
+ avatarUrl: null,
+ provider: 'apple',
+ providerKey: 'appleId',
+ providerId: 'apple-sub-123',
+ username: 'user',
+ name: 'User',
+ emailVerified: true,
+ });
+
+ expect(resizeAvatar).not.toHaveBeenCalled();
+ expect(updateUser).not.toHaveBeenCalled();
+ expect(getUserById).toHaveBeenCalledWith('newUser123');
+ });
+});