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: Restrict Stop to the Generation's Owner and Contain S3 Ingest Stream Errors (#17256)
Marco Beretta379a6ff24735
diff --git a/api/server/middleware/abortMiddleware.js b/api/server/middleware/abortMiddleware.jsindex 1dfd994d18..871a5d6abb 100644--- a/api/server/middleware/abortMiddleware.js+++ b/api/server/middleware/abortMiddleware.js@@ -6,6 +6,7 @@ const {countTokens,isAbortError,GenerationJobManager,+ abortOwnedGeneration,sanitizeMessageForTransmit,buildAbortedResponseMetadata,announceReply,@@ -31,8 +32,8 @@ async function abortMessage(req, res) {const conversationId = abortKey?.split(':')?.[0] ?? req.user.id;const userId = req.user.id;- // Use GenerationJobManager to abort the job (streamId === conversationId)- const abortResult = await GenerationJobManager.abortJob(conversationId);+ // streamId === conversationId; only the job's owner may stop it+ const abortResult = await abortOwnedGeneration(conversationId, req.user, GenerationJobManager);if (!abortResult.success) {if (!res.headersSent) {diff --git a/api/server/middleware/abortMiddleware.spec.js b/api/server/middleware/abortMiddleware.spec.jsindex b5f73b8397..795cd7ebd4 100644--- a/api/server/middleware/abortMiddleware.spec.js+++ b/api/server/middleware/abortMiddleware.spec.js@@ -32,10 +32,12 @@ jest.mock('@librechat/api', () => ({isAbortError: jest.requireActual('@librechat/api').isAbortError,hasPersistableAbortContent: jest.requireActual('@librechat/api').hasPersistableAbortContent,announceReply: jest.requireActual('@librechat/api').announceReply,+ abortOwnedGeneration: jest.requireActual('@librechat/api').abortOwnedGeneration,countTokens: jest.fn().mockResolvedValue(100),isEnabled: jest.fn().mockReturnValue(false),sendEvent: jest.fn(),GenerationJobManager: {+ getJob: jest.fn(),abortJob: jest.fn(),},recordCollectedUsage: mockRecordCollectedUsage,@@ -197,6 +199,24 @@ describe('abortMiddleware - handleAbort billing', () => {mockGetTransactionsConfig.mockReturnValue({ enabled: false });mockRecordCollectedUsage.mockResolvedValue({ input_tokens: 100, output_tokens: 50 });db.getConvo.mockResolvedValue({ title: 'Test Chat' });+ GenerationJobManager.getJob.mockResolvedValue({+ createdAt: 1,+ metadata: { userId: 'user-123' },+ });+ });++ it.each([+ ['another user', { userId: 'user-456' }],+ ['another tenant', { userId: 'user-123', tenantId: 'tenant-b' }],+ ])('does not stop or persist a run owned by %s', async (_case, metadata) => {+ GenerationJobManager.getJob.mockResolvedValue({ createdAt: 1, metadata });+ const res = { ...buildRes(), status: jest.fn().mockReturnThis() };++ await handleAbort()({ ...buildReq(), user: { id: 'user-123', tenantId: 'tenant-a' } }, res);++ expect(GenerationJobManager.abortJob).not.toHaveBeenCalled();+ expect(db.saveMessage).not.toHaveBeenCalled();+ expect(res.status).toHaveBeenCalledWith(204);});it.each([null, undefined])(diff --git a/e2e/specs/mock/scenarios/abort-ownership.spec.ts b/e2e/specs/mock/scenarios/abort-ownership.spec.tsnew file mode 100644index 0000000000..074173f3c8--- /dev/null+++ b/e2e/specs/mock/scenarios/abort-ownership.spec.ts@@ -0,0 +1,134 @@+import { expect, test } from '@playwright/test';+import type { APIRequestContext, Page } from '@playwright/test';+import { randomUUID } from 'node:crypto';+import { getE2EUser } from '../../../setup/user';+import { deleteConversations, deleteMessagesByConversation, withMongo } from '../db';+import {+ MOCK_ENDPOINTS,+ NEW_CHAT_PATH,+ messagesView,+ selectMockEndpoint,+ sendMessage,+} from '../helpers';++/** The legacy Stop route that resolves any non-assistants endpoint by conversation id. */+const LEGACY_ABORT_PATH = '/api/assistants/v2/chat/abort';+const PASSWORD = 'Abort-Ownership-1!';++async function login(request: APIRequestContext, email: string, password: string) {+ const response = await request.post('/api/auth/login', { data: { email, password } });+ expect(response.ok()).toBeTruthy();+ const { token } = (await response.json()) as { token: string };+ return token;+}++async function registerOtherUser(request: APIRequestContext) {+ const email = `abort-other-${randomUUID().slice(0, 8)}@example.com`;+ const register = await request.post('/api/auth/register', {+ data: { email, name: 'Abort Other', password: PASSWORD, confirm_password: PASSWORD },+ });+ expect(register.ok()).toBeTruthy();+ return login(request, email, PASSWORD);+}++/** Starts a slow scripted reply and returns its conversation once the reply is streaming. */+async function startSlowReply(page: Page, prompt: string) {+ await page.goto(NEW_CHAT_PATH);+ await selectMockEndpoint(page, MOCK_ENDPOINTS[0]);+ const run = await sendMessage(page, prompt);+ expect(run.ok()).toBeTruthy();+ await expect(messagesView(page).getByText('chunk-010')).toBeVisible({ timeout: 15_000 });++ let conversationId = '';+ await expect+ .poll(+ async () => {+ const row = await withMongo((db) =>+ db.collection('messages').findOne({ text: prompt, isCreatedByUser: true }),+ );+ conversationId = (row?.conversationId as string | undefined) ?? '';+ return conversationId;+ },+ { timeout: 10_000 },+ )+ .not.toBe('');+ return conversationId;+}++function abort(request: APIRequestContext, token: string, conversationId: string) {+ return request.post(LEGACY_ABORT_PATH, {+ headers: { Authorization: `Bearer ${token}` },+ data: { abortKey: `${conversationId}:${conversationId}`, endpoint: 'agents' },+ failOnStatusCode: false,+ });+}++async function cleanup(conversationId: string) {+ if (!conversationId) {+ return;+ }+ await deleteMessagesByConversation([conversationId]);+ await deleteConversations([conversationId]);+}++test.describe('stop ownership', () => {+ test("another user's stop request leaves the generation running @scenario:another-user-cannot-stop-a-generation", async ({+ page,+ playwright,+ baseURL,+ }) => {+ test.setTimeout(120_000);+ const other = await playwright.request.newContext({ baseURL });+ let conversationId = '';+ try {+ const otherToken = await registerOtherUser(other);+ const prompt = `E2E_SLOW_REPLY:abort-other-${randomUUID().slice(0, 8)}`;+ conversationId = await startSlowReply(page, prompt);++ const response = await abort(other, otherToken, conversationId);+ expect(response.status()).toBe(204);++ /* The run streams to its last scripted chunk and is saved whole. */+ await expect(messagesView(page).getByText('chunk-159')).toBeVisible({ timeout: 30_000 });+ await expect(page.getByRole('button', { name: 'Stop generating' })).toHaveCount(0, {+ timeout: 20_000,+ });+ await page.reload();+ await expect(messagesView(page).getByText('chunk-159')).toBeVisible({ timeout: 20_000 });+ } finally {+ await other.dispose();+ await cleanup(conversationId);+ }+ });++ test('the owner can still stop their generation through the same route @scenario:owner-stops-own-generation', async ({+ page,+ playwright,+ baseURL,+ }) => {+ test.setTimeout(120_000);+ const owner = await playwright.request.newContext({ baseURL });+ let conversationId = '';+ try {+ const { email, password } = getE2EUser();+ const ownerToken = await login(owner, email, password);+ const prompt = `E2E_SLOW_REPLY:abort-owner-${randomUUID().slice(0, 8)}`;+ conversationId = await startSlowReply(page, prompt);++ const response = await abort(owner, ownerToken, conversationId);+ expect(response.status()).toBe(200);+ const body = (await response.json()) as { final?: boolean };+ expect(body.final).toBe(true);++ await expect(page.getByRole('button', { name: 'Stop generating' })).toHaveCount(0, {+ timeout: 20_000,+ });+ await page.reload();+ await expect(messagesView(page).getByText('chunk-010')).toBeVisible({ timeout: 20_000 });+ await expect(messagesView(page).getByText('chunk-159')).toHaveCount(0);+ } finally {+ await owner.dispose();+ await cleanup(conversationId);+ }+ });+});diff --git a/e2e/specs/mock/scenarios/parts.spec.ts b/e2e/specs/mock/scenarios/parts.spec.tsindex dc9505561e..6b2e0577c2 100644--- a/e2e/specs/mock/scenarios/parts.spec.ts+++ b/e2e/specs/mock/scenarios/parts.spec.ts@@ -116,7 +116,9 @@ test.describe('UI parts view of persisted messages', () => {content: '# Parts view\n',});expect(upload.ok()).toBeTruthy();- await expect(page.getByRole('button', { name: fileName })).toBeVisible({ timeout: 15_000 });+ await expect(+ page.getByTestId('composer-tray').getByRole('button', { name: fileName }),+ ).toBeVisible({ timeout: 15_000 });const response = await sendMessageAndWaitForCompletion(page, replyPrompt(label));expect(response.ok()).toBeTruthy();diff --git a/packages/api/src/storage/s3/__tests__/crud.test.ts b/packages/api/src/storage/s3/__tests__/crud.test.tsindex b9c6e5e2ed..1187493cfe 100644--- a/packages/api/src/storage/s3/__tests__/crud.test.ts+++ b/packages/api/src/storage/s3/__tests__/crud.test.ts@@ -767,6 +767,32 @@ describe('S3 CRUD', () => {expect(putInput.Body).toEqual(Buffer.from('decoded'));});+ it('rejects instead of crashing when the response body fails mid-stream', async () => {+ (global.fetch as unknown as jest.Mock).mockResolvedValueOnce({+ ok: true,+ headers: {+ get: (name: string) => (name.toLowerCase() === 'content-type' ? 'image/png' : null),+ },+ body: new ReadableStream<Uint8Array>({+ start(controller) {+ controller.enqueue(Buffer.from('partial'));+ controller.error(new Error('socket reset'));+ },+ }),+ arrayBuffer: jest.fn(),+ });++ const { saveURLToS3WithMetadata } = await import('../crud');+ await expect(+ saveURLToS3WithMetadata({+ userId: 'user123',+ URL: 'https://example.com/image.jpg',+ fileName: 'downloaded.jpg',+ }),+ ).rejects.toThrow('socket reset');+ expect(s3Mock.commandCalls(PutObjectCommand)).toHaveLength(0);+ });+it('throws error on non-ok response', async () => {(global.fetch as unknown as jest.Mock).mockResolvedValueOnce({ok: false,diff --git a/packages/api/src/storage/s3/crud.ts b/packages/api/src/storage/s3/crud.tsindex 5ddcf9cfa7..ee0bf4f14c 100644--- a/packages/api/src/storage/s3/crud.ts+++ b/packages/api/src/storage/s3/crud.ts@@ -1,5 +1,5 @@import fs from 'fs';-import { Readable } from 'stream';+import { Readable, pipeline } from 'stream';import { logger } from '@librechat/data-schemas';import { FileSources } from 'librechat-data-provider';import { getSignedUrl } from '@aws-sdk/s3-request-presigner';@@ -569,9 +569,14 @@ export async function saveURLToS3WithMetadata({const contentType = response.headers.get('content-type') ?? '';if (response.body) {- const source = Readable.fromWeb(- response.body as unknown as Parameters<typeof Readable.fromWeb>[0],- ).pipe(createRemoteFileByteLimitTransform(maxBytes));+ const source = createRemoteFileByteLimitTransform(maxBytes);+ pipeline(+ Readable.fromWeb(response.body as unknown as Parameters<typeof Readable.fromWeb>[0]),+ source,+ () => {+ /** Pipeline forwards failures to the returned stream and closes both ends. */+ },+ );const result = await saveReadableToS3({userId,body: source,diff --git a/packages/api/src/user/generations.spec.ts b/packages/api/src/user/generations.spec.tsindex da2681a3fe..5e9b001f28 100644--- a/packages/api/src/user/generations.spec.ts+++ b/packages/api/src/user/generations.spec.ts@@ -1,13 +1,16 @@-import type { UserGenerationControl } from './generations';-import { stopUserGenerations } from './generations';+import type { OwnedGenerationControl, UserGenerationControl } from './generations';+import { abortOwnedGeneration, stopUserGenerations } from './generations';/** Methods read `this`, as the real GenerationJobManager does, so an unbound call throws. */class FakeJobManager {private jobs = new Map([['s1', { createdAt: 7, metadata: { userId: 'u1', terminalPersistencePending: false } }],['s2', { createdAt: 8, metadata: { userId: 'other' } }],+ ['s3', { createdAt: 9, metadata: { userId: 'u1', tenantId: 't1' } }],]);+ readonly abortOptions: unknown[] = [];+readonly aborted: string[] = [];async getAccountCleanupJobIdsForUser() {@@ -18,8 +21,13 @@ class FakeJobManager {return this.jobs.get(streamId);}- async abortJob(streamId: string) {+ async getJob(streamId: string) {+ return this.jobs.get(streamId);+ }++ async abortJob(streamId: string, options?: unknown) {this.aborted.push(streamId);+ this.abortOptions.push(options);return { success: true };}}@@ -47,3 +55,30 @@ describe('stopUserGenerations', () => {).rejects.toThrow('could not be confirmed stopped');});});++describe('abortOwnedGeneration', () => {+ const control = (manager: FakeJobManager) => manager as unknown as OwnedGenerationControl;++ it("stops the caller's own run, fenced to the epoch it read", async () => {+ const manager = new FakeJobManager();++ const result = await abortOwnedGeneration('s1', { id: 'u1' }, control(manager));++ expect(result.success).toBe(true);+ expect(manager.aborted).toEqual(['s1']);+ expect(manager.abortOptions).toEqual([{ expectedCreatedAt: 7 }]);+ });++ it.each([+ ['another user', 's2', { id: 'u1' }],+ ['another tenant', 's3', { id: 'u1', tenantId: 't2' }],+ ['a missing run', 'missing', { id: 'u1' }],+ ])('reports %s as not found without stopping anything', async (_case, streamId, user) => {+ const manager = new FakeJobManager();++ const result = await abortOwnedGeneration(streamId, user, control(manager));++ expect(result).toMatchObject({ success: false, failureReason: 'job_not_found', jobData: null });+ expect(manager.aborted).toEqual([]);+ });+});diff --git a/packages/api/src/user/generations.ts b/packages/api/src/user/generations.tsindex f57de9a79d..8667ee4370 100644--- a/packages/api/src/user/generations.ts+++ b/packages/api/src/user/generations.ts@@ -1,3 +1,4 @@+import type { AbortResult } from '../stream/interfaces/IJobStore';import type { GenerationJob } from '../types/stream';import { waitForGenerationPersistence } from '../stream/persistence';import { isStopConfirmed } from '../stream/interfaces/IJobStore';@@ -53,3 +54,39 @@ export async function stopUserGenerations(),);}++/** The slice of the generation job manager needed to stop one caller-owned run. */+export interface OwnedGenerationControl {+ getJob: (streamId: string) => Promise<CleanupJob | undefined>;+ abortJob: (streamId: string, options: { expectedCreatedAt: number }) => Promise<AbortResult>;+}++/**+ * Stops `streamId` only when `user` owns it. A run owned by another user or tenant reads as+ * `job_not_found`, so a caller cannot tell it apart from one that does not exist. The epoch+ * fence keeps a replacement started between the read and the stop from being aborted.+ */+export async function abortOwnedGeneration(+ streamId: string,+ user: { id: string; tenantId?: string },+ control: OwnedGenerationControl,+): Promise<AbortResult> {+ const job = await control.getJob(streamId);+ const tenantId = job?.metadata?.tenantId;+ if (+ !job ||+ job.metadata?.userId !== user.id ||+ (tenantId != null && tenantId !== user.tenantId)+ ) {+ return {+ success: false,+ failureReason: 'job_not_found',+ jobData: null,+ content: [],+ finalEvent: null,+ text: '',+ collectedUsage: [],+ };+ }+ return control.abortJob(streamId, { expectedCreatedAt: job.createdAt });+}