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: Load User Keys on Resumed Requests (#15749)

Danny Avila639d606c5332
diff --git a/packages/api/src/endpoints/google/initialize.spec.ts b/packages/api/src/endpoints/google/initialize.spec.ts
index 44cf9994b5..6645cd7978 100644
--- a/packages/api/src/endpoints/google/initialize.spec.ts
+++ b/packages/api/src/endpoints/google/initialize.spec.ts
@@ -129,6 +129,27 @@ describe('initializeGoogle', () => {
);
});
+ it('loads the stored API key when a resumed request omits expiry metadata', async () => {
+ process.env.GOOGLE_KEY = 'user_provided';
+ const db = createDb();
+
+ await initializeGoogle({
+ req: createReq(),
+ endpoint: EModelEndpoint.google,
+ model_parameters: { model: 'gemini-2.5-flash' },
+ db,
+ });
+
+ expect(db.getUserKey).toHaveBeenCalledWith({
+ userId: 'user-1',
+ name: EModelEndpoint.google,
+ });
+ expect(mockCheckUserKeyExpiry).not.toHaveBeenCalled();
+
+ const [credentials] = getGoogleConfigCall();
+ expect(credentials).toBe('user-google-key');
+ });
+
it('resolves configured headers at init (merged over endpoints.all) before getGoogleConfig', async () => {
process.env.GOOGLE_KEY = 'test-api-key';
diff --git a/packages/api/src/endpoints/google/initialize.ts b/packages/api/src/endpoints/google/initialize.ts
index 94dc6654b3..3af58cedb8 100644
--- a/packages/api/src/endpoints/google/initialize.ts
+++ b/packages/api/src/endpoints/google/initialize.ts
@@ -39,6 +39,8 @@ export async function initializeGoogle(
let userKey = null;
if (expiresAt && useUserProvidedGoogleKey) {
checkUserKeyExpiry(expiresAt, EModelEndpoint.google);
+ }
+ if (useUserProvidedGoogleKey) {
userKey = await db.getUserKey({ userId: user?.id ?? '', name: EModelEndpoint.google });
}
diff --git a/packages/api/src/endpoints/openai/initialize.spec.ts b/packages/api/src/endpoints/openai/initialize.spec.ts
index 980dc8cfe4..0a1d65df66 100644
--- a/packages/api/src/endpoints/openai/initialize.spec.ts
+++ b/packages/api/src/endpoints/openai/initialize.spec.ts
@@ -145,11 +145,12 @@ describe('initializeOpenAI – SSRF guard wiring', () => {
expect(mockGetOpenAIConfig).not.toHaveBeenCalled();
});
- it('should not validate a stale user Azure URL when an admin model group baseURL is selected', async () => {
+ it('should not load stale user Azure values when an admin model group config is selected', async () => {
const params = createParams({
- AZURE_API_KEY: 'az-env-key',
+ AZURE_API_KEY: AuthType.USER_PROVIDED,
AZURE_OPENAI_BASEURL: AuthType.USER_PROVIDED,
});
+ (params.db.getUserKeyValues as jest.Mock).mockResolvedValue(null);
params.endpoint = EModelEndpoint.azureOpenAI;
params.model_parameters = { model: 'gpt-4o' };
params.req.config = {
@@ -176,6 +177,7 @@ describe('initializeOpenAI – SSRF guard wiring', () => {
(params as unknown as { _restore: () => void })._restore();
}
+ expect(params.db.getUserKeyValues).not.toHaveBeenCalled();
expect(mockValidateEndpointURL).not.toHaveBeenCalled();
expect(mockGetOpenAIConfig).toHaveBeenCalledWith(
'az-admin-key',
@@ -188,6 +190,33 @@ describe('initializeOpenAI – SSRF guard wiring', () => {
});
});
+describe('initializeOpenAI – user-provided credentials', () => {
+ afterEach(() => {
+ jest.clearAllMocks();
+ });
+
+ it('loads the stored API key when a resumed request omits expiry metadata', async () => {
+ const params = createParams({ OPENAI_API_KEY: AuthType.USER_PROVIDED });
+ params.req.body = {};
+
+ try {
+ await initializeOpenAI(params);
+ } finally {
+ (params as unknown as { _restore: () => void })._restore();
+ }
+
+ expect(params.db.getUserKeyValues).toHaveBeenCalledWith({
+ userId: 'user-1',
+ name: EModelEndpoint.openAI,
+ });
+ expect(mockGetOpenAIConfig).toHaveBeenCalledWith(
+ 'sk-user-key',
+ expect.any(Object),
+ EModelEndpoint.openAI,
+ );
+ });
+});
+
describe('initializeOpenAI – custom headers', () => {
afterEach(() => {
jest.clearAllMocks();
diff --git a/packages/api/src/endpoints/openai/initialize.ts b/packages/api/src/endpoints/openai/initialize.ts
index b86cf43f35..7df6e1b996 100644
--- a/packages/api/src/endpoints/openai/initialize.ts
+++ b/packages/api/src/endpoints/openai/initialize.ts
@@ -64,10 +64,23 @@ export async function initializeOpenAI(
const userProvidesKey = isUserProvided(credentials[endpoint as keyof typeof credentials]);
const userProvidesURL = isUserProvided(configuredBaseURL);
+ const isAzureOpenAI = endpoint === EModelEndpoint.azureOpenAI;
+ const azureConfig = isAzureOpenAI && appConfig?.endpoints?.[EModelEndpoint.azureOpenAI];
+ const mappedAzureConfig = azureConfig
+ ? mapModelToAzureConfig({
+ modelName: modelName || '',
+ modelGroupMap: azureConfig.modelGroupMap,
+ groupMap: azureConfig.groupMap,
+ })
+ : null;
+ const needsUserKey = userProvidesKey && !mappedAzureConfig;
+ const needsUserURL = userProvidesURL && !mappedAzureConfig?.baseURL;
let userValues: UserKeyValues | null = null;
- if (expiresAt && (userProvidesKey || userProvidesURL)) {
+ if (expiresAt && (needsUserKey || needsUserURL)) {
checkUserKeyExpiry(expiresAt, endpoint);
+ }
+ if (needsUserKey || needsUserURL) {
userValues = await db.getUserKeyValues({ userId: user?.id ?? '', name: endpoint });
}
@@ -96,22 +109,11 @@ export async function initializeOpenAI(
? mergeHeaders(allConfig?.headers, openAIConfig?.headers)
: undefined;
- const isAzureOpenAI = endpoint === EModelEndpoint.azureOpenAI;
- const azureConfig = isAzureOpenAI && appConfig?.endpoints?.[EModelEndpoint.azureOpenAI];
let isServerless = false;
- if (isAzureOpenAI && azureConfig) {
+ if (isAzureOpenAI && azureConfig && mappedAzureConfig) {
const { modelGroupMap, groupMap } = azureConfig;
- const {
- azureOptions,
- baseURL: configBaseURL,
- headers = {},
- serverless,
- } = mapModelToAzureConfig({
- modelName: modelName || '',
- modelGroupMap,
- groupMap,
- });
+ const { azureOptions, baseURL: configBaseURL, headers = {}, serverless } = mappedAzureConfig;
isServerless = serverless === true;
clientOptions.reverseProxyUrl = configBaseURL ?? clientOptions.reverseProxyUrl;