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
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🔐 fix: Authenticate Code Session Probes the Way Their Route Expects (#16688)
Danny Avilac5a5fbdaf146
diff --git a/packages/api/src/agents/__tests__/initialize.test.ts b/packages/api/src/agents/__tests__/initialize.test.tsindex 50b4a05c4f..8953ad2f42 100644--- a/packages/api/src/agents/__tests__/initialize.test.ts+++ b/packages/api/src/agents/__tests__/initialize.test.ts@@ -3677,6 +3677,16 @@ describe('initializeAgent — execute_code capability expansion', () => {);expect(result.codeExecutionContext?.environmentId).toBe('runtime-vm');expect(agent.code_environment_id).toBe('application-vm');+ /* The liveness probe inside priming authenticates to this route the way its uploads+ * do, which needs the request to mint from and the route's worker to bind to. */+ expect(primeResources).toHaveBeenCalledWith(+ expect.objectContaining({+ req,+ codeBaseUrl: 'https://runtime-vm.example.com/v1',+ codeExecutionProfile: 'stateful',+ codeBridgeWorkerId: 'worker-runtime-vm',+ }),+ );});it.each([false, true])(diff --git a/packages/api/src/agents/initialize.ts b/packages/api/src/agents/initialize.tsindex f294c6a8f9..1ab746e634 100644--- a/packages/api/src/agents/initialize.ts+++ b/packages/api/src/agents/initialize.ts@@ -2002,6 +2002,7 @@ export async function initializeAgent(provisionState,warnings: provisionWarnings,} = await primeResources({+ req: params.req,principal: user,getFiles: db.getFiles as never,filterFiles: db.filterFilesByAgentAccess,@@ -2018,6 +2019,8 @@ export async function initializeAgent(provisionCandidates: deferredProvisionFiles as unknown as TFile[],codeRouteKey: codeExecutionContext.executionRouteKey ?? codeExecutionContext.executionProfile,codeBaseUrl: codeExecutionContext.baseUrl,+ codeExecutionProfile: codeExecutionContext.executionProfile,+ codeBridgeWorkerId: codeExecutionContext.bridgeWorkerId,screenPersistentFiles: (files) => {/* Persistent agent files are read inside primeResources, so they miss both checks* the caller already applied to this turn's other files. They face the samediff --git a/packages/api/src/agents/resources.test.ts b/packages/api/src/agents/resources.test.tsindex 5a016364a5..9e3530be7d 100644--- a/packages/api/src/agents/resources.test.ts+++ b/packages/api/src/agents/resources.test.ts@@ -2184,6 +2184,60 @@ describe('primeResources', () => {expect(statefulFile.metadata?.codeEnvRefs?.default).toBeDefined();});+ it("authenticates an attached route's probe for its worker without the managed key", async () => {+ /* The managed key authorizes only the default Code API; an attached route answers+ * it with a 401, so it is neither loaded nor sent, and the probe carries the+ * route's profile and worker for a worker-bound bearer instead. */+ process.env.CODEAPI_AUTH_PROVIDER = 'librechat-jwt';+ const checkSessionsAlive = jest.fn().mockResolvedValue(new Set(['attached-file']));+ const loadCodeApiKey = jest.fn().mockResolvedValue('managed-key');+ const attachedFile = makeCodeFile({+ file_id: 'attached-file',+ metadata: {+ codeEnvRefs: {+ 'stateful:env1': {+ kind: 'user',+ id: 'user1',+ storage_session_id: 'sess-attached',+ file_id: 'remote-attached',+ executionProfile: 'stateful',+ executionRouteKey: 'stateful:env1',+ },+ },+ },+ });++ await primeResources({+ req: mockReq,+ appConfig: mockAppConfig,+ getFiles: mockGetFiles,+ filterFiles: mockFilterFiles,+ tool_resources: {},+ attachments: Promise.resolve([attachedFile]),+ requestFileSet,+ agentId: 'agent1',+ enabledToolResources: new Set([EToolResources.execute_code]),+ checkSessionsAlive,+ loadCodeApiKey,+ codeRouteKey: 'stateful:env1',+ codeBaseUrl: 'https://code-byom.example.com/v1',+ codeExecutionProfile: 'stateful',+ codeBridgeWorkerId: 'worker-1',+ });++ expect(loadCodeApiKey).not.toHaveBeenCalled();+ expect(checkSessionsAlive).toHaveBeenCalledWith(+ expect.objectContaining({+ req: mockReq,+ apiKey: undefined,+ baseURL: 'https://code-byom.example.com/v1',+ routeKey: 'stateful:env1',+ executionProfile: 'stateful',+ bridgeWorkerId: 'worker-1',+ }),+ );+ });+it('keeps alive pre-categorized files out of the provisioning queue', async () => {process.env.CODEAPI_AUTH_PROVIDER = 'librechat-jwt';const checkSessionsAlive = jest.fn().mockResolvedValue(new Set(['alive-file']));diff --git a/packages/api/src/agents/resources.ts b/packages/api/src/agents/resources.tsindex e312a5d767..743b2e6a04 100644--- a/packages/api/src/agents/resources.ts+++ b/packages/api/src/agents/resources.ts@@ -18,6 +18,7 @@ import type {} from 'librechat-data-provider';import type { IMongoFile, AppConfig, IUser } from '@librechat/data-schemas';import type { FilterQuery, QueryOptions, ProjectionType } from 'mongoose';+import type { CodeExecutionContext } from './execution';import type { ServerRequest } from '~/types';import { TOOL_RESOURCE_KEYS } from './orphans';@@ -79,6 +80,8 @@ export type TCheckSessionsAlive = (params: {staleSafeWindowMs?: number;baseURL?: string;routeKey?: string;+ executionProfile?: CodeExecutionContext['executionProfile'];+ bridgeWorkerId?: string;}) => Promise<Set<string>>;/** Loads CODE_API_KEY for a user. Call once per request. */@@ -402,6 +405,8 @@ const computeProvisionState = async ({agentId,codeRouteKey,codeBaseUrl,+ codeExecutionProfile,+ codeBridgeWorkerId,agentScopedFileIds,persistedResourceMembership,staleSafeWindowMs,@@ -412,6 +417,9 @@ const computeProvisionState = async ({codeRouteKey?: string;/** Base URL of the deployment this turn runs on, used to probe its own refs. */codeBaseUrl?: string;+ /** Wire profile and worker of that deployment, which decide the probe's auth. */+ codeExecutionProfile?: CodeExecutionContext['executionProfile'];+ codeBridgeWorkerId?: string;agentScopedFileIds?: ReadonlySet<string>;resourcePrincipal?: Pick<IUser, 'id' | 'role'>;enabledToolResources?: Set<EToolResources>;@@ -490,9 +498,15 @@ const computeProvisionState = async ({/** Code API auth is optional: deployments may use a legacy key, JWT bearer minting,* or no auth at all, and the upload path handles each. Credentials therefore gate- * only the liveness probe, never whether files are queued for provisioning. */+ * only the liveness probe, never whether files are queued for provisioning. The+ * legacy key authorizes only the default Code API, so another route never loads it. */let codeApiKey: string | undefined;- if (filesWithIdentifiers.length > 0 && loadCodeApiKey && resourcePrincipal?.id) {+ if (+ filesWithIdentifiers.length > 0 &&+ activeCodeRouteKey === 'default' &&+ loadCodeApiKey &&+ resourcePrincipal?.id+ ) {try {codeApiKey = await loadCodeApiKey(resourcePrincipal.id);} catch (error) {@@ -511,6 +525,8 @@ const computeProvisionState = async ({apiKey: codeApiKey,baseURL: codeBaseUrl,routeKey: activeCodeRouteKey,+ executionProfile: codeExecutionProfile,+ bridgeWorkerId: codeBridgeWorkerId,staleSafeWindowMs,});}@@ -643,6 +659,8 @@ export const primeResources = async ({screenPersistentFiles,codeRouteKey,codeBaseUrl,+ codeExecutionProfile,+ codeBridgeWorkerId,}: {req?: ServerRequest;principal?: Pick<IUser, 'id' | 'role'>;@@ -675,6 +693,10 @@ export const primeResources = async ({/** Base URL of that deployment. Refs are deployment-local, so the liveness probe runs* against the Code API that issued them rather than the default one. */codeBaseUrl?: string;+ /** Wire profile and worker of that deployment. The probe authenticates to it the way+ * its uploads and executions do, so a worker-bound route gets a worker-bound bearer. */+ codeExecutionProfile?: CodeExecutionContext['executionProfile'];+ codeBridgeWorkerId?: string;}): Promise<{attachments: Array<TFile | undefined> | undefined;requestAttachments: Array<TFile | undefined> | undefined;@@ -904,6 +926,8 @@ export const primeResources = async ({agentId,codeRouteKey,codeBaseUrl,+ codeExecutionProfile,+ codeBridgeWorkerId,agentScopedFileIds: persistedResourceFileIds,persistedResourceMembership,staleSafeWindowMs: appConfig?.fileConfig?.codeEnvLivenessSafeWindowMs,@@ -968,6 +992,8 @@ export const primeResources = async ({agentId,codeRouteKey,codeBaseUrl,+ codeExecutionProfile,+ codeBridgeWorkerId,agentScopedFileIds: persistedResourceFileIds,persistedResourceMembership,staleSafeWindowMs: appConfig?.fileConfig?.codeEnvLivenessSafeWindowMs,diff --git a/packages/api/src/files/provision/service.spec.ts b/packages/api/src/files/provision/service.spec.tsindex 7fce0c6401..253c97b8dd 100644--- a/packages/api/src/files/provision/service.spec.ts+++ b/packages/api/src/files/provision/service.spec.ts@@ -1,5 +1,6 @@import { Readable } from 'node:stream';import { AxiosError, AxiosHeaders } from 'axios';+import { generateKeyPairSync } from 'node:crypto';import type { TFile } from 'librechat-data-provider';import type { ServerRequest } from '~/types';@@ -423,5 +424,146 @@ describe('createProvisionService', () => {expect(alive.has('f5')).toBe(false);});++ describe('on an attached or stateful route', () => {+ const ROUTE_KEY = 'stateful:env1';+ const BYOM_URL = 'https://code-byom.test/v1';+ const AUTH_ENV = [+ 'CODEAPI_AUTH_PROVIDER',+ 'CODEAPI_JWT_PRIVATE_KEY',+ 'CODEAPI_JWT_ALGORITHM',+ ] as const;+ const savedEnv = new Map<string, string | undefined>();++ const routeFile = (id: string) =>+ makeFile({+ file_id: id,+ metadata: {+ codeEnvRefs: {+ [ROUTE_KEY]: {+ kind: 'user',+ id: 'u1',+ storage_session_id: 'sess-route',+ file_id: `remote-${id}`,+ executionProfile: 'stateful',+ executionRouteKey: ROUTE_KEY,+ provisionedAt: 1,+ },+ },+ } as never,+ });++ const decodeClaims = (authorization: string): Record<string, unknown> =>+ JSON.parse(Buffer.from(authorization.split('.')[1], 'base64url').toString('utf8'));++ beforeEach(() => {+ for (const name of AUTH_ENV) {+ savedEnv.set(name, process.env[name]);+ }+ });++ afterEach(() => {+ for (const name of AUTH_ENV) {+ const value = savedEnv.get(name);+ if (value === undefined) {+ delete process.env[name];+ } else {+ process.env[name] = value;+ }+ }+ });++ it('mints a bearer bound to the route worker and never sends the managed key', async () => {+ process.env.CODEAPI_AUTH_PROVIDER = 'librechat-jwt';+ process.env.CODEAPI_JWT_ALGORITHM = 'EdDSA';+ process.env.CODEAPI_JWT_PRIVATE_KEY = generateKeyPairSync('ed25519')+ .privateKey.export({ format: 'pem', type: 'pkcs8' })+ .toString();+ mockGetCodeApiAuthHeaders.mockImplementation(+ jest.requireActual<typeof import('~/auth/codeapi')>('~/auth/codeapi')+ .getCodeApiAuthHeaders,+ );+ mockAxios.mockResolvedValue({ data: [{ fileId: 'remote-r1' }] });+ const { service } = buildService();++ const alive = await service.checkSessionsAlive({+ files: [routeFile('r1')],+ req,+ apiKey: 'managed-key',+ baseURL: BYOM_URL,+ routeKey: ROUTE_KEY,+ executionProfile: 'stateful',+ bridgeWorkerId: 'worker-1',+ });++ expect(alive.has('r1')).toBe(true);+ expect(mockGetCodeApiAuthHeaders).toHaveBeenCalledWith(req, 'worker-1');+ const { url, headers } = mockAxios.mock.calls[0][0];+ expect(url).toBe(`${BYOM_URL}/files/sess-route`);+ expect(headers['X-API-Key']).toBeUndefined();+ expect(headers['X-CodeAPI-Expected-Profile']).toBe('stateful');+ expect(headers['X-LibreChat-Code-Worker-ID']).toBe('worker-1');+ expect(headers.Authorization).toMatch(/^Bearer /);+ expect(decodeClaims(headers.Authorization)).toEqual(+ expect.objectContaining({ sub: 'u1', code_worker_id: 'worker-1' }),+ );+ });++ it('leaves refs unverified without a request when no bearer can be minted', async () => {+ /* Without a request there is nothing to mint from, and the managed key is not a+ * credential this route accepts: probing would only collect a 401. */+ process.env.CODEAPI_AUTH_PROVIDER = 'librechat-jwt';+ const { service } = buildService();++ const alive = await service.checkSessionsAlive({+ files: [routeFile('r2')],+ apiKey: 'managed-key',+ baseURL: BYOM_URL,+ routeKey: ROUTE_KEY,+ executionProfile: 'stateful',+ bridgeWorkerId: 'worker-1',+ });++ expect(alive.has('r2')).toBe(true);+ expect(mockAxios).not.toHaveBeenCalled();+ });++ it('sends no managed key to the route when JWT auth is disabled', async () => {+ /* Matches the route's own uploads, which send only what the bearer helper yields. */+ delete process.env.CODEAPI_AUTH_PROVIDER;+ mockAxios.mockResolvedValue({ data: [] });+ const { service } = buildService();++ const alive = await service.checkSessionsAlive({+ files: [routeFile('r3')],+ req,+ apiKey: 'managed-key',+ baseURL: BYOM_URL,+ routeKey: ROUTE_KEY,+ executionProfile: 'stateful',+ });++ expect(alive.has('r3')).toBe(false);+ const { headers } = mockAxios.mock.calls[0][0];+ expect(headers['X-API-Key']).toBeUndefined();+ expect(headers.Authorization).toBeUndefined();+ expect(headers['X-CodeAPI-Expected-Profile']).toBe('stateful');+ });++ it('still uses the managed key on the default route when no bearer can be minted', async () => {+ process.env.CODEAPI_AUTH_PROVIDER = 'librechat-jwt';+ mockAxios.mockResolvedValue({ data: [{ fileId: 'remote-f7' }] });+ const { service } = buildService();++ const alive = await service.checkSessionsAlive({+ files: [staleFile('f7')],+ apiKey: 'managed-key',+ routeKey: 'default',+ });++ expect(alive.has('f7')).toBe(true);+ expect(mockAxios.mock.calls[0][0].headers['X-API-Key']).toBe('managed-key');+ });+ });});});diff --git a/packages/api/src/files/provision/service.ts b/packages/api/src/files/provision/service.tsindex 9177588760..e50ba1e8f7 100644--- a/packages/api/src/files/provision/service.ts+++ b/packages/api/src/files/provision/service.ts@@ -13,6 +13,7 @@ import {} from 'librechat-data-provider';import type { CodeEnvRef, CodeEnvRefMap, TFile } from 'librechat-data-provider';import type { Readable } from 'node:stream';+import type { CodeExecutionContext } from '~/agents/execution';import type { CodeEnvIdentity } from '~/files/code/identity';import type { CodeApiRateLimitBudget } from '~/utils';import type { ServerRequest } from '~/types';@@ -27,6 +28,7 @@ import {} from '~/utils';import { getCodeEnvUploadFilename, getUploadedCodeEnvFilename } from '../code/form';import { buildCodeEnvIdentityParams } from '~/files/code/identity';+import { codeExecutionHeaders } from '~/agents/execution';import { getCodeApiAuthHeaders } from '~/auth/codeapi';import { resolveDownloadPath } from '~/storage/path';@@ -123,6 +125,10 @@ export interface ProvisionService {* against the Code API that issued it, not against the default one. */baseURL?: string;routeKey?: string;+ /** Wire profile and worker of that deployment. A worker-bound route authorizes+ * only a bearer minted for its worker, exactly as its uploads and reads do. */+ executionProfile?: CodeExecutionContext['executionProfile'];+ bridgeWorkerId?: string;}) => Promise<Set<string>>;}@@ -203,6 +209,41 @@ export function createProvisionService({};}+ /**+ * Auth for a liveness probe against the route that issued the refs, matching what that+ * route's uploads, reads and executions send. The legacy key belongs to the default+ * Code API alone: an attached or stateful route never receives it, and a worker-bound+ * route gets a bearer minted for its worker plus the route headers. Returns null when+ * there is nothing to authenticate with, neither a request to mint from nor a key this+ * route accepts, since the probe could only be rejected.+ */+ async function buildSessionProbeHeaders({+ apiKey,+ req,+ routeKey,+ executionProfile,+ bridgeWorkerId,+ }: {+ apiKey?: string;+ req?: ServerRequest;+ routeKey?: string;+ executionProfile?: CodeExecutionContext['executionProfile'];+ bridgeWorkerId?: string;+ }): Promise<Record<string, string> | null> {+ const isDefaultRoute = (routeKey ?? 'default') === 'default';+ const authHeaders = await getCodeApiAuthHeaders(req, bridgeWorkerId);+ const legacyKey = isDefaultRoute && authHeaders.Authorization == null ? apiKey : undefined;+ if (req == null && authHeaders.Authorization == null && !legacyKey) {+ return null;+ }+ return {+ 'User-Agent': 'LibreChat/1.0',+ ...(legacyKey ? { 'X-API-Key': legacyKey } : {}),+ ...authHeaders,+ ...(executionProfile ? codeExecutionHeaders({ executionProfile, bridgeWorkerId }) : {}),+ };+ }+/** Env var holding the code-execution API key (symmetric with LIBRECHAT_CODE_BASEURL). */const CODE_API_KEY_FIELD = 'LIBRECHAT_CODE_API_KEY';@@ -501,6 +542,8 @@ export function createProvisionService({staleSafeWindowMs = 6 * 60 * 60 * 1000,baseURL: routeBaseURL,routeKey,+ executionProfile,+ bridgeWorkerId,}: {files: TFile[];apiKey?: string;@@ -508,6 +551,8 @@ export function createProvisionService({staleSafeWindowMs?: number;baseURL?: string;routeKey?: string;+ executionProfile?: CodeExecutionContext['executionProfile'];+ bridgeWorkerId?: string;}): Promise<Set<string>> {const aliveFileIds = new Set<string>();const now = Date.now();@@ -556,26 +601,48 @@ export function createProvisionService({group.entries.push({ file_id: file.file_id, remoteFileId: ref.file_id });}- // One API call per session (in parallel)- const baseURL = routeBaseURL ?? getCodeBaseURL();+ if (sessionGroups.size === 0) {+ return aliveFileIds;+ }+ const keepUnverified = () => {+ for (const file of files) {+ if (file?.file_id) {+ aliveFileIds.add(file.file_id);+ }+ }+ return aliveFileIds;+ };+/* Minting can fail on its own, for example when the request carries no tenant* context. That is an unverifiable probe, not an expired file, so it is handled* like any other probe failure: every ref stays alive rather than the rejection* propagating out and aborting initialization for the whole turn. */- let headers;+ let headers: Record<string, string> | null;try {- headers = await buildCodeApiHeaders({ apiKey, req });+ headers = await buildSessionProbeHeaders({+ apiKey,+ req,+ routeKey,+ executionProfile,+ bridgeWorkerId,+ });} catch (error) {logger.warn(`[checkSessionsAlive] Could not build Code API auth headers; treating ${files.length} reference(s) as unverified: ${(error as Error).message}`,);- for (const file of files) {- if (file?.file_id) {- aliveFileIds.add(file.file_id);- }- }- return aliveFileIds;+ return keepUnverified();}+ /* Sending a credential the route does not accept only earns a 401, so a route with+ * no usable credential this turn is left unverified without a request. */+ if (headers == null) {+ logger.debug(+ `[checkSessionsAlive] No credential this route accepts is available; treating ${files.length} reference(s) as unverified`,+ );+ return keepUnverified();+ }++ // One API call per session (in parallel)+ const baseURL = routeBaseURL ?? getCodeBaseURL();const sessionChecks = Array.from(sessionGroups.values()).map(async ({ session_id, identity, entries: fileEntries }) => {try {