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: Bound Document Name Collision Processing (#17386)
lia-by-librechat[bot]68826ec343d7
diff --git a/api/server/utils/import/importers.spec.js b/api/server/utils/import/importers.spec.jsindex ca6e0686fa..21dfe36ad9 100644--- a/api/server/utils/import/importers.spec.js+++ b/api/server/utils/import/importers.spec.js@@ -1,4 +1,6 @@const fs = require('fs');+const { formatMessage } = require('@librechat/agents');+const { dedupeDocumentNames, ContentTraversalLimitError } = require('@librechat/api');const path = require('path');const {EModelEndpoint,@@ -60,6 +62,41 @@ it('strips server-private owner metadata from untrusted LibreChat imports', asynexpect(importBatchBuilder.messages[0]).not.toHaveProperty('privacyRevision');});+it.each([2000, 8000])('bounds replay of %i imported inline document blocks', async (count) => {+ const content = Array.from({ length: count / 2 }, (_, index) => {+ const name = 'a'.repeat(196) + index.toString(36).padStart(4, '0');+ const document = { type: 'document', document: { name } };+ return [document, structuredClone(document)];+ }).flat();+ const jsonData = {+ conversationId: 'imported-documents',+ title: 'Imported documents',+ endpoint: EModelEndpoint.agents,+ messages: [+ {+ messageId: 'source',+ parentMessageId: Constants.NO_PARENT,+ isCreatedByUser: false,+ content,+ },+ ],+ };+ const builder = new ImportBatchBuilder('owner');+ await getImporter(jsonData)(jsonData, 'owner', () => builder);+ const imported = builder.messages[0];+ expect(imported.content).toEqual(content);+ const payload = [formatMessage({ message: imported })];++ if (count === 8000) {+ expect(() => dedupeDocumentNames(payload)).toThrow(ContentTraversalLimitError);+ } else {+ dedupeDocumentNames(payload);+ const names = payload[0].content.map((part) => part.document.name.toLowerCase());+ expect(new Set(names).size).toBe(count);+ }+ expect(imported.content).toEqual(content);+});+describe('importChatGptConvo', () => {it('should import conversation correctly', async () => {const expectedNumberOfMessages = 19;diff --git a/packages/api/src/files/encode/document.spec.ts b/packages/api/src/files/encode/document.spec.tsindex c73423a3ae..66a86bf27e 100644--- a/packages/api/src/files/encode/document.spec.ts+++ b/packages/api/src/files/encode/document.spec.ts@@ -3,6 +3,11 @@ import { mbToBytes } from 'librechat-data-provider';import type { AppConfig, IMongoFile } from '@librechat/data-schemas';import type { BedrockDocumentBlock, ServerRequest } from '~/types';import type { PayloadContentPart } from './document';+import {+ CONTENT_TRAVERSAL_MAX_DEPTH,+ CONTENT_TRAVERSAL_MAX_NODES,+ ContentTraversalLimitError,+} from '~/protection/adapters/nested';import { encodeAndFormatDocuments, dedupeDocumentNames } from './document';/** Mock the validation module */@@ -1282,6 +1287,104 @@ describe('dedupeDocumentNames', () => {expect(second[2].length).toBeLessThanOrEqual(200);});+ it.each([1000, 2000])('bounds collision lookups for %i shared-prefix documents', (count) => {+ const content = Array.from({ length: count / 2 }, (_, index) => {+ const name = 'a'.repeat(196) + index.toString(36).padStart(4, '0');+ return [bedrock(name), bedrock(name)];+ }).flat();+ const messages: Message[] = [{ content }];+ const has = jest.spyOn(Set.prototype, 'has');++ dedupeDocumentNames(messages);++ const lookups = has.mock.calls.length;+ has.mockRestore();+ const result = names(contentOf(messages[0]));+ expect(lookups).toBeLessThan(count * 6);+ expect(new Set(result.map((name) => name.toLowerCase())).size).toBe(count);+ expect(result.every((name) => name.length <= 200)).toBe(true);+ expect(names(content)).toEqual(content.map((part) => part.document.name));+ });++ it('shares progress after suffix digit widths change and skips occupied suffixes', () => {+ const prefix = 'A'.repeat(196);+ const occupied = Array.from({ length: 1000 }, (_, index) => {+ const suffix = ` (${index + 2})`;+ return bedrock(prefix.slice(0, 200 - suffix.length) + suffix);+ });+ const originals = [prefix + '0000', prefix.toLowerCase() + '0001', prefix + ' '];+ const content = [...occupied, ...originals.flatMap((name) => [bedrock(name), bedrock(name)])];+ const messages: Message[] = [{ content }];++ dedupeDocumentNames(messages);++ const result = names(contentOf(messages[0]));+ expect(result.slice(-6)).toEqual([+ originals[0],+ `${prefix.slice(0, 193)} (1002)`,+ originals[1],+ `${prefix.toLowerCase().slice(0, 193)} (1003)`,+ originals[2],+ `${prefix.slice(0, 193)} (1004)`,+ ]);+ expect(new Set(result.map((name) => name.toLowerCase())).size).toBe(content.length);+ });++ it('bounds imported names before case folding and resolves truncation collisions', () => {+ const prefix = 'a'.repeat(200);+ const messages: Message[] = [+ { content: [bedrock(prefix + 'x'.repeat(100000)), bedrock(prefix)] },+ ];++ dedupeDocumentNames(messages);++ expect(names(contentOf(messages[0]))).toEqual([prefix, `${prefix.slice(0, 196)} (2)`]);+ });++ it('rejects oversized replay arrays before copying or naming their parts', () => {+ const content = [bedrock('notes'), bedrock('notes')];+ content.length = CONTENT_TRAVERSAL_MAX_NODES;+ const messages: Message[] = [{ content }];++ expect(() => dedupeDocumentNames(messages)).toThrow(ContentTraversalLimitError);+ expect(messages[0].content).toBe(content);+ expect(content[1].document.name).toBe('notes');+ });++ it('shares the part budget across messages and nested steer media', () => {+ const media: PayloadContentPart[] = new Array(CONTENT_TRAVERSAL_MAX_NODES - 3);+ const messages: Message[] = [+ { content: [{ type: 'steer', media }] },+ { content: [bedrock('x')] },+ ];++ expect(() => dedupeDocumentNames(messages)).toThrow(ContentTraversalLimitError);+ });++ it('accepts a replay exactly at the node budget and bounds the message array', () => {+ const messages: Message[] = [{ content: new Array(CONTENT_TRAVERSAL_MAX_NODES - 1) }];++ expect(() => dedupeDocumentNames(messages)).not.toThrow();+ expect(() => dedupeDocumentNames(new Array(CONTENT_TRAVERSAL_MAX_NODES + 1))).toThrow(+ ContentTraversalLimitError,+ );+ });++ it('bounds recursive media and cycles without overflowing the stack', () => {+ const root: PayloadContentPart = { type: 'steer', media: [] };+ let part = root;+ for (let depth = 0; depth < CONTENT_TRAVERSAL_MAX_DEPTH; depth++) {+ const child: PayloadContentPart = { type: 'steer', media: [] };+ part.media = [child];+ part = child;+ }+ const messages: Message[] = [{ content: [root] }];++ expect(() => dedupeDocumentNames(messages)).toThrow(ContentTraversalLimitError);+ root.media = [root];+ expect(() => dedupeDocumentNames(messages)).toThrow(ContentTraversalLimitError);+ });+it('leaves blocks without a Bedrock document name alone', () => {const anthropic = {type: 'document',diff --git a/packages/api/src/files/encode/document.ts b/packages/api/src/files/encode/document.tsindex 7fa7323499..27444f6446 100644--- a/packages/api/src/files/encode/document.ts+++ b/packages/api/src/files/encode/document.ts@@ -15,6 +15,11 @@ import type {DocumentResult,ServerRequest,} from '~/types';+import {+ CONTENT_TRAVERSAL_MAX_DEPTH,+ CONTENT_TRAVERSAL_MAX_NODES,+ ContentTraversalLimitError,+} from '~/protection/adapters/nested';import {getFileStream,getConfiguredFileSizeLimit,@@ -34,25 +39,26 @@ export interface PayloadContentPart {media?: PayloadContentPart[];}-/** `name (n)`, trimmed so the suffix fits the limit and never follows a space. */-function numberedDocumentName(name: string, index: number): string {- const suffix = ` (${index})`;- return `${name.slice(0, BEDROCK_DOCUMENT_NAME_LIMIT - suffix.length).trimEnd()}${suffix}`;-}--/** Hands out each name once, numbering repeats in the order they are asked for. */+/** Hands out each name once, sharing suffix progress between colliding truncated prefixes. */function createDocumentNamer(): (name: string) => string {const used = new Set<string>();const nextIndex = new Map<string, number>();- return (original) => {- const baseKey = original.toLowerCase();+ return (input) => {+ const original = input.slice(0, BEDROCK_DOCUMENT_NAME_LIMIT);let name = original;- let index = nextIndex.get(baseKey) ?? 2;+ let index = 2;while (used.has(name.toLowerCase())) {- name = numberedDocumentName(original, index);- index++;+ const suffix = ` (${index})`;+ const prefix = original.slice(0, BEDROCK_DOCUMENT_NAME_LIMIT - suffix.length).trimEnd();+ const key = `${suffix.length}:${prefix.toLowerCase()}`;+ const next = nextIndex.get(key);+ if (next != null && next > index) {+ index = next;+ continue;+ }+ name = `${prefix}${suffix}`;+ nextIndex.set(key, ++index);}- nextIndex.set(baseKey, index);used.add(name.toLowerCase());return name;};@@ -62,7 +68,13 @@ function createDocumentNamer(): (name: string) => string {function renameDocumentParts(parts: PayloadContentPart[],nameOf: (name: string) => string,+ budget: { remaining: number },+ depth = 0,): PayloadContentPart[] {+ if (depth > CONTENT_TRAVERSAL_MAX_DEPTH || parts.length > budget.remaining) {+ throw new ContentTraversalLimitError();+ }+ budget.remaining -= parts.length;let renamed: PayloadContentPart[] | undefined;for (let i = 0; i < parts.length; i++) {const part = parts[i];@@ -72,7 +84,7 @@ function renameDocumentParts(const name = nameOf(original);next = name === original ? part : { ...part, document: { ...part.document, name } };} else if (Array.isArray(part?.media)) {- const media = renameDocumentParts(part.media, nameOf);+ const media = renameDocumentParts(part.media, nameOf, budget, depth + 1);next = media === part.media ? part : { ...part, media };}if (next !== part) {@@ -90,17 +102,21 @@ function renameDocumentParts(* formatted payload in conversation order (`report`, `report (2)`), leaving the first occurrence,* every other kind of part, and the parts themselves untouched: a message whose documents change* gets a new content array. Names compare case-insensitively, as the restriction is not- * documented to be case-sensitive.+ * documented to be case-sensitive. Replay always enforces traversal limits, even without filters.*/export function dedupeDocumentNames(messages: Array<{ content?: string | PayloadContentPart[] | null }>,): void {+ const budget = { remaining: CONTENT_TRAVERSAL_MAX_NODES - messages.length };+ if (budget.remaining < 0) {+ throw new ContentTraversalLimitError();+ }const nameOf = createDocumentNamer();for (const message of messages) {if (!Array.isArray(message?.content)) {continue;}- const content = renameDocumentParts(message.content, nameOf);+ const content = renameDocumentParts(message.content, nameOf, budget);if (content !== message.content) {message.content = content;}