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: Route Cross-Region Attachments Through CloudFront (#16082)

Ravi Kumar Lc7665ab1aebb
diff --git a/packages/api/src/storage/cloudfront/__tests__/crud.test.ts b/packages/api/src/storage/cloudfront/__tests__/crud.test.ts
index cde16c73e0..8b24bc6f75 100644
--- a/packages/api/src/storage/cloudfront/__tests__/crud.test.ts
+++ b/packages/api/src/storage/cloudfront/__tests__/crud.test.ts
@@ -1,4 +1,5 @@
import { Readable } from 'stream';
+import { finished } from 'stream/promises';
import type { TFile } from 'librechat-data-provider';
import type { CloudFrontFullConfig } from '~/cdn/cloudfront';
import type { ServerRequest } from '~/types';
@@ -10,17 +11,20 @@ const mockSaveURLToS3WithMetadata = jest.fn();
const mockUploadFileToS3 = jest.fn();
const mockDeleteFileFromS3 = jest.fn();
const mockGetS3FileStream = jest.fn();
+const mockParseS3Key = jest.fn();
const mockExtractKeyFromS3Url = jest.fn<string, [string]>();
const mockResolveStoredS3Key = jest.fn<string, [TFile]>();
const mockCloudFrontSend = jest.fn();
const mockGetSignedUrl = jest.fn<string, [object]>();
const mockLogger = { debug: jest.fn(), info: jest.fn(), warn: jest.fn(), error: jest.fn() };
+const originalFetch = global.fetch;
jest.mock('~/cdn/cloudfront', () => ({
getCloudFrontConfig: mockGetCloudFrontConfig,
}));
jest.mock('~/storage/s3/crud', () => ({
+ parseS3Key: mockParseS3Key,
getS3Key: mockGetS3Key,
saveBufferToS3: mockSaveBufferToS3,
saveURLToS3WithMetadata: mockSaveURLToS3WithMetadata,
@@ -62,6 +66,10 @@ function makeConfig(overrides: Partial<CloudFrontFullConfig> = {}): CloudFrontFu
}
describe('CloudFront CRUD', () => {
+ afterEach(() => {
+ global.fetch = originalFetch;
+ });
+
beforeEach(() => {
jest.clearAllMocks();
mockGetS3Key.mockImplementation(
@@ -610,9 +618,137 @@ describe('CloudFront CRUD', () => {
});
describe('getCloudFrontFileStream', () => {
- it('delegates to getS3FileStream', async () => {
+ const remoteKey = 'i/r/eu-west-1/images/u/f.webp';
+ const responseFor = (body: Readable) =>
+ new Response(Readable.toWeb(body) as unknown as BodyInit);
+
+ beforeEach(() => {
+ mockExtractKeyFromS3Url.mockReturnValue(remoteKey);
+ mockParseS3Key.mockReturnValue({ storageRegion: 'eu-west-1' });
+ });
+
+ afterEach(() => {
+ jest.useRealTimers();
+ });
+
+ it('forwards a body error to the returned stream', async () => {
+ const body = new Readable({ read() {} });
+ jest.spyOn(global, 'fetch').mockResolvedValue(responseFor(body));
+ const { getCloudFrontFileStream } = await import('~/storage/cloudfront/crud');
+ const stream = await getCloudFrontFileStream({} as ServerRequest, remoteKey);
+ const result = finished(stream);
+ body.destroy(new Error('terminated'));
+ await expect(result).rejects.toThrow('terminated');
+ });
+
+ it('forwards caller cancellation after headers and cancels the body', async () => {
+ const body = new Readable({ read() {} });
+ const controller = new AbortController();
+ jest.spyOn(global, 'fetch').mockResolvedValue(responseFor(body));
+ const { getCloudFrontFileStream } = await import('~/storage/cloudfront/crud');
+ const stream = await getCloudFrontFileStream({} as ServerRequest, remoteKey, {
+ signal: controller.signal,
+ });
+ const result = finished(stream);
+ controller.abort();
+ await expect(result).rejects.toMatchObject({ name: 'AbortError' });
+ expect(body.destroyed).toBe(true);
+ });
+
+ it('cancels the body when the returned stream is destroyed', async () => {
+ const body = new Readable({ read() {} });
+ jest.spyOn(global, 'fetch').mockResolvedValue(responseFor(body));
+ const { getCloudFrontFileStream } = await import('~/storage/cloudfront/crud');
+ const stream = await getCloudFrontFileStream({} as ServerRequest, remoteKey);
+ const result = finished(stream);
+ stream.destroy();
+ await expect(result).rejects.toThrow('Premature close');
+ expect(body.destroyed).toBe(true);
+ });
+
+ it('cancels the body when the streamed byte limit is exceeded', async () => {
+ const limits = await import('~/storage/url');
+ jest.spyOn(limits, 'getRemoteFileFetchMaxBytes').mockReturnValue(3);
+ const body = new Readable({ read() {} });
+ jest.spyOn(global, 'fetch').mockResolvedValue(responseFor(body));
+ const { getCloudFrontFileStream } = await import('~/storage/cloudfront/crud');
+ const stream = await getCloudFrontFileStream({} as ServerRequest, remoteKey);
+ const result = finished(stream);
+ stream.resume();
+ body.push('large');
+ await expect(result).rejects.toThrow('Remote file response too large');
+ expect(body.destroyed).toBe(true);
+ });
+
+ it('preserves caller cancellation before headers and clears the timeout', async () => {
+ jest.useFakeTimers();
+ jest.spyOn(global, 'fetch').mockImplementation(
+ (_url, init) =>
+ new Promise((_resolve, reject) => {
+ init?.signal?.addEventListener('abort', () => reject(init.signal?.reason), {
+ once: true,
+ });
+ }),
+ );
+ const { getCloudFrontFileStream } = await import('~/storage/cloudfront/crud');
+ const controller = new AbortController();
+ const result = getCloudFrontFileStream({} as ServerRequest, remoteKey, {
+ signal: controller.signal,
+ });
+ controller.abort();
+ await expect(result).rejects.toMatchObject({ name: 'AbortError' });
+ expect(jest.getTimerCount()).toBe(0);
+ });
+
+ it.each([false, true])(
+ 'times out response headers with caller signal=%s',
+ async (withSignal) => {
+ jest.useFakeTimers();
+ jest.spyOn(global, 'fetch').mockImplementation(
+ (_url, init) =>
+ new Promise((_resolve, reject) => {
+ init?.signal?.addEventListener('abort', () => reject(init.signal?.reason), {
+ once: true,
+ });
+ }),
+ );
+ const { getCloudFrontFileStream } = await import('~/storage/cloudfront/crud');
+ const { getRemoteFileFetchTimeoutMs } = await import('~/storage/url');
+ const controller = new AbortController();
+ const result = getCloudFrontFileStream(
+ {} as ServerRequest,
+ remoteKey,
+ withSignal ? { signal: controller.signal } : undefined,
+ );
+ await Promise.all([
+ expect(result).rejects.toMatchObject({ name: 'AbortError' }),
+ jest.advanceTimersByTimeAsync(getRemoteFileFetchTimeoutMs()),
+ ]);
+ expect(controller.signal.aborted).toBe(false);
+ expect(jest.getTimerCount()).toBe(0);
+ },
+ );
+
+ it('clears the header timeout while the body is still streaming', async () => {
+ jest.useFakeTimers();
+ const body = new Readable({ read() {} });
+ jest.spyOn(global, 'fetch').mockResolvedValue(responseFor(body));
+ const { getCloudFrontFileStream } = await import('~/storage/cloudfront/crud');
+ const stream = await getCloudFrontFileStream({} as ServerRequest, remoteKey);
+ expect(jest.getTimerCount()).toBe(0);
+ jest.useRealTimers();
+ const result = finished(stream);
+ stream.resume();
+ body.push('remote');
+ body.push(null);
+ await result;
+ });
+
+ it('reads a local-region key directly from S3', async () => {
const readable = new Readable();
mockGetS3FileStream.mockResolvedValue(readable);
+ mockExtractKeyFromS3Url.mockReturnValue('r/us-east-1/images/u/f.webp');
+ mockParseS3Key.mockReturnValue({ storageRegion: 'us-east-1' });
const mockReq = { user: { id: 'u' } } as ServerRequest;
const { getCloudFrontFileStream } = await import('~/storage/cloudfront/crud');
@@ -623,11 +759,85 @@ describe('CloudFront CRUD', () => {
expect(mockGetS3FileStream).toHaveBeenCalledWith(
mockReq,
- 'https://d123.cloudfront.net/images/u/f.webp',
+ 'r/us-east-1/images/u/f.webp',
undefined,
);
expect(result).toBe(readable);
});
+
+ it('reads a cross-region key through CloudFront', async () => {
+ mockGetCloudFrontConfig.mockReturnValue(
+ makeConfig({ privateKey: 'pk-secret', keyPairId: 'K123' }),
+ );
+ mockExtractKeyFromS3Url.mockReturnValue('i/r/eu-west-1/images/u/f.webp');
+ mockParseS3Key.mockReturnValue({ storageRegion: 'eu-west-1' });
+ mockGetSignedUrl.mockReturnValue(
+ 'https://d123.cloudfront.net/i/r/eu-west-1/images/u/f.webp?Policy=abc',
+ );
+ global.fetch = jest.fn().mockResolvedValue({
+ ok: true,
+ headers: { get: () => '6' },
+ body: Readable.toWeb(Readable.from('remote')),
+ }) as unknown as typeof fetch;
+
+ const { getCloudFrontFileStream } = await import('~/storage/cloudfront/crud');
+ const stream = await getCloudFrontFileStream(
+ { user: { id: 'u' } } as ServerRequest,
+ 'i/r/eu-west-1/images/u/f.webp',
+ );
+ const chunks: Buffer[] = [];
+ for await (const chunk of stream) {
+ chunks.push(Buffer.from(chunk));
+ }
+
+ expect(Buffer.concat(chunks).toString()).toBe('remote');
+ expect(mockGetS3FileStream).not.toHaveBeenCalled();
+ expect(global.fetch).toHaveBeenCalledWith(
+ 'https://d123.cloudfront.net/i/r/eu-west-1/images/u/f.webp?Policy=abc',
+ expect.objectContaining({ signal: expect.any(Object) }),
+ );
+ });
+
+ it('preserves a cross-region CloudFront 404 for missing-attachment handling', async () => {
+ mockExtractKeyFromS3Url.mockReturnValue('i/r/eu-west-1/images/u/missing.webp');
+ mockParseS3Key.mockReturnValue({ storageRegion: 'eu-west-1' });
+ const cancel = jest.fn().mockResolvedValue(undefined);
+ global.fetch = jest.fn().mockResolvedValue({
+ ok: false,
+ status: 404,
+ statusText: 'Not Found',
+ body: { cancel },
+ }) as unknown as typeof fetch;
+
+ const { getCloudFrontFileStream } = await import('~/storage/cloudfront/crud');
+ await expect(
+ getCloudFrontFileStream(
+ { user: { id: 'u' } } as ServerRequest,
+ 'i/r/eu-west-1/images/u/missing.webp',
+ ),
+ ).rejects.toMatchObject({ status: 404 });
+ expect(cancel).toHaveBeenCalled();
+ });
+
+ it('cancels an oversized cross-region response', async () => {
+ mockExtractKeyFromS3Url.mockReturnValue('i/r/eu-west-1/images/u/large.webp');
+ mockParseS3Key.mockReturnValue({ storageRegion: 'eu-west-1' });
+ const cancel = jest.fn().mockResolvedValue(undefined);
+ global.fetch = jest.fn().mockResolvedValue({
+ ok: true,
+ headers: { get: () => String(513 * 1024 * 1024) },
+ body: { cancel },
+ }) as unknown as typeof fetch;
+
+ const { getCloudFrontFileStream } = await import('~/storage/cloudfront/crud');
+ await expect(
+ getCloudFrontFileStream(
+ { user: { id: 'u' } } as ServerRequest,
+ 'i/r/eu-west-1/images/u/large.webp',
+ ),
+ ).rejects.toThrow('Remote file response too large');
+ expect(cancel).toHaveBeenCalled();
+ });
});
describe('getCloudFrontDownloadURL', () => {
diff --git a/packages/api/src/storage/cloudfront/crud.ts b/packages/api/src/storage/cloudfront/crud.ts
index 21801728cf..764c4e34ee 100644
--- a/packages/api/src/storage/cloudfront/crud.ts
+++ b/packages/api/src/storage/cloudfront/crud.ts
@@ -1,9 +1,9 @@
import crypto from 'crypto';
+import { Readable, pipeline } from 'stream';
import { logger } from '@librechat/data-schemas';
import { getSignedUrl } from '@aws-sdk/cloudfront-signer';
import { CloudFrontClient, CreateInvalidationCommand } from '@aws-sdk/client-cloudfront';
import type { TFile } from 'librechat-data-provider';
-import type { Readable } from 'stream';
import type {
SaveBufferParams,
GetURLParams,
@@ -15,7 +15,9 @@ import type {
} from '~/storage/types';
import type { ServerRequest } from '~/types';
import {
+ parseS3Key,
getS3Key,
+ extractKeyFromS3Url,
saveBufferToS3,
saveURLToS3WithMetadata,
uploadFileToS3,
@@ -23,6 +25,12 @@ import {
getS3FileStream,
resolveStoredS3Key,
} from '~/storage/s3/crud';
+import {
+ getRemoteFileFetchMaxBytes,
+ getRemoteFileFetchTimeoutMs,
+ assertRemoteFileContentLength,
+ createRemoteFileByteLimitTransform,
+} from '~/storage/url';
import { AVATAR_BASE_PATH, DEFAULT_BASE_PATH as defaultBasePath } from '~/storage/constants';
import { sanitizeContentDispositionFilename } from '~/storage/validation';
import { getCloudFrontConfig } from '~/cdn/cloudfront';
@@ -267,13 +275,65 @@ export async function deleteFileFromCloudFront(req: ServerRequest, file: TFile):
}
}
-/** Get file stream from S3 storage. */
+/** Get a file stream from its owning regional origin. */
export async function getCloudFrontFileStream(
req: ServerRequest,
filePath: string,
options?: { signal?: AbortSignal },
): Promise<Readable> {
- return getS3FileStream(req, filePath, options);
+ const key = extractKeyFromS3Url(filePath);
+ const storageRegion = parseS3Key(key)?.storageRegion;
+ const localStorageRegion = getCloudFrontConfig()?.storageRegion ?? s3Config.AWS_REGION;
+
+ if (!storageRegion || storageRegion === localStorageRegion) {
+ return getS3FileStream(req, key, options);
+ }
+
+ const maxBytes = getRemoteFileFetchMaxBytes();
+ const unsignedUrl = buildCloudFrontUrl(key);
+ const config = getCloudFrontConfig();
+ const url = config?.privateKey && config.keyPairId ? signUrl(unsignedUrl) : unsignedUrl;
+ const controller = new AbortController();
+ const signal = options?.signal
+ ? AbortSignal.any([options.signal, controller.signal])
+ : controller.signal;
+ const timeout = setTimeout(() => controller.abort(), getRemoteFileFetchTimeoutMs());
+ let response: Response;
+ try {
+ response = await fetch(url, { signal });
+ } finally {
+ clearTimeout(timeout);
+ }
+
+ if (!response.ok) {
+ await response.body?.cancel().catch(() => undefined);
+ const error = new Error(
+ `[getCloudFrontFileStream] CloudFront returned ${response.status} ${response.statusText}`,
+ ) as Error & { status: number };
+ error.status = response.status;
+ throw error;
+ }
+ if (!response.body) {
+ throw new Error('[getCloudFrontFileStream] CloudFront response body is empty');
+ }
+
+ try {
+ assertRemoteFileContentLength(response.headers, maxBytes);
+ } catch (error) {
+ await response.body.cancel().catch(() => undefined);
+ throw error;
+ }
+ const stream = createRemoteFileByteLimitTransform(maxBytes);
+ pipeline(
+ Readable.fromWeb(response.body as unknown as Parameters<typeof Readable.fromWeb>[0], {
+ signal,
+ }),
+ stream,
+ () => {
+ /** Pipeline forwards failures to the returned stream and closes both ends. */
+ },
+ );
+ return stream;
}
/** Get a signed CloudFront URL for an authorized file download. */