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

📄 feat: Rich File Artifact Previews for DOCX, CSV, XLSX, PPTX (#12934)

Danny Avilaf20419d0b7b1
diff --git a/api/package.json b/api/package.json
index 7007791088..c5b588610e 100644
--- a/api/package.json
+++ b/api/package.json
@@ -105,6 +105,7 @@
"passport-local": "^1.0.0",
"pdfjs-dist": "^5.4.624",
"rate-limit-redis": "^4.2.0",
+ "sanitize-html": "^2.13.0",
"sharp": "^0.33.5",
"traverse": "^0.6.7",
"ua-parser-js": "^1.0.36",
@@ -116,6 +117,7 @@
"zod": "^3.22.4"
},
"devDependencies": {
+ "@types/sanitize-html": "^2.13.0",
"jest": "^30.2.0",
"mongodb-memory-server": "^11.0.1",
"nodemon": "^3.0.3",
diff --git a/api/server/controllers/agents/callbacks.js b/api/server/controllers/agents/callbacks.js
index 2af4d5b451..dc8f6cc65a 100644
--- a/api/server/controllers/agents/callbacks.js
+++ b/api/server/controllers/agents/callbacks.js
@@ -824,6 +824,19 @@ function createResponsesToolEndCallback({ req, res, tracker, artifactPromises })
width: fileMetadata.width,
height: fileMetadata.height,
tool_call_id: output.tool_call_id,
+ /* Inline text / sanitized HTML preview from
+ * `extractCodeArtifactText` — drives the file artifact panel's
+ * rich preview for DOCX/XLSX/CSV/PPTX. Pass null explicitly
+ * (rather than undefined) so the wire format is stable for the
+ * empty-text gate on the client. */
+ text: fileMetadata.text ?? null,
+ /* Trust signal so the client can route .docx/.csv/.xlsx/
+ * .pptx attachments to the office HTML buckets only when
+ * the backend produced sanitized full-document HTML.
+ * RAG-uploaded plain text from mammoth.extractRawText
+ * arrives without this flag and must NOT be injected as
+ * HTML — Codex P1 review on PR #12934. */
+ textFormat: fileMetadata.textFormat ?? null,
};
writeResponsesAttachment(res, tracker, attachment, metadata);
}
diff --git a/api/server/services/Files/Code/__tests__/process-traversal.spec.js b/api/server/services/Files/Code/__tests__/process-traversal.spec.js
index 016f3a71c6..099794821d 100644
--- a/api/server/services/Files/Code/__tests__/process-traversal.spec.js
+++ b/api/server/services/Files/Code/__tests__/process-traversal.spec.js
@@ -27,6 +27,11 @@ jest.mock('@librechat/api', () => {
createAxiosInstance: jest.fn(() => mockAxios),
classifyCodeArtifact: jest.fn(() => 'other'),
extractCodeArtifactText: jest.fn(async () => null),
+ /* `processCodeOutput` calls this to derive the trust flag persisted
+ * on `IMongoFile.textFormat` — Codex P1 review on PR #12934. The
+ * mock returns null in lockstep with the null `text` above so
+ * downstream consumers don't see a phantom format. */
+ getExtractedTextFormat: jest.fn(() => null),
codeServerHttpAgent: new http.Agent({ keepAlive: false }),
codeServerHttpsAgent: new https.Agent({ keepAlive: false }),
};
diff --git a/api/server/services/Files/Code/process.js b/api/server/services/Files/Code/process.js
index 5ae78a7e9a..3594cd2cb0 100644
--- a/api/server/services/Files/Code/process.js
+++ b/api/server/services/Files/Code/process.js
@@ -12,6 +12,7 @@ const {
codeServerHttpAgent,
codeServerHttpsAgent,
extractCodeArtifactText,
+ getExtractedTextFormat,
} = require('@librechat/api');
const {
Tools,
@@ -271,6 +272,15 @@ const processCodeOutput = async ({
const leafName = path.basename(safeName);
const category = classifyCodeArtifact(leafName, mimeType);
const text = await extractCodeArtifactText(buffer, leafName, mimeType, category);
+ /* `textFormat` accompanies `text` so the client can gate
+ * office-HTML-bucket routing on a trusted signal — clients MUST
+ * NOT inject `text` into the iframe as HTML unless `textFormat ===
+ * 'html'`. RAG-uploaded `.docx` etc. arrive with plain text from
+ * mammoth.extractRawText and would otherwise be hijacked by the
+ * extension-based office routing into the HTML-injection path
+ * (Codex P1 review on PR #12934). null on extract failure — the
+ * client treats absence as 'text' for safety. */
+ const textFormat = getExtractedTextFormat(leafName, mimeType, text);
const file = {
file_id,
@@ -293,6 +303,7 @@ const processCodeOutput = async ({
// text — `createFile` uses findOneAndUpdate with $set semantics, which
// would otherwise leave a stale value behind.
text: text ?? null,
+ textFormat: textFormat ?? null,
};
await createFile(file, true);
diff --git a/api/server/services/Files/Code/process.spec.js b/api/server/services/Files/Code/process.spec.js
index e0d7b08fd3..8b12278c9a 100644
--- a/api/server/services/Files/Code/process.spec.js
+++ b/api/server/services/Files/Code/process.spec.js
@@ -43,6 +43,7 @@ mockAxios.isAxiosError = jest.fn(() => false);
const mockClassifyCodeArtifact = jest.fn(() => 'other');
const mockExtractCodeArtifactText = jest.fn(async () => null);
+const mockGetExtractedTextFormat = jest.fn((_name, _mime, text) => (text == null ? null : 'text'));
jest.mock('@librechat/api', () => {
const http = require('http');
const https = require('https');
@@ -63,6 +64,15 @@ jest.mock('@librechat/api', () => {
*/
classifyCodeArtifact: (...args) => mockClassifyCodeArtifact(...args),
extractCodeArtifactText: (...args) => mockExtractCodeArtifactText(...args),
+ /* `processCodeOutput` derives the `textFormat` trust flag for
+ * `IMongoFile` from this helper — Codex P1 review on PR #12934.
+ * The mock returns 'text' for non-null extractor output and null
+ * otherwise so the downstream `file.textFormat` field is set to
+ * a believable shape without modeling the office-HTML branch
+ * (the dispatcher under test isn't exercising that path). Per-
+ * test overrides via `mockGetExtractedTextFormat.mockReturnValue`
+ * if a case needs to assert the 'html' value. */
+ getExtractedTextFormat: (...args) => mockGetExtractedTextFormat(...args),
codeServerHttpAgent: new http.Agent({ keepAlive: false }),
codeServerHttpsAgent: new https.Agent({ keepAlive: false }),
};
diff --git a/client/src/components/Artifacts/Artifacts.tsx b/client/src/components/Artifacts/Artifacts.tsx
index e2a322b1ad..2778402a57 100644
--- a/client/src/components/Artifacts/Artifacts.tsx
+++ b/client/src/components/Artifacts/Artifacts.tsx
@@ -1,4 +1,4 @@
-import { useRef, useState, useEffect, useCallback } from 'react';
+import { useRef, useState, useEffect, useCallback, useMemo } from 'react';
import copy from 'copy-to-clipboard';
import * as Tabs from '@radix-ui/react-tabs';
import { Code, Play, RefreshCw, X } from 'lucide-react';
@@ -11,6 +11,8 @@ import useArtifacts from '~/hooks/Artifacts/useArtifacts';
import DownloadArtifact from './DownloadArtifact';
import ArtifactVersion from './ArtifactVersion';
import ArtifactTabs from './ArtifactTabs';
+import { isPreviewOnlyArtifact } from '~/utils/artifacts';
+import { displayFilename } from '~/components/Chat/Messages/Content/Parts/attachmentTypes';
import { useLocalize } from '~/hooks';
import { cn } from '~/utils';
import store from '~/store';
@@ -37,18 +39,21 @@ export default function Artifacts() {
const setArtifactsVisible = useSetRecoilState(store.artifactsVisibility);
const resetCurrentArtifactId = useResetRecoilState(store.currentArtifactId);
- const tabOptions = [
- {
- value: 'code',
- label: localize('com_ui_code'),
- icon: <Code className="size-4" />,
- },
- {
- value: 'preview',
- label: localize('com_ui_preview'),
- icon: <Play className="size-4" />,
- },
- ];
+ const allTabOptions = useMemo(
+ () => [
+ {
+ value: 'code',
+ label: localize('com_ui_code'),
+ icon: <Code className="size-4" />,
+ },
+ {
+ value: 'preview',
+ label: localize('com_ui_preview'),
+ icon: <Play className="size-4" />,
+ },
+ ],
+ [localize],
+ );
useEffect(() => {
setIsMounted(true);
@@ -88,6 +93,33 @@ export default function Artifacts() {
setCurrentArtifactId,
} = useArtifacts();
+ /* Office artifacts (DOCX/SPREADSHEET/PRESENTATION) have no source view —
+ * the underlying file is binary and the "code" tab would display the
+ * generated HTML blob, which isn't useful. Filter the tab options and
+ * snap the active tab when the user lands on an office artifact while
+ * the code tab is selected. */
+ const isPreviewOnly = isPreviewOnlyArtifact(currentArtifact?.type);
+ const tabOptions = useMemo(() => {
+ if (!isPreviewOnly) {
+ return allTabOptions;
+ }
+ /* When only the preview tab is shown, the generic "Preview" label is
+ * a no-op pill — surface the document filename there instead. The
+ * Play icon stays as a visual cue for "rendered preview". `displayFilename`
+ * handles the sandbox dotfile suffix the upload pipeline applies. */
+ const filename = displayFilename(currentArtifact?.title);
+ const previewTab = allTabOptions.find((opt) => opt.value === 'preview');
+ if (!previewTab) {
+ return allTabOptions;
+ }
+ return [filename ? { ...previewTab, label: filename } : previewTab];
+ }, [allTabOptions, isPreviewOnly, currentArtifact?.title]);
+ useEffect(() => {
+ if (isPreviewOnly && activeTab === 'code') {
+ setActiveTab('preview');
+ }
+ }, [isPreviewOnly, activeTab, setActiveTab]);
+
const handleCopyArtifact = useCallback(() => {
const content = currentArtifact?.content ?? '';
if (!content) {
diff --git a/client/src/components/Chat/Messages/Content/Parts/__tests__/ArtifactRouting.test.tsx b/client/src/components/Chat/Messages/Content/Parts/__tests__/ArtifactRouting.test.tsx
index 8a4d6728fe..4b6c93ae92 100644
--- a/client/src/components/Chat/Messages/Content/Parts/__tests__/ArtifactRouting.test.tsx
+++ b/client/src/components/Chat/Messages/Content/Parts/__tests__/ArtifactRouting.test.tsx
@@ -183,15 +183,40 @@ describe('Attachment routing for tool artifacts', () => {
expect(screen.queryByText('com_ui_artifact_click')).not.toBeInTheDocument();
});
- it('falls through to the inline <pre> for unsupported text types (CSV)', () => {
- const csv = baseAttachment({
- filename: 'data.csv',
- text: 'a,b,c\n1,2,3',
+ it('falls through to the inline <pre> for unsupported text types (JSON)', () => {
+ /* CSV used to fall through here, but now routes to the SPREADSHEET
+ * preview bucket. JSON is still inline-only (no dedicated viewer
+ * yet); use it as the canonical "unrouted text" example. */
+ const json = baseAttachment({
+ filename: 'data.json',
+ type: 'application/json',
+ text: '{"a":1,"b":2}',
} as Partial<TAttachment>);
- const { container } = renderWith(<Attachment attachment={csv} />);
+ const { container } = renderWith(<Attachment attachment={json} />);
expect(container.querySelector('pre')).not.toBeNull();
expect(screen.queryByTestId('mermaid-render')).not.toBeInTheDocument();
});
+
+ it.each([
+ ['data.csv', 'text/csv'],
+ ['workbook.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
+ ['report.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
+ ['deck.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'],
+ ])('routes %s through the office preview panel (panel artifact)', (filename, type) => {
+ const att = baseAttachment({
+ file_id: `office-${filename}`,
+ filename,
+ type,
+ text: '<!DOCTYPE html><body><table><tr><td>x</td></tr></table></body>',
+ } as Partial<TAttachment>);
+ renderWith(<Attachment attachment={att} />);
+ expect(screen.getByText(filename)).toBeInTheDocument();
+ /* Auto-pressed open button (streaming + non-CODE bucket) — same UX as
+ * the HTML panel artifact above. */
+ expect(screen.getByRole('button', { pressed: true })).toBeInTheDocument();
+ const downloadPattern = new RegExp(`com_ui_download.*${filename.replace('.', '\\.')}`, 'i');
+ expect(screen.getByRole('button', { name: downloadPattern })).toBeInTheDocument();
+ });
});
describe('ToolArtifactCard click behaviour', () => {
@@ -645,8 +670,9 @@ describe('AttachmentGroup routing', () => {
} as Partial<TAttachment>),
baseAttachment({
file_id: 'c',
- filename: 'data.csv',
- text: 'a,b,c\n1,2,3',
+ filename: 'data.json',
+ type: 'application/json',
+ text: '{"a":1}',
} as Partial<TAttachment>),
baseAttachment({
file_id: 'd',
@@ -661,7 +687,7 @@ describe('AttachmentGroup routing', () => {
expect(screen.getByText('index.html')).toBeInTheDocument();
// Mermaid render
expect(screen.getByTestId('mermaid-render')).toBeInTheDocument();
- // Inline text fallback for CSV
+ // Inline text fallback for JSON (CSV now goes to SPREADSHEET)
expect(container.querySelector('pre')).not.toBeNull();
// FileContainer for the plain zip (and potentially others)
expect(screen.getAllByTestId('file-container').length).toBeGreaterThan(0);
diff --git a/client/src/components/Chat/Messages/Content/Parts/__tests__/LogContent.test.tsx b/client/src/components/Chat/Messages/Content/Parts/__tests__/LogContent.test.tsx
index fa14cca11c..bb34bb1dee 100644
--- a/client/src/components/Chat/Messages/Content/Parts/__tests__/LogContent.test.tsx
+++ b/client/src/components/Chat/Messages/Content/Parts/__tests__/LogContent.test.tsx
@@ -103,13 +103,17 @@ describe('LogContent attachment routing', () => {
expect(screen.queryByRole('button', { pressed: true })).not.toBeInTheDocument();
});
- it('routes text-bearing CSV through inline <pre>, not the panel', () => {
- const csv = baseAttachment({
+ it('routes text-bearing JSON through inline <pre>, not the panel', () => {
+ /* CSV used to fall through here, but now routes to the SPREADSHEET
+ * preview bucket. JSON has no dedicated viewer yet, so it remains
+ * the canonical "unrouted text" example. */
+ const json = baseAttachment({
file_id: 'c',
- filename: 'data.csv',
- text: 'a,b,c\n1,2,3',
+ filename: 'data.json',
+ type: 'application/json',
+ text: '{"a":1,"b":2}',
} as Partial<TAttachment>);
- const { container } = renderWith(<LogContent output="" attachments={[csv]} />);
+ const { container } = renderWith(<LogContent output="" attachments={[json]} />);
expect(container.querySelector('pre')).not.toBeNull();
expect(screen.queryByRole('button', { pressed: true })).not.toBeInTheDocument();
expect(screen.queryByTestId('mermaid-render')).not.toBeInTheDocument();
@@ -125,28 +129,47 @@ describe('LogContent attachment routing', () => {
expect(screen.getByTestId('log-link')).toHaveAttribute('data-filename', 'archive.zip');
});
- it('renders a panel card for pptx with empty text using the placeholder', () => {
- // pptx text extraction is deferred; the artifact still routes through
- // the panel and gets the localized placeholder content. Here the
- // localize mock returns the key, so we assert by class/structure.
+ it('renders a panel card for a pptx with backend-rendered HTML in text', () => {
+ /* PPTX (and DOCX/XLSX/CSV) now route through the office preview
+ * bucket with a strict empty-text gate — the artifact only registers
+ * once the backend's `bufferToOfficeHtml` has produced the slide-list
+ * HTML and shipped it via `attachment.text`. */
const pptx = baseAttachment({
file_id: 'e',
filename: 'slides.pptx',
- text: undefined as unknown as string,
+ type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
+ text: '<!DOCTYPE html><body><ol><li>Slide 1</li></ol></body>',
} as Partial<TAttachment>);
renderWith(<LogContent output="" attachments={[pptx]} />);
expect(screen.getByText('slides.pptx')).toBeInTheDocument();
});
+ it('falls back to the legacy download branch for an office file with no extracted text', () => {
+ /* Empty `text` for an office type fails the strict gate, so the
+ * artifact stays unregistered and the file flows to the download
+ * fallback (LogLink), not a half-rendered panel card. */
+ const pptx = baseAttachment({
+ file_id: 'e2',
+ filename: 'slides.pptx',
+ type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
+ text: undefined as unknown as string,
+ } as Partial<TAttachment>);
+ renderWith(<LogContent output="" attachments={[pptx]} />);
+ expect(screen.queryByRole('button', { pressed: true })).not.toBeInTheDocument();
+ expect(screen.getByTestId('log-link')).toHaveAttribute('data-filename', 'slides.pptx');
+ });
+
it('routes an expired panel-eligible attachment through the legacy expired path', () => {
- // Without this gate, an expired pptx/html/etc. would render as a
- // clickable artifact card backed by a dead download link. The
- // legacy "download expired" message must win for any panel-eligible
- // entry whose `expiresAt` is in the past.
+ /* Without this gate, an expired pptx/html/etc. with extracted HTML
+ * would render as a clickable artifact card backed by a dead
+ * download link. The legacy "download expired" message must win for
+ * any panel-eligible entry whose `expiresAt` is in the past — even
+ * when the office HTML is present. */
const expired = baseAttachment({
file_id: 'x-expired',
filename: 'slides.pptx',
- text: undefined as unknown as string,
+ type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
+ text: '<!DOCTYPE html><body><ol><li>Slide 1</li></ol></body>',
expiresAt: Date.now() - 60_000,
} as Partial<TAttachment>);
renderWith(<LogContent output="" attachments={[expired]} />);
@@ -183,8 +206,13 @@ describe('LogContent attachment routing', () => {
} as Partial<TAttachment>),
baseAttachment({
file_id: 't',
- filename: 'notes.csv',
- text: 'a,b\n1,2',
+ /* JSON stays on the inline `<pre>` rendering path. CSV used to
+ * live here too but now routes to the SPREADSHEET preview
+ * bucket, so it would no longer satisfy the "inline pre" check
+ * below. */
+ filename: 'notes.json',
+ type: 'application/json',
+ text: '{"a":1,"b":2}',
} as Partial<TAttachment>),
] as TAttachment[];
const { container } = renderWith(
@@ -194,7 +222,7 @@ describe('LogContent attachment routing', () => {
expect(screen.getByText('index.html')).toBeInTheDocument();
// mermaid render present
expect(screen.getByTestId('mermaid-render')).toBeInTheDocument();
- // CSV inline pre present
+ // JSON inline pre present
expect(container.querySelector('pre')).not.toBeNull();
});
});
diff --git a/client/src/components/Chat/Messages/Content/Parts/__tests__/TextAttachment.test.tsx b/client/src/components/Chat/Messages/Content/Parts/__tests__/TextAttachment.test.tsx
index 7a6d0b003e..c5a8369796 100644
--- a/client/src/components/Chat/Messages/Content/Parts/__tests__/TextAttachment.test.tsx
+++ b/client/src/components/Chat/Messages/Content/Parts/__tests__/TextAttachment.test.tsx
@@ -56,10 +56,15 @@ jest.mock('~/utils', () => ({
const textAttachment = (overrides: Partial<TAttachment> = {}): TAttachment =>
({
file_id: 'file-1',
- filename: 'output.csv',
- filepath: '/files/output.csv',
- type: 'text/csv',
- text: 'a,b,c\n1,2,3',
+ /* JSON stays on the inline `<pre>` rendering path. CSV used to live
+ * here too but now routes through the SPREADSHEET artifact panel
+ * (Recoil-bound), so a CSV fixture would force every test in this
+ * file to add a `RecoilRoot` wrapper. JSON has the same shape (text-
+ * bearing, downloadable, expandable) without the panel coupling. */
+ filename: 'output.json',
+ filepath: '/files/output.json',
+ type: 'application/json',
+ text: '{"a":1,"b":2,"c":3}',
...overrides,
}) as TAttachment;
@@ -102,7 +107,7 @@ describe('TextAttachment (via Attachment default export)', () => {
const { container } = render(<Attachment attachment={textAttachment()} />);
const pre = container.querySelector('pre');
expect(pre).not.toBeNull();
- expect(pre!.textContent).toBe('a,b,c\n1,2,3');
+ expect(pre!.textContent).toBe('{"a":1,"b":2,"c":3}');
});
it('renders a download chip when filepath is present', () => {
@@ -163,10 +168,11 @@ describe('AttachmentGroup', () => {
});
it('routes text-bearing attachments through the text rendering path', () => {
- // `.csv` is text-bearing but not artifact-eligible (CSV gets a
- // dedicated viewer in a follow-up), so it falls through to the
- // inline <pre> renderer rather than the side panel card.
- const attachments = [textAttachment({ file_id: 'a', filename: 'a.csv' })] as TAttachment[];
+ /* `.json` is text-bearing but not artifact-eligible (JSON has no
+ * dedicated viewer yet), so it falls through to the inline <pre>
+ * renderer rather than the side panel card. CSV used to live here
+ * too but now routes through the SPREADSHEET artifact panel. */
+ const attachments = [textAttachment({ file_id: 'a', filename: 'a.json' })] as TAttachment[];
const { container } = render(<AttachmentGroup attachments={attachments} />);
expect(container.querySelector('pre')).not.toBeNull();
});
diff --git a/client/src/components/Chat/Messages/Content/Parts/__tests__/attachmentTypes.test.ts b/client/src/components/Chat/Messages/Content/Parts/__tests__/attachmentTypes.test.ts
index e396ab1418..02113c8a25 100644
--- a/client/src/components/Chat/Messages/Content/Parts/__tests__/attachmentTypes.test.ts
+++ b/client/src/components/Chat/Messages/Content/Parts/__tests__/attachmentTypes.test.ts
@@ -133,9 +133,13 @@ describe('artifactTypeForAttachment', () => {
});
it('returns null for unsupported extensions', () => {
+ /* CSV / DOCX / XLSX / PPTX now route through the office preview
+ * buckets (rich HTML preview); use a binary type with no preview
+ * pipeline instead. */
const attachment = baseAttachment({
- filename: 'data.csv',
- text: 'a,b,c',
+ filename: 'photo.jpg',
+ type: 'image/jpeg',
+ text: undefined,
} as Partial<TAttachment>);
expect(artifactTypeForAttachment(attachment)).toBeNull();
});
diff --git a/client/src/hooks/Artifacts/useArtifactProps.ts b/client/src/hooks/Artifacts/useArtifactProps.ts
index 3a7b17c24a..f76093a997 100644
--- a/client/src/hooks/Artifacts/useArtifactProps.ts
+++ b/client/src/hooks/Artifacts/useArtifactProps.ts
@@ -45,6 +45,21 @@ export default function useArtifactProps({ artifact }: { artifact: Artifact }) {
return ['content.md', getMarkdownFiles(artifact.content ?? '')];
}
+ /* Office preview buckets (DOCX/SPREADSHEET/PRESENTATION): the backend
+ * already produced a complete sanitized HTML document via
+ * `bufferToOfficeHtml` and shipped it as `attachment.text`. Hand it
+ * to the Sandpack `static` template's `index.html` slot directly —
+ * no wrapping, no transformation, no client-side parsing libs. The
+ * empty-text gate in `detectArtifactTypeFromFile` guarantees we
+ * never reach this branch with an empty content payload. */
+ if (
+ type === TOOL_ARTIFACT_TYPES.DOCX ||
+ type === TOOL_ARTIFACT_TYPES.SPREADSHEET ||
+ type === TOOL_ARTIFACT_TYPES.PRESENTATION
+ ) {
+ return ['index.html', { 'index.html': artifact.content ?? '' }];
+ }
+
const fileKey = getArtifactFilename(artifact.type ?? '', artifact.language);
const files = removeNullishValues({
[fileKey]: artifact.content,
diff --git a/client/src/utils/__tests__/artifacts.test.ts b/client/src/utils/__tests__/artifacts.test.ts
index 02e7494c15..b664bc3456 100644
--- a/client/src/utils/__tests__/artifacts.test.ts
+++ b/client/src/utils/__tests__/artifacts.test.ts
@@ -2,9 +2,11 @@ import {
buildSandpackOptions,
detectArtifactTypeFromFile,
fileToArtifact,
+ isPreviewOnlyArtifact,
languageForFilename,
TOOL_ARTIFACT_TYPES,
} from '../artifacts';
+import type { ToolArtifactType } from '../artifacts';
const TAILWIND_CDN = 'https://cdn.tailwindcss.com/3.4.17#tailwind.js';
@@ -55,35 +57,195 @@ describe('detectArtifactTypeFromFile', () => {
['flow.mmd', TOOL_ARTIFACT_TYPES.MERMAID],
['flow.mermaid', TOOL_ARTIFACT_TYPES.MERMAID],
['readme.txt', TOOL_ARTIFACT_TYPES.PLAIN_TEXT],
- ['report.docx', TOOL_ARTIFACT_TYPES.PLAIN_TEXT],
['notes.odt', TOOL_ARTIFACT_TYPES.PLAIN_TEXT],
- ['slides.pptx', TOOL_ARTIFACT_TYPES.PLAIN_TEXT],
+ ['report.docx', TOOL_ARTIFACT_TYPES.DOCX],
+ ['data.csv', TOOL_ARTIFACT_TYPES.SPREADSHEET],
+ ['workbook.xlsx', TOOL_ARTIFACT_TYPES.SPREADSHEET],
+ ['legacy.xls', TOOL_ARTIFACT_TYPES.SPREADSHEET],
+ ['sheet.ods', TOOL_ARTIFACT_TYPES.SPREADSHEET],
+ ['slides.pptx', TOOL_ARTIFACT_TYPES.PRESENTATION],
])('classifies %s by extension', (filename, expected) => {
- expect(detectArtifactTypeFromFile({ filename, type: '', text: 'content' })).toBe(expected);
+ /* Office types require `textFormat: 'html'` to route to their HTML
+ * preview buckets — the security gate added for Codex P1 review on
+ * PR #12934. Plain-text/markdown/code/etc. don't take that path so
+ * they pass through unchanged. */
+ const isOfficeBucket = (
+ [
+ TOOL_ARTIFACT_TYPES.DOCX,
+ TOOL_ARTIFACT_TYPES.SPREADSHEET,
+ TOOL_ARTIFACT_TYPES.PRESENTATION,
+ ] as ToolArtifactType[]
+ ).includes(expected);
+ const textFormat = isOfficeBucket ? ('html' as const) : undefined;
+ expect(detectArtifactTypeFromFile({ filename, type: '', text: 'content', textFormat })).toBe(
+ expected,
+ );
});
it.each([
- ['index.html', TOOL_ARTIFACT_TYPES.HTML],
- ['App.tsx', TOOL_ARTIFACT_TYPES.REACT],
- ['flow.mmd', TOOL_ARTIFACT_TYPES.MERMAID],
- ])('returns null when %s has no text (%s viewer needs real content)', (filename) => {
- expect(detectArtifactTypeFromFile({ filename, type: '', text: '' })).toBeNull();
- expect(detectArtifactTypeFromFile({ filename, type: '', text: undefined })).toBeNull();
- });
+ ['index.html', TOOL_ARTIFACT_TYPES.HTML, undefined],
+ ['App.tsx', TOOL_ARTIFACT_TYPES.REACT, undefined],
+ ['flow.mmd', TOOL_ARTIFACT_TYPES.MERMAID, undefined],
+ /* Office preview buckets need server-rendered HTML in `text` to render
+ * — the empty-text gate keeps the artifact off the panel until the
+ * backend's `bufferToOfficeHtml` finishes. The `textFormat: 'html'`
+ * trust flag is required for the routing to even land on the office
+ * bucket; without it, the security gate downgrades to PLAIN_TEXT
+ * (Codex P1 review). The strict empty-text gate then returns null. */
+ ['report.docx', TOOL_ARTIFACT_TYPES.DOCX, 'html' as const],
+ ['data.csv', TOOL_ARTIFACT_TYPES.SPREADSHEET, 'html' as const],
+ ['workbook.xlsx', TOOL_ARTIFACT_TYPES.SPREADSHEET, 'html' as const],
+ ['slides.pptx', TOOL_ARTIFACT_TYPES.PRESENTATION, 'html' as const],
+ ])(
+ 'returns null when %s has no text (renderer needs real content)',
+ (filename, _expected, textFormat) => {
+ expect(detectArtifactTypeFromFile({ filename, type: '', text: '', textFormat })).toBeNull();
+ expect(
+ detectArtifactTypeFromFile({ filename, type: '', text: undefined, textFormat }),
+ ).toBeNull();
+ },
+ );
it.each([
['readme.txt', TOOL_ARTIFACT_TYPES.PLAIN_TEXT],
- ['slides.pptx', TOOL_ARTIFACT_TYPES.PLAIN_TEXT],
- ['report.docx', TOOL_ARTIFACT_TYPES.PLAIN_TEXT],
['notes.md', TOOL_ARTIFACT_TYPES.MARKDOWN],
+ ['notes.odt', TOOL_ARTIFACT_TYPES.PLAIN_TEXT],
])(
- 'still routes %s through the panel without text (deferred-extraction case)',
+ 'still routes %s through the panel without text (deferred-extraction case for plain-text/markdown)',
(filename, expected) => {
expect(detectArtifactTypeFromFile({ filename, type: '', text: '' })).toBe(expected);
expect(detectArtifactTypeFromFile({ filename, type: '', text: undefined })).toBe(expected);
},
);
+ it.each([
+ [
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ TOOL_ARTIFACT_TYPES.DOCX,
+ ],
+ [
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
+ TOOL_ARTIFACT_TYPES.SPREADSHEET,
+ ],
+ ['application/vnd.ms-excel', TOOL_ARTIFACT_TYPES.SPREADSHEET],
+ ['application/vnd.oasis.opendocument.spreadsheet', TOOL_ARTIFACT_TYPES.SPREADSHEET],
+ ['text/csv', TOOL_ARTIFACT_TYPES.SPREADSHEET],
+ ['application/csv', TOOL_ARTIFACT_TYPES.SPREADSHEET],
+ /* Legacy CSV MIME variant — backend's `CSV_MIME_PATTERN` accepts
+ * it, so the client must too or extensionless CSVs with this MIME
+ * would be skipped despite the backend producing valid HTML.
+ * Regression for Codex P3 review on PR #12934. */
+ ['text/comma-separated-values', TOOL_ARTIFACT_TYPES.SPREADSHEET],
+ /* Legacy XLS MIME aliases — backend's `excelMimeTypes` regex
+ * accepts the full set used by older browsers/servers; the client
+ * mirrors via the same regex so an extensionless XLS with any of
+ * these legacy MIMEs gets routed to the spreadsheet bucket and
+ * the artifact actually shows up on the panel. Regression for
+ * Codex P1 review on PR #12934. */
+ ['application/x-ms-excel', TOOL_ARTIFACT_TYPES.SPREADSHEET],
+ ['application/x-msexcel', TOOL_ARTIFACT_TYPES.SPREADSHEET],
+ ['application/msexcel', TOOL_ARTIFACT_TYPES.SPREADSHEET],
+ ['application/x-excel', TOOL_ARTIFACT_TYPES.SPREADSHEET],
+ ['application/x-dos_ms_excel', TOOL_ARTIFACT_TYPES.SPREADSHEET],
+ ['application/xls', TOOL_ARTIFACT_TYPES.SPREADSHEET],
+ ['application/x-xls', TOOL_ARTIFACT_TYPES.SPREADSHEET],
+ [
+ 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
+ TOOL_ARTIFACT_TYPES.PRESENTATION,
+ ],
+ ])('routes office MIME %s to its preview bucket when extension is missing', (mime, expected) => {
+ /* `textFormat: 'html'` is required so the security gate (Codex P1 on
+ * PR #12934) lets routing proceed to the office HTML bucket — without
+ * it, the gate would downgrade to PLAIN_TEXT to keep RAG-extracted
+ * plain-text from being injected as HTML. */
+ expect(
+ detectArtifactTypeFromFile({
+ filename: 'noext',
+ type: mime,
+ text: '<html>x</html>',
+ textFormat: 'html',
+ }),
+ ).toBe(expected);
+ });
+
+ /* Codex P1 SECURITY: office HTML routing requires the backend's explicit
+ * `textFormat: 'html'` trust flag. Without it (e.g. RAG-uploaded `.docx`
+ * with mammoth.extractRawText plain text in `attachment.text`, or any
+ * legacy attachment from before this flag existed), routing falls back
+ * to PLAIN_TEXT so the markdown viewer escapes content rather than
+ * letting useArtifactProps inject the text as `index.html`. */
+ it('downgrades office types to PLAIN_TEXT when textFormat is missing (legacy attachments)', () => {
+ expect(
+ detectArtifactTypeFromFile({
+ filename: 'report.docx',
+ type: '',
+ text: 'Plain text mammoth extracted from a docx — must NOT render as HTML.',
+ }),
+ ).toBe(TOOL_ARTIFACT_TYPES.PLAIN_TEXT);
+ expect(
+ detectArtifactTypeFromFile({
+ filename: 'data.csv',
+ type: 'text/csv',
+ text: 'col1,col2\n1,2',
+ }),
+ ).toBe(TOOL_ARTIFACT_TYPES.PLAIN_TEXT);
+ expect(
+ detectArtifactTypeFromFile({
+ filename: 'slides.pptx',
+ type: '',
+ text: 'Slide 1: Intro\nSlide 2: Outro',
+ }),
+ ).toBe(TOOL_ARTIFACT_TYPES.PLAIN_TEXT);
+ });
+
+ it('downgrades office types to PLAIN_TEXT when textFormat is "text" (explicit non-HTML)', () => {
+ /* The backend marks RAG/text-extraction output as `textFormat: 'text'`
+ * to make the trust contract explicit. Either no flag or 'text' both
+ * route to PLAIN_TEXT — only 'html' unlocks the office HTML bucket. */
+ expect(
+ detectArtifactTypeFromFile({
+ filename: 'report.docx',
+ type: '',
+ text: '<script>alert(1)</script>',
+ textFormat: 'text',
+ }),
+ ).toBe(TOOL_ARTIFACT_TYPES.PLAIN_TEXT);
+ });
+
+ it('downgrades office types to PLAIN_TEXT when textFormat is null (DB legacy)', () => {
+ /* Mongoose returns `null` for fields the document was saved without
+ * — covers attachments persisted before the textFormat field existed. */
+ expect(
+ detectArtifactTypeFromFile({
+ filename: 'workbook.xlsx',
+ type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
+ text: '<table><tr><td>1</td></tr></table>',
+ textFormat: null,
+ }),
+ ).toBe(TOOL_ARTIFACT_TYPES.PLAIN_TEXT);
+ });
+
+ /* Regression: an office attachment with no `text` AND no `textFormat`
+ * must NOT downgrade to PLAIN_TEXT (which has the lenient empty-text
+ * gate and would render as a half-empty panel card). The historical
+ * contract for office types with missing extraction is "fall through
+ * to the legacy download UI"; the security gate's empty-text exception
+ * preserves that. CI regression on PR #12934 — `LogContent.test.tsx`
+ * "falls back to the legacy download branch for an office file with
+ * no extracted text" was the canary. */
+ it.each([
+ ['report.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
+ ['data.csv', 'text/csv'],
+ ['workbook.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
+ ['slides.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'],
+ ])(
+ '%s with no text and no textFormat returns null (preserves legacy download path)',
+ (filename, type) => {
+ expect(detectArtifactTypeFromFile({ filename, type, text: '' })).toBeNull();
+ expect(detectArtifactTypeFromFile({ filename, type, text: undefined })).toBeNull();
+ },
+ );
+
it('falls back to MIME when the extension is unknown', () => {
expect(detectArtifactTypeFromFile({ filename: 'noext', type: 'text/html', text: 'x' })).toBe(
TOOL_ARTIFACT_TYPES.HTML,
@@ -114,11 +276,13 @@ describe('detectArtifactTypeFromFile', () => {
});
it('returns null for unsupported types', () => {
+ /* PDFs have no rich preview path on the client (the artifact panel
+ * doesn't host a PDF viewer); they fall back to the download UI. */
expect(
- detectArtifactTypeFromFile({ filename: 'output.csv', type: 'text/csv', text: 'a,b' }),
+ detectArtifactTypeFromFile({ filename: 'doc.pdf', type: 'application/pdf', text: 'x' }),
).toBeNull();
expect(
- detectArtifactTypeFromFile({ filename: 'doc.pdf', type: 'application/pdf', text: 'x' }),
+ detectArtifactTypeFromFile({ filename: 'photo.jpg', type: 'image/jpeg', text: 'binary' }),
).toBeNull();
});
@@ -177,12 +341,11 @@ describe('detectArtifactTypeFromFile', () => {
);
});
- it('does NOT route data formats to CODE (CSV / JSON / YAML / TOML / XML)', () => {
+ it('does NOT route data formats to CODE (JSON / YAML / TOML / XML)', () => {
/* These get dedicated viewers in follow-ups; for now they fall
- * through to inline rendering (return null). */
- expect(
- detectArtifactTypeFromFile({ filename: 'data.csv', type: 'text/csv', text: 'a,b' }),
- ).toBeNull();
+ * through to inline rendering (return null). CSV is the exception:
+ * it routes to the SPREADSHEET preview bucket — covered separately
+ * in the "classifies %s by extension" suite above. */
expect(
detectArtifactTypeFromFile({ filename: 'data.json', type: 'application/json', text: '{}' }),
).toBeNull();
@@ -397,7 +560,10 @@ describe('fileToArtifact', () => {
});
it('returns null for unsupported types so callers can fall through', () => {
- expect(fileToArtifact({ ...baseFile, filename: 'data.csv', type: 'text/csv' })).toBeNull();
+ expect(fileToArtifact({ ...baseFile, filename: 'photo.jpg', type: 'image/jpeg' })).toBeNull();
+ expect(
+ fileToArtifact({ ...baseFile, filename: 'doc.pdf', type: 'application/pdf' }),
+ ).toBeNull();
});
/* End-to-end test for the CODE bucket. The classification path is
@@ -502,13 +668,18 @@ describe('fileToArtifact', () => {
});
it('uses the caller-provided placeholder when a deferred-extraction file has no text', () => {
- // Backend extractor returns null for pptx (deferred). Client sees
- // `text === null` and substitutes the localized placeholder.
+ /* Plain-text and markdown remain on the lenient empty-text gate so the
+ * artifact card can render a "preparing preview…" placeholder while
+ * extraction is in flight. (Office preview buckets — DOCX, SPREADSHEET,
+ * PRESENTATION — use the strict gate instead: their renderers need
+ * server-rendered HTML, so the artifact stays unregistered until the
+ * `text` field arrives. See the strict-gate test in the
+ * `detectArtifactTypeFromFile` suite.) */
const artifact = fileToArtifact(
{
...baseFile,
- filename: 'slides.pptx',
- type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
+ filename: 'notes.txt',
+ type: 'text/plain',
text: null as unknown as string,
},
{ placeholder: '_Coming soon_' },
@@ -521,13 +692,115 @@ describe('fileToArtifact', () => {
it('falls back to empty content when no placeholder is supplied and text is missing', () => {
const artifact = fileToArtifact({
...baseFile,
- filename: 'slides.pptx',
- type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
+ filename: 'notes.txt',
+ type: 'text/plain',
text: undefined,
});
+ expect(artifact).not.toBeNull();
expect(artifact!.content).toBe('');
});
+ it('returns null for office preview buckets without text (strict gate)', () => {
+ /* `textFormat: 'html'` is required for routing to land on the office
+ * bucket in the first place; without it the security gate downgrades
+ * to PLAIN_TEXT (which has the lenient empty-text gate). The strict
+ * empty-text gate then fires and the artifact stays unregistered. */
+ expect(
+ fileToArtifact({
+ ...baseFile,
+ filename: 'slides.pptx',
+ type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
+ text: undefined,
+ textFormat: 'html',
+ }),
+ ).toBeNull();
+ expect(
+ fileToArtifact({
+ ...baseFile,
+ filename: 'report.docx',
+ type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ text: '',
+ textFormat: 'html',
+ }),
+ ).toBeNull();
+ expect(
+ fileToArtifact({
+ ...baseFile,
+ filename: 'data.csv',
+ type: 'text/csv',
+ text: undefined,
+ textFormat: 'html',
+ }),
+ ).toBeNull();
+ });
+
+ it('builds a SPREADSHEET artifact for csv/xlsx with backend-rendered HTML in text', () => {
+ const csv = fileToArtifact({
+ ...baseFile,
+ filename: 'data.csv',
+ type: 'text/csv',
+ text: '<!DOCTYPE html><table><tr><td>1</td></tr></table>',
+ textFormat: 'html',
+ });
+ expect(csv).not.toBeNull();
+ expect(csv!.type).toBe(TOOL_ARTIFACT_TYPES.SPREADSHEET);
+ expect(csv!.title).toBe('data.csv');
+ expect(csv!.content).toContain('<table>');
+
+ const xlsx = fileToArtifact({
+ ...baseFile,
+ filename: 'workbook.xlsx',
+ type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
+ text: '<!DOCTYPE html><body>sheet</body>',
+ textFormat: 'html',
+ });
+ expect(xlsx).not.toBeNull();
+ expect(xlsx!.type).toBe(TOOL_ARTIFACT_TYPES.SPREADSHEET);
+ });
+
+ it('builds a DOCX artifact for .docx with backend-rendered HTML in text', () => {
+ const artifact = fileToArtifact({
+ ...baseFile,
+ filename: 'report.docx',
+ type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ text: '<!DOCTYPE html><body><p>hello</p></body>',
+ textFormat: 'html',
+ });
+ expect(artifact).not.toBeNull();
+ expect(artifact!.type).toBe(TOOL_ARTIFACT_TYPES.DOCX);
+ });
+
+ it('builds a PRESENTATION artifact for .pptx with backend-rendered HTML in text', () => {
+ const artifact = fileToArtifact({
+ ...baseFile,
+ filename: 'deck.pptx',
+ type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
+ text: '<!DOCTYPE html><body><ol><li>Slide 1</li></ol></body>',
+ textFormat: 'html',
+ });
+ expect(artifact).not.toBeNull();
+ expect(artifact!.type).toBe(TOOL_ARTIFACT_TYPES.PRESENTATION);
+ });
+
+ /* Codex P1 SECURITY companion: legacy/RAG path where `textFormat` is
+ * missing — the security gate downgrades to PLAIN_TEXT, which has the
+ * lenient empty-text gate. The text round-trips into the markdown
+ * viewer as escaped content rather than being injected as HTML. */
+ it('downgrades to a PLAIN_TEXT artifact when an office file has text but no textFormat flag', () => {
+ const artifact = fileToArtifact({
+ ...baseFile,
+ filename: 'report.docx',
+ type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ /* This is what mammoth.extractRawText returns for a RAG upload —
+ * not sanitized HTML, just the document's flowed text. The
+ * security gate ensures it's never injected as HTML. */
+ text: 'Document body text from extractRawText. <script>alert(1)</script>',
+ });
+ expect(artifact).not.toBeNull();
+ expect(artifact!.type).toBe(TOOL_ARTIFACT_TYPES.PLAIN_TEXT);
+ expect(artifact!.content).toContain('<script>');
+ });
+
it('preserves an empty string as legitimate content (does not fall through to placeholder)', () => {
// A user can write a 0-byte `.md` or `.txt`; that's a valid artifact
// with empty content, not "extraction unavailable."
@@ -585,3 +858,33 @@ describe('fileToArtifact', () => {
expect(artifact!.id).toBe('tool-artifact-index.html');
});
});
+
+describe('isPreviewOnlyArtifact', () => {
+ /* The Artifacts panel hides the "code" tab and snaps `activeTab` to
+ * 'preview' when the current artifact is preview-only — i.e. the
+ * underlying file is binary and the generated HTML blob isn't a
+ * useful "code" view. Regression for review finding #6 on PR #12934.
+ * Without this test, removing a type from the predicate (or adding a
+ * non-office type) would silently leave users seeing the raw HTML
+ * blob in the code tab. */
+ it.each([
+ [TOOL_ARTIFACT_TYPES.DOCX, true],
+ [TOOL_ARTIFACT_TYPES.SPREADSHEET, true],
+ [TOOL_ARTIFACT_TYPES.PRESENTATION, true],
+ [TOOL_ARTIFACT_TYPES.HTML, false],
+ [TOOL_ARTIFACT_TYPES.REACT, false],
+ [TOOL_ARTIFACT_TYPES.MARKDOWN, false],
+ [TOOL_ARTIFACT_TYPES.MERMAID, false],
+ [TOOL_ARTIFACT_TYPES.CODE, false],
+ [TOOL_ARTIFACT_TYPES.PLAIN_TEXT, false],
+ ])('type %s returns %s', (type, expected) => {
+ expect(isPreviewOnlyArtifact(type)).toBe(expected);
+ });
+
+ it.each([[null], [undefined], [''], ['application/pdf'], ['text/plain'], ['some/random-type']])(
+ 'returns false for non-artifact type %s',
+ (type) => {
+ expect(isPreviewOnlyArtifact(type)).toBe(false);
+ },
+ );
+});
diff --git a/client/src/utils/artifacts.ts b/client/src/utils/artifacts.ts
index 3a17c843b2..c51e83cfa3 100644
--- a/client/src/utils/artifacts.ts
+++ b/client/src/utils/artifacts.ts
@@ -1,5 +1,5 @@
import dedent from 'dedent';
-import { shadcnComponents } from 'librechat-data-provider';
+import { excelMimeTypes, shadcnComponents } from 'librechat-data-provider';
import type {
SandpackProviderProps,
SandpackPredefinedTemplate,
@@ -12,6 +12,13 @@ const artifactFilename = {
'application/vnd.ant.react': 'App.tsx',
'text/html': 'index.html',
'application/vnd.code-html': 'index.html',
+ /* Office preview buckets — the backend produces a complete sanitized
+ * `index.html` document (head + body) and ships it via `attachment.text`.
+ * The Sandpack `static` template loads it as-is. See
+ * `packages/api/src/files/documents/html.ts`. */
+ 'application/vnd.librechat.docx-preview': 'index.html',
+ 'application/vnd.librechat.spreadsheet-preview': 'index.html',
+ 'application/vnd.librechat.presentation-preview': 'index.html',
// mermaid and markdown types are handled separately in useArtifactProps.ts
default: 'index.html',
// 'css': 'css',
@@ -44,6 +51,11 @@ const artifactTemplate: Record<
'text/markdown': 'static',
'text/md': 'static',
'text/plain': 'static',
+ /* Office preview buckets ride the same static pipeline — the backend
+ * already sanitized the HTML, so we just hand it to Sandpack. */
+ 'application/vnd.librechat.docx-preview': 'static',
+ 'application/vnd.librechat.spreadsheet-preview': 'static',
+ 'application/vnd.librechat.presentation-preview': 'static',
default: 'static',
// 'css': 'css',
// 'javascript': 'js',
@@ -137,6 +149,11 @@ const dependenciesMap: Record<
'text/markdown': {},
'text/md': {},
'text/plain': {},
+ /* Office preview HTML is fully self-contained (CSS-only sheet tabs, no
+ * JS), so no Sandpack-side packages are needed. */
+ 'application/vnd.librechat.docx-preview': {},
+ 'application/vnd.librechat.spreadsheet-preview': {},
+ 'application/vnd.librechat.presentation-preview': {},
default: standardDependencies,
};
@@ -265,10 +282,44 @@ export const TOOL_ARTIFACT_TYPES = {
MERMAID: 'application/vnd.mermaid',
PLAIN_TEXT: 'text/plain',
CODE: 'application/vnd.code',
+ /* Office-format rich previews. The backend renders the binary file as a
+ * complete sanitized HTML document and ships it via `attachment.text`;
+ * the client routes these types through the Sandpack `static` template's
+ * `index.html` slot. The values are synthetic LibreChat-internal MIMEs
+ * — they don't appear on disk or in HTTP headers, only on the artifact
+ * object — so they can't collide with the canonical office MIMEs that
+ * the routing maps key off of. */
+ DOCX: 'application/vnd.librechat.docx-preview',
+ SPREADSHEET: 'application/vnd.librechat.spreadsheet-preview',
+ PRESENTATION: 'application/vnd.librechat.presentation-preview',
} as const;
export type ToolArtifactType = (typeof TOOL_ARTIFACT_TYPES)[keyof typeof TOOL_ARTIFACT_TYPES];
+/**
+ * Artifact types whose preview is server-rendered HTML — there's no
+ * source for a "code" view because the underlying file is binary, and
+ * showing the generated HTML blob in a code editor would just be
+ * noise. Used by the artifacts panel to hide the code tab and snap
+ * the active tab to "preview" when one of these is selected.
+ *
+ * Exposed via a predicate (rather than the bare set) so callers can't
+ * accidentally widen the set and so the membership check is unit-
+ * testable without mounting the full Artifacts component.
+ */
+const PREVIEW_ONLY_ARTIFACT_TYPES: ReadonlySet<ToolArtifactType> = new Set([
+ TOOL_ARTIFACT_TYPES.DOCX,
+ TOOL_ARTIFACT_TYPES.SPREADSHEET,
+ TOOL_ARTIFACT_TYPES.PRESENTATION,
+]);
+
+export function isPreviewOnlyArtifact(type: string | null | undefined): boolean {
+ if (type == null) {
+ return false;
+ }
+ return PREVIEW_ONLY_ARTIFACT_TYPES.has(type as ToolArtifactType);
+}
+
/**
* Extension → fenced-code-block language hint for the CODE bucket. The
* key is the lowercased file extension (no dot); the value is the
@@ -501,13 +552,19 @@ const EXTENSION_TO_TOOL_ARTIFACT_TYPE: Record<string, ToolArtifactType> = {
mdx: TOOL_ARTIFACT_TYPES.MARKDOWN,
mmd: TOOL_ARTIFACT_TYPES.MERMAID,
mermaid: TOOL_ARTIFACT_TYPES.MERMAID,
- // Plain text + office documents fall through to the markdown-style
- // viewer until dedicated renderers land. `pptx` is wired up here so
- // the routing fires as soon as backend text extraction is added.
txt: TOOL_ARTIFACT_TYPES.PLAIN_TEXT,
- docx: TOOL_ARTIFACT_TYPES.PLAIN_TEXT,
+ // ODT has no rich HTML producer — it stays on the markdown-text path.
odt: TOOL_ARTIFACT_TYPES.PLAIN_TEXT,
- pptx: TOOL_ARTIFACT_TYPES.PLAIN_TEXT,
+ /* Office formats with rich HTML previews. The backend's
+ * `extractCodeArtifactText` path produces a complete sanitized HTML
+ * document via `bufferToOfficeHtml` and ships it through
+ * `attachment.text`. */
+ docx: TOOL_ARTIFACT_TYPES.DOCX,
+ csv: TOOL_ARTIFACT_TYPES.SPREADSHEET,
+ xlsx: TOOL_ARTIFACT_TYPES.SPREADSHEET,
+ xls: TOOL_ARTIFACT_TYPES.SPREADSHEET,
+ ods: TOOL_ARTIFACT_TYPES.SPREADSHEET,
+ pptx: TOOL_ARTIFACT_TYPES.PRESENTATION,
};
/* Append every entry in `CODE_EXTENSION_TO_LANGUAGE` to the routing map
@@ -563,14 +620,28 @@ const MIME_TO_TOOL_ARTIFACT_TYPE: Record<string, ToolArtifactType> = {
'text/x-lua': TOOL_ARTIFACT_TYPES.CODE,
'text/x-swift': TOOL_ARTIFACT_TYPES.CODE,
'text/css': TOOL_ARTIFACT_TYPES.CODE,
- // Office MIME types fall through to the plain-text bucket here too —
- // matches the extension map so a file whose extension was stripped
- // somewhere upstream still routes to the panel.
+ // Office MIME types — route to the rich HTML preview buckets when the
+ // canonical MIME is present. ODT remains on the plain-text path (no
+ // dedicated HTML producer). These complement the extension map for
+ // extensionless filenames.
'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
- TOOL_ARTIFACT_TYPES.PLAIN_TEXT,
+ TOOL_ARTIFACT_TYPES.DOCX,
'application/vnd.oasis.opendocument.text': TOOL_ARTIFACT_TYPES.PLAIN_TEXT,
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
+ TOOL_ARTIFACT_TYPES.SPREADSHEET,
+ 'application/vnd.ms-excel': TOOL_ARTIFACT_TYPES.SPREADSHEET,
+ 'application/vnd.oasis.opendocument.spreadsheet': TOOL_ARTIFACT_TYPES.SPREADSHEET,
+ 'text/csv': TOOL_ARTIFACT_TYPES.SPREADSHEET,
+ 'application/csv': TOOL_ARTIFACT_TYPES.SPREADSHEET,
+ /* `text/comma-separated-values` is a legacy CSV MIME variant — rare
+ * in modern HTTP traffic but still emitted by some sandboxes. Kept
+ * in lock-step with the backend's `CSV_MIME_PATTERN` in
+ * `packages/api/src/files/documents/html.ts` so an extensionless CSV
+ * with this MIME doesn't slip through the client routing while the
+ * backend has already produced full HTML for it. */
+ 'text/comma-separated-values': TOOL_ARTIFACT_TYPES.SPREADSHEET,
'application/vnd.openxmlformats-officedocument.presentationml.presentation':
- TOOL_ARTIFACT_TYPES.PLAIN_TEXT,
+ TOOL_ARTIFACT_TYPES.PRESENTATION,
// Note: bare `text/plain` is NOT mapped here. The extension map handles
// `.txt` explicitly; routing every unrecognized-extension `text/plain`
// file (extensionless scripts, .env, etc.) through the panel would be a
@@ -591,8 +662,21 @@ const MIME_TO_TOOL_ARTIFACT_TYPE: Record<string, ToolArtifactType> = {
* and Mermaid buckets still require real content because their viewers
* (sandpack / mermaid.js) error on empty input.
*/
+/**
+ * Office preview buckets the backend MUST mark as `textFormat: 'html'`
+ * before the client will inject `attachment.text` as `index.html`.
+ * Routing to these buckets without the trust flag would let plain
+ * text from RAG-uploaded `.docx` etc. (mammoth.extractRawText output)
+ * be rendered as HTML — Codex P1 review on PR #12934.
+ */
+const OFFICE_HTML_BUCKETS: ReadonlySet<ToolArtifactType> = new Set([
+ TOOL_ARTIFACT_TYPES.DOCX,
+ TOOL_ARTIFACT_TYPES.SPREADSHEET,
+ TOOL_ARTIFACT_TYPES.PRESENTATION,
+]);
+
export function detectArtifactTypeFromFile(
- attachment: Partial<Pick<TFile, 'filename' | 'type' | 'text'>>,
+ attachment: Partial<Pick<TFile, 'filename' | 'type' | 'text' | 'textFormat'>>,
): ToolArtifactType | null {
/* Compute the basename once and reuse it across the extension AND
* bare-name lookups. Both `extensionOf(filename)` and
@@ -608,17 +692,60 @@ export function detectArtifactTypeFromFile(
const byBareName = byExtension
? undefined
: EXTENSION_TO_TOOL_ARTIFACT_TYPE[bareNameFromBasename(base)];
- const type =
- byExtension ?? byBareName ?? MIME_TO_TOOL_ARTIFACT_TYPE[baseMime(attachment.type)] ?? null;
+ /* Exact-match MIME lookup first; for the spreadsheet bucket the
+ * backend's `officeHtmlBucket` accepts the broad `excelMimeTypes`
+ * regex (covers `application/x-ms-excel`, `application/x-xls`,
+ * `application/msexcel`, `application/x-dos_ms_excel`, etc.). The
+ * client must accept the same set or extensionless XLS uploads with
+ * legacy MIMEs would have backend HTML produced but never get
+ * routed/registered on the panel. */
+ const normalizedMime = baseMime(attachment.type);
+ const byMime =
+ MIME_TO_TOOL_ARTIFACT_TYPE[normalizedMime] ??
+ (excelMimeTypes.test(normalizedMime) ? TOOL_ARTIFACT_TYPES.SPREADSHEET : undefined);
+ const type = byExtension ?? byBareName ?? byMime ?? null;
if (type == null) {
return null;
}
+ /* SECURITY GATE: office HTML buckets inject `attachment.text` into
+ * the iframe via `index.html`. Plain text from a RAG-extracted
+ * .docx (mammoth output) would become executable markup here.
+ * Require the backend's explicit `textFormat: 'html'` trust signal
+ * before allowing this routing — fall back to PLAIN_TEXT (markdown
+ * viewer, escapes content) for everything else.
+ *
+ * Legacy attachments stored before this field existed have
+ * `textFormat === undefined`; they may have HTML in `text` from the
+ * pre-flag period and the safe default ('text' bucket) is OK for
+ * those — they were rendering correctly before this flag, and the
+ * markdown viewer escaping HTML is a safety upgrade, not a
+ * regression. Codex P1 review on PR #12934.
+ *
+ * Empty-text exception: if the office attachment has NO text at all,
+ * downgrading to PLAIN_TEXT would route an empty-text-tolerant
+ * bucket onto the panel (a half-rendered card showing only the
+ * filename). The historical contract for office types with missing
+ * text is "fall through to the legacy download path"; preserve that
+ * by returning null here. The downgrade only matters when there's
+ * actual text content that needs safe-escaping. */
+ if (OFFICE_HTML_BUCKETS.has(type) && attachment.textFormat !== 'html') {
+ if (!attachment.text) {
+ return null;
+ }
+ return TOOL_ARTIFACT_TYPES.PLAIN_TEXT;
+ }
if (
!attachment.text &&
type !== TOOL_ARTIFACT_TYPES.PLAIN_TEXT &&
type !== TOOL_ARTIFACT_TYPES.MARKDOWN &&
type !== TOOL_ARTIFACT_TYPES.CODE
) {
+ /* HTML, REACT, MERMAID, and the office preview buckets all require
+ * real content — their renderers (sandpack iframes / mermaid.js /
+ * the office HTML pipeline) error or render blank without it. The
+ * artifact stays unregistered until the backend produces text;
+ * `ToolArtifactCard`'s self-heal effect re-fires on drift so the
+ * card transitions cleanly when text arrives. */
return null;
}
return type;
@@ -700,6 +827,12 @@ export function fileToArtifact(
type !== TOOL_ARTIFACT_TYPES.MARKDOWN &&
type !== TOOL_ARTIFACT_TYPES.CODE
) {
+ /* HTML, REACT, MERMAID, and the office preview buckets all require
+ * real content — their renderers (sandpack iframes / mermaid.js /
+ * the office HTML pipeline) error or render blank without it. The
+ * artifact stays unregistered until the backend produces text;
+ * `ToolArtifactCard`'s self-heal effect re-fires on drift so the
+ * card transitions cleanly when text arrives. */
return null;
}
/* For CODE artifacts, resolve the language hint at construction time
diff --git a/package-lock.json b/package-lock.json
index a6963ab7a8..183eff0c19 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -120,6 +120,7 @@
"passport-local": "^1.0.0",
"pdfjs-dist": "^5.4.624",
"rate-limit-redis": "^4.2.0",
+ "sanitize-html": "^2.13.0",
"sharp": "^0.33.5",
"traverse": "^0.6.7",
"ua-parser-js": "^1.0.36",
@@ -131,6 +132,7 @@
"zod": "^3.22.4"
},
"devDependencies": {
+ "@types/sanitize-html": "^2.13.0",
"jest": "^30.2.0",
"mongodb-memory-server": "^11.0.1",
"nodemon": "^3.0.3",
@@ -21178,6 +21180,16 @@
"integrity": "sha512-60BCwRFOZCQhDncwQdxxeOEEkbc5dIMccYLwbxsS4TUNeVECQ/pBJ0j09mrHOl/JJvpRPGwO9SvE4nR2Nb/a4Q==",
"dev": true
},
+ "node_modules/@types/sanitize-html": {
+ "version": "2.16.1",
+ "resolved": "https://registry.npmjs.org/@types/sanitize-html/-/sanitize-html-2.16.1.tgz",
+ "integrity": "sha512-n9wjs8bCOTyN/ynwD8s/nTcTreIHB1vf31vhLMGqUPNHaweKC4/fAl4Dj+hUlCTKYgm4P3k83fmiFfzkZ6sgMA==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "htmlparser2": "^10.1"
+ }
+ },
"node_modules/@types/scheduler": {
"version": "0.16.8",
"resolved": "https://registry.npmjs.org/@types/scheduler/-/scheduler-0.16.8.tgz",
@@ -25380,7 +25392,6 @@
"version": "4.3.1",
"resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.3.1.tgz",
"integrity": "sha512-3sUqbMEc77XqpdNO7FRyRog+eW3ph+GYCbj+rK+uYyRMuwsVy0rMiVtPn+QJlKFvWP/1PYpapqYn0Me2knFn+A==",
- "dev": true,
"engines": {
"node": ">=0.10.0"
}
@@ -26177,7 +26188,6 @@
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz",
"integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==",
- "dev": true,
"engines": {
"node": ">=10"
},
@@ -29585,6 +29595,15 @@
"node": ">=0.10.0"
}
},
+ "node_modules/is-plain-object": {
+ "version": "5.0.0",
+ "resolved": "https://registry.npmjs.org/is-plain-object/-/is-plain-object-5.0.0.tgz",
+ "integrity": "sha512-VRSzKkbMm5jMDoKLbltAkFQ5Qr7VDiTFGXxYFXXowVj387GeGNOCsOH6Msy00SGZ3Fp84b1Naa1psqgcCIEP5Q==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=0.10.0"
+ }
+ },
"node_modules/is-potential-custom-element-name": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/is-potential-custom-element-name/-/is-potential-custom-element-name-1.0.1.tgz",
@@ -35341,6 +35360,12 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
+ "node_modules/parse-srcset": {
+ "version": "1.0.2",
+ "resolved": "https://registry.npmjs.org/parse-srcset/-/parse-srcset-1.0.2.tgz",
+ "integrity": "sha512-/2qh0lav6CmI15FzA3i/2Bzk2zCgQhGMkvhOhKNcBVQ1ldgpbfiNTVslmooUmWJcADi1f1kIeynbDRVzNlfR6Q==",
+ "license": "MIT"
+ },
"node_modules/parse5": {
"version": "7.3.0",
"resolved": "https://registry.npmjs.org/parse5/-/parse5-7.3.0.tgz",
@@ -39963,6 +39988,20 @@
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
},
+ "node_modules/sanitize-html": {
+ "version": "2.17.3",
+ "resolved": "https://registry.npmjs.org/sanitize-html/-/sanitize-html-2.17.3.tgz",
+ "integrity": "sha512-Kn4srCAo2+wZyvCNKCSyB2g8RQ8IkX/gQs2uqoSRNu5t9I2qvUyAVvRDiFUVAiX3N3PNuwStY0eNr+ooBHVWEg==",
+ "license": "MIT",
+ "dependencies": {
+ "deepmerge": "^4.2.2",
+ "escape-string-regexp": "^4.0.0",
+ "htmlparser2": "^10.1.0",
+ "is-plain-object": "^5.0.0",
+ "parse-srcset": "^1.0.2",
+ "postcss": "^8.3.11"
+ }
+ },
"node_modules/sax": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/sax/-/sax-1.5.0.tgz",
@@ -44243,6 +44282,7 @@
"@types/node": "^20.3.0",
"@types/node-fetch": "^2.6.13",
"@types/react": "^18.2.18",
+ "@types/sanitize-html": "^2.13.0",
"@types/winston": "^2.4.4",
"@types/yauzl": "^2.10.3",
"aws-sdk-client-mock": "^4.1.0",
@@ -44256,6 +44296,7 @@
"rimraf": "^6.1.3",
"rollup": "^4.34.9",
"rollup-plugin-peer-deps-external": "^2.2.4",
+ "sanitize-html": "^2.13.0",
"ts-node": "^10.9.2",
"typescript": "^5.0.4",
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz",
@@ -44297,6 +44338,7 @@
"node-fetch": "2.7.0",
"pdfjs-dist": "^5.4.624",
"rate-limit-redis": "^4.2.0",
+ "sanitize-html": "^2.13.0",
"sharp": "^0.33.5",
"undici": "^7.24.1",
"yauzl": "^3.2.1",
diff --git a/packages/api/package.json b/packages/api/package.json
index 788fa791d4..ca3f4fd868 100644
--- a/packages/api/package.json
+++ b/packages/api/package.json
@@ -64,6 +64,7 @@
"@types/node": "^20.3.0",
"@types/node-fetch": "^2.6.13",
"@types/react": "^18.2.18",
+ "@types/sanitize-html": "^2.13.0",
"@types/winston": "^2.4.4",
"@types/yauzl": "^2.10.3",
"aws-sdk-client-mock": "^4.1.0",
@@ -77,6 +78,7 @@
"rimraf": "^6.1.3",
"rollup": "^4.34.9",
"rollup-plugin-peer-deps-external": "^2.2.4",
+ "sanitize-html": "^2.13.0",
"ts-node": "^10.9.2",
"typescript": "^5.0.4",
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz",
@@ -122,6 +124,7 @@
"node-fetch": "2.7.0",
"pdfjs-dist": "^5.4.624",
"rate-limit-redis": "^4.2.0",
+ "sanitize-html": "^2.13.0",
"sharp": "^0.33.5",
"undici": "^7.24.1",
"yauzl": "^3.2.1",
diff --git a/packages/api/src/agents/responses/handlers.ts b/packages/api/src/agents/responses/handlers.ts
index 712be852bd..c63e284f5d 100644
--- a/packages/api/src/agents/responses/handlers.ts
+++ b/packages/api/src/agents/responses/handlers.ts
@@ -823,6 +823,25 @@ export interface AttachmentData {
height?: number;
/** Associated tool call ID */
tool_call_id?: string;
+ /**
+ * Inline text or sanitized HTML preview (sized to MAX_TEXT_CACHE_BYTES).
+ * Populated by `extractCodeArtifactText` for tool-output files: raw text
+ * for plain-text artifacts, sanitized rich HTML for office formats
+ * (DOCX/XLSX/CSV/PPTX). The frontend feeds HTML through the Sandpack
+ * `static` template via `index.html`. Null if extraction was unavailable
+ * (binary, oversized, or unsupported).
+ */
+ text?: string | null;
+ /**
+ * Format of the `text` field — `'html'` if `text` is a complete
+ * sanitized HTML document the client is permitted to inject into the
+ * iframe via `index.html`, `'text'` if it's plain text. Clients MUST
+ * gate office-bucket routing on `textFormat === 'html'`; legacy
+ * attachments and RAG-extracted plain text don't have this set and
+ * must default to safe (markdown-escaped) rendering. Codex P1 review
+ * on PR #12934.
+ */
+ textFormat?: 'html' | 'text' | null;
/** Additional metadata */
[key: string]: unknown;
}
diff --git a/packages/api/src/files/code/extract.spec.ts b/packages/api/src/files/code/extract.spec.ts
index 4614a0cfef..a2847b6428 100644
--- a/packages/api/src/files/code/extract.spec.ts
+++ b/packages/api/src/files/code/extract.spec.ts
@@ -1,21 +1,52 @@
import * as os from 'os';
import * as path from 'path';
-import { extractCodeArtifactText, MAX_TEXT_CACHE_BYTES, MAX_TEXT_EXTRACT_BYTES } from './extract';
+import {
+ extractCodeArtifactText,
+ getExtractedTextFormat,
+ MAX_TEXT_CACHE_BYTES,
+ MAX_TEXT_EXTRACT_BYTES,
+} from './extract';
const docxText = '__DOCX_PARSED__';
+/* parseDocument throws on any originalname containing this token, so
+ * tests can force a failure with whatever extension/MIME they need
+ * (the legacy docx-named constant is preserved for backward-compat
+ * with tests that still reference it). */
const docxFailureName = 'force-docx-failure.docx';
const parseDocumentCalls: Array<{ path: string; originalname: string }> = [];
jest.mock('~/files/documents/crud', () => ({
parseDocument: jest.fn(async ({ file }: { file: { path: string; originalname: string } }) => {
parseDocumentCalls.push({ path: file.path, originalname: file.originalname });
- if (file.originalname === docxFailureName) {
+ if (file.originalname.includes('force-docx-failure')) {
throw new Error('parse failed');
}
return { text: docxText, filename: file.originalname, bytes: docxText.length };
}),
}));
+/* The office HTML producer is mocked here so the existing fallback-path
+ * assertions (parseDocument receives the canonical MIME) keep exercising
+ * `extractDocument`. Tests that need real HTML output drive `bufferToOfficeHtml`
+ * directly via its own spec file (`html.spec.ts`); a separate `office-html`
+ * describe block below exercises the integration with this mock relaxed.
+ *
+ * `officeHtmlBucket` is the gate predicate the upstream uses to decide
+ * whether to call the dispatcher at all. We pass through to the real
+ * implementation so the gate routes the same files in tests as in prod. */
+const mockOfficeHtml = jest.fn(
+ async (_buffer: Buffer, _name: string, _mime: string) => null as string | null,
+);
+jest.mock('~/files/documents/html', () => {
+ const actual =
+ jest.requireActual<typeof import('~/files/documents/html')>('~/files/documents/html');
+ return {
+ bufferToOfficeHtml: (buffer: Buffer, name: string, mime: string) =>
+ mockOfficeHtml(buffer, name, mime),
+ officeHtmlBucket: actual.officeHtmlBucket,
+ };
+});
+
describe('extractCodeArtifactText', () => {
describe('utf8-text', () => {
it('decodes a UTF-8 buffer', async () => {
@@ -70,73 +101,78 @@ describe('extractCodeArtifactText', () => {
});
describe('document', () => {
+ /* These tests exercise the legacy `extractDocument` path, which now
+ * only fires for `category === 'document'` files that the office HTML
+ * dispatcher does NOT claim — i.e. PDF and ODT. All ZIP-backed office
+ * formats (DOCX/XLSX/XLS/ODS) bypass `extractDocument` entirely
+ * because they're HTML-or-null per the SEC fix in PR #12934
+ * (Codex P1 review: text-fallback under `index.html` was XSS). */
beforeEach(() => {
parseDocumentCalls.length = 0;
});
- it('routes through parseDocument and returns its text', async () => {
- const buffer = Buffer.from('PKfake-docx');
+ it('routes through parseDocument and returns its text (ODT)', async () => {
+ const buffer = Buffer.from('PKfake-odt');
const text = await extractCodeArtifactText(
buffer,
- 'report.docx',
- 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ 'notes.odt',
+ 'application/vnd.oasis.opendocument.text',
'document',
);
expect(text).toBe(docxText);
});
it('returns null when parseDocument throws', async () => {
- const buffer = Buffer.from('PKfake-docx');
+ const buffer = Buffer.from('PKfake-odt');
const text = await extractCodeArtifactText(
buffer,
- docxFailureName,
- 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ docxFailureName.replace('.docx', '.odt'),
+ 'application/vnd.oasis.opendocument.text',
'document',
);
expect(text).toBeNull();
});
- it('rewrites a generic sniffed MIME to the canonical document MIME by extension', async () => {
+ it('rewrites a generic sniffed MIME to the canonical document MIME by extension (ODT)', async () => {
// Code-output buffers for office docs are commonly sniffed as
// application/zip — without canonicalization, parseDocument would
// reject these and inline previews would silently disappear.
- const buffer = Buffer.from('PKfake-docx');
- await extractCodeArtifactText(buffer, 'report.docx', 'application/zip', 'document');
- expect(parseDocumentCalls[0]?.originalname).toBe('report.docx');
+ const buffer = Buffer.from('PKfake-odt');
+ await extractCodeArtifactText(buffer, 'notes.odt', 'application/zip', 'document');
+ expect(parseDocumentCalls[0]?.originalname).toBe('notes.odt');
});
it.each([
- [
- 'report.docx',
- 'application/zip',
- 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
- ],
- [
- 'data.xlsx',
- 'application/octet-stream',
- 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
- ],
- ['legacy.xls', 'application/octet-stream', 'application/vnd.ms-excel'],
- ['sheet.ods', 'application/zip', 'application/vnd.oasis.opendocument.spreadsheet'],
['notes.odt', 'application/zip', 'application/vnd.oasis.opendocument.text'],
- ])('passes canonical mimetype for %s when sniff returns %s', async (name, sniffed, _canon) => {
- const parseDocumentMock = (
- jest.requireMock('~/files/documents/crud') as {
- parseDocument: jest.Mock;
- }
- ).parseDocument;
- parseDocumentMock.mockClear();
- await extractCodeArtifactText(Buffer.from('PK'), name, sniffed, 'document');
- const call = parseDocumentMock.mock.calls[0]?.[0];
- expect(call?.file?.mimetype).toBe(_canon);
- });
+ ['report.pdf', 'application/octet-stream', 'application/pdf'],
+ ])(
+ 'passes canonical mimetype for %s when sniff returns %s (legacy parseDocument path)',
+ async (name, sniffed, _canon) => {
+ const parseDocumentMock = (
+ jest.requireMock('~/files/documents/crud') as {
+ parseDocument: jest.Mock;
+ }
+ ).parseDocument;
+ parseDocumentMock.mockClear();
+ await extractCodeArtifactText(Buffer.from('PK'), name, sniffed, 'document');
+ const call = parseDocumentMock.mock.calls[0]?.[0];
+ /* PDF doesn't have an entry in `documentMimeFromExtension` so the
+ * sniffed MIME passes through unchanged. ODT does — gets
+ * canonicalized to ODT_MIME. */
+ expect(call?.file?.mimetype).toBe(_canon === 'application/pdf' ? sniffed : _canon);
+ },
+ );
it('writes the temp file inside os.tmpdir() regardless of artifact name', async () => {
- const buffer = Buffer.from('PKfake-docx');
+ /* Path traversal defense — even a malicious filename like
+ * `../../../etc/passwd.odt` must end up inside os.tmpdir() with a
+ * sanitized basename. Uses ODT since the office HTML path now
+ * short-circuits all ZIP-backed office formats. */
+ const buffer = Buffer.from('PKfake-odt');
await extractCodeArtifactText(
buffer,
- '../../../etc/passwd.docx',
- 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ '../../../etc/passwd.odt',
+ 'application/vnd.oasis.opendocument.text',
'document',
);
const call = parseDocumentCalls[0];
@@ -144,12 +180,30 @@ describe('extractCodeArtifactText', () => {
const tmpRoot = path.resolve(os.tmpdir());
expect(path.resolve(call.path).startsWith(tmpRoot)).toBe(true);
expect(call.path).not.toContain('..');
- expect(call.originalname).toBe('passwd.docx');
+ expect(call.originalname).toBe('passwd.odt');
+ });
+
+ it('does NOT call parseDocument for office HTML types (DOCX/XLSX/XLS/ODS)', async () => {
+ /* Lock in the SEC contract: the four office HTML buckets are
+ * HTML-or-null and never fall back to `extractDocument`. */
+ mockOfficeHtml.mockResolvedValue(null);
+ const cases: Array<[string, string]> = [
+ ['report.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
+ ['data.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
+ ['legacy.xls', 'application/vnd.ms-excel'],
+ ['sheet.ods', 'application/vnd.oasis.opendocument.spreadsheet'],
+ ];
+ for (const [name, mime] of cases) {
+ const text = await extractCodeArtifactText(Buffer.from('PK'), name, mime, 'document');
+ expect(text).toBeNull();
+ }
+ expect(parseDocumentCalls.length).toBe(0);
});
});
describe('skipped categories', () => {
- it('returns null for pptx', async () => {
+ it('returns null for pptx when HTML rendering also fails', async () => {
+ mockOfficeHtml.mockResolvedValueOnce(null);
const text = await extractCodeArtifactText(
Buffer.from('PK'),
'slides.pptx',
@@ -169,4 +223,377 @@ describe('extractCodeArtifactText', () => {
expect(text).toBeNull();
});
});
+
+ describe('office-html', () => {
+ beforeEach(() => {
+ mockOfficeHtml.mockReset();
+ parseDocumentCalls.length = 0;
+ });
+
+ it('returns the HTML rendering for a docx when the producer succeeds', async () => {
+ mockOfficeHtml.mockResolvedValueOnce('<!DOCTYPE html><html><body>docx html</body></html>');
+ const text = await extractCodeArtifactText(
+ Buffer.from('PK'),
+ 'report.docx',
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ 'document',
+ );
+ expect(text).toBe('<!DOCTYPE html><html><body>docx html</body></html>');
+ expect(parseDocumentCalls.length).toBe(0);
+ });
+
+ it('returns the HTML rendering for a pptx when the producer succeeds', async () => {
+ mockOfficeHtml.mockResolvedValueOnce('<!DOCTYPE html><html><body>slides</body></html>');
+ const text = await extractCodeArtifactText(
+ Buffer.from('PK'),
+ 'deck.pptx',
+ 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
+ 'pptx',
+ );
+ expect(text).toContain('slides');
+ });
+
+ it('returns the HTML rendering for csv (overriding utf8-text raw output)', async () => {
+ mockOfficeHtml.mockResolvedValueOnce('<!DOCTYPE html><table><tr><td>a</td></tr></table>');
+ const text = await extractCodeArtifactText(
+ Buffer.from('a,b\n1,2', 'utf-8'),
+ 'data.csv',
+ 'text/csv',
+ 'utf8-text',
+ );
+ expect(text).toContain('<table>');
+ });
+
+ /* SECURITY: office types are HTML-or-null, with NO text fallback.
+ * Codex P1 review on PR #12934 caught that the previous fallback
+ * shipped raw text under an `index.html` slot on the client — a
+ * literal `<script>` in document body would have been rendered as
+ * executable markup. The tests below lock in the safe contract:
+ * failed HTML rendering → null → file becomes download-only. */
+ it('returns null (does not fall back to raw text) when CSV HTML rendering fails', async () => {
+ mockOfficeHtml.mockResolvedValueOnce(null);
+ const text = await extractCodeArtifactText(
+ Buffer.from('a,b\n1,2\n', 'utf-8'),
+ 'data.csv',
+ 'text/csv',
+ 'utf8-text',
+ );
+ expect(text).toBeNull();
+ });
+
+ it('returns null (does not fall back to parseDocument) when DOCX HTML rendering returns null', async () => {
+ mockOfficeHtml.mockResolvedValueOnce(null);
+ const text = await extractCodeArtifactText(
+ Buffer.from('PKfake-docx'),
+ 'report.docx',
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ 'document',
+ );
+ expect(text).toBeNull();
+ /* parseDocument MUST NOT be called — its plain-text output would
+ * be injected into the iframe as HTML. */
+ expect(parseDocumentCalls.length).toBe(0);
+ });
+
+ it('returns null (does not fall back to parseDocument) when DOCX HTML rendering throws', async () => {
+ mockOfficeHtml.mockRejectedValueOnce(new Error('mammoth blew up'));
+ const text = await extractCodeArtifactText(
+ Buffer.from('PKfake-docx'),
+ 'report.docx',
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ 'document',
+ );
+ expect(text).toBeNull();
+ expect(parseDocumentCalls.length).toBe(0);
+ });
+
+ it('XSS regression: failed XLSX HTML render does not ship raw text containing <script>', async () => {
+ /* If an attacker-controlled .xlsx makes mammoth/SheetJS time out
+ * or throw, the previous fallback would call extractDocument and
+ * ship its plain-text output verbatim in `attachment.text`. The
+ * client routes by extension to SPREADSHEET and feeds that text
+ * into `index.html`. A spreadsheet cell containing the literal
+ * string `<script>alert(1)</script>` would then execute inside
+ * the Sandpack iframe. The fix returns null instead, and the
+ * client's empty-text gate keeps the artifact off the panel. */
+ mockOfficeHtml.mockResolvedValueOnce(null);
+ const text = await extractCodeArtifactText(
+ Buffer.from('PKfake-xlsx'),
+ 'attack.xlsx',
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
+ 'document',
+ );
+ expect(text).toBeNull();
+ expect(parseDocumentCalls.length).toBe(0);
+ });
+
+ it('substitutes a "preview too large" banner when HTML exceeds the cache cap', async () => {
+ /* Regression for review finding #2 on PR #12934. The earlier
+ * implementation byte-truncated the producer's HTML at the
+ * UTF-8 boundary, which would land mid-tag and ship malformed
+ * markup like `<table><tr><td>con\n…[truncated]` to the iframe.
+ * The new behavior swaps the entire payload for a small valid
+ * HTML banner — under the cap by construction. */
+ const huge = 'X'.repeat(MAX_TEXT_CACHE_BYTES + 5_000);
+ mockOfficeHtml.mockResolvedValueOnce(huge);
+ const text = await extractCodeArtifactText(
+ Buffer.from('PK'),
+ 'big.docx',
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ 'document',
+ );
+ expect(text).not.toBeNull();
+ // Always under the cap.
+ expect(Buffer.byteLength(text!, 'utf-8')).toBeLessThanOrEqual(MAX_TEXT_CACHE_BYTES);
+ // Valid HTML doc, not byte-truncated markup.
+ expect(text!).toMatch(/^<!DOCTYPE html>/);
+ expect(text!).toContain('</html>');
+ // No truncation marker (which would only appear on the legacy path).
+ expect(text!).not.toContain('…[truncated]');
+ // User-facing banner content.
+ expect(text!).toContain('Preview exceeds the size limit');
+ });
+
+ it('passes through HTML output unchanged when within the cache cap', async () => {
+ const small = '<!DOCTYPE html><html><body><p>hello</p></body></html>';
+ mockOfficeHtml.mockResolvedValueOnce(small);
+ const text = await extractCodeArtifactText(
+ Buffer.from('PK'),
+ 'small.docx',
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ 'document',
+ );
+ expect(text).toBe(small);
+ });
+
+ it('falls back to parseDocument for pdf (HTML producer returns null for unsupported types)', async () => {
+ // Default mock returns null — the producer's own dispatcher would do the
+ // same for PDF since pdf has no HTML rendering. Whether we call it or
+ // skip it is an implementation detail; what matters is that PDF still
+ // routes to parseDocument and yields the docx-mock text.
+ const text = await extractCodeArtifactText(
+ Buffer.from('%PDF-1.4'),
+ 'doc.pdf',
+ 'application/pdf',
+ 'document',
+ );
+ expect(text).toBe(docxText);
+ expect(parseDocumentCalls[0]?.originalname).toBe('doc.pdf');
+ });
+
+ it('does not call the office HTML producer for plain .txt utf8-text', async () => {
+ const text = await extractCodeArtifactText(
+ Buffer.from('hello world\n', 'utf-8'),
+ 'note.txt',
+ 'text/plain',
+ 'utf8-text',
+ );
+ expect(mockOfficeHtml).not.toHaveBeenCalled();
+ expect(text).toBe('hello world\n');
+ });
+
+ it('routes extensionless CSV-by-MIME (utf8-text category) through the office HTML path', async () => {
+ /* Regression for the Codex review on PR #12934. A tool emitting
+ * `data` with `text/csv` classifies as `utf8-text` (csv has no
+ * extension here, MIME is text/* which the classifier treats as
+ * utf8-text). The previous gate skipped the office-render branch
+ * because the extension wasn't in OFFICE_HTML_EXTENSIONS — so the
+ * raw CSV text shipped to the client, which routes by MIME to the
+ * SPREADSHEET bucket and expects a full HTML document. The fix
+ * shares the dispatcher's MIME-aware predicate so the gate fires
+ * here too. */
+ mockOfficeHtml.mockResolvedValueOnce('<!DOCTYPE html><table><tr><td>1</td></tr></table>');
+ const text = await extractCodeArtifactText(
+ Buffer.from('a,b\n1,2', 'utf-8'),
+ 'data',
+ 'text/csv',
+ 'utf8-text',
+ );
+ expect(mockOfficeHtml).toHaveBeenCalledWith(expect.any(Buffer), 'data', 'text/csv');
+ expect(text).toContain('<table>');
+ });
+
+ it.each([
+ ['workbook', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
+ ['workbook', 'application/vnd.ms-excel'],
+ ['workbook', 'application/vnd.oasis.opendocument.spreadsheet'],
+ ['report', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
+ ['deck', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'],
+ ])('routes extensionless office files by MIME alone (%s, %s)', async (name, mime) => {
+ mockOfficeHtml.mockResolvedValueOnce('<!DOCTYPE html><body>x</body></html>');
+ const category = mime.includes('presentation')
+ ? 'pptx'
+ : mime.startsWith('text/')
+ ? 'utf8-text'
+ : 'document';
+ const text = await extractCodeArtifactText(Buffer.from('PK'), name, mime, category);
+ expect(mockOfficeHtml).toHaveBeenCalledWith(expect.any(Buffer), name, mime);
+ expect(text).toContain('<body>');
+ });
+
+ it('returns null (and falls back to download UI) when the producer rejects a zip bomb', async () => {
+ /* Defense-in-depth check for SEC review on PR #12934. When
+ * `bufferToOfficeHtml` throws `ZipBombError` (because a zip-bomb
+ * DOCX/XLSX/PPTX got through the compressed-size gate), the outer
+ * extractor must swallow it and return null — that signals to the
+ * code-output controller to register the file as a regular
+ * download instead of a panel artifact. Crucially, it must NOT
+ * fall back to `extractDocument` text either: the client would
+ * inject that text into `index.html` and a literal `<script>`
+ * tag in the document body would execute (Codex P1 review). */
+ const bombError = Object.assign(new Error('zip bomb suspected'), {
+ name: 'ZipBombError',
+ code: 'ZIP_BOMB',
+ });
+ mockOfficeHtml.mockRejectedValueOnce(bombError);
+ const text = await extractCodeArtifactText(
+ Buffer.from('PK'),
+ 'evil.docx',
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ 'document',
+ );
+ expect(text).toBeNull();
+ expect(parseDocumentCalls.length).toBe(0);
+ });
+
+ /* Regression for Codex P2 review on PR #12934. The classifier returns
+ * 'other' for a small but real set of inputs that the new dispatcher
+ * can still route — extensionless `application/csv`, extensionless
+ * office MIMEs with parameters, etc. The early `category === 'other'`
+ * return must NOT short-circuit before `hasOfficeHtmlPath` is checked,
+ * or those inputs silently lose the rich preview. */
+ it('routes extensionless application/csv through office HTML even when category=other', async () => {
+ mockOfficeHtml.mockResolvedValueOnce('<!DOCTYPE html><table><tr><td>1</td></tr></table>');
+ const text = await extractCodeArtifactText(
+ Buffer.from('a,b\n1,2', 'utf-8'),
+ 'data',
+ 'application/csv',
+ 'other',
+ );
+ expect(mockOfficeHtml).toHaveBeenCalledWith(expect.any(Buffer), 'data', 'application/csv');
+ expect(text).toContain('<table>');
+ });
+
+ it('routes extensionless office MIME with parameters through office HTML even when category=other', async () => {
+ mockOfficeHtml.mockResolvedValueOnce('<!DOCTYPE html><body>x</body></html>');
+ const text = await extractCodeArtifactText(
+ Buffer.from('PK'),
+ 'workbook',
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=binary',
+ 'other',
+ );
+ expect(mockOfficeHtml).toHaveBeenCalled();
+ expect(text).toContain('<body>');
+ });
+
+ it('still returns null for true binary "other" files (no MIME match)', async () => {
+ /* Defense check: the category=other early return is preserved when
+ * `hasOfficeHtmlPath` returns false. A JPEG should not be handed to
+ * the office producer. */
+ const text = await extractCodeArtifactText(
+ Buffer.from([0xff, 0xd8, 0xff]),
+ 'photo.jpg',
+ 'image/jpeg',
+ 'other',
+ );
+ expect(mockOfficeHtml).not.toHaveBeenCalled();
+ expect(text).toBeNull();
+ });
+ });
+});
+
+/* `getExtractedTextFormat` is the trust-flag classifier consumed by
+ * `processCodeOutput` (api/server/services/Files/Code/process.js) to
+ * persist `textFormat` on the file record. The client's security gate
+ * in `detectArtifactTypeFromFile` reads that flag to decide whether
+ * routing an office attachment to the HTML preview bucket is safe.
+ * These tests pin the contract: HTML for office paths, 'text' for
+ * everything else, null for missing input. Codex P1 review on PR #12934. */
+describe('getExtractedTextFormat', () => {
+ it.each([
+ ['report.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
+ ['data.csv', 'text/csv'],
+ ['workbook.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
+ ['legacy.xls', 'application/vnd.ms-excel'],
+ ['sheet.ods', 'application/vnd.oasis.opendocument.spreadsheet'],
+ ['slides.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'],
+ ])('returns "html" for office path %s (%s)', (name, mime) => {
+ expect(getExtractedTextFormat(name, mime, '<table>...</table>')).toBe('html');
+ });
+
+ it.each([
+ ['noext', 'application/x-ms-excel', '<table></table>'],
+ ['noext', 'application/x-msexcel', '<table></table>'],
+ ['noext', 'application/msexcel', '<table></table>'],
+ ['noext', 'application/x-excel', '<table></table>'],
+ ['noext', 'application/x-dos_ms_excel', '<table></table>'],
+ ['noext', 'application/xls', '<table></table>'],
+ ['noext', 'application/x-xls', '<table></table>'],
+ ])('returns "html" for legacy XLS MIME alias %s (%s)', (name, mime, text) => {
+ /* Mirrors the client's `excelMimeTypes` regex acceptance — keeping
+ * the trust flag aligned across boundaries means no extensionless
+ * XLS upload with a legacy MIME ever lands on the HTML bucket
+ * without `textFormat: 'html'`. */
+ expect(getExtractedTextFormat(name, mime, text)).toBe('html');
+ });
+
+ it('returns "text" for plain UTF-8 outputs (notes, source code, JSON)', () => {
+ expect(getExtractedTextFormat('note.txt', 'text/plain', 'hello world')).toBe('text');
+ expect(getExtractedTextFormat('script.py', 'text/x-python', 'print(1)')).toBe('text');
+ expect(getExtractedTextFormat('data.json', 'application/json', '{"a":1}')).toBe('text');
+ });
+
+ it('returns "text" for parseDocument paths that are NOT office HTML buckets', () => {
+ /* PDF/ODT/HTML go through `parseDocument` and produce plain text;
+ * mark them as such so the client never injects them as HTML. */
+ expect(getExtractedTextFormat('doc.pdf', 'application/pdf', 'extracted text')).toBe('text');
+ expect(
+ getExtractedTextFormat('notes.odt', 'application/vnd.oasis.opendocument.text', 'odt text'),
+ ).toBe('text');
+ expect(getExtractedTextFormat('page.html', 'text/html', '<p>raw</p>')).toBe('text');
+ });
+
+ it('returns null when the extractor produced nothing', () => {
+ /* `text == null` (extraction skipped, parser failed, binary unsupported)
+ * → no `textFormat` to persist. The downstream caller short-circuits
+ * the field so the DB doesn't carry a half-truth. */
+ expect(
+ getExtractedTextFormat(
+ 'report.docx',
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ null,
+ ),
+ ).toBeNull();
+ expect(getExtractedTextFormat('photo.jpg', 'image/jpeg', null)).toBeNull();
+ });
+
+ it('returns "html" even when text is empty (the path is what counts, not content length)', () => {
+ /* An office producer that returns "" is still an office path — the
+ * trust flag follows the dispatch decision, not the byte count. The
+ * client's empty-text gate keeps the artifact off the panel anyway. */
+ expect(
+ getExtractedTextFormat(
+ 'report.docx',
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ '',
+ ),
+ ).toBe('html');
+ });
+
+ it('classifies by MIME alone when the filename has no recognized extension', () => {
+ /* extensionless office files (e.g. tool-emitted blobs with a generic
+ * name) still get the right trust flag if the MIME is canonical. */
+ expect(
+ getExtractedTextFormat(
+ 'noext',
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
+ '<table></table>',
+ ),
+ ).toBe('html');
+ });
+
+ it('returns "text" when neither extension nor MIME marks the file as office', () => {
+ expect(getExtractedTextFormat('noext', 'application/octet-stream', 'whatever')).toBe('text');
+ expect(getExtractedTextFormat('', '', 'whatever')).toBe('text');
+ });
});
diff --git a/packages/api/src/files/code/extract.ts b/packages/api/src/files/code/extract.ts
index d0a1ff58ba..d61337df20 100644
--- a/packages/api/src/files/code/extract.ts
+++ b/packages/api/src/files/code/extract.ts
@@ -5,15 +5,58 @@ import { randomUUID } from 'crypto';
import { logger } from '@librechat/data-schemas';
import type { CodeArtifactCategory } from './classify';
import { parseDocument } from '~/files/documents/crud';
+import { bufferToOfficeHtml, officeHtmlBucket } from '~/files/documents/html';
import { isBinaryBuffer } from '~/skills/binary';
import { withTimeout } from '~/utils/promise';
export const MAX_TEXT_CACHE_BYTES = 512 * 1024;
export const MAX_TEXT_EXTRACT_BYTES = 1024 * 1024;
const DOCUMENT_PARSE_TIMEOUT_MS = 8_000;
+const OFFICE_HTML_TIMEOUT_MS = 12_000;
const TRUNCATION_MARKER = '\n\n…[truncated]';
const TRUNCATION_MARKER_BYTES = Buffer.byteLength(TRUNCATION_MARKER, 'utf-8');
+/**
+ * Decide whether a buffer is a candidate for rich HTML preview. Wraps the
+ * shared `officeHtmlBucket` predicate from `~/files/documents/html` so the
+ * gate here stays in lock-step with what the dispatcher will actually
+ * route — including extensionless office files identified solely by MIME
+ * (e.g. a tool emitting `data` with `text/csv`, which would otherwise
+ * classify as `utf8-text`, skip the office gate, and ship raw CSV text
+ * to the client's SPREADSHEET bucket that expects full HTML).
+ */
+const hasOfficeHtmlPath = (name: string, mimeType: string): boolean =>
+ officeHtmlBucket(name, mimeType) !== null;
+
+/**
+ * Classify the format of a string returned by `extractCodeArtifactText`
+ * so callers can persist it alongside the text and the client can gate
+ * "inject into iframe as HTML" on a trusted signal. Returns:
+ * - `'html'` when the file went down the office-HTML producer path
+ * (output is a complete sanitized HTML document)
+ * - `'text'` for everything else (utf8 plain text, parseDocument
+ * output for PDF/ODT, etc.)
+ * - `null` when there's no text to format (caller should also skip
+ * setting `textFormat` on the record)
+ *
+ * Inference is by extension/MIME via `officeHtmlBucket`, mirroring the
+ * dispatch logic inside `extractCodeArtifactText` exactly. Keeping the
+ * inference at the caller (rather than baking format into the
+ * extractor's return type) avoids breaking the existing function
+ * signature and its 30+ tests, while still giving downstream consumers
+ * a definitive trust signal.
+ */
+export function getExtractedTextFormat(
+ name: string,
+ mimeType: string,
+ text: string | null,
+): 'html' | 'text' | null {
+ if (text == null) {
+ return null;
+ }
+ return hasOfficeHtmlPath(name, mimeType) ? 'html' : 'text';
+}
+
/**
* Truncate UTF-8 content to fit within MAX_TEXT_CACHE_BYTES. Walks back to a
* code-point boundary so the cut never lands inside a multi-byte sequence
@@ -101,13 +144,77 @@ const extractDocument = async (
};
/**
- * Extract a UTF-8 text representation of a code-execution artifact for inline
+ * Minimal valid HTML document substituted when a producer's output
+ * exceeds `MAX_TEXT_CACHE_BYTES`. Byte-truncating the producer's HTML
+ * would land mid-tag (e.g. `<table><tr><td>con\n…[truncated]`) and the
+ * Sandpack iframe would render the malformed markup unpredictably —
+ * see review finding #2 on PR #12934. The banner stays under the cap by
+ * construction.
+ */
+const OVERSIZED_HTML_BANNER = `<!DOCTYPE html>
+<html lang="en"><head><meta charset="UTF-8"><title>Preview</title>
+<style>body{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;color:#6b7280;padding:16px;font-size:14px;line-height:1.5}@media(prefers-color-scheme:dark){body{color:#9ca3af;background:#1a1a2e}}</style>
+</head><body>Preview exceeds the size limit. Download the file to view the full contents.</body></html>`;
+
+/**
+ * Render an office-format buffer as a sanitized HTML preview document. Used
+ * for docx, xlsx/xls/ods, csv, and pptx — produces interactive HTML the
+ * frontend feeds into the Sandpack `static` template via `index.html`.
+ *
+ * **HTML-or-null contract** (security-critical, do NOT relax). Returns
+ * `null` if the file isn't an office type, rendering timed out, or the
+ * parser threw. Callers MUST NOT fall back to plain-text extraction on
+ * null — the client routes office types into `index.html` and would
+ * inject any text-shaped fallback as executable HTML, creating an XSS
+ * vector. The pre-fix versions of this function permitted text fallback
+ * and that path was a real vulnerability; see Codex P1 review on
+ * PR #12934 (commit b06f08a) for the original bug and remediation.
+ *
+ * On oversized output (>`MAX_TEXT_CACHE_BYTES`), returns a small
+ * "preview too large" banner document instead of byte-truncating the
+ * producer's HTML — slicing mid-tag would ship malformed markup to the
+ * iframe.
+ */
+const renderOfficeHtml = async (
+ buffer: Buffer,
+ name: string,
+ mimeType: string,
+): Promise<string | null> => {
+ try {
+ const html = await withTimeout(
+ bufferToOfficeHtml(buffer, name, mimeType),
+ OFFICE_HTML_TIMEOUT_MS,
+ `bufferToOfficeHtml exceeded ${OFFICE_HTML_TIMEOUT_MS}ms`,
+ );
+ if (html == null) {
+ return null;
+ }
+ if (Buffer.byteLength(html, 'utf-8') > MAX_TEXT_CACHE_BYTES) {
+ return OVERSIZED_HTML_BANNER;
+ }
+ return html;
+ } catch (error) {
+ logger.debug(
+ `[renderOfficeHtml] Failed to render "${name}" (${mimeType}): ${(error as Error).message}`,
+ );
+ return null;
+ }
+};
+
+/**
+ * Extract a string representation of a code-execution artifact for inline
* rendering. Returns `null` for binary, oversized, or unsupported files; the
* caller should fall back to the standard download UI in that case.
*
+ * Office types (docx, xlsx/xls/ods, csv, pptx) are rendered as sanitized
+ * HTML by the producers in `~/files/documents/html`. The frontend feeds the
+ * HTML into the Sandpack `static` template via `index.html`. CSV is special-
+ * cased here — its category is `utf8-text` (raw CSV is text), but we want
+ * the styled-table preview when the file extension says CSV.
+ *
+ * - office (docx/xlsx/xls/ods/csv/pptx): sanitized HTML preview
* - utf8-text: decodes the buffer (with a binary safety net)
- * - document: dispatches to the existing PDF/DOCX/XLSX/ODT parser
- * - pptx: not yet supported in this PR — returns null (follow-up work)
+ * - document: dispatches to the existing PDF/ODT parser
* - other: returns null (binary file, no inline preview)
*/
export async function extractCodeArtifactText(
@@ -116,17 +223,51 @@ export async function extractCodeArtifactText(
mimeType: string,
category: CodeArtifactCategory,
): Promise<string | null> {
- if (category === 'other' || category === 'pptx') {
- return null;
- }
if (buffer.length > MAX_TEXT_EXTRACT_BYTES) {
return null;
}
try {
+ /* Office HTML preview is independent of `category` — `officeHtmlBucket`
+ * routes by extension OR (parameter-normalized) MIME, and may route
+ * inputs the legacy classifier labels as 'other'. Concrete cases:
+ * - extensionless `application/csv` (CSV MIMEs aren't in the
+ * classifier's text-MIME set and don't start with `text/`)
+ * - extensionless office MIMEs with parameters like
+ * `application/vnd...spreadsheetml.sheet; charset=binary`
+ * Without checking `hasOfficeHtmlPath` BEFORE the `category === 'other'`
+ * early return, those inputs would silently fall back to download-only
+ * even though the new dispatcher and the client both expect HTML.
+ *
+ * For office types it is **HTML-or-null** with no text fallback.
+ * The client routes these by extension/MIME to the office preview
+ * buckets and feeds `attachment.text` straight into the Sandpack
+ * iframe's `index.html`. Substituting plain text on producer failure
+ * (timeout, malformed file, zip-bomb rejection) would render literal
+ * `<script>` from a DOCX/XLSX/CSV body as executable markup — a
+ * direct XSS vector. Returning null here lets the client's empty-
+ * text gate keep the artifact off the panel and fall back to the
+ * regular download UI, matching what PPTX already does. */
+ if (hasOfficeHtmlPath(name, mimeType)) {
+ const html = await renderOfficeHtml(buffer, name, mimeType);
+ return html;
+ }
+ if (category === 'other') {
+ return null;
+ }
if (category === 'utf8-text') {
return extractUtf8(buffer);
}
- return await extractDocument(buffer, name, mimeType);
+ if (category === 'document') {
+ /* Reaches here only for non-office documents (PDF, ODT) — neither
+ * is routed to an HTML preview bucket on the client (PDF has no
+ * client routing, ODT routes to PLAIN_TEXT which renders through
+ * the markdown viewer with proper escaping). Plain text is safe. */
+ return await extractDocument(buffer, name, mimeType);
+ }
+ /* category === 'pptx' that didn't go through the office HTML path
+ * (shouldn't happen — pptx ext is in OFFICE_HTML_EXTENSIONS — but
+ * defended in depth). */
+ return null;
} catch (error) {
logger.debug(
`[extractCodeArtifactText] Failed to extract "${name}" (${mimeType}): ${(error as Error).message}`,
diff --git a/packages/api/src/files/documents/crud.ts b/packages/api/src/files/documents/crud.ts
index 20d547cf26..4457a30b3d 100644
--- a/packages/api/src/files/documents/crud.ts
+++ b/packages/api/src/files/documents/crud.ts
@@ -3,6 +3,7 @@ import yauzl from 'yauzl';
import { megabyte, excelMimeTypes, FileSources } from 'librechat-data-provider';
import type { TextItem } from 'pdfjs-dist/types/src/display/api';
import type { MistralOCRUploadResult } from '~/types';
+import { assertSafeZipSize } from './zipSafety';
type FileParseFn = (file: Express.Multer.File) => Promise<string>;
@@ -93,8 +94,14 @@ async function pdfToText(file: Express.Multer.File): Promise<string> {
/** Parses Word document, returns text inside. */
async function wordDocToText(file: Express.Multer.File): Promise<string> {
+ const buffer = await fs.promises.readFile(file.path);
+ /* Reject zip-bomb DOCX before mammoth's internal extractor runs.
+ * mammoth has no decompressed-size cap of its own; without this, a
+ * sub-1MB compressed bomb (~200x ratio) would block the event loop
+ * and spike RSS to ~1GB. See SEC review on PR #12934. */
+ await assertSafeZipSize(buffer, { name: file.originalname ?? 'docx' });
const { extractRawText } = await import('mammoth');
- const rawText = await extractRawText({ buffer: await fs.promises.readFile(file.path) });
+ const rawText = await extractRawText({ buffer });
return rawText.value;
}
@@ -104,6 +111,12 @@ async function excelSheetToText(file: Express.Multer.File): Promise<string> {
// readFile() fails with "Cannot access file". read() takes a pre-loaded Buffer instead.
const { read, utils } = await import('xlsx');
const data = await fs.promises.readFile(file.path);
+ /* Reject zip-bomb XLSX/ODS before SheetJS's internal extractor runs.
+ * `.xls` (BIFF/CFB) is not a ZIP — magic-byte check skips the
+ * validator for it (yauzl would reject it as malformed anyway). */
+ if (data.length >= 4 && data[0] === 0x50 && data[1] === 0x4b) {
+ await assertSafeZipSize(data, { name: file.originalname ?? 'spreadsheet' });
+ }
const workbook = read(data, { type: 'buffer' });
let text = '';
diff --git a/packages/api/src/files/documents/html.spec.ts b/packages/api/src/files/documents/html.spec.ts
new file mode 100644
index 0000000000..6725b577be
--- /dev/null
+++ b/packages/api/src/files/documents/html.spec.ts
@@ -0,0 +1,975 @@
+import path from 'path';
+import * as fs from 'fs';
+import JSZip from 'jszip';
+import { megabyte } from 'librechat-data-provider';
+import {
+ _internal,
+ bufferToOfficeHtml,
+ csvToHtml,
+ excelSheetToHtml,
+ officeHtmlBucket,
+ pptxToHtml,
+ pptxToSlideListHtml,
+ sanitizeOfficeHtml,
+ wordDocToHtml,
+} from './html';
+import { ZipBombError } from './zipSafety';
+
+const fixturesDir = __dirname;
+const readFixture = (name: string): Buffer => fs.readFileSync(path.join(fixturesDir, name));
+
+describe('Office HTML producers', () => {
+ describe('wordDocToHtml', () => {
+ /* The dispatcher chooses CDN vs mammoth by buffer size. The fixture
+ * sample.docx is small (~6 KB) so it goes down the CDN path. Tests
+ * that need the mammoth output specifically call
+ * `_internal.wordDocToHtmlViaMammoth` directly. */
+
+ test('routes a small docx (≤ cap) through the CDN-rendered path', async () => {
+ const html = await wordDocToHtml(readFixture('sample.docx'));
+ expect(html).toMatch(/^<!DOCTYPE html>/);
+ // CDN wrapper signatures.
+ expect(html).toContain('id="lc-doc-data"');
+ expect(html).toContain('docx.renderAsync');
+ expect(html).toContain('cdn.jsdelivr.net/npm/docx-preview@');
+ /* The CDN doc now ALSO embeds the mammoth-rendered fallback in a
+ * hidden `#lc-fallback` block — Codex P2 review on PR #12934. The
+ * iframe bootstrap reveals it whenever `docx-preview` can't load
+ * (corporate firewall, offline) so air-gapped operators see a
+ * readable preview instead of "Preview unavailable". The
+ * fallback's `<article class="lc-docx">` wrapper is the
+ * server-rendered mammoth output, sanitized through the same
+ * pipeline as the standalone mammoth path. */
+ expect(html).toContain('id="lc-fallback"');
+ expect(html).toContain('<article class="lc-docx">');
+ });
+
+ test('routes a docx above the size cap through the mammoth fallback', async () => {
+ /* Synthesize an oversized buffer by reading the fixture and
+ * verifying the dispatcher picks the mammoth path. We can't
+ * cheaply forge a multi-hundred-KB DOCX that mammoth can still
+ * parse, so this asserts the size predicate by calling
+ * `wordDocToHtmlViaMammoth` directly — the dispatcher's `if`
+ * branch is itself trivial to inspect. */
+ const html = await _internal.wordDocToHtmlViaMammoth(readFixture('sample.docx'));
+ expect(html).toContain('<article class="lc-docx">');
+ expect(html).toContain('This is a sample DOCX file.');
+ });
+
+ test('mammoth fallback strips <script> tags and event handlers', async () => {
+ const html = await _internal.wordDocToHtmlViaMammoth(readFixture('sample.docx'));
+ expect(html).not.toMatch(/<script\b/i);
+ expect(html).not.toMatch(/onerror=/i);
+ });
+
+ test('mammoth fallback emits the docx-specific extra CSS', async () => {
+ const html = await _internal.wordDocToHtmlViaMammoth(readFixture('sample.docx'));
+ expect(html).toContain('.lc-docx table tr:first-child td');
+ expect(html).toContain('.lc-docx p:has(> strong:only-child)');
+ expect(html).toContain('.lc-docx h2');
+ });
+
+ describe('CDN-rendered path', () => {
+ /* These tests lock in the security-relevant structure of the
+ * embedded-binary HTML wrapper. If any of these assertions break
+ * after a refactor, the iframe is one config tweak away from
+ * being a vehicle for outbound exfiltration or supply-chain
+ * compromise. */
+
+ /* `wordDocToHtmlViaCdn` now takes a pre-rendered mammoth body
+ * string as a second argument (Codex P2 review on PR #12934 —
+ * the body is embedded inside `#lc-fallback` for air-gapped
+ * deployments). Tests use a placeholder body to assert pure
+ * wrapper-structure behavior; the dispatcher-level test above
+ * exercises the real mammoth body from the sample fixture. */
+ const FAKE_FALLBACK_BODY = '<p>fallback-body</p>';
+
+ test('embeds the binary as base64 that round-trips to the original bytes', async () => {
+ const original = readFixture('sample.docx');
+ const html = await _internal.wordDocToHtmlViaCdn(original, FAKE_FALLBACK_BODY);
+ const match = html.match(
+ /<script id="lc-doc-data" type="application\/octet-stream;base64">([^<]*)<\/script>/,
+ );
+ expect(match).not.toBeNull();
+ const decoded = Buffer.from(match![1], 'base64');
+ expect(decoded.equals(original)).toBe(true);
+ });
+
+ test('pins both CDN scripts to specific versions with SRI integrity', async () => {
+ const html = await _internal.wordDocToHtmlViaCdn(
+ readFixture('sample.docx'),
+ FAKE_FALLBACK_BODY,
+ );
+ // Both deps loaded from jsdelivr at pinned versions.
+ expect(html).toContain('https://cdn.jsdelivr.net/npm/[email protected]/');
+ expect(html).toContain('https://cdn.jsdelivr.net/npm/[email protected]/');
+ // Both have SRI integrity attributes.
+ const integrityMatches = html.match(/integrity="sha384-[A-Za-z0-9+/=]+"/g);
+ expect(integrityMatches).not.toBeNull();
+ expect(integrityMatches!.length).toBe(2);
+ // Both have crossorigin="anonymous" (required for SRI on cross-origin).
+ const crossoriginMatches = html.match(/crossorigin="anonymous"/g);
+ expect(crossoriginMatches).not.toBeNull();
+ expect(crossoriginMatches!.length).toBe(2);
+ });
+
+ test('CSP locks the iframe down: no outbound connect, no eval, scripts only from jsdelivr', async () => {
+ const html = await _internal.wordDocToHtmlViaCdn(
+ readFixture('sample.docx'),
+ FAKE_FALLBACK_BODY,
+ );
+ const cspMatch = html.match(
+ /<meta http-equiv="Content-Security-Policy" content="([^"]+)">/,
+ );
+ expect(cspMatch).not.toBeNull();
+ const csp = cspMatch![1];
+ /* `connect-src` is restricted to self + jsdelivr (where the
+ * renderer script came from). Allowing these is necessary for
+ * DevTools sourcemap fetches; broader exfiltration paths
+ * (arbitrary HTTPS, websockets, etc.) stay blocked. Manual
+ * e2e on PR #12934 — strict `'none'` filled DevTools console
+ * with `.min.js.map` violations every time the iframe was
+ * inspected. */
+ expect(csp).toMatch(/connect-src 'self' https:\/\/cdn\.jsdelivr\.net/);
+ expect(csp).not.toMatch(/connect-src[^;]*\*/); // no wildcard
+ // No `<base>` tampering, no form submission either.
+ expect(csp).toMatch(/base-uri 'none'/);
+ expect(csp).toMatch(/form-action 'none'/);
+ // Scripts only from jsdelivr (plus inline for our renderer
+ // bootstrap). No 'unsafe-eval' anywhere.
+ expect(csp).toMatch(/script-src https:\/\/cdn\.jsdelivr\.net 'unsafe-inline'/);
+ expect(csp).not.toMatch(/unsafe-eval/);
+ });
+
+ test('embeds the mammoth-rendered fallback body in #lc-fallback (air-gapped deployments)', async () => {
+ const html = await _internal.wordDocToHtmlViaCdn(
+ readFixture('sample.docx'),
+ FAKE_FALLBACK_BODY,
+ );
+ /* Visible loading state. */
+ expect(html).toContain('Loading preview…');
+ /* The fallback body now contains the server-rendered mammoth
+ * output (the placeholder body in this test). When the iframe
+ * detects `docx-preview` failed to load, `showFallback`
+ * un-hides this block — Codex P2 review on PR #12934. The old
+ * static "Preview unavailable" text is gone in favor of a
+ * notice + the actual document content. */
+ expect(html).toContain('id="lc-fallback"');
+ expect(html).toContain(FAKE_FALLBACK_BODY);
+ expect(html).toContain('High-fidelity renderer unavailable');
+ /* The bootstrap script checks `typeof docx === 'undefined'`
+ * so a CDN outage degrades to the fallback rather than an
+ * empty iframe. */
+ expect(html).toContain("typeof docx === 'undefined'");
+ /* And it hides the empty render slot when fallback shows so
+ * the mammoth content owns the viewport. */
+ expect(html).toContain("document.getElementById('lc-render')");
+ expect(html).toContain('render.hidden = true');
+ });
+
+ test('size-fallback threshold is the documented 350 KB', async () => {
+ /* Lock the public threshold so a future refactor doesn't drift
+ * away from the value referenced in the JSDoc and the
+ * `MAX_TEXT_CACHE_BYTES` reasoning above it. */
+ expect(_internal.MAX_DOCX_CDN_BINARY_BYTES).toBe(350 * 1024);
+ });
+
+ test('output cap mirrors `MAX_TEXT_CACHE_BYTES` from extract.ts', async () => {
+ /* Pin the cycle-avoidance constant. If the upstream
+ * `MAX_TEXT_CACHE_BYTES` ever changes (e.g. lifting the cap
+ * for office types specifically), update both at the same
+ * time or the dispatcher's size-budget path will misfire. */
+ expect(_internal.OFFICE_HTML_OUTPUT_CAP).toBe(512 * 1024);
+ });
+
+ test('output stays within the cache cap for the standard fixture', async () => {
+ /* The fixture isn't large enough to hit the size-budget
+ * fallback, but the resulting HTML *must* fit under the cap so
+ * `attachment.text` doesn't get truncated mid-document.
+ * Pinning this on the standard fixture catches regressions
+ * where wrapper boilerplate or DOCX_EXTRA_CSS grows past the
+ * 512 KB ceiling. Codex P2 review on PR #12934. */
+ const html = await wordDocToHtml(readFixture('sample.docx'));
+ expect(Buffer.byteLength(html, 'utf-8')).toBeLessThanOrEqual(
+ _internal.OFFICE_HTML_OUTPUT_CAP,
+ );
+ });
+
+ test('locks color-scheme to light so OS dark-mode does not fade body text', async () => {
+ /* docx-preview emits page-style rendering (white pages with
+ * the doc's native colors). When the iframe declares both
+ * light and dark color-schemes AND the OS is in dark mode,
+ * browsers inherit dark-mode colors for unset text properties
+ * — docx-preview's body text (no explicit color) inherited
+ * our `--fg: #e5e7eb` on the white page bg, rendering as
+ * barely-visible light grey. Manual e2e regression on PR
+ * #12934. The CDN doc must declare ONLY light scheme; the
+ * mammoth-only fallback (`wrapAsDocument`) is unaffected
+ * because it owns its own bg + text colors. */
+ const html = await _internal.wordDocToHtmlViaCdn(
+ readFixture('sample.docx'),
+ '<p>fallback</p>',
+ );
+ expect(html).toMatch(/color-scheme:\s*light\b/);
+ expect(html).not.toMatch(/color-scheme:\s*light\s+dark/);
+ expect(html).not.toMatch(/prefers-color-scheme:\s*dark/);
+ });
+ });
+
+ describe('OFFICE_PREVIEW_DISABLE_CDN escape hatch', () => {
+ /* Air-gapped / corporate-filtered networks where jsdelivr is
+ * unreachable need a way to force the mammoth path so DOCX
+ * previews don't degrade to "Preview unavailable" on every open.
+ * Codex P2 review on PR #12934. */
+ const ORIGINAL = process.env.OFFICE_PREVIEW_DISABLE_CDN;
+ afterEach(() => {
+ if (ORIGINAL === undefined) {
+ delete process.env.OFFICE_PREVIEW_DISABLE_CDN;
+ } else {
+ process.env.OFFICE_PREVIEW_DISABLE_CDN = ORIGINAL;
+ }
+ });
+
+ test('default behavior (env unset): small docx → CDN path', async () => {
+ delete process.env.OFFICE_PREVIEW_DISABLE_CDN;
+ const html = await wordDocToHtml(readFixture('sample.docx'));
+ expect(html).toContain('id="lc-doc-data"');
+ });
+
+ it.each([['true'], ['1'], ['yes'], ['TRUE'], ['Yes'], [' true ']])(
+ 'env=%s forces the mammoth fallback even for small files',
+ async (value) => {
+ process.env.OFFICE_PREVIEW_DISABLE_CDN = value;
+ const html = await wordDocToHtml(readFixture('sample.docx'));
+ // Mammoth-path signature, NOT CDN path.
+ expect(html).toContain('<article class="lc-docx">');
+ expect(html).not.toContain('id="lc-doc-data"');
+ expect(html).not.toContain('cdn.jsdelivr.net');
+ },
+ );
+
+ it.each([['false'], ['0'], ['no'], [''], ['anything-else']])(
+ 'env=%j does not disable the CDN path',
+ async (value) => {
+ process.env.OFFICE_PREVIEW_DISABLE_CDN = value;
+ const html = await wordDocToHtml(readFixture('sample.docx'));
+ expect(html).toContain('id="lc-doc-data"');
+ },
+ );
+ });
+ });
+
+ describe('excelSheetToHtml', () => {
+ test('renders all sheets of a multi-sheet workbook into the HTML document', async () => {
+ const html = await excelSheetToHtml(readFixture('sample.xlsx'));
+ expect(html).toMatch(/^<!DOCTYPE html>/);
+ // Each sheet name should appear as a tab label.
+ expect(html).toContain('Sheet One');
+ expect(html).toContain('Second Sheet');
+ // Cell values from both sheets should render.
+ expect(html).toContain('first');
+ expect(html).toContain('Second');
+ // Tab strip uses pure-CSS radio inputs — verify the chrome wired up.
+ expect(html).toContain('lc-sheet-tab-radio');
+ expect(html).toContain('lc-sheet-panel-0');
+ expect(html).toContain('lc-sheet-panel-1');
+ });
+
+ test('omits the tab strip when only one sheet is present', async () => {
+ const html = await excelSheetToHtml(readFixture('sample.xls'));
+ // Cell content should render even though there's no tab strip.
+ expect(html).toContain('first');
+ expect(html).toContain('<table');
+ // Single sheet — no <nav class="lc-sheet-tabs">.
+ expect(html).not.toContain('class="lc-sheet-tabs"');
+ });
+
+ test('renders ods workbooks the same way', async () => {
+ const html = await excelSheetToHtml(readFixture('sample.ods'));
+ expect(html).toContain('Sheet One');
+ expect(html).toContain('Second Sheet');
+ });
+ });
+
+ describe('csvToHtml', () => {
+ test('renders a basic CSV as a single-table HTML document with no tab strip', async () => {
+ const csv = Buffer.from('name,age,city\nAlice,30,NYC\nBob,25,SF\n', 'utf-8');
+ const html = await csvToHtml(csv);
+ expect(html).toMatch(/^<!DOCTYPE html>/);
+ expect(html).toContain('Alice');
+ expect(html).toContain('NYC');
+ expect(html).toContain('<table');
+ expect(html).not.toContain('class="lc-sheet-tabs"');
+ });
+
+ test('handles CSV with embedded commas via quoted fields', async () => {
+ const csv = Buffer.from('label,value\n"hello, world",42\n', 'utf-8');
+ const html = await csvToHtml(csv);
+ expect(html).toContain('hello, world');
+ expect(html).toContain('42');
+ });
+
+ test('handles an empty CSV without crashing', async () => {
+ const html = await csvToHtml(Buffer.from('', 'utf-8'));
+ expect(html).toMatch(/^<!DOCTYPE html>/);
+ });
+ });
+
+ describe('pptxToSlideListHtml', () => {
+ /** Build a minimal valid PPTX with N synthesized slides for testing. */
+ const buildPptx = async (
+ slides: Array<{ title: string; body?: string[] }>,
+ ): Promise<Buffer> => {
+ const zip = new JSZip();
+ const slideXml = (title: string, body: string[] = []) => {
+ const titleP = `<a:p><a:r><a:t>${title}</a:t></a:r></a:p>`;
+ const bodyPs = body.map((b) => `<a:p><a:r><a:t>${b}</a:t></a:r></a:p>`).join('');
+ return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
+ xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
+ <p:cSld>
+ <p:spTree>
+ <p:sp>
+ <p:txBody>
+ ${titleP}
+ ${bodyPs}
+ </p:txBody>
+ </p:sp>
+ </p:spTree>
+ </p:cSld>
+</p:sld>`;
+ };
+ slides.forEach((s, i) => {
+ zip.file(`ppt/slides/slide${i + 1}.xml`, slideXml(s.title, s.body));
+ });
+ // Add a dummy non-slide entry so we exercise the filter.
+ zip.file('docProps/core.xml', '<core/>');
+ return zip.generateAsync({ type: 'nodebuffer' });
+ };
+
+ test('extracts slide titles and body bullets in slide-number order', async () => {
+ const pptx = await buildPptx([
+ { title: 'Welcome', body: ['First point', 'Second point'] },
+ { title: 'Agenda', body: ['Item A', 'Item B', 'Item C'] },
+ { title: 'Thanks!' },
+ ]);
+ const html = await pptxToSlideListHtml(pptx);
+ expect(html).toMatch(/^<!DOCTYPE html>/);
+ expect(html).toContain('Slide 1');
+ expect(html).toContain('Welcome');
+ expect(html).toContain('First point');
+ expect(html).toContain('Slide 2');
+ expect(html).toContain('Agenda');
+ expect(html).toContain('Item C');
+ expect(html).toContain('Slide 3');
+ expect(html).toContain('Thanks!');
+ // Title appears before body in the doc.
+ expect(html.indexOf('Welcome')).toBeLessThan(html.indexOf('First point'));
+ });
+
+ test('handles a slide with no extractable text gracefully', async () => {
+ const pptx = await buildPptx([{ title: '' }]);
+ const html = await pptxToSlideListHtml(pptx);
+ expect(html).toContain('Slide 1');
+ expect(html).toContain('(empty slide)');
+ });
+
+ test('returns a friendly empty-state document when no slides are present', async () => {
+ const zip = new JSZip();
+ zip.file('docProps/core.xml', '<core/>');
+ const pptx = await zip.generateAsync({ type: 'nodebuffer' });
+ const html = await pptxToSlideListHtml(pptx);
+ expect(html).toContain('contains no readable slides');
+ });
+
+ test('decodes XML entities in slide text', async () => {
+ const pptx = await buildPptx([{ title: 'A &amp; B', body: ['x &lt; y'] }]);
+ const html = await pptxToSlideListHtml(pptx);
+ expect(html).toContain('A &amp; B'); // re-escaped on output
+ expect(html).toContain('x &lt; y');
+ });
+ });
+
+ describe('pptxToHtml dispatcher', () => {
+ /** Mirror the buildPptx helper from the slide-list block. */
+ const buildPptx = async (
+ slides: Array<{ title: string; body?: string[] }>,
+ ): Promise<Buffer> => {
+ const zip = new JSZip();
+ const slideXml = (title: string, body: string[] = []) => {
+ const titleP = `<a:p><a:r><a:t>${title}</a:t></a:r></a:p>`;
+ const bodyPs = body.map((b) => `<a:p><a:r><a:t>${b}</a:t></a:r></a:p>`).join('');
+ return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
+ xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
+ <p:cSld><p:spTree><p:sp><p:txBody>${titleP}${bodyPs}</p:txBody></p:sp></p:spTree></p:cSld>
+</p:sld>`;
+ };
+ slides.forEach((s, i) => {
+ zip.file(`ppt/slides/slide${i + 1}.xml`, slideXml(s.title, s.body));
+ });
+ zip.file('docProps/core.xml', '<core/>');
+ return zip.generateAsync({ type: 'nodebuffer' });
+ };
+
+ test('routes a small pptx (≤ cap) through the CDN-rendered path', async () => {
+ const pptx = await buildPptx([{ title: 'Hello', body: ['First slide'] }]);
+ const html = await pptxToHtml(pptx);
+ expect(html).toMatch(/^<!DOCTYPE html>/);
+ expect(html).toContain('id="lc-doc-data"');
+ expect(html).toContain('pptxPreview.init');
+ expect(html).toContain('cdn.jsdelivr.net/npm/pptx-preview@');
+ /* The slide-list `<ol class="lc-pptx-list">` is now embedded
+ * INSIDE the CDN doc as a runtime fallback for the silent-
+ * empty-render case (pptx-preview's compatibility issues with
+ * pptxgenjs decks). It lives in the hidden `#lc-fallback`
+ * block; the bootstrap reveals it when `hasRenderedContent()`
+ * detects empty wrappers. Manual e2e on PR #12934. */
+ expect(html).toContain('class="lc-pptx-list"');
+ expect(html).toContain('id="lc-fallback"');
+ });
+
+ describe('CDN-rendered path', () => {
+ test('embeds the binary as base64 that round-trips to the original bytes', async () => {
+ const original = await buildPptx([{ title: 'Test' }]);
+ const html = await _internal.pptxToHtmlViaCdn(
+ original,
+ '<ol class="lc-pptx-list"><li>fb</li></ol>',
+ );
+ const match = html.match(
+ /<script id="lc-doc-data" type="application\/octet-stream;base64">([^<]*)<\/script>/,
+ );
+ expect(match).not.toBeNull();
+ const decoded = Buffer.from(match![1], 'base64');
+ expect(decoded.equals(original)).toBe(true);
+ });
+
+ test('pins pptx-preview to a specific version with SRI integrity', async () => {
+ const pptx = await buildPptx([{ title: 'X' }]);
+ const html = await _internal.pptxToHtmlViaCdn(
+ pptx,
+ '<ol class="lc-pptx-list"><li>fb</li></ol>',
+ );
+ expect(html).toContain('https://cdn.jsdelivr.net/npm/[email protected]/');
+ expect(html).toMatch(/integrity="sha384-[A-Za-z0-9+/=]+"/);
+ expect(html).toContain('crossorigin="anonymous"');
+ });
+
+ test('CSP locks the iframe down: connect-src restricted, no eval, no base/form tampering', async () => {
+ const pptx = await buildPptx([{ title: 'X' }]);
+ const html = await _internal.pptxToHtmlViaCdn(
+ pptx,
+ '<ol class="lc-pptx-list"><li>fb</li></ol>',
+ );
+ const cspMatch = html.match(
+ /<meta http-equiv="Content-Security-Policy" content="([^"]+)">/,
+ );
+ expect(cspMatch).not.toBeNull();
+ const csp = cspMatch![1];
+ /* `connect-src` allows self + jsdelivr (the script's origin)
+ * so DevTools sourcemap fetches succeed and any same-origin
+ * fetches the renderer makes at runtime work. Wildcard
+ * exfiltration paths stay blocked. Manual e2e on PR #12934 —
+ * strict `'none'` filled DevTools console with `.min.js.map`
+ * violations every time the iframe was inspected. */
+ expect(csp).toMatch(/connect-src 'self' https:\/\/cdn\.jsdelivr\.net/);
+ expect(csp).not.toMatch(/connect-src[^;]*\*/); // no wildcard
+ expect(csp).toMatch(/base-uri 'none'/);
+ expect(csp).toMatch(/form-action 'none'/);
+ expect(csp).toMatch(/script-src https:\/\/cdn\.jsdelivr\.net 'unsafe-inline'/);
+ expect(csp).not.toMatch(/unsafe-eval/);
+ /* PPTX must allow blob:-only Web Workers — pptx-preview's
+ * bundled echarts dep spins up workers via blob: URLs for
+ * chart rendering. Without this, the renderer's async
+ * pipeline throws unhandled rejections and the iframe shows
+ * a black screen. */
+ expect(csp).toMatch(/worker-src blob:/);
+ });
+
+ test('exposes a fallback message that surfaces if the renderer fails to load or times out', async () => {
+ const pptx = await buildPptx([{ title: 'X' }]);
+ const html = await _internal.pptxToHtmlViaCdn(
+ pptx,
+ '<ol class="lc-pptx-list"><li>fb</li></ol>',
+ );
+ // Visible loading state + fallback that swaps in on error.
+ expect(html).toContain('Loading preview…');
+ /* Fallback now embeds the slide-list view + a notice
+ * explaining the high-fidelity renderer failed. The old
+ * static "Preview unavailable" text is replaced with the
+ * actual readable content. Manual e2e on PR #12934 (pptxgenjs
+ * compatibility issue with pptx-preview). */
+ expect(html).toContain('High-fidelity renderer unavailable');
+ expect(html).toContain('class="lc-pptx-list"');
+ // The renderer-not-loaded check.
+ expect(html).toContain("typeof pptxPreview === 'undefined'");
+ // The unhandledrejection + error listeners — pptx-preview's
+ // bundled deps raise async rejections that don't surface
+ // through the outer Promise.
+ expect(html).toContain("addEventListener('unhandledrejection'");
+ expect(html).toContain("addEventListener('error'");
+ // The 8-second timeout safety net for silent renderer failures.
+ expect(html).toContain('renderer-timeout');
+ expect(html).toContain('renderer-empty-slide-list');
+ expect(html).toContain("querySelectorAll('.pptx-preview-slide-wrapper')");
+ /* Diagnostic surfacing: the failure reason must be exposed
+ * three ways so a debugging user can find it without reading
+ * source — manual e2e on PR #12934 ("'Preview unavailable'
+ * with no way to know why"):
+ * 1. Visible inline via a `<details>` element so it's one
+ * click away in the iframe itself
+ * 2. `title` attribute on the fallback for hover tooltip
+ * 3. `console.error` so DevTools shows it in red */
+ expect(html).toContain('id="lc-fallback-reason"');
+ expect(html).toContain('Diagnostic details');
+ expect(html).toContain('fallback.title = reasonText');
+ expect(html).toContain("console.error('[pptx-preview] fallback fired:'");
+ });
+
+ test('size-fallback threshold is the documented 350 KB', () => {
+ expect(_internal.MAX_PPTX_CDN_BINARY_BYTES).toBe(350 * 1024);
+ });
+
+ test('embeds the slide-list fallback in the CDN doc with empty-render detection', async () => {
+ /* pptx-preview silently produces empty 960×540 wrappers for
+ * pptxgenjs-generated decks (manual e2e on PR #12934 — it
+ * parses the file enough to create the placeholder, then
+ * fails to populate it). The bootstrap now detects the
+ * empty-render case via `hasRenderedContent()` and reveals
+ * the embedded slide-list fallback so the user always gets
+ * readable content. */
+ const pptx = await buildPptx([{ title: 'A' }, { title: 'B' }]);
+ const html = await _internal.pptxToHtmlViaCdn(
+ pptx,
+ '<ol class="lc-pptx-list"><li>SENTINEL_FALLBACK</li></ol>',
+ );
+ /* Slide-list fallback body is embedded inside `#lc-fallback`. */
+ expect(html).toContain('id="lc-fallback"');
+ expect(html).toContain('SENTINEL_FALLBACK');
+ /* Empty-render detection helper exists in the bootstrap. */
+ expect(html).toContain('hasRenderedContent');
+ expect(html).toContain('renderer-empty-slide-list');
+ expect(html).toContain("querySelectorAll('.pptx-preview-slide-wrapper')");
+ /* The slide-list fallback CSS is inlined so the fallback
+ * renders with the same look as the standalone slide-list
+ * path (CSP locks `style-src` to inline only). */
+ expect(html).toContain('.lc-pptx-list');
+ expect(html).toContain('.lc-pptx-slide');
+ });
+
+ test('bootstrap wraps + scales each slide so it fits the iframe width', async () => {
+ /* pptx-preview emits slides at the init dimensions (960×540
+ * by default). Without post-processing, narrow artifact panels
+ * scroll horizontally and slides spill outside the viewport.
+ * The bootstrap wraps each rendered slide in `.lc-slide-wrap`
+ * and applies `transform: scale(panel_width / 960)` so the
+ * panel always fits — manual e2e feedback on PR #12934.
+ *
+ * The wrap is applied ONCE, after `previewer.preview` resolves
+ * (and the post-render container is visible). Wrapping during
+ * streaming via MutationObserver caused pptx-preview to throw
+ * — its internal pipeline holds references to the appended
+ * slides and broke when we moved them under a parent wrap. */
+ const pptx = await buildPptx([{ title: 'A' }, { title: 'B' }]);
+ const html = await _internal.pptxToHtmlViaCdn(
+ pptx,
+ '<ol class="lc-pptx-list"><li>fb</li></ol>',
+ );
+ /* The wrapper class used by the CSS rules. */
+ expect(html).toContain('lc-slide-wrap');
+ /* The wrap function + the per-slide scale function. The
+ * scale uses each slide's actual rendered native width (not
+ * a constant) so panels wider than 960px still fill — no
+ * upscale cap means we never leave whitespace on the sides
+ * of the panel. */
+ expect(html).toContain('wrapSlides');
+ expect(html).toContain('scaleFor');
+ expect(html).toContain('availableWidth');
+ /* The width-only scale formula. Negative assertion below
+ * forbids re-introducing the height-aware variant — that
+ * caused pptx-preview to render slides as solid-black
+ * rectangles (manual e2e regression on PR #12934). */
+ expect(html).toContain('availableWidth() / (nativeW || SLIDE_W)');
+ /* Negative assertions for two failed iterations:
+ * - `Math.min(1, ...)` upscale cap left whitespace on
+ * panels wider than 960px.
+ * - `Math.min(sw, sh)` height-aware scale + viewport-
+ * derived height calculations interacted with pptx-
+ * preview's internal layout and produced black slides.
+ * - `body { display: flex }` and `min-height: 100vh` on
+ * html/body also broke pptx-preview's render path.
+ * Vertical empty space below short decks is acceptable; the
+ * iframe bg shows through and matches the panel theme. */
+ expect(html).not.toMatch(/Math\.min\(\s*1\s*,/);
+ expect(html).not.toContain('Math.min(sw, sh)');
+ expect(html).not.toContain('availableHeight');
+ expect(html).not.toContain('min-height: 100vh');
+ expect(html).not.toMatch(/body\s*\{[^}]*display:\s*flex/);
+ /* Container is hidden during render and revealed by the
+ * `finalize` step so the unscaled flash never reaches the
+ * user. */
+ expect(html).toContain("container.style.visibility = 'hidden'");
+ expect(html).toContain("container.style.visibility = 'visible'");
+ /* ResizeObserver re-fits on panel resize. (No
+ * MutationObserver — streaming wraps broke pptx-preview.) */
+ expect(html).toContain('ResizeObserver');
+ expect(html).not.toContain('MutationObserver');
+ /* Native dimensions cached on the slide dataset so re-fitting
+ * on resize never measures an already-transformed box. */
+ expect(html).toContain('lcNativeW');
+ });
+ });
+
+ describe('OFFICE_PREVIEW_DISABLE_CDN escape hatch', () => {
+ const ORIGINAL = process.env.OFFICE_PREVIEW_DISABLE_CDN;
+ afterEach(() => {
+ if (ORIGINAL === undefined) {
+ delete process.env.OFFICE_PREVIEW_DISABLE_CDN;
+ } else {
+ process.env.OFFICE_PREVIEW_DISABLE_CDN = ORIGINAL;
+ }
+ });
+
+ test('default behavior (env unset): small pptx → CDN path', async () => {
+ delete process.env.OFFICE_PREVIEW_DISABLE_CDN;
+ const pptx = await buildPptx([{ title: 'Hello' }]);
+ const html = await pptxToHtml(pptx);
+ expect(html).toContain('id="lc-doc-data"');
+ });
+
+ it.each([['true'], ['1'], ['yes'], ['TRUE']])(
+ 'env=%s forces the slide-list fallback even for small files',
+ async (value) => {
+ process.env.OFFICE_PREVIEW_DISABLE_CDN = value;
+ const pptx = await buildPptx([{ title: 'Hello', body: ['Line 1'] }]);
+ const html = await pptxToHtml(pptx);
+ // Slide-list signature.
+ expect(html).toContain('class="lc-pptx-list"');
+ expect(html).toContain('Hello');
+ expect(html).not.toContain('id="lc-doc-data"');
+ expect(html).not.toContain('cdn.jsdelivr.net');
+ },
+ );
+ });
+ });
+
+ describe('bufferToOfficeHtml dispatcher', () => {
+ test('routes by extension when MIME is generic', async () => {
+ const html = await bufferToOfficeHtml(
+ readFixture('sample.docx'),
+ 'sample.docx',
+ 'application/octet-stream',
+ );
+ expect(html).not.toBeNull();
+ // sample.docx is small → CDN path. Lock the dispatcher routing
+ // by checking for a CDN-path signature rather than the literal
+ // document text (which only appears in the mammoth fallback).
+ expect(html!).toContain('id="lc-doc-data"');
+ expect(html!).toContain('docx-preview@');
+ });
+
+ test('routes by MIME when extension is missing', async () => {
+ const html = await bufferToOfficeHtml(
+ readFixture('sample.xlsx'),
+ 'workbook',
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
+ );
+ expect(html).not.toBeNull();
+ expect(html!).toContain('Sheet One');
+ });
+
+ test('routes csv by extension', async () => {
+ const html = await bufferToOfficeHtml(
+ Buffer.from('a,b\n1,2', 'utf-8'),
+ 'data.csv',
+ 'application/octet-stream',
+ );
+ expect(html).not.toBeNull();
+ expect(html!).toContain('<table');
+ });
+
+ test('routes csv by MIME when extension is missing', async () => {
+ const html = await bufferToOfficeHtml(Buffer.from('a,b\n1,2', 'utf-8'), 'data', 'text/csv');
+ expect(html).not.toBeNull();
+ expect(html!).toContain('<table');
+ });
+
+ test('returns null for unrecognized types', async () => {
+ const html = await bufferToOfficeHtml(Buffer.from('hello'), 'notes.txt', 'text/plain');
+ expect(html).toBeNull();
+ });
+
+ test('extension wins over MIME (sniff misclassifies docx as application/zip)', async () => {
+ const html = await bufferToOfficeHtml(
+ readFixture('sample.docx'),
+ 'sample.docx',
+ 'application/zip',
+ );
+ expect(html).not.toBeNull();
+ expect(html!).toContain('lc-docx');
+ });
+ });
+
+ describe('officeHtmlBucket predicate', () => {
+ /* The shared predicate is the single source of truth for "should the
+ * office HTML pipeline handle this file?". The upstream gate in
+ * `extract.ts` calls it directly; the dispatcher above delegates to
+ * it. Tests here lock in MIME-only routing for extensionless inputs
+ * (the case Codex flagged on PR #12934). */
+ it.each([
+ ['report.docx', 'application/zip', 'docx'],
+ ['report.docx', '', 'docx'],
+ ['noext', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'docx'],
+ ['data.csv', 'application/octet-stream', 'csv'],
+ ['data', 'text/csv', 'csv'],
+ ['data', 'application/csv', 'csv'],
+ ['workbook.xlsx', '', 'spreadsheet'],
+ ['legacy.xls', '', 'spreadsheet'],
+ ['sheet.ods', '', 'spreadsheet'],
+ ['noext', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'spreadsheet'],
+ ['noext', 'application/vnd.ms-excel', 'spreadsheet'],
+ ['noext', 'application/vnd.oasis.opendocument.spreadsheet', 'spreadsheet'],
+ ['deck.pptx', '', 'pptx'],
+ [
+ 'noext',
+ 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
+ 'pptx',
+ ],
+ ])('classifies (%s, %s) as %s', (name, mime, expected) => {
+ expect(officeHtmlBucket(name, mime)).toBe(expected);
+ });
+
+ it.each([
+ ['note.txt', 'text/plain'],
+ ['code.py', 'text/x-python'],
+ ['data.json', 'application/json'],
+ ['photo.jpg', 'image/jpeg'],
+ ['doc.pdf', 'application/pdf'],
+ ['archive.zip', 'application/zip'],
+ ['notes.odt', 'application/vnd.oasis.opendocument.text'],
+ ['noext', 'text/plain'],
+ ['noext', ''],
+ ])('returns null for non-office (%s, %s)', (name, mime) => {
+ expect(officeHtmlBucket(name, mime)).toBeNull();
+ });
+
+ /* Regression for Codex P2 review on PR #12934. A binary office file
+ * with a mismatched MIME (e.g. a tool sandbox sets `text/csv` on
+ * everything it ships) must NOT be re-routed to a different bucket
+ * just because the MIME matches a different bucket's pattern. The
+ * documented "extension wins" precedence is enforced by checking
+ * extensions exhaustively before any MIME pattern fires. */
+ it.each([
+ ['deck.pptx', 'text/csv', 'pptx'],
+ ['workbook.xlsx', 'text/csv', 'spreadsheet'],
+ [
+ 'legacy.xls',
+ 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
+ 'spreadsheet',
+ ],
+ ['sheet.ods', 'text/csv', 'spreadsheet'],
+ ['report.docx', 'text/csv', 'docx'],
+ ['report.docx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'docx'],
+ [
+ 'data.csv',
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ 'csv',
+ ],
+ ])('extension wins over conflicting MIME: (%s, %s) → %s', (name, mime, expected) => {
+ expect(officeHtmlBucket(name, mime)).toBe(expected);
+ });
+
+ /* Regression for Codex P2 (server/client routing parity). The
+ * client's `detectArtifactTypeFromFile` routes by extension first
+ * for ANY known extension (not just office) — `.txt` →
+ * PLAIN_TEXT regardless of MIME. If the server fell back to MIME
+ * for these files and produced office HTML, the client would
+ * still route to PLAIN_TEXT, escape the HTML through the markdown
+ * viewer, and the user would see raw `<html>...` markup. The
+ * server now refuses to fall back to MIME when extension is
+ * non-empty — symmetric "extension wins" precedence with the
+ * client.
+ *
+ * Trade-off: a true DOCX renamed to `myfile.bin` + DOCX MIME no
+ * longer routes through office HTML on the server. Acceptable —
+ * extension/MIME mismatch is user error, and the security gate
+ * on the client would have produced a PLAIN_TEXT fall-through
+ * anyway (no preview either way). */
+ it.each([
+ // Known non-office extension + office MIME — must NOT route to office
+ ['notes.txt', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
+ ['notes.md', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
+ ['data.json', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
+ ['code.py', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'],
+ ['page.html', 'text/csv'],
+ ['styles.css', 'text/csv'],
+ // Unknown extension + office MIME — also returns null (server is
+ // conservative; client's security gate will downgrade these to
+ // PLAIN_TEXT anyway since `textFormat` would not be 'html')
+ ['blob.bin', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
+ ['data.dat', 'text/csv'],
+ ])('non-empty non-office extension wins over office MIME: (%s, %s) → null', (name, mime) => {
+ expect(officeHtmlBucket(name, mime)).toBeNull();
+ });
+
+ /* Regression for Codex P2 review on PR #12934. Real Content-Type
+ * headers carry parameters like `; charset=utf-8` and `; boundary`;
+ * the predicate must strip them before matching, otherwise the
+ * backend silently falls through to raw text while the client's
+ * `baseMime` strips the same parameters and routes the file to the
+ * spreadsheet bucket — yielding a broken preview. */
+ it.each([
+ ['data', 'text/csv; charset=utf-8', 'csv'],
+ ['data', 'text/csv;charset=utf-8', 'csv'],
+ ['data', 'TEXT/CSV; CHARSET=UTF-8', 'csv'],
+ ['data', 'application/csv; charset=ascii', 'csv'],
+ ['data', 'text/comma-separated-values', 'csv'],
+ ['data', 'text/comma-separated-values; charset=utf-8', 'csv'],
+ [
+ 'workbook',
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=binary',
+ 'spreadsheet',
+ ],
+ [
+ 'report',
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=binary',
+ 'docx',
+ ],
+ [
+ 'deck',
+ 'application/vnd.openxmlformats-officedocument.presentationml.presentation; foo=bar',
+ 'pptx',
+ ],
+ ])('strips MIME parameters before matching: (%s, %s) → %s', (name, mime, expected) => {
+ expect(officeHtmlBucket(name, mime)).toBe(expected);
+ });
+ });
+
+ describe('zip-bomb defense (SEC review on PR #12934)', () => {
+ /* Build a sub-1MB compressed ZIP that inflates to >25MB (default
+ * per-entry cap). Mirrors the SEC validation PoC: highly compressed
+ * runs of zero bytes squeeze through any compressed-size gate but
+ * blow up the parser. */
+ const buildBombArchive = async (
+ entries: Array<{ name: string; decompressedBytes: number }>,
+ ): Promise<Buffer> => {
+ const zip = new JSZip();
+ for (const { name, decompressedBytes } of entries) {
+ zip.file(name, Buffer.alloc(decompressedBytes, 0));
+ }
+ return zip.generateAsync({
+ type: 'nodebuffer',
+ compression: 'DEFLATE',
+ compressionOptions: { level: 9 },
+ });
+ };
+
+ test('wordDocToHtml rejects a zip-bomb DOCX before mammoth touches it', async () => {
+ const bomb = await buildBombArchive([
+ { name: 'word/document.xml', decompressedBytes: 50 * megabyte },
+ ]);
+ expect(bomb.length).toBeLessThan(1 * megabyte);
+ await expect(wordDocToHtml(bomb)).rejects.toThrow(ZipBombError);
+ });
+
+ test('excelSheetToHtml rejects a zip-bomb XLSX before SheetJS touches it', async () => {
+ const bomb = await buildBombArchive([
+ { name: 'xl/worksheets/sheet1.xml', decompressedBytes: 50 * megabyte },
+ ]);
+ expect(bomb.length).toBeLessThan(1 * megabyte);
+ await expect(excelSheetToHtml(bomb)).rejects.toThrow(ZipBombError);
+ });
+
+ test('pptxToSlideListHtml rejects a zip-bomb PPTX before slide extraction', async () => {
+ const bomb = await buildBombArchive([
+ { name: 'ppt/slides/slide1.xml', decompressedBytes: 50 * megabyte },
+ ]);
+ expect(bomb.length).toBeLessThan(1 * megabyte);
+ await expect(pptxToSlideListHtml(bomb)).rejects.toThrow(ZipBombError);
+ });
+
+ test('bufferToOfficeHtml propagates ZipBombError so callers can fail safe', async () => {
+ const bomb = await buildBombArchive([
+ { name: 'word/document.xml', decompressedBytes: 50 * megabyte },
+ ]);
+ await expect(bufferToOfficeHtml(bomb, 'evil.docx', '')).rejects.toThrow(ZipBombError);
+ });
+
+ test('legitimate small office files are not impacted by the safety check', async () => {
+ /* Real DOCX fixture from the test fixtures directory should still
+ * render — paranoid validation that the safety check doesn't false-
+ * positive on tiny legitimate inputs. Small fixture takes the CDN
+ * path, so we assert by wrapper structure instead of doc text. */
+ const fixture = readFixture('sample.docx');
+ const html = await wordDocToHtml(fixture);
+ expect(html).toMatch(/^<!DOCTYPE html>/);
+ expect(html).toContain('id="lc-doc-data"');
+ });
+ });
+
+ describe('sanitizeOfficeHtml security', () => {
+ test('strips <script> tags entirely', async () => {
+ const out = await sanitizeOfficeHtml('<p>before<script>alert(1)</script>after</p>');
+ expect(out).not.toMatch(/<script\b/i);
+ expect(out).not.toContain('alert(1)');
+ expect(out).toContain('before');
+ expect(out).toContain('after');
+ });
+
+ test('drops event-handler attributes from surviving tags', async () => {
+ const out = await sanitizeOfficeHtml('<img src="https://x.test/a.png" onerror="alert(1)">');
+ expect(out).not.toMatch(/onerror=/i);
+ expect(out).toContain('https://x.test/a.png');
+ });
+
+ test('strips javascript: URLs from anchors', async () => {
+ const out = await sanitizeOfficeHtml('<a href="javascript:alert(1)">click</a>');
+ expect(out).not.toMatch(/javascript:/i);
+ // The text survives even when the href is dropped.
+ expect(out).toContain('click');
+ });
+
+ test('rejects data: URLs in <a href> (only <img src> may use data:)', async () => {
+ /* The Sandpack iframe sandbox does NOT gate `target="_blank"`
+ * navigations. A surviving `<a href="data:text/html,...">` would
+ * open attacker-controlled HTML in a new tab on click. The
+ * sanitizer scopes `data:` to <img> only — global schemes are
+ * http/https/mailto. Regression guard for the Codex review on
+ * PR #12934. */
+ const out = await sanitizeOfficeHtml(
+ '<a href="data:text/html,<script>alert(1)</script>">click</a>',
+ );
+ expect(out).not.toContain('data:');
+ expect(out).not.toContain('script');
+ expect(out).toContain('click');
+ });
+
+ test('preserves data: URLs in <img src> (mammoth inlines DOCX images as base64)', async () => {
+ const tinyPng =
+ 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=';
+ const out = await sanitizeOfficeHtml(`<img src="${tinyPng}" alt="dot">`);
+ expect(out).toContain(tinyPng);
+ expect(out).toContain('alt="dot"');
+ });
+
+ test('preserves http(s) and mailto links on anchors', async () => {
+ const out = await sanitizeOfficeHtml(
+ '<a href="https://example.com">site</a> <a href="mailto:[email protected]">mail</a>',
+ );
+ expect(out).toContain('https://example.com');
+ expect(out).toContain('mailto:[email protected]');
+ });
+
+ test('forces target=_blank rel=noopener on surviving anchors', async () => {
+ const out = await sanitizeOfficeHtml('<a href="https://example.com">link</a>');
+ expect(out).toContain('target="_blank"');
+ expect(out).toContain('rel="noopener noreferrer"');
+ });
+
+ test('strips <iframe> entirely', async () => {
+ const out = await sanitizeOfficeHtml('<p>x</p><iframe src="https://evil.test"></iframe>');
+ expect(out).not.toMatch(/<iframe\b/i);
+ expect(out).not.toContain('evil.test');
+ });
+ });
+});
diff --git a/packages/api/src/files/documents/html.ts b/packages/api/src/files/documents/html.ts
new file mode 100644
index 0000000000..7bac37804c
--- /dev/null
+++ b/packages/api/src/files/documents/html.ts
@@ -0,0 +1,1633 @@
+import yauzl from 'yauzl';
+import { excelMimeTypes, megabyte } from 'librechat-data-provider';
+import { tryLibreOfficePreview } from './libreoffice';
+import { assertSafeZipSize } from './zipSafety';
+
+/**
+ * Maximum decompressed size we'll accept from a single PPTX entry. Mirrors the
+ * `ODT_MAX_DECOMPRESSED_SIZE` cap in `crud.ts` — both pptx and odt are zip
+ * archives whose central directory's `uncompressedSize` cannot be trusted.
+ */
+const PPTX_MAX_ENTRY_SIZE = 50 * megabyte;
+
+/** Cap on the number of slides we'll render — pathological decks bail early. */
+const PPTX_MAX_SLIDES = 500;
+
+/** Per-sheet row cap for spreadsheet rendering. Above this we truncate with a
+ * visible banner so a 100k-row sheet doesn't blow the 512KB cache cap. */
+const SPREADSHEET_MAX_ROWS_PER_SHEET = 5_000;
+
+/** Lazy-loaded `sanitize-html` module (commonjs interop). */
+let sanitizeHtmlModule: typeof import('sanitize-html') | null = null;
+async function getSanitizer(): Promise<typeof import('sanitize-html')> {
+ if (sanitizeHtmlModule) {
+ return sanitizeHtmlModule;
+ }
+ const mod = await import('sanitize-html');
+ sanitizeHtmlModule = (mod.default ?? mod) as typeof import('sanitize-html');
+ return sanitizeHtmlModule;
+}
+
+/**
+ * Sanitize HTML produced by mammoth / SheetJS / our own pptx renderer for
+ * embedding in the Sandpack `static` iframe. Allows the structural and
+ * formatting tags those producers emit, plus inline `data:` images (mammoth
+ * inlines DOCX images as base64). Strips `<script>`, `<iframe>`, event
+ * handlers, `javascript:` URLs, and color-bearing inline styles.
+ *
+ * The Sandpack iframe is itself a sandbox — this is defense in depth, not
+ * the sole barrier.
+ */
+export async function sanitizeOfficeHtml(html: string): Promise<string> {
+ const sanitizeHtml = await getSanitizer();
+ return sanitizeHtml(html, {
+ allowedTags: [
+ 'a',
+ 'b',
+ 'blockquote',
+ 'br',
+ 'caption',
+ 'code',
+ 'col',
+ 'colgroup',
+ 'div',
+ 'em',
+ 'figure',
+ 'figcaption',
+ 'h1',
+ 'h2',
+ 'h3',
+ 'h4',
+ 'h5',
+ 'h6',
+ 'hr',
+ 'i',
+ 'img',
+ 'li',
+ 'ol',
+ 'p',
+ 'pre',
+ 'section',
+ 'small',
+ 'span',
+ 'strong',
+ 'sub',
+ 'sup',
+ 'table',
+ 'tbody',
+ 'td',
+ 'tfoot',
+ 'th',
+ 'thead',
+ 'tr',
+ 'u',
+ 'ul',
+ ],
+ allowedAttributes: {
+ a: ['href', 'name', 'target', 'rel', 'title'],
+ img: ['src', 'alt', 'title', 'width', 'height'],
+ table: ['border', 'cellpadding', 'cellspacing', 'class'],
+ td: ['colspan', 'rowspan', 'class', 'style'],
+ th: ['colspan', 'rowspan', 'scope', 'class', 'style'],
+ col: ['span', 'width', 'style'],
+ colgroup: ['span', 'width', 'style'],
+ div: ['class'],
+ span: ['class'],
+ section: ['class', 'aria-labelledby'],
+ h1: ['id', 'class'],
+ h2: ['id', 'class'],
+ h3: ['id', 'class'],
+ h4: ['id', 'class'],
+ h5: ['id', 'class'],
+ h6: ['id', 'class'],
+ p: ['class'],
+ ol: ['class'],
+ ul: ['class'],
+ li: ['class'],
+ blockquote: ['class'],
+ tbody: ['class'],
+ thead: ['class'],
+ tr: ['class'],
+ tfoot: ['class'],
+ },
+ /* `data:` is intentionally NOT in the global scheme list — it's only
+ * allowed for `<img src>` (mammoth inlines DOCX images as base64
+ * `data:image/...` URIs). Without this scoping, an `<a href="data:
+ * text/html,<script>…</script>">` smuggled through DOCX/PPTX would
+ * survive sanitization and open attacker-controlled HTML in a new
+ * tab when clicked — the Sandpack iframe sandbox doesn't gate
+ * `target="_blank"` navigations. */
+ allowedSchemes: ['http', 'https', 'mailto'],
+ allowedSchemesByTag: {
+ img: ['http', 'https', 'data'],
+ },
+ transformTags: {
+ a: (tagName, attribs) => ({
+ tagName,
+ attribs: { ...attribs, rel: 'noopener noreferrer', target: '_blank' },
+ }),
+ },
+ /* sanitize-html runs `allowedAttributes` BEFORE per-attribute filtering, so
+ * `style` only survives where we explicitly allow it (td/th/col/colgroup).
+ * For those we still want to drop color declarations — defense against the
+ * mammoth/SheetJS hardcoded color-on-white issue. */
+ allowedStyles: {
+ '*': {
+ 'text-align': [/^left$|^right$|^center$|^justify$/],
+ 'font-weight': [/^[1-9]00$|^bold$|^normal$/],
+ 'font-style': [/^italic$|^normal$/],
+ width: [/^\d+(?:\.\d+)?(?:px|%|em|rem)?$/],
+ height: [/^\d+(?:\.\d+)?(?:px|%|em|rem)?$/],
+ 'min-width': [/^\d+(?:\.\d+)?(?:px|%|em|rem)?$/],
+ 'vertical-align': [/^top$|^middle$|^bottom$|^baseline$/],
+ },
+ },
+ });
+}
+
+/**
+ * Wrap a sanitized HTML body in a complete document with the styles we want
+ * inside the Sandpack iframe. The CSS palette uses `prefers-color-scheme` so
+ * the iframe inherits dark/light from its parent (Sandpack iframes inherit
+ * the prefers-color-scheme media query from the host document).
+ */
+function wrapAsDocument(bodyHtml: string, extraHeadHtml = ''): string {
+ return `<!DOCTYPE html>
+<html lang="en">
+<head>
+<meta charset="UTF-8">
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<title>Preview</title>
+<style>
+:root {
+ color-scheme: light dark;
+ --bg: #ffffff;
+ --fg: #1f2937;
+ --muted: #6b7280;
+ --border: rgba(128, 128, 128, 0.25);
+ --row-alt: rgba(128, 128, 128, 0.04);
+ --row-hover: rgba(59, 130, 246, 0.08);
+ --header-bg: rgba(243, 244, 246, 0.95);
+ --tab-active-bg: rgba(229, 231, 235, 0.95);
+ --tab-bg: transparent;
+ --link: #2563eb;
+}
+@media (prefers-color-scheme: dark) {
+ :root {
+ --bg: #1a1a2e;
+ --fg: #e5e7eb;
+ --muted: #9ca3af;
+ --border: rgba(128, 128, 128, 0.35);
+ --row-alt: rgba(255, 255, 255, 0.03);
+ --row-hover: rgba(59, 130, 246, 0.12);
+ --header-bg: rgba(31, 41, 55, 0.95);
+ --tab-active-bg: rgba(55, 65, 81, 0.95);
+ --link: #60a5fa;
+ }
+}
+* { box-sizing: border-box; }
+html, body {
+ margin: 0;
+ padding: 0;
+ background: var(--bg);
+ color: var(--fg);
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
+ font-size: 14px;
+ line-height: 1.5;
+}
+body { padding: 16px; }
+a { color: var(--link); }
+img { max-width: 100%; height: auto; }
+h1 { font-size: 1.5rem; font-weight: 700; margin: 0.75em 0 0.5em; }
+h2 { font-size: 1.25rem; font-weight: 600; margin: 0.75em 0 0.5em; }
+h3 { font-size: 1.1rem; font-weight: 600; margin: 0.5em 0 0.25em; }
+h4, h5, h6 { font-weight: 600; margin: 0.5em 0 0.25em; }
+p { margin: 0.4em 0; }
+ul, ol { padding-left: 1.5em; margin: 0.5em 0; }
+table { border-collapse: collapse; font-size: 0.85rem; line-height: 1.3; margin: 0.5em 0; max-width: 100%; }
+th, td {
+ border: 1px solid var(--border);
+ padding: 0.4em 0.6em;
+ text-align: left;
+ vertical-align: top;
+ white-space: nowrap;
+}
+thead th, table th[scope="col"] {
+ background: var(--header-bg);
+ font-weight: 600;
+ position: sticky;
+ top: 0;
+ z-index: 1;
+}
+tbody tr:nth-child(even) { background: var(--row-alt); }
+tbody tr:hover { background: var(--row-hover); }
+.lc-banner {
+ background: var(--row-alt);
+ border: 1px solid var(--border);
+ border-radius: 6px;
+ padding: 6px 10px;
+ margin: 0 0 8px;
+ font-size: 0.8rem;
+ color: var(--muted);
+}
+.lc-table-scroll { overflow-x: auto; max-width: 100%; }
+${extraHeadHtml}
+</style>
+</head>
+<body>
+${bodyHtml}
+</body>
+</html>`;
+}
+
+/* =============================================================================
+ * DOCX → HTML
+ * ============================================================================= */
+
+/**
+ * Style-map directives that broaden mammoth's default heading detection.
+ * Mammoth's stock map only promotes paragraphs whose ms-word style name
+ * matches `Heading 1`/`Heading 2`/etc. — useless for code-generated
+ * docs (e.g. python-docx output) that apply direct character formatting
+ * instead of named styles. The `Title`/`Subtitle` mappings catch the
+ * built-in title styles used by Word's Insert > Cover Page workflow;
+ * explicit `Heading 1` thru `Heading 6` mappings retain mammoth's
+ * defaults; and `:fresh` tells mammoth not to merge consecutive
+ * matching paragraphs into a single heading element.
+ */
+const DOCX_STYLE_MAP = [
+ "p[style-name='Title'] => h1.lc-docx-title:fresh",
+ "p[style-name='Subtitle'] => h2.lc-docx-subtitle:fresh",
+ "p[style-name='Heading 1'] => h1:fresh",
+ "p[style-name='Heading 2'] => h2:fresh",
+ "p[style-name='Heading 3'] => h3:fresh",
+ "p[style-name='Heading 4'] => h4:fresh",
+ "p[style-name='Heading 5'] => h5:fresh",
+ "p[style-name='Heading 6'] => h6:fresh",
+ "p[style-name='Quote'] => blockquote:fresh",
+];
+
+/**
+ * CSS layered on top of `wrapAsDocument`'s base styles to give mammoth's
+ * flat output more visual structure. Mammoth strips the navy banners,
+ * cell shading, and column layouts that direct-formatted docs apply, so
+ * the source loses most of its presentation. We compensate with three
+ * targeted heuristics:
+ *
+ * 1. The first row of any `<table>` gets sticky-header styling
+ * regardless of `<thead>` (mammoth never emits `<thead>`).
+ * 2. Tables get alternating row stripes so dense data blocks stay
+ * scannable even without the source's hand-tuned shading.
+ * 3. A bold-only-child paragraph (`<p><strong>X</strong></p>`) is the
+ * python-docx idiom for a "section heading"; styled as a
+ * pseudo-h2 with a thin accent border so the document's structure
+ * survives the round-trip.
+ */
+const DOCX_EXTRA_CSS = `
+.lc-docx h1 { font-size: 1.5rem; font-weight: 700; margin: 1em 0 0.5em; padding-bottom: 0.3em; border-bottom: 2px solid var(--border); }
+.lc-docx h2 { font-size: 1.2rem; font-weight: 600; margin: 1em 0 0.4em; padding-left: 0.6em; border-left: 3px solid var(--link); }
+.lc-docx h3 { font-size: 1.05rem; font-weight: 600; margin: 0.8em 0 0.3em; color: var(--link); }
+.lc-docx-title { text-align: center; border-bottom: none; }
+.lc-docx-subtitle { text-align: center; border-left: none; padding-left: 0; color: var(--muted); font-style: italic; font-weight: 400; }
+.lc-docx p { margin: 0.5em 0; }
+.lc-docx p:has(> strong:only-child) { margin: 1em 0 0.4em; padding-left: 0.6em; border-left: 3px solid var(--link); font-size: 1.05rem; }
+.lc-docx p:has(> strong:only-child) strong { font-weight: 600; }
+.lc-docx table { width: 100%; max-width: 100%; }
+.lc-docx td, .lc-docx th { white-space: normal; }
+.lc-docx table tr:first-child td { background: var(--header-bg); font-weight: 600; }
+.lc-docx table tr:nth-child(even):not(:first-child) td { background: var(--row-alt); }
+.lc-docx ul, .lc-docx ol { margin: 0.5em 0; padding-left: 1.6em; }
+.lc-docx li { margin: 0.15em 0; }
+.lc-docx blockquote { border-left: 3px solid var(--border); color: var(--muted); margin: 0.8em 0; padding: 0.2em 0 0.2em 0.8em; }
+`.trim();
+
+/* =============================================================================
+ * DOCX CDN renderer (high-fidelity path)
+ *
+ * Embeds the binary as base64 inside a self-contained HTML document and
+ * relies on `docx-preview` loaded from a pinned CDN URL with SRI
+ * integrity to render it inside the Sandpack iframe. The iframe is a
+ * real browser DOM — `docx-preview`'s "browser-first" design is a
+ * feature here, not a limitation: we get cell shading, run-level
+ * colors/fonts, headers/footers, columns, and inline images at no
+ * server-side parsing cost.
+ *
+ * Trade-offs vs the mammoth path:
+ * + Far higher visual fidelity (4/5 vs 2/5).
+ * + No server-side jsdom; no extra Node deps; iframe sandbox isolates
+ * any parser bug from the API process.
+ * − base64 inflates the binary by ~33%, so files above
+ * `MAX_DOCX_CDN_BINARY_BYTES` fall back to the mammoth path so the
+ * wrapped HTML doesn't blow the `MAX_TEXT_CACHE_BYTES` (512KB) cap
+ * on `attachment.text`. Telemetry should track how often we hit the
+ * fallback — if it's frequent, the next move is to lift the cap
+ * for office types specifically rather than embed via signed URL.
+ *
+ * Library + version pinning (jsdelivr SRI hashes computed at the
+ * version listed; refresh by `openssl dgst -sha384 -binary FILE |
+ * openssl base64 -A` on the file at the URL):
+ * docx-preview 0.3.7 — Apache-2.0, ~75KB UMD
+ * jszip 3.10.1 — MIT, ~98KB (peer dep of docx-preview)
+ * Both are pinned to specific minor versions; SRI guarantees the byte
+ * content can't change underneath us even if the version were ever
+ * republished.
+ */
+const DOCX_PREVIEW_CDN = {
+ jszip: {
+ src: 'https://cdn.jsdelivr.net/npm/[email protected]/dist/jszip.min.js',
+ integrity: 'sha384-+mbV2IY1Zk/X1p/nWllGySJSUN8uMs+gUAN10Or95UBH0fpj6GfKgPmgC5EXieXG',
+ },
+ docxPreview: {
+ src: 'https://cdn.jsdelivr.net/npm/[email protected]/dist/docx-preview.min.js',
+ integrity: 'sha384-Fw+ZM2MtvxCe867uRzZY5GtGP+gs0NLvrlJS768RZWuKhOHMN4Fln3i3gMt1NSyQ',
+ },
+} as const;
+
+/**
+ * Maximum DOCX binary size (in bytes) we'll embed via the CDN-rendered
+ * path. Empirical headroom: with ~33% base64 inflation and ~5KB of
+ * wrapper boilerplate, 350KB of binary fits well under the 512KB
+ * `MAX_TEXT_CACHE_BYTES` cap on `attachment.text` with margin to spare.
+ */
+const MAX_DOCX_CDN_BINARY_BYTES = 350 * 1024;
+
+/**
+ * Mirror of `MAX_TEXT_CACHE_BYTES` from `~/files/code/extract` — the
+ * 512 KB ceiling that `attachment.text` is truncated to before hitting
+ * the SSE wire and the database. We mirror (rather than import) to
+ * avoid the cycle: `extract.ts` already imports `bufferToOfficeHtml`
+ * from this module. The dispatcher uses this to drop CDN-with-fallback
+ * docs that would exceed the cap and fall back to mammoth-only.
+ *
+ * If the upstream constant ever changes, update this value too. The
+ * `cap-mirrors-extract` test in `html.spec.ts` pins the relationship.
+ */
+const OFFICE_HTML_OUTPUT_CAP = 512 * 1024;
+
+/**
+ * Build the CDN-rendered HTML document for a DOCX. The base64 payload
+ * lives inside a `<script type="application/octet-stream;base64">`
+ * — this is HTML5-spec-compliant for "data block" scripts (browsers
+ * treat unrecognized script types as opaque text) and avoids any risk
+ * of HTML escaping a `</script>` substring inside the binary.
+ *
+ * The CSP locks the page down to the pinned CDN host: scripts only
+ * from jsdelivr (with SRI), no outbound `fetch`/`XHR`, no eval, images
+ * only `self`/`data:`/`blob:` (docx-preview uses `URL.createObjectURL`
+ * for inline images), styles inline (`docx-preview` injects per-doc
+ * styles into `<head>` at render time).
+ */
+function buildDocxCdnDocument(base64: string, mammothFallbackHtml: string): string {
+ /* `connect-src` allows fetches to:
+ * - `'self'`: the sandpack-static-server origin the iframe runs in
+ * (covers any same-origin sourcemap fetches the bundler embedded)
+ * - `https://cdn.jsdelivr.net`: where the renderer script came from
+ * (DevTools fetches `.min.js.map` from the same host as the script
+ * to map minified line numbers; with `'none'` the console fills
+ * with CSP violations every time DevTools is open)
+ *
+ * Exfiltration risk is minimal: the iframe is cross-origin to the
+ * LibreChat host so an attacker can't read application data from it,
+ * and the only meaningful target ('self' or jsdelivr) isn't useful
+ * for exfiltrating slide content to a host the attacker controls. */
+ const csp = [
+ "default-src 'none'",
+ "script-src https://cdn.jsdelivr.net 'unsafe-inline'",
+ "style-src 'unsafe-inline'",
+ "img-src 'self' data: blob:",
+ 'font-src data:',
+ "connect-src 'self' https://cdn.jsdelivr.net",
+ "base-uri 'none'",
+ "form-action 'none'",
+ ].join('; ');
+ /* Body styling for the embedded mammoth fallback. The CDN-rendered
+ * path normally hides this content, but on air-gapped networks where
+ * `cdn.jsdelivr.net` is blocked the fallback handler reveals it so
+ * the user gets a readable preview instead of the legacy "Preview
+ * unavailable" message — Codex P2 review on PR #12934. We inline the
+ * shared `DOCX_EXTRA_CSS` rules here (rather than `<link>` to a
+ * cross-origin sheet) because the CSP locks `style-src` to inline
+ * only and the wrapped mammoth output uses the same `.lc-docx`
+ * classes. */
+ return `<!DOCTYPE html>
+<html lang="en">
+<head>
+<meta charset="UTF-8">
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<meta http-equiv="Content-Security-Policy" content="${csp}">
+<title>Preview</title>
+<style>
+/* Lock the docx-preview iframe to LIGHT color-scheme. docx-preview
+ * is designed for page-style rendering (white pages with the docs
+ * native colors). When the OS is in dark mode and we declare
+ * color-scheme as both light and dark, browsers inherit dark-mode
+ * colors for inheritable text properties, which made docx-previews
+ * body text (no explicit color) inherit our light-grey --fg on the
+ * white page bg — barely-visible translucent rendering on PR #12934
+ * manual e2e. Hardcoding light keeps the page contrast right; the
+ * mammoth-only path (wrapAsDocument) still respects the users OS
+ * scheme since it owns its own bg + text colors. */
+:root { color-scheme: light; --bg: #ffffff; --fg: #1f2937; --muted: #6b7280; --link: #2563eb; --border: #e5e7eb; --header-bg: #f3f4f6; --row-alt: #f9fafb; }
+html, body { margin: 0; padding: 0; background: var(--bg); color: var(--fg); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
+#lc-render { padding: 16px; }
+#lc-fallback { padding: 24px 24px 32px; font-size: 14px; line-height: 1.5; color: var(--fg); }
+#lc-fallback-notice { font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--border); padding-bottom: 8px; margin: 0 0 16px; }
+.lc-docx-loading { display: flex; align-items: center; justify-content: center; height: 60vh; color: var(--muted); font-size: 14px; }
+${DOCX_EXTRA_CSS}
+/* docx-preview emits its own per-document <style> tags inside #lc-render
+ * — leave them be. These rules just keep the host frame consistent with
+ * dark mode and bound the rendered document width. */
+/* docx-preview wraps each section in .docx-wrapper and sets explicit
+ * inline width on .docx from the source pageSize. With ignoreWidth:true
+ * set on the renderer those inline widths are skipped, but we override
+ * defensively so the doc fills the artifact panel even if a future
+ * library version regresses. padding:0 on the wrapper drops the margin
+ * docx-preview otherwise reserves for page-edge whitespace. */
+#lc-render .docx-wrapper {
+ background: transparent !important;
+ padding: 0 !important;
+ width: 100% !important;
+}
+#lc-render .docx-wrapper > section.docx {
+ width: 100% !important;
+ max-width: 100% !important;
+ min-height: 0 !important;
+ padding: 0 !important;
+ box-shadow: none !important;
+ margin: 0 0 1em 0 !important;
+}
+/* Removed dark-mode override here — locking the CDN doc to
+ * color-scheme: light means --fg is always the light-mode dark-grey.
+ * Re-introducing a dark-mode media query would override docx-previews
+ * native text color and re-create the translucent rendering issue. */
+</style>
+<script src="${DOCX_PREVIEW_CDN.jszip.src}" integrity="${DOCX_PREVIEW_CDN.jszip.integrity}" crossorigin="anonymous"></script>
+<script src="${DOCX_PREVIEW_CDN.docxPreview.src}" integrity="${DOCX_PREVIEW_CDN.docxPreview.integrity}" crossorigin="anonymous"></script>
+</head>
+<body>
+<div id="lc-render"><div class="lc-docx-loading">Loading preview…</div></div>
+<div id="lc-fallback" hidden>
+<p id="lc-fallback-notice">High-fidelity renderer unavailable (CDN blocked or offline). Showing the simplified preview below.</p>
+<article class="lc-docx">${mammothFallbackHtml}</article>
+</div>
+<script id="lc-doc-data" type="application/octet-stream;base64">${base64}</script>
+<script>
+(function () {
+ function showFallback(reason) {
+ var loading = document.querySelector('.lc-docx-loading');
+ if (loading) { loading.remove(); }
+ /* Hide the empty render slot so the embedded mammoth preview owns
+ * the viewport — otherwise a stripe of padded empty space sits
+ * above the fallback content. Codex P2 review on PR #12934. */
+ var render = document.getElementById('lc-render');
+ if (render) { render.hidden = true; }
+ var fallback = document.getElementById('lc-fallback');
+ var reasonText = reason ? String(reason).slice(0, 500) : 'no reason reported';
+ if (fallback) {
+ fallback.hidden = false;
+ fallback.title = reasonText;
+ }
+ /* Mirror to DevTools so the reason is visible without inspecting
+ * the title attribute or expanding details elements. */
+ if (typeof console !== 'undefined' && console.warn) {
+ console.warn('[docx-preview] CDN renderer fell through to mammoth:', reasonText);
+ }
+ }
+ if (typeof docx === 'undefined' || typeof docx.renderAsync !== 'function') {
+ showFallback('renderer-not-loaded');
+ return;
+ }
+ try {
+ var b64 = document.getElementById('lc-doc-data').textContent.trim();
+ var bytes = Uint8Array.from(atob(b64), function (c) { return c.charCodeAt(0); });
+ docx.renderAsync(bytes.buffer, document.getElementById('lc-render'), null, {
+ className: 'docx',
+ inWrapper: true,
+ /* ignoreWidth: true tells docx-preview to skip the document's
+ * native page width (8.5in / 21cm typically) and render at the
+ * iframe container's full width. Without this the rendered doc
+ * floats with whitespace on either side in a wide artifact
+ * panel — fine on letter-sized screens, ugly in our context. */
+ ignoreWidth: true,
+ ignoreHeight: false,
+ ignoreFonts: false,
+ breakPages: true,
+ ignoreLastRenderedPageBreak: true,
+ experimental: false,
+ trimXmlDeclaration: true,
+ useBase64URL: false,
+ useMathMLPolyfill: false,
+ renderHeaders: true,
+ renderFooters: true,
+ renderFootnotes: true,
+ renderEndnotes: true,
+ }).then(function () {
+ var loading = document.querySelector('.lc-docx-loading');
+ if (loading) { loading.remove(); }
+ }).catch(function (err) {
+ showFallback(err && err.message);
+ });
+ } catch (err) {
+ showFallback(err && err.message);
+ }
+})();
+</script>
+</body>
+</html>`;
+}
+
+/**
+ * Run mammoth + sanitization to produce the inner DOCX body HTML
+ * (the `<article>` contents). Shared between the standalone mammoth
+ * path and the CDN path's fallback embedding so both render through
+ * the exact same pipeline — no diverging sanitization rules. Codex P2
+ * review on PR #12934.
+ */
+async function renderMammothBody(buffer: Buffer): Promise<string> {
+ const { convertToHtml } = await import('mammoth');
+ const result = await convertToHtml({ buffer }, { styleMap: DOCX_STYLE_MAP });
+ return sanitizeOfficeHtml(result.value);
+}
+
+async function wordDocToHtmlViaCdn(buffer: Buffer, mammothFallbackBody: string): Promise<string> {
+ return buildDocxCdnDocument(buffer.toString('base64'), mammothFallbackBody);
+}
+
+async function wordDocToHtmlViaMammoth(buffer: Buffer): Promise<string> {
+ const sanitized = await renderMammothBody(buffer);
+ return wrapAsDocument(`<article class="lc-docx">${sanitized}</article>`, DOCX_EXTRA_CSS);
+}
+
+/**
+ * Whether the CDN-rendered DOCX path is enabled for this process.
+ * Operators on air-gapped or filtered corporate networks (where
+ * `cdn.jsdelivr.net` is blocked) should set
+ * `OFFICE_PREVIEW_DISABLE_CDN=true` so DOCX previews fall back to the
+ * server-side mammoth renderer instead of degrading to "Preview
+ * unavailable" in the iframe — Codex P2 review on PR #12934.
+ *
+ * Read at call time (rather than module load) so jest tests can flip
+ * the env in a `beforeEach` without `jest.resetModules()`. The cost is
+ * a single property access per render. Truthy values: `true`, `1`,
+ * `yes` (case-insensitive). Anything else (including unset) means
+ * "use the CDN path when the size dispatcher picks it."
+ */
+function isOfficePreviewCdnDisabled(): boolean {
+ const v = process.env.OFFICE_PREVIEW_DISABLE_CDN;
+ if (v == null) {
+ return false;
+ }
+ return /^(1|true|yes)$/i.test(v.trim());
+}
+
+/**
+ * Convert a `.docx` buffer to a sandboxed HTML document. Two render
+ * paths, chosen by file size:
+ *
+ * 1. **CDN-rendered (default for files ≤ 350 KB binary)**: embeds the
+ * binary as base64 and lets `docx-preview` render it inside the
+ * Sandpack iframe. High visual fidelity — preserves cell shading,
+ * run-level colors/fonts, headers/footers, columns, and images.
+ * The mammoth-rendered HTML is *also* embedded as a hidden
+ * `<div id="lc-fallback">` block; the iframe's bootstrap script
+ * reveals it whenever `docx-preview` fails to load (corporate
+ * firewall blocking jsdelivr, offline desktop, etc.) so air-
+ * gapped deployments still get a readable preview instead of a
+ * "Preview unavailable" message — Codex P2 review on PR #12934.
+ * 2. **Mammoth-only (fallback for larger files, files where the
+ * combined CDN-doc-with-fallback would blow the cache cap, OR
+ * when the CDN path is explicitly disabled via
+ * `OFFICE_PREVIEW_DISABLE_CDN=true`)**: server-side semantic HTML
+ * conversion. Lower fidelity (flat paragraphs, no shading) but
+ * produces compact output that fits the `MAX_TEXT_CACHE_BYTES`
+ * (512 KB) cap on `attachment.text` even for large documents,
+ * and works without external network.
+ *
+ * Both paths pre-flight through `assertSafeZipSize` so a zip-bomb DOCX
+ * is rejected before either renderer touches it — mammoth's internal
+ * extraction has no decompressed-size cap and would happily inflate a
+ * sub-1MB compressed bomb to 200+ MB of XML. See SEC review on PR
+ * #12934 for the original DoS finding.
+ */
+export async function wordDocToHtml(buffer: Buffer): Promise<string> {
+ await assertSafeZipSize(buffer, { name: 'docx' });
+ /* Opt-in LibreOffice path: highest fidelity for any DOCX feature
+ * mammoth/docx-preview can't reproduce (complex tables, drawing
+ * objects, charts, embedded objects). Returns null when disabled,
+ * binary missing, conversion failed, or output exceeds the cache
+ * cap — falls through to the existing pipeline in any of those
+ * cases so a misconfiguration doesn't break previews. */
+ const lo = await tryLibreOfficePreview(buffer, 'docx', OFFICE_HTML_OUTPUT_CAP);
+ if (lo) {
+ return lo;
+ }
+ if (isOfficePreviewCdnDisabled() || buffer.length > MAX_DOCX_CDN_BINARY_BYTES) {
+ return wordDocToHtmlViaMammoth(buffer);
+ }
+ /* Render mammoth first so its sanitized output can be embedded as
+ * the iframe's air-gapped fallback. If the combined size would
+ * exceed the 512 KB cache cap, drop to mammoth-only — the user
+ * loses high-fidelity rendering but still sees the document. The
+ * size budget applies after mammoth runs because we can't know its
+ * output size from the binary size alone. */
+ const mammothBody = await renderMammothBody(buffer);
+ const cdnDoc = await wordDocToHtmlViaCdn(buffer, mammothBody);
+ if (Buffer.byteLength(cdnDoc, 'utf-8') > OFFICE_HTML_OUTPUT_CAP) {
+ return wrapAsDocument(`<article class="lc-docx">${mammothBody}</article>`, DOCX_EXTRA_CSS);
+ }
+ return cdnDoc;
+}
+
+/* =============================================================================
+ * XLSX / XLS / ODS → HTML (multi-sheet with pure-CSS tab strip)
+ * ============================================================================= */
+
+/** A workbook sheet rendered to its `<table>` HTML and metadata. */
+interface RenderedSheet {
+ name: string;
+ html: string;
+ truncated: boolean;
+ totalRows: number;
+}
+
+async function renderWorkbookSheets(
+ workbook: import('xlsx').WorkBook,
+ XLSX: typeof import('xlsx'),
+): Promise<RenderedSheet[]> {
+ const sheets: RenderedSheet[] = [];
+ for (const sheetName of workbook.SheetNames) {
+ const ws = workbook.Sheets[sheetName];
+ const ref = ws['!ref'];
+ let totalRows = 0;
+ let truncated = false;
+ if (ref) {
+ const range = XLSX.utils.decode_range(ref);
+ totalRows = range.e.r - range.s.r + 1;
+ if (totalRows > SPREADSHEET_MAX_ROWS_PER_SHEET) {
+ const cappedEnd = range.s.r + SPREADSHEET_MAX_ROWS_PER_SHEET - 1;
+ ws['!ref'] = XLSX.utils.encode_range({
+ s: range.s,
+ e: { r: cappedEnd, c: range.e.c },
+ });
+ truncated = true;
+ }
+ }
+ const html = XLSX.utils.sheet_to_html(ws, { editable: false, header: '', footer: '' });
+ sheets.push({ name: sheetName, html, truncated, totalRows });
+ }
+ return sheets;
+}
+
+/**
+ * Build a self-contained HTML document with a pure-CSS tab strip for
+ * multi-sheet workbooks. Sheet switching uses radio inputs + `:checked ~`
+ * sibling selectors so no JavaScript is needed inside the iframe.
+ */
+function renderSpreadsheetHtml(sheets: RenderedSheet[]): string {
+ if (sheets.length === 0) {
+ return wrapAsDocument('<p class="lc-banner">This workbook contains no sheets.</p>');
+ }
+
+ const tabStripCss = `
+.lc-sheet-tabs { display: flex; flex-wrap: wrap; gap: 2px; padding: 0 0 8px; border-bottom: 1px solid var(--border); margin-bottom: 12px; }
+.lc-sheet-tab-radio { position: absolute; opacity: 0; pointer-events: none; }
+.lc-sheet-tab-label {
+ cursor: pointer;
+ padding: 4px 12px;
+ border-radius: 6px 6px 0 0;
+ font-size: 0.8rem;
+ color: var(--muted);
+ border: 1px solid transparent;
+ border-bottom: none;
+ user-select: none;
+}
+.lc-sheet-tab-label:hover { color: var(--fg); background: var(--row-alt); }
+.lc-sheet-panel { display: none; }
+${sheets
+ .map(
+ (_, i) =>
+ `#lc-sheet-tab-${i}:checked ~ .lc-sheet-tabs label[for="lc-sheet-tab-${i}"] { color: var(--fg); background: var(--tab-active-bg); border-color: var(--border); font-weight: 600; }
+#lc-sheet-tab-${i}:checked ~ #lc-sheet-panel-${i} { display: block; }`,
+ )
+ .join('\n')}
+`.trim();
+
+ const radios = sheets
+ .map(
+ (_, i) =>
+ `<input type="radio" name="lc-sheet" id="lc-sheet-tab-${i}" class="lc-sheet-tab-radio"${i === 0 ? ' checked' : ''}>`,
+ )
+ .join('\n');
+
+ const tabs =
+ sheets.length > 1
+ ? `<nav class="lc-sheet-tabs">${sheets
+ .map(
+ (s, i) =>
+ `<label class="lc-sheet-tab-label" for="lc-sheet-tab-${i}">${escapeHtml(s.name)}</label>`,
+ )
+ .join('')}</nav>`
+ : '';
+
+ const panels = sheets
+ .map((s, i) => {
+ const banner = s.truncated
+ ? `<div class="lc-banner">Showing first ${SPREADSHEET_MAX_ROWS_PER_SHEET.toLocaleString()} of ${s.totalRows.toLocaleString()} rows. Download the file to see the rest.</div>`
+ : '';
+ return `<section class="lc-sheet-panel" id="lc-sheet-panel-${i}">${banner}<div class="lc-table-scroll">${s.html}</div></section>`;
+ })
+ .join('\n');
+
+ return wrapAsDocument(`${radios}\n${tabs}\n${panels}`, tabStripCss);
+}
+
+function escapeHtml(input: string): string {
+ return input
+ .replace(/&/g, '&amp;')
+ .replace(/</g, '&lt;')
+ .replace(/>/g, '&gt;')
+ .replace(/"/g, '&quot;')
+ .replace(/'/g, '&#39;');
+}
+
+/**
+ * Convert a workbook buffer (`.xlsx`, `.xls`, `.ods`) to a sandboxed HTML
+ * document. Each sheet is rendered as its own `<table>` and the document
+ * carries a pure-CSS tab strip for sheet switching.
+ *
+ * Pre-flights ZIP-backed formats (`.xlsx`/`.ods`) through
+ * `assertSafeZipSize` to reject zip bombs before SheetJS reaches them.
+ * `.xls` is a binary CFB format, not a ZIP — it doesn't have the
+ * decompression-amplification attack surface, so the safety check is
+ * skipped for it (yauzl would reject it as malformed anyway).
+ */
+export async function excelSheetToHtml(buffer: Buffer): Promise<string> {
+ /* Cheap magic-byte check so we only run the ZIP validator on actual
+ * ZIP-backed inputs. `.xls` (BIFF/CFB) starts with `D0 CF 11 E0`; ZIPs
+ * start with `PK\x03\x04`. Skipping the validator on a non-ZIP input
+ * also avoids confusing yauzl errors leaking out as ZipBombError. */
+ if (buffer.length >= 4 && buffer[0] === 0x50 && buffer[1] === 0x4b) {
+ await assertSafeZipSize(buffer, { name: 'spreadsheet' });
+ }
+ const XLSX = await import('xlsx');
+ const workbook = XLSX.read(buffer, { type: 'buffer' });
+ const sheets = await renderWorkbookSheets(workbook, XLSX);
+ /* The per-sheet HTML from `sheet_to_html` is generally well-formed but we
+ * still sanitize it (defense in depth). The chrome (tab strip, banners) is
+ * our own code and doesn't need sanitization. */
+ const sanitizedSheets = await Promise.all(
+ sheets.map(async (s) => ({ ...s, html: await sanitizeOfficeHtml(s.html) })),
+ );
+ return renderSpreadsheetHtml(sanitizedSheets);
+}
+
+/* =============================================================================
+ * CSV → HTML
+ * ============================================================================= */
+
+/** Convert a CSV buffer to a sandboxed HTML document with a single table. */
+export async function csvToHtml(buffer: Buffer): Promise<string> {
+ const XLSX = await import('xlsx');
+ const text = buffer.toString('utf-8');
+ /* `XLSX.read` with `type: 'string'` accepts CSV as well as XML/JSON
+ * formats; the default sheet name for CSV is `Sheet1` which we relabel
+ * below for a friendlier tab. */
+ const workbook = XLSX.read(text, { type: 'string', raw: true });
+ const sheets = await renderWorkbookSheets(workbook, XLSX);
+ // Single sheet for CSV — relabel to "CSV" for clarity, no tab strip emitted.
+ const sanitized = await Promise.all(
+ sheets.map(async (s) => ({ ...s, name: 'CSV', html: await sanitizeOfficeHtml(s.html) })),
+ );
+ return renderSpreadsheetHtml(sanitized);
+}
+
+/* =============================================================================
+ * PPTX → slide-list HTML
+ * ============================================================================= */
+
+interface PptxSlide {
+ number: number;
+ title: string;
+ body: string[];
+}
+
+/**
+ * Stream `ppt/slides/slide*.xml` out of a PPTX buffer using yauzl. Mirrors
+ * the anti-zip-bomb pattern used by `extractOdtContentXml` in `crud.ts` —
+ * counts real decompressed bytes mid-inflate so a falsified central-directory
+ * `uncompressedSize` cannot bypass the cap. Returns slides in slide-number
+ * order; ignores everything else in the archive.
+ *
+ * Uses `yauzl.fromBuffer` (no disk I/O) — the safety pre-flight in
+ * `assertSafeZipSize` already proved the buffer is well-formed enough
+ * to walk, and fromBuffer keeps the hot path memory-only.
+ */
+function extractPptxSlideXml(buffer: Buffer): Promise<Array<{ number: number; xml: string }>> {
+ return new Promise((resolve, reject) => {
+ yauzl.fromBuffer(buffer, { lazyEntries: true }, (err, zipfile) => {
+ if (err) {
+ return reject(err);
+ }
+ if (!zipfile) {
+ return reject(new Error('Failed to open PPTX buffer'));
+ }
+
+ let settled = false;
+ const slides: Array<{ number: number; xml: string }> = [];
+ const finish = (error: Error | null) => {
+ if (settled) {
+ return;
+ }
+ settled = true;
+ try {
+ zipfile.close();
+ } catch {
+ /* zipfile.close() is best-effort — yauzl will throw if a
+ * stream is mid-flight. We've already settled the outer
+ * promise, so swallow this so the original error (if any)
+ * isn't replaced by a close-time exception. Mirrors
+ * `assertSafeZipSize`'s defensive pattern in zipSafety.ts. */
+ }
+ if (error) {
+ reject(error);
+ } else {
+ slides.sort((a, b) => a.number - b.number);
+ resolve(slides);
+ }
+ };
+
+ zipfile.readEntry();
+ zipfile.on('entry', (entry: yauzl.Entry) => {
+ const slideMatch = entry.fileName.match(/^ppt\/slides\/slide(\d+)\.xml$/);
+ if (!slideMatch) {
+ zipfile.readEntry();
+ return;
+ }
+ if (slides.length >= PPTX_MAX_SLIDES) {
+ // Cap reached — drain remaining entries silently.
+ zipfile.readEntry();
+ return;
+ }
+ const slideNumber = Number.parseInt(slideMatch[1], 10);
+ zipfile.openReadStream(entry, (streamErr, readStream) => {
+ if (streamErr || !readStream) {
+ return finish(streamErr ?? new Error('Failed to open slide stream'));
+ }
+
+ let totalBytes = 0;
+ const chunks: Buffer[] = [];
+
+ readStream.on('data', (chunk: Buffer) => {
+ totalBytes += chunk.byteLength;
+ if (totalBytes > PPTX_MAX_ENTRY_SIZE) {
+ readStream.destroy(
+ new Error(
+ `PPTX slide${slideNumber}.xml exceeds the ${PPTX_MAX_ENTRY_SIZE / megabyte}MB decompressed limit`,
+ ),
+ );
+ return;
+ }
+ chunks.push(chunk);
+ });
+
+ readStream.on('end', () => {
+ slides.push({ number: slideNumber, xml: Buffer.concat(chunks).toString('utf-8') });
+ zipfile.readEntry();
+ });
+ readStream.on('error', (readErr: Error) => finish(readErr));
+ });
+ });
+
+ zipfile.on('end', () => finish(null));
+ zipfile.on('error', (zipErr: Error) => finish(zipErr));
+ });
+ });
+}
+
+/**
+ * Pull the visible text out of a slide XML. PPTX text lives in `<a:t>`
+ * elements, grouped by `<a:p>` (paragraph). We keep paragraph boundaries so
+ * the rendered slide preserves bullet/line structure.
+ *
+ * Pure regex (no DOMParser available in Node by default; the markup is
+ * tightly defined by the OOXML spec, so regex is robust enough for the text-
+ * only preview).
+ */
+function extractSlideText(xml: string): { title: string; body: string[] } {
+ const paragraphs: string[] = [];
+ const paragraphMatches = xml.matchAll(/<a:p\b[^>]*>([\s\S]*?)<\/a:p>/g);
+ for (const match of paragraphMatches) {
+ const innerXml = match[1];
+ const runs: string[] = [];
+ const runMatches = innerXml.matchAll(/<a:t\b[^>]*>([\s\S]*?)<\/a:t>/g);
+ for (const run of runMatches) {
+ runs.push(decodeXmlEntities(run[1]));
+ }
+ const text = runs.join('').trim();
+ if (text.length > 0) {
+ paragraphs.push(text);
+ }
+ }
+ const title = paragraphs[0] ?? '';
+ const body = paragraphs.slice(1);
+ return { title, body };
+}
+
+const XML_ENTITIES: Record<string, string> = {
+ '&lt;': '<',
+ '&gt;': '>',
+ '&amp;': '&',
+ '&quot;': '"',
+ '&apos;': "'",
+};
+function decodeXmlEntities(input: string): string {
+ return input.replace(/&(?:lt|gt|amp|quot|apos);/g, (m) => XML_ENTITIES[m] ?? m);
+}
+
+/* CSS for the slide-list view. Extracted to a constant so it can also
+ * be inlined into the CDN doc when the slide-list is embedded as a
+ * fallback there — Codex feedback / manual e2e on PR #12934 ("pptx-
+ * preview created an empty wrapper for the pptxgenjs deck"). */
+const PPTX_SLIDE_LIST_CSS = `
+.lc-pptx-list { display: flex; flex-direction: column; gap: 16px; padding: 0; margin: 0; list-style: none; }
+.lc-pptx-slide {
+ border: 1px solid var(--border);
+ border-radius: 8px;
+ padding: 16px 20px;
+ background: var(--bg);
+ position: relative;
+}
+.lc-pptx-slide-number {
+ display: inline-block;
+ font-size: 0.7rem;
+ text-transform: uppercase;
+ letter-spacing: 0.06em;
+ color: var(--muted);
+ margin-bottom: 6px;
+}
+.lc-pptx-slide-title { font-size: 1.15rem; font-weight: 600; margin: 0 0 8px; }
+.lc-pptx-slide-body { margin: 0; padding-left: 1.25em; }
+.lc-pptx-slide-body li { margin: 0.2em 0; }
+.lc-pptx-slide-empty { color: var(--muted); font-style: italic; margin: 0; }
+`.trim();
+
+/**
+ * Build the slide-list body HTML (just the `<ol>` element, no document
+ * wrapper). Used both for standalone slide-list rendering and for
+ * embedding inside the CDN doc as an air-gap / parser-failure
+ * fallback — same pattern as the mammoth fallback inside the DOCX CDN
+ * doc. Returns an empty-state banner for zero-slide decks.
+ */
+function renderPptxSlidesBody(slides: PptxSlide[]): string {
+ if (slides.length === 0) {
+ return '<p class="lc-banner">This presentation contains no readable slides.</p>';
+ }
+ const items = slides
+ .map((slide) => {
+ const titleHtml = slide.title
+ ? `<h2 class="lc-pptx-slide-title">${escapeHtml(slide.title)}</h2>`
+ : '';
+ const bodyHtml =
+ slide.body.length > 0
+ ? `<ul class="lc-pptx-slide-body">${slide.body.map((line) => `<li>${escapeHtml(line)}</li>`).join('')}</ul>`
+ : !slide.title
+ ? '<p class="lc-pptx-slide-empty">(empty slide)</p>'
+ : '';
+ return `<li class="lc-pptx-slide">
+ <span class="lc-pptx-slide-number">Slide ${slide.number}</span>
+ ${titleHtml}
+ ${bodyHtml}
+</li>`;
+ })
+ .join('\n');
+ return `<ol class="lc-pptx-list">${items}</ol>`;
+}
+
+function renderPptxSlidesHtml(slides: PptxSlide[]): string {
+ return wrapAsDocument(renderPptxSlidesBody(slides), PPTX_SLIDE_LIST_CSS);
+}
+
+/**
+ * Convert a `.pptx` buffer to a slide-list HTML document — text-only
+ * preview rendering each slide as a card (slide number, title, body
+ * bullets). Honest about NOT preserving complex visual layouts,
+ * charts, theming, or embedded media. Used as the safe fallback path
+ * when the CDN renderer is disabled, the binary exceeds the embed
+ * size cap, or the high-fidelity path can't be reached.
+ *
+ * Pre-flights through `assertSafeZipSize` so a zip-bomb PPTX can't blow
+ * up the slide-XML extraction pass.
+ */
+export async function pptxToSlideListHtml(buffer: Buffer): Promise<string> {
+ await assertSafeZipSize(buffer, { name: 'pptx' });
+ const rawSlides = await extractPptxSlideXml(buffer);
+ const slides: PptxSlide[] = rawSlides.map(({ number, xml }) => {
+ const { title, body } = extractSlideText(xml);
+ return { number, title, body };
+ });
+ return renderPptxSlidesHtml(slides);
+}
+
+/* =============================================================================
+ * PPTX CDN renderer (high-fidelity path)
+ *
+ * Mirrors the DOCX CDN architecture: embeds the binary as base64 and
+ * lets `pptx-preview` (loaded from a pinned jsdelivr URL with SRI) do
+ * the rendering inside the Sandpack iframe. The library is ISC-licensed
+ * (npm package permits commercial use) and ships a UMD bundle that
+ * exposes a global `pptxPreview.init(container, options)` →
+ * `previewer.preview(arrayBuffer)` API per the package README.
+ *
+ * Trade-offs vs the slide-list path:
+ * + Far higher visual fidelity — preserves slide layouts, theme
+ * colors, fonts, basic shape positioning, embedded images.
+ * + No server-side rendering cost; iframe sandbox isolates parser
+ * bugs from the API process.
+ * − UMD bundle is ~1.36 MB (contains echarts, tslib, lodash, uuid,
+ * jszip). Loaded once per iframe lifetime; browser-cached after.
+ * − Renderer hasn't been broadly browser-tested by us across the
+ * full spectrum of decks. See PR #12934 commit notes — fallback
+ * paths cover the cases where we hit a class of files that don't
+ * render: client-side "Preview unavailable" UX (CDN unreachable
+ * or `typeof pptxPreview === 'undefined'`) and the
+ * `OFFICE_PREVIEW_DISABLE_CDN` env-var hatch (forces the slide-
+ * list server-side).
+ */
+const PPTX_PREVIEW_CDN = {
+ pptxPreview: {
+ src: 'https://cdn.jsdelivr.net/npm/[email protected]/dist/pptx-preview.umd.js',
+ integrity: 'sha384-CwntHHT2FbwZXuCmbf6K93YaEB9xRVVLaqFJ7pdMykeQABb/3MA0sbt2lGbgi1Mr',
+ },
+} as const;
+
+/**
+ * Same 350 KB binary cap as DOCX — keeps the base64-inflated wrapped
+ * HTML under `MAX_TEXT_CACHE_BYTES` (512 KB) on `attachment.text`.
+ * PPTX files often exceed this once embedded media is involved; the
+ * dispatcher's slide-list fallback handles the larger cases.
+ */
+const MAX_PPTX_CDN_BINARY_BYTES = 350 * 1024;
+
+/**
+ * Build the CDN-rendered HTML document for a PPTX. Same wrapper shape
+ * and CSP as the DOCX equivalent — see `buildDocxCdnDocument` for the
+ * security-relevant rationale on script-src/connect-src/base-uri/etc.
+ *
+ * Slides need a fixed-size container for `pptx-preview` to render into
+ * — it computes per-slide layouts against the configured width/height
+ * rather than against the iframe's variable size. We use 960×540
+ * (16:9 standard) and let CSS scale the rendered output to fit the
+ * iframe via `transform: scale(...)`. The slides scroll vertically
+ * once the renderer paints them.
+ */
+function buildPptxCdnDocument(base64: string, slideListFallbackBody: string): string {
+ /* PPTX-specific CSP relaxations vs DOCX:
+ * - `worker-src blob:` — pptx-preview's bundled echarts dep spins up
+ * Web Workers via blob: URLs for chart rendering. Without this,
+ * workers default to `default-src 'none'`, get blocked, and
+ * echarts throws an unhandled-rejection deep inside its async
+ * pipeline that we can't catch from the bootstrap script. The
+ * visible symptom is a black iframe with the renderer half-
+ * started. Allowing blob:-only workers is the minimum surface to
+ * unblock rendering without permitting arbitrary worker URLs. */
+ /* `connect-src` allows fetches to:
+ * - `'self'`: the sandpack-static-server origin the iframe runs in
+ * (covers any same-origin fetches pptx-preview or its bundled
+ * echarts make at render time, plus same-origin sourcemap loads)
+ * - `https://cdn.jsdelivr.net`: where the renderer script came from
+ * (DevTools fetches `.min.js.map` from there to map minified
+ * line numbers; with `'none'` the console fills with CSP
+ * violations every time DevTools is open)
+ *
+ * Exfiltration risk is minimal: the iframe is cross-origin to the
+ * LibreChat host so an attacker can't read application data, and
+ * the only meaningful targets ('self' or jsdelivr) aren't useful
+ * for exfiltrating slide content to a host the attacker controls. */
+ const csp = [
+ "default-src 'none'",
+ "script-src https://cdn.jsdelivr.net 'unsafe-inline'",
+ "style-src 'unsafe-inline'",
+ 'worker-src blob:',
+ "img-src 'self' data: blob:",
+ 'font-src data:',
+ "connect-src 'self' https://cdn.jsdelivr.net",
+ "base-uri 'none'",
+ "form-action 'none'",
+ ].join('; ');
+ return `<!DOCTYPE html>
+<html lang="en">
+<head>
+<meta charset="UTF-8">
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<meta http-equiv="Content-Security-Policy" content="${csp}">
+<title>Preview</title>
+<style>
+:root { color-scheme: light dark; --bg: #ffffff; --fg: #1f2937; --muted: #6b7280; }
+@media (prefers-color-scheme: dark) { :root { --bg: #1a1a2e; --fg: #e5e7eb; --muted: #9ca3af; } }
+html, body { margin: 0; padding: 0; background: var(--bg); color: var(--fg); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
+#lc-render { padding: 16px; box-sizing: border-box; display: flex; flex-direction: column; align-items: center; gap: 16px; }
+/* Each rendered slide is wrapped post-hoc by the bootstrap script in
+ * an .lc-slide-wrap div with explicit width/height set inline. The
+ * inner slide keeps its native pixel size and gets transformed via
+ * a CSS scale so it fits the iframe width without horizontal
+ * scrolling. Without this, pptx-preview emits slides at whatever
+ * resolution it computed against (typically 960×540) and overflows
+ * narrow panels — manual e2e feedback on PR #12934. */
+.lc-slide-wrap {
+ position: relative;
+ margin: 0 auto;
+ overflow: hidden;
+ background: #ffffff;
+ box-shadow: 0 2px 8px rgba(0, 0, 0, 0.18);
+ border-radius: 4px;
+ flex-shrink: 0;
+}
+.lc-slide-wrap > * {
+ position: absolute;
+ top: 0;
+ left: 0;
+ transform-origin: top left;
+}
+#lc-fallback { padding: 16px; font-size: 14px; line-height: 1.5; color: var(--fg); }
+#lc-fallback-notice { font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--border); padding-bottom: 8px; margin: 0 0 16px; }
+.lc-pptx-loading { display: flex; align-items: center; justify-content: center; height: 60vh; color: var(--muted); font-size: 14px; }
+/* Slide-list fallback styles. Inlined here so the fallback renders
+ * with the same look as the standalone slide-list path when the CDN
+ * renderer fails. CSS variable defaults match wrapAsDocument so
+ * dark-mode borders/muted text resolve sensibly. */
+:root { --border: rgba(128, 128, 128, 0.25); --link: #2563eb; }
+@media (prefers-color-scheme: dark) { :root { --border: rgba(128, 128, 128, 0.35); --link: #60a5fa; } }
+${PPTX_SLIDE_LIST_CSS}
+</style>
+<script src="${PPTX_PREVIEW_CDN.pptxPreview.src}" integrity="${PPTX_PREVIEW_CDN.pptxPreview.integrity}" crossorigin="anonymous"></script>
+</head>
+<body>
+<div id="lc-render"><div class="lc-pptx-loading">Loading preview…</div></div>
+<div id="lc-fallback" hidden>
+ <p id="lc-fallback-notice">High-fidelity renderer unavailable or returned no slide content. Showing the simplified slide-list view below.</p>
+ ${slideListFallbackBody}
+ <details style="font-size: 12px; color: var(--muted); margin: 16px 0 0;">
+ <summary style="cursor: pointer;">Diagnostic details</summary>
+ <div id="lc-fallback-reason" style="margin-top: 6px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; word-break: break-word;"></div>
+ </details>
+</div>
+<script id="lc-doc-data" type="application/octet-stream;base64">${base64}</script>
+<script>
+(function () {
+ var settled = false;
+ function showFallback(reason) {
+ if (settled) { return; }
+ settled = true;
+ var loading = document.querySelector('.lc-pptx-loading');
+ if (loading) { loading.remove(); }
+ var container = document.getElementById('lc-render');
+ if (container) {
+ /* Hide the render slot (which may contain pptx-previews
+ * empty/black wrappers) so the slide-list fallback owns the
+ * viewport. */
+ container.hidden = true;
+ container.innerHTML = '';
+ }
+ var fallback = document.getElementById('lc-fallback');
+ var reasonText = reason ? String(reason).slice(0, 500) : 'no reason reported';
+ if (fallback) {
+ fallback.hidden = false;
+ /* Title (hover tooltip) AND inline diagnostic block. The block
+ * is folded behind a details element so the friendly message
+ * stays the primary read but the reason is one click away —
+ * no need to open DevTools to figure out why a preview failed. */
+ fallback.title = reasonText;
+ var reasonEl = document.getElementById('lc-fallback-reason');
+ if (reasonEl) { reasonEl.textContent = reasonText; }
+ }
+ /* Mirror to DevTools so power users see the reason in red without
+ * having to find and expand the details element. The console
+ * message survives even after the iframe is detached. */
+ if (typeof console !== 'undefined' && console.error) {
+ console.error('[pptx-preview] fallback fired:', reasonText);
+ }
+ }
+ function markSuccess() { settled = true; }
+ function renderedSlideCount() {
+ return container ? container.querySelectorAll('.pptx-preview-slide-wrapper').length : 0;
+ }
+
+ /* pptx-preview bundled deps (echarts, etc.) raise rejections deep
+ * inside their async pipelines that the outer Promise from
+ * previewer.preview does not always surface. Listen at the window
+ * level so we do not go silent on any of them. */
+ window.addEventListener('unhandledrejection', function (e) {
+ if (settled) { return; }
+ showFallback((e.reason && e.reason.message) || 'unhandled-rejection');
+ });
+ window.addEventListener('error', function (e) {
+ if (settled) { return; }
+ showFallback((e.error && e.error.message) || e.message || 'script-error');
+ });
+
+ if (typeof pptxPreview === 'undefined' || typeof pptxPreview.init !== 'function') {
+ showFallback('renderer-not-loaded');
+ return;
+ }
+ try {
+ var b64 = document.getElementById('lc-doc-data').textContent.trim();
+ var bytes = Uint8Array.from(atob(b64), function (c) { return c.charCodeAt(0); });
+ var container = document.getElementById('lc-render');
+ var loading = document.querySelector('.lc-pptx-loading');
+ if (loading) { loading.remove(); }
+ /* pptx-preview renders into a fixed pixel size; we keep it at the
+ * native 16:9 default so its internal layout calculations are
+ * right, then post-process to scale each slide to the iframe's
+ * width via CSS transform. */
+ var SLIDE_W = 960, SLIDE_H = 540;
+ /* Hide the container while pptx-preview renders so the user
+ * doesnt see the unscaled 960px-wide flash. We reveal once
+ * wrap+scale has settled. */
+ container.style.visibility = 'hidden';
+ var previewer = pptxPreview.init(container, { width: SLIDE_W, height: SLIDE_H });
+
+ function availableWidth() {
+ /* clientWidth includes the 16px padding on each side via
+ * box-sizing:border-box; we subtract both so the wrap exactly
+ * fills the content box without spilling into padding. */
+ var w = (container.clientWidth || window.innerWidth) - 32;
+ return w > 0 ? w : 600;
+ }
+
+ /* Width-only scale: each slide divides the panel width by its
+ * actual rendered native width, so we always fill the panel
+ * horizontally. No upscale cap — pptx-preview rasterizes against
+ * the init canvas (960×540), text/charts are vector or canvas-
+ * rendered, so they stay legible when scaled up.
+ *
+ * NOTE: a previous iteration tried min(width-fit, height-fit) to
+ * also fill the panel vertically. That broke pptx-previews
+ * rendering (slides came out as solid black rectangles). The
+ * librarys internal layout interacts unpredictably with min-height
+ * 100vh on the document root and viewport-derived height
+ * calculations during its CSS injection step. Sticking to
+ * width-only keeps rendering reliable; vertical empty space below
+ * a short deck shows the bodys bg color, which inherits from
+ * var(--bg) and matches the panel theme — manual e2e feedback on
+ * PR #12934. */
+ function scaleFor(nativeW) {
+ return availableWidth() / (nativeW || SLIDE_W);
+ }
+
+ /* Wrap each rendered slide and apply the scale. Called ONCE,
+ * after pptx-preview is done — wrapping during streaming would
+ * move slides out from under the librarys references and break
+ * its internal state. */
+ function wrapSlides() {
+ var children = Array.prototype.slice.call(container.children);
+ for (var i = 0; i < children.length; i++) {
+ var slide = children[i];
+ if (!slide.classList || slide.classList.contains('lc-slide-wrap') || slide.classList.contains('lc-pptx-loading')) {
+ continue;
+ }
+ /* Cache the slides actual rendered size BEFORE applying any
+ * transform — measurements after a CSS scale no longer reflect
+ * native pixels and would feed back into wrong sizing on
+ * resize. */
+ var nativeW = slide.offsetWidth || SLIDE_W;
+ var nativeH = slide.offsetHeight || SLIDE_H;
+ if (slide.dataset) {
+ slide.dataset.lcNativeW = String(nativeW);
+ slide.dataset.lcNativeH = String(nativeH);
+ }
+ var scale = scaleFor(nativeW);
+ var wrap = document.createElement('div');
+ wrap.className = 'lc-slide-wrap';
+ wrap.style.width = (nativeW * scale) + 'px';
+ wrap.style.height = (nativeH * scale) + 'px';
+ slide.style.transformOrigin = 'top left';
+ slide.style.transform = 'scale(' + scale + ')';
+ container.insertBefore(wrap, slide);
+ wrap.appendChild(slide);
+ }
+ }
+
+ /* Re-fit existing wraps on iframe resize (panel drag, window
+ * resize). Reads the cached native dimensions from the slides
+ * dataset so we never re-measure an already-scaled box. */
+ function refit() {
+ var wraps = container.querySelectorAll('.lc-slide-wrap');
+ for (var i = 0; i < wraps.length; i++) {
+ var wrap = wraps[i];
+ var inner = wrap.firstElementChild;
+ if (!inner || !inner.dataset) { continue; }
+ var nativeW = parseFloat(inner.dataset.lcNativeW) || SLIDE_W;
+ var nativeH = parseFloat(inner.dataset.lcNativeH) || SLIDE_H;
+ var scale = scaleFor(nativeW);
+ wrap.style.width = (nativeW * scale) + 'px';
+ wrap.style.height = (nativeH * scale) + 'px';
+ inner.style.transform = 'scale(' + scale + ')';
+ }
+ }
+
+ /* Detect the silent-failure case: pptx-preview resolves without
+ * rendering any real slide nodes. Common with pptxgenjs-generated
+ * PPTXs — the parser creates an empty host wrapper, but never
+ * populates pptx-preview slide nodes with slide content.
+ * Manual e2e on PR #12934. */
+ function hasRenderedContent() {
+ var slides = container.querySelectorAll('.pptx-preview-slide-wrapper');
+ if (slides.length === 0) { return false; }
+ for (var i = 0; i < slides.length; i++) {
+ if (slides[i].children.length > 0 || (slides[i].textContent || '').trim().length > 0) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ function finalize() {
+ wrapSlides();
+ if (!hasRenderedContent()) {
+ showFallback('renderer-empty-slide-list');
+ return;
+ }
+ container.style.visibility = 'visible';
+ /* Resize observer set up AFTER the wrap so it doesnt fire
+ * during the wrap pass. */
+ if (typeof ResizeObserver !== 'undefined') {
+ new ResizeObserver(refit).observe(document.body);
+ } else {
+ window.addEventListener('resize', refit);
+ }
+ markSuccess();
+ }
+
+ var result = previewer.preview(bytes.buffer);
+ if (result && typeof result.then === 'function') {
+ result
+ .then(function (pptx) {
+ if (!pptx || !pptx.slides || pptx.slides.length === 0) {
+ showFallback('renderer-empty-slide-list');
+ return;
+ }
+ finalize();
+ })
+ .catch(function (err) { showFallback(err && err.message); });
+ }
+ /* Safety net: if 8 seconds in the preview promise hasnt resolved,
+ * accept whatever pptx-preview managed to render and apply the
+ * wrap+scale to it. Empty container → fallback. */
+ setTimeout(function () {
+ if (settled) { return; }
+ if (renderedSlideCount() > 0) {
+ finalize();
+ return;
+ }
+ showFallback('renderer-timeout');
+ }, 8000);
+ } catch (err) {
+ showFallback(err && err.message);
+ }
+})();
+</script>
+</body>
+</html>`;
+}
+
+/**
+ * Render the slide-list body (just the `<ol>` HTML) for a buffer.
+ * Used by `pptxToHtml` to embed as a fallback inside the CDN doc and
+ * by `pptxToSlideListHtmlInternal` for the standalone path. */
+async function renderPptxSlidesBodyForBuffer(buffer: Buffer): Promise<string> {
+ const rawSlides = await extractPptxSlideXml(buffer);
+ const slides: PptxSlide[] = rawSlides.map(({ number, xml }) => {
+ const { title, body } = extractSlideText(xml);
+ return { number, title, body };
+ });
+ return renderPptxSlidesBody(slides);
+}
+
+async function pptxToHtmlViaCdn(buffer: Buffer, slideListFallbackBody: string): Promise<string> {
+ return buildPptxCdnDocument(buffer.toString('base64'), slideListFallbackBody);
+}
+
+/**
+ * Convert a `.pptx` buffer to a sandboxed HTML document. Two render
+ * paths, chosen by file size and the `OFFICE_PREVIEW_DISABLE_CDN`
+ * escape hatch:
+ *
+ * 1. **CDN-rendered (default for files ≤ 350 KB binary)**: embeds
+ * the binary as base64 and lets `pptx-preview` render it inside
+ * the Sandpack iframe. High visual fidelity.
+ * 2. **Slide-list (fallback)**: server-side text-only extraction
+ * (each slide → card with title + bullets). Lower fidelity but
+ * compact, network-independent, and proven against arbitrary
+ * decks. Triggered when the binary exceeds the embed cap, when
+ * `OFFICE_PREVIEW_DISABLE_CDN=true` is set (air-gapped /
+ * filtered networks), or when the CDN script fails to load
+ * (handled iframe-side).
+ *
+ * Pre-flights through `assertSafeZipSize` so a zip-bomb PPTX is
+ * rejected before either renderer touches it.
+ */
+export async function pptxToHtml(buffer: Buffer): Promise<string> {
+ await assertSafeZipSize(buffer, { name: 'pptx' });
+ /* Opt-in LibreOffice path: PDF rendering of slides preserves layout,
+ * fonts, charts, and embedded objects far better than the slide-list
+ * fallback or even pptx-preview's canvas approach. Falls through on
+ * any error per the contract in `tryLibreOfficePreview`. */
+ const lo = await tryLibreOfficePreview(buffer, 'pptx', OFFICE_HTML_OUTPUT_CAP);
+ if (lo) {
+ return lo;
+ }
+ if (isOfficePreviewCdnDisabled() || buffer.length > MAX_PPTX_CDN_BINARY_BYTES) {
+ return pptxToSlideListHtmlInternal(buffer);
+ }
+ /* Render the slide-list first so we can embed it inside the CDN doc
+ * as a runtime fallback. pptx-preview is unreliable on PPTXs not
+ * generated by Microsoft PowerPoint (e.g. pptxgenjs decks) — it
+ * silently produces empty wrappers. The iframe bootstrap detects
+ * the empty-render case and reveals this slide-list fallback so the
+ * user always gets readable content. Manual e2e on PR #12934. */
+ const slideListBody = await renderPptxSlidesBodyForBuffer(buffer);
+ const cdnDoc = await pptxToHtmlViaCdn(buffer, slideListBody);
+ /* Combined size budget: if base64 binary + slide-list fallback +
+ * wrapper would exceed the cache cap, drop CDN entirely and ship
+ * the slide-list standalone. Same pattern as the DOCX dispatcher's
+ * size budget. */
+ if (Buffer.byteLength(cdnDoc, 'utf-8') > OFFICE_HTML_OUTPUT_CAP) {
+ return pptxToSlideListHtmlInternal(buffer);
+ }
+ return cdnDoc;
+}
+
+/**
+ * Slide-list path that skips its own `assertSafeZipSize` call — used
+ * by `pptxToHtml` after the dispatcher has already validated the
+ * buffer. `pptxToSlideListHtml` (public, validating) stays the entry
+ * point for tests and direct callers.
+ */
+async function pptxToSlideListHtmlInternal(buffer: Buffer): Promise<string> {
+ const rawSlides = await extractPptxSlideXml(buffer);
+ const slides: PptxSlide[] = rawSlides.map(({ number, xml }) => {
+ const { title, body } = extractSlideText(xml);
+ return { number, title, body };
+ });
+ return renderPptxSlidesHtml(slides);
+}
+
+/**
+ * Test-only re-exports. Each path has distinct visual-fidelity and
+ * size characteristics that warrant direct testing rather than
+ * round-tripping through the size-based dispatcher with synthetically
+ * padded fixtures. Not part of the public API — callers in production
+ * code should always go through `wordDocToHtml` / `pptxToHtml`.
+ */
+export const _internal = {
+ wordDocToHtmlViaCdn,
+ wordDocToHtmlViaMammoth,
+ MAX_DOCX_CDN_BINARY_BYTES,
+ OFFICE_HTML_OUTPUT_CAP,
+ pptxToHtmlViaCdn,
+ MAX_PPTX_CDN_BINARY_BYTES,
+};
+
+/* =============================================================================
+ * Dispatcher
+ * ============================================================================= */
+
+const DOCX_MIME = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
+const PPTX_MIME = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
+const ODS_MIME = 'application/vnd.oasis.opendocument.spreadsheet';
+const CSV_MIME_PATTERN = /^(text\/csv|application\/csv|text\/comma-separated-values)$/i;
+
+function extensionOf(name: string): string {
+ const dot = name.lastIndexOf('.');
+ if (dot < 0 || dot === name.length - 1) {
+ return '';
+ }
+ return name.slice(dot + 1).toLowerCase();
+}
+
+/**
+ * Strip MIME parameters (`; charset=utf-8`, `; boundary=...`) and
+ * lowercase the result so exact-match MIME comparisons survive servers
+ * and tool sandboxes that decorate Content-Type headers. Without this,
+ * an extensionless `text/csv; charset=utf-8` would slip past the bucket
+ * predicate (returning null) while the client's MIME router — which
+ * already strips parameters via `baseMime` — would route it to
+ * SPREADSHEET expecting an HTML body that the backend never produced.
+ * Mirrors `client/src/utils/artifacts.ts:baseMime`.
+ */
+function baseMime(mime: string): string {
+ if (!mime) {
+ return '';
+ }
+ const semi = mime.indexOf(';');
+ return (semi < 0 ? mime : mime.slice(0, semi)).trim().toLowerCase();
+}
+
+/**
+ * The four buckets the dispatcher can route to. Used by both
+ * `bufferToOfficeHtml` (which actually renders) and `officeHtmlBucket`
+ * (which the upstream gate in `extract.ts` calls to decide whether to
+ * invoke the dispatcher at all). Sharing one source of truth prevents the
+ * gate from going out of sync with the dispatcher — a previous version
+ * had a narrower extension-only gate that missed extensionless office
+ * files identified solely by MIME (e.g. a tool emitting `data` with
+ * `text/csv`), which then routed to the SPREADSHEET bucket on the
+ * client expecting full HTML and got raw text instead.
+ */
+export type OfficeHtmlBucket = 'docx' | 'spreadsheet' | 'csv' | 'pptx';
+
+const OFFICE_EXTENSIONS: Record<string, OfficeHtmlBucket> = {
+ docx: 'docx',
+ csv: 'csv',
+ xlsx: 'spreadsheet',
+ xls: 'spreadsheet',
+ ods: 'spreadsheet',
+ pptx: 'pptx',
+};
+
+/**
+ * Classify a file by extension OR MIME into the bucket the office HTML
+ * dispatcher would route it to, or `null` if no producer applies.
+ *
+ * **Extension always wins** over MIME for any known office extension.
+ * Without this precedence, a `deck.pptx` whose `Content-Type` was
+ * mislabeled `text/csv` (a known footgun when tool sandboxes ship
+ * generic MIMEs) would be routed to `csvToHtml` and try to parse a
+ * binary PPTX as CSV — yielding garbage at best, a parse exception at
+ * worst.
+ *
+ * **MIME fallback fires ONLY when the extension is empty.** This
+ * mirrors the client's `detectArtifactTypeFromFile` precedence: the
+ * client routes any file with a known non-office extension (`.txt`,
+ * `.md`, `.json`, etc.) to its non-office bucket REGARDLESS of MIME.
+ * If the server produced office HTML for `.txt + DOCX MIME`, the
+ * client would route by extension to PLAIN_TEXT, escape the HTML
+ * through the markdown viewer, and the user would see raw `<html>...`
+ * markup instead of the rendered preview. Codex P2 review on PR
+ * #12934 — symmetric server/client extension precedence is the only
+ * way to avoid this kind of mismatch without a shared routing table.
+ *
+ * Trade-off: a true DOCX renamed to `myfile.bin` + DOCX MIME no longer
+ * routes through office HTML on the server (it falls to the existing
+ * extract path → `textFormat: 'text'` → client renders raw bytes via
+ * PLAIN_TEXT). Acceptable: extension-vs-MIME mismatch is user error,
+ * and the previous behavior would have surfaced as a security-gate
+ * fall-through to PLAIN_TEXT on the client anyway (no preview either
+ * way; the new behavior just avoids producing HTML the client can't
+ * use).
+ */
+export function officeHtmlBucket(name: string, mimeType: string): OfficeHtmlBucket | null {
+ const ext = extensionOf(name);
+ const byExtension = OFFICE_EXTENSIONS[ext];
+ if (byExtension) {
+ return byExtension;
+ }
+ if (ext !== '') {
+ /* Non-empty non-office extension wins — see contract above. */
+ return null;
+ }
+ /* MIME fallback for genuinely extensionless files (tool-emitted
+ * blobs with generic names, etc.). Strip parameters first —
+ * Content-Type headers routinely carry `; charset=utf-8` or
+ * `; boundary=...` decorations that would otherwise fail exact-
+ * match comparisons. */
+ const normalized = baseMime(mimeType);
+ if (normalized === DOCX_MIME) {
+ return 'docx';
+ }
+ if (CSV_MIME_PATTERN.test(normalized)) {
+ return 'csv';
+ }
+ if (excelMimeTypes.test(normalized) || normalized === ODS_MIME) {
+ return 'spreadsheet';
+ }
+ if (normalized === PPTX_MIME) {
+ return 'pptx';
+ }
+ return null;
+}
+
+/**
+ * Route an office-format buffer to the matching HTML producer. Returns `null`
+ * if the file isn't a recognized office type — caller should fall through to
+ * its existing text/binary handling.
+ */
+export async function bufferToOfficeHtml(
+ buffer: Buffer,
+ name: string,
+ mimeType: string,
+): Promise<string | null> {
+ const bucket = officeHtmlBucket(name, mimeType);
+ switch (bucket) {
+ case 'docx':
+ return wordDocToHtml(buffer);
+ case 'csv':
+ return csvToHtml(buffer);
+ case 'spreadsheet':
+ return excelSheetToHtml(buffer);
+ case 'pptx':
+ return pptxToHtml(buffer);
+ default:
+ return null;
+ }
+}
diff --git a/packages/api/src/files/documents/libreoffice.spec.ts b/packages/api/src/files/documents/libreoffice.spec.ts
new file mode 100644
index 0000000000..449ade21bc
--- /dev/null
+++ b/packages/api/src/files/documents/libreoffice.spec.ts
@@ -0,0 +1,385 @@
+import { spawnSync } from 'child_process';
+import * as fs from 'fs';
+import * as path from 'path';
+import {
+ _resetLibreOfficeProbeCache,
+ buildPdfEmbedDocument,
+ convertOfficeToPdf,
+ isLibreOfficeEnabled,
+ isLibreOfficeEnabledFor,
+ LibreOfficeConversionError,
+ LibreOfficeUnavailableError,
+ LIBREOFFICE_TIMEOUT_MS,
+ MAX_LIBREOFFICE_PDF_BYTES,
+ probeLibreOfficeBinary,
+ tryLibreOfficePreview,
+} from './libreoffice';
+
+/* Detect whether the host has a LibreOffice binary so the integration
+ * tests below can conditionally engage. Most CI runners don't have
+ * LibreOffice installed (it's a 500 MB dependency); we still exercise
+ * the env-gating, the wrapper builder, and the failure-fallthrough
+ * contract in those environments. */
+function hasLibreOfficeOnPath(): boolean {
+ for (const candidate of ['soffice', 'libreoffice']) {
+ const result = spawnSync(candidate, ['--version'], { stdio: 'ignore' });
+ if (result.status === 0) {
+ return true;
+ }
+ }
+ return false;
+}
+const LIBREOFFICE_INSTALLED = hasLibreOfficeOnPath();
+const itIfLibreOffice = LIBREOFFICE_INSTALLED ? it : it.skip;
+
+/* Fixtures sit next to the source files (matches `html.spec.ts` resolution). */
+const FIXTURES_DIR = __dirname;
+function readFixture(name: string): Buffer {
+ return fs.readFileSync(path.join(FIXTURES_DIR, name));
+}
+
+describe('libreoffice (env gating + wrapper)', () => {
+ const ORIGINAL_FLAG = process.env.OFFICE_PREVIEW_LIBREOFFICE;
+
+ afterEach(() => {
+ _resetLibreOfficeProbeCache();
+ if (ORIGINAL_FLAG === undefined) {
+ delete process.env.OFFICE_PREVIEW_LIBREOFFICE;
+ } else {
+ process.env.OFFICE_PREVIEW_LIBREOFFICE = ORIGINAL_FLAG;
+ }
+ });
+
+ describe('isLibreOfficeEnabled (any-format check)', () => {
+ it.each([['true'], ['1'], ['yes'], ['TRUE'], ['Yes'], [' true ']])(
+ 'returns true for %j (truthy = all formats enabled)',
+ (value) => {
+ process.env.OFFICE_PREVIEW_LIBREOFFICE = value;
+ expect(isLibreOfficeEnabled()).toBe(true);
+ },
+ );
+
+ it.each([['false'], ['0'], ['no'], ['']])('returns false for %j', (value) => {
+ process.env.OFFICE_PREVIEW_LIBREOFFICE = value;
+ expect(isLibreOfficeEnabled()).toBe(false);
+ });
+
+ it('returns false when the env var is unset', () => {
+ delete process.env.OFFICE_PREVIEW_LIBREOFFICE;
+ expect(isLibreOfficeEnabled()).toBe(false);
+ });
+
+ it('returns true for a comma-separated format list (any format counts)', () => {
+ process.env.OFFICE_PREVIEW_LIBREOFFICE = 'pptx';
+ expect(isLibreOfficeEnabled()).toBe(true);
+ process.env.OFFICE_PREVIEW_LIBREOFFICE = 'pptx,docx';
+ expect(isLibreOfficeEnabled()).toBe(true);
+ });
+ });
+
+ describe('isLibreOfficeEnabledFor (per-format opt-in)', () => {
+ /* Per-format gating lets operators trade off cold-start latency
+ * against fidelity per format. DOCX renders ~instantly via
+ * docx-preview, but PPTX needs LibreOffice for any reasonable
+ * fidelity (pptx-preview chokes on pptxgenjs decks). Operators
+ * should be able to enable LibreOffice for PPTX only. */
+ it.each([
+ ['true', 'pptx', true],
+ ['true', 'docx', true],
+ ['true', 'xlsx', true],
+ ['1', 'pptx', true],
+ ['yes', 'docx', true],
+ ['false', 'pptx', false],
+ ['', 'pptx', false],
+ ['pptx', 'pptx', true],
+ ['pptx', 'docx', false],
+ ['docx', 'pptx', false],
+ ['docx', 'docx', true],
+ ['pptx,docx', 'pptx', true],
+ ['pptx,docx', 'docx', true],
+ ['pptx,docx', 'xlsx', false],
+ [' pptx , docx ', 'pptx', true], // whitespace-tolerant
+ ['PPTX', 'pptx', true], // case-insensitive
+ ['pptx, ,docx', 'docx', true], // empty list entries dropped
+ ['pptx, ,docx', 'pptx', true],
+ ['pptx, ,docx', 'xlsx', false],
+ ])('env=%j format=%j → %s', (envValue, format, expected) => {
+ process.env.OFFICE_PREVIEW_LIBREOFFICE = envValue;
+ expect(isLibreOfficeEnabledFor(format)).toBe(expected);
+ });
+
+ it('returns false for any format when the env var is unset', () => {
+ delete process.env.OFFICE_PREVIEW_LIBREOFFICE;
+ expect(isLibreOfficeEnabledFor('pptx')).toBe(false);
+ expect(isLibreOfficeEnabledFor('docx')).toBe(false);
+ expect(isLibreOfficeEnabledFor('xlsx')).toBe(false);
+ });
+
+ it('matches format names case-insensitively', () => {
+ process.env.OFFICE_PREVIEW_LIBREOFFICE = 'pptx';
+ expect(isLibreOfficeEnabledFor('PPTX')).toBe(true);
+ expect(isLibreOfficeEnabledFor('Pptx')).toBe(true);
+ });
+ });
+
+ describe('buildPdfEmbedDocument', () => {
+ /* The wrapper structure is the security-relevant surface. These
+ * tests lock down the CSP, the iframe shape, and the fallback
+ * contract — independent of whether LibreOffice is actually
+ * installed. Same posture as the docx-preview / pptx-preview CDN
+ * wrappers. */
+ const FAKE_PDF_B64 = 'JVBERi0xLjQK'; // "%PDF-1.4\n" base64
+
+ it('emits a complete sandboxed HTML document that renders via pdf.js to canvas', () => {
+ const html = buildPdfEmbedDocument(FAKE_PDF_B64);
+ expect(html).toMatch(/^<!DOCTYPE html>/);
+ expect(html).toContain('<title>Preview</title>');
+ /* PDF bytes embedded as a base64 data block — pdf.js decodes
+ * them at runtime and renders to canvas. We do NOT use any
+ * `<iframe src="data:application/pdf;...">` or `src="blob:...">`
+ * pattern because Chrome blocks BOTH data: AND blob: PDF
+ * navigations in sandboxed iframes (the built-in PDF viewer
+ * requires a top-level browsing context). The Sandpack host
+ * iframe is sandboxed, so neither approach renders. PDF.js
+ * draws to canvas which works in any context. Manual e2e on
+ * PR #12934. */
+ expect(html).toContain('id="lc-pdf-data"');
+ expect(html).toContain(FAKE_PDF_B64);
+ expect(html).toContain('id="lc-render"');
+ /* Negative assertions: we MUST NOT have any nested iframe
+ * navigation to a PDF URL. A future "let's just embed the PDF
+ * natively" rewrite can't silently re-introduce the Chrome
+ * block. */
+ expect(html).not.toMatch(/src="data:application\/pdf/);
+ expect(html).not.toMatch(/src="blob:[^"]*application\/pdf/);
+ expect(html).not.toMatch(/<iframe[^>]+id="lc-pdf"/);
+ /* PDF.js loaded from CDN. */
+ expect(html).toContain('cdn.jsdelivr.net/npm/pdfjs-dist@');
+ expect(html).toContain('pdfjsLib.getDocument');
+ expect(html).toContain('GlobalWorkerOptions.workerSrc');
+ expect(html).toContain('page.render');
+ });
+
+ it('CSP allows pdf.js script + worker from jsdelivr; locks down everything else', () => {
+ const html = buildPdfEmbedDocument(FAKE_PDF_B64);
+ const cspMatch = html.match(/<meta http-equiv="Content-Security-Policy" content="([^"]+)">/);
+ expect(cspMatch).not.toBeNull();
+ const csp = cspMatch![1];
+ expect(csp).toMatch(/default-src 'none'/);
+ /* pdf.js needs its main script + worker. Both come from the
+ * same jsdelivr host. */
+ expect(csp).toMatch(/script-src https:\/\/cdn\.jsdelivr\.net 'unsafe-inline'/);
+ expect(csp).toMatch(/worker-src[^;]*https:\/\/cdn\.jsdelivr\.net/);
+ expect(csp).toMatch(/worker-src[^;]*\bblob:/);
+ /* Negative assertions for the previous-iteration approaches:
+ * no `frame-src` (no nested iframes anymore), no PDF data: or
+ * blob: navigation paths. */
+ expect(csp).not.toMatch(/frame-src/);
+ /* No outbound HTTP from the rendered iframe — pdf.js doesn't
+ * fetch anything (PDF bytes are inline, fonts subset-embedded
+ * by LibreOffice). */
+ expect(csp).toMatch(/connect-src 'none'/);
+ expect(csp).not.toMatch(/unsafe-eval/);
+ expect(csp).toMatch(/base-uri 'none'/);
+ expect(csp).toMatch(/form-action 'none'/);
+ });
+
+ it('exposes a fallback message + diagnostic disclosure when pdf.js fails', () => {
+ const html = buildPdfEmbedDocument(FAKE_PDF_B64);
+ expect(html).toContain('id="lc-fallback"');
+ expect(html).toContain('PDF preview unavailable');
+ expect(html).toContain('id="lc-fallback-reason"');
+ expect(html).toContain('Diagnostic details');
+ /* Multiple failure paths feed showFallback: pdf.js not loaded,
+ * unhandled rejection from the parser, sync error from the
+ * bootstrap, render timeout. */
+ expect(html).toContain("typeof pdfjsLib === 'undefined'");
+ expect(html).toContain("addEventListener('unhandledrejection'");
+ expect(html).toContain('pdf-render-timeout');
+ expect(html).toContain('15000');
+ /* Reasons logged to console.error for power-user debugging. */
+ expect(html).toContain("console.error('[libreoffice-pdf] fallback fired:'");
+ });
+
+ it('embeds large base64 payloads inside the data block without escaping issues', () => {
+ /* The base64 alphabet (A-Za-z0-9+/=) contains no characters that
+ * could break out of `<script type="application/octet-stream;
+ * base64">...</script>`. Sanity-check the data round-trip. */
+ const big = 'A'.repeat(100_000);
+ const html = buildPdfEmbedDocument(big);
+ const dataBlock = html.match(
+ /<script id="lc-pdf-data" type="application\/octet-stream;base64">([^<]+)<\/script>/,
+ );
+ expect(dataBlock).not.toBeNull();
+ expect(dataBlock![1]).toBe(big);
+ });
+ });
+
+ describe('tryLibreOfficePreview (gating + fallthrough contract)', () => {
+ it('returns null when the env flag is unset (default behavior)', async () => {
+ delete process.env.OFFICE_PREVIEW_LIBREOFFICE;
+ const buf = readFixture('sample.docx');
+ const out = await tryLibreOfficePreview(buf, 'docx', 512 * 1024);
+ expect(out).toBeNull();
+ });
+
+ it('returns null when env flag is "false"', async () => {
+ process.env.OFFICE_PREVIEW_LIBREOFFICE = 'false';
+ const buf = readFixture('sample.docx');
+ const out = await tryLibreOfficePreview(buf, 'docx', 512 * 1024);
+ expect(out).toBeNull();
+ });
+
+ it('never throws — falls through to null on any conversion failure', async () => {
+ /* Even if the binary IS available, a malformed buffer should
+ * cause `convertOfficeToPdf` to throw and `tryLibreOfficePreview`
+ * to swallow it. The dispatcher pipeline takes over from there. */
+ process.env.OFFICE_PREVIEW_LIBREOFFICE = 'true';
+ const garbage = Buffer.from('this-is-definitely-not-a-docx');
+ let threw = false;
+ try {
+ const out = await tryLibreOfficePreview(garbage, 'docx', 512 * 1024);
+ expect(out).toBeNull();
+ } catch {
+ threw = true;
+ }
+ expect(threw).toBe(false);
+ });
+ });
+
+ describe('probeLibreOfficeBinary', () => {
+ it("caches the probe result so we don't re-spawn `--version` on every call", async () => {
+ _resetLibreOfficeProbeCache();
+ const first = await probeLibreOfficeBinary();
+ const second = await probeLibreOfficeBinary();
+ /* Reference equality — second call returns the cached object. */
+ expect(second).toBe(first);
+ });
+
+ it('returns availability=false with a reason when the binary is missing', async () => {
+ _resetLibreOfficeProbeCache();
+ /* Force the probe to miss by clobbering PATH so neither
+ * `soffice` nor `libreoffice` resolves. */
+ const originalPath = process.env.PATH;
+ process.env.PATH = '/nonexistent';
+ try {
+ const probe = await probeLibreOfficeBinary();
+ expect(probe.available).toBe(false);
+ expect(probe.binary).toBeNull();
+ expect(probe.reason).toMatch(/found on .?PATH/i);
+ } finally {
+ process.env.PATH = originalPath;
+ _resetLibreOfficeProbeCache();
+ }
+ });
+ });
+
+ describe('error tagging', () => {
+ it('LibreOfficeUnavailableError preserves the .name tag for callers', () => {
+ const err = new LibreOfficeUnavailableError('binary missing');
+ expect(err.name).toBe('LibreOfficeUnavailableError');
+ expect(err).toBeInstanceOf(Error);
+ });
+
+ it('LibreOfficeConversionError carries optional stderr context', () => {
+ const err = new LibreOfficeConversionError('exit 1', 'soffice: oops');
+ expect(err.name).toBe('LibreOfficeConversionError');
+ expect(err.stderr).toBe('soffice: oops');
+ });
+
+ it('exports tunable subprocess limits so tests can read documented values', () => {
+ expect(LIBREOFFICE_TIMEOUT_MS).toBe(30_000);
+ expect(MAX_LIBREOFFICE_PDF_BYTES).toBe(50 * 1024 * 1024);
+ });
+ });
+});
+
+/* eslint-disable jest/no-standalone-expect -- the `itIfLibreOffice` alias
+ * collapses to `it.skip` when LibreOffice isn't installed; the lint rule
+ * doesn't realize the `expect`s below are still scoped to a real test
+ * block via the alias. Disabling locally is cleaner than restructuring
+ * to satisfy a static check that's wrong about this pattern. */
+describe('libreoffice integration (skipped unless LibreOffice is on $PATH)', () => {
+ /* These tests engage only when `soffice` (or `libreoffice`) is
+ * actually installed. Local dev with the binary present runs them;
+ * stock CI runners skip. The skip is per-test rather than describe-
+ * level so a printout makes the gating visible. */
+ const ORIGINAL_FLAG = process.env.OFFICE_PREVIEW_LIBREOFFICE;
+
+ beforeEach(() => {
+ _resetLibreOfficeProbeCache();
+ });
+
+ afterEach(() => {
+ if (ORIGINAL_FLAG === undefined) {
+ delete process.env.OFFICE_PREVIEW_LIBREOFFICE;
+ } else {
+ process.env.OFFICE_PREVIEW_LIBREOFFICE = ORIGINAL_FLAG;
+ }
+ });
+
+ itIfLibreOffice('probeLibreOfficeBinary detects the installed binary', async () => {
+ const probe = await probeLibreOfficeBinary();
+ expect(probe.available).toBe(true);
+ expect(probe.binary).toMatch(/^(soffice|libreoffice)$/);
+ expect(probe.versionLine).toMatch(/LibreOffice/i);
+ });
+
+ itIfLibreOffice(
+ 'convertOfficeToPdf converts a DOCX to PDF bytes',
+ async () => {
+ const buf = readFixture('sample.docx');
+ const pdf = await convertOfficeToPdf(buf, 'docx');
+ /* PDF magic bytes: 25 50 44 46 == "%PDF". */
+ expect(pdf.subarray(0, 4).toString('ascii')).toBe('%PDF');
+ expect(pdf.length).toBeGreaterThan(500);
+ },
+ 35_000,
+ );
+
+ itIfLibreOffice(
+ 'convertOfficeToPdf converts a PPTX to PDF bytes',
+ async () => {
+ const buf = readFixture('sample.pptx');
+ const pdf = await convertOfficeToPdf(buf, 'pptx');
+ expect(pdf.subarray(0, 4).toString('ascii')).toBe('%PDF');
+ },
+ 35_000,
+ );
+
+ itIfLibreOffice(
+ 'tryLibreOfficePreview produces a PDF embed doc when env=true and binary is available',
+ async () => {
+ process.env.OFFICE_PREVIEW_LIBREOFFICE = 'true';
+ _resetLibreOfficeProbeCache();
+ const buf = readFixture('sample.docx');
+ const out = await tryLibreOfficePreview(buf, 'docx', 512 * 1024);
+ expect(out).not.toBeNull();
+ expect(out!).toMatch(/^<!DOCTYPE html>/);
+ /* PDF bytes embedded as a base64 data block; pdf.js renders to
+ * canvas (Chrome blocks both data: and blob: PDF navigations
+ * in sandboxed iframes — the canvas path is the only thing
+ * that works in our context). */
+ expect(out!).toContain('id="lc-pdf-data"');
+ expect(out!).toContain('pdfjsLib.getDocument');
+ expect(Buffer.byteLength(out!, 'utf-8')).toBeLessThanOrEqual(512 * 1024);
+ },
+ 35_000,
+ );
+
+ itIfLibreOffice(
+ 'returns null when the embedded PDF would exceed the output cap',
+ async () => {
+ /* Force the cap below the smallest possible PDF so the size
+ * check trips. The conversion still runs (verifying the
+ * subprocess works) but `tryLibreOfficePreview` declines to
+ * emit oversized output and the dispatcher falls through. */
+ process.env.OFFICE_PREVIEW_LIBREOFFICE = 'true';
+ _resetLibreOfficeProbeCache();
+ const buf = readFixture('sample.docx');
+ const out = await tryLibreOfficePreview(buf, 'docx', 100);
+ expect(out).toBeNull();
+ },
+ 35_000,
+ );
+});
diff --git a/packages/api/src/files/documents/libreoffice.ts b/packages/api/src/files/documents/libreoffice.ts
new file mode 100644
index 0000000000..aedbf624d1
--- /dev/null
+++ b/packages/api/src/files/documents/libreoffice.ts
@@ -0,0 +1,556 @@
+import { spawn } from 'child_process';
+import { mkdtemp, readFile, rm, writeFile } from 'fs/promises';
+import { tmpdir } from 'os';
+import { join } from 'path';
+
+/**
+ * LibreOffice-backed office preview pipeline.
+ *
+ * Convert a DOCX / PPTX (eventually XLSX, ODT) buffer to PDF via
+ * `soffice --headless --convert-to pdf`, base64-encode it into a sandboxed
+ * HTML document, and let pdf.js render each page to `<canvas>` inside the
+ * Sandpack iframe. Result: high-fidelity rendering for any format
+ * LibreOffice opens (which is "everything"), works inside Sandpack's
+ * sandboxed iframe (where the browser's native PDF viewer is blocked).
+ * The trade-off is the LibreOffice binary on the server (~250-350 MB disk,
+ * ~2-3 s cold-start per first conversion in a process).
+ *
+ * Off by default. Operators opt in via the `OFFICE_PREVIEW_LIBREOFFICE`
+ * env var AND ensuring `soffice` (or `libreoffice`) is on `$PATH`. The
+ * env value is interpreted three ways:
+ * - Truthy (`true`, `1`, `yes`): all formats use LibreOffice
+ * - Falsy (`false`, `0`, `no`, empty, unset): no formats — fall through
+ * - Comma-separated list (`pptx`, `pptx,docx`): only those formats
+ *
+ * The list form lets operators trade off cold-start latency against
+ * fidelity per format. Practically: `pptx` is the most common opt-in —
+ * pptx-preview chokes on pptxgenjs decks and the slide-list fallback
+ * loses all formatting; LibreOffice handles them well. DOCX renders
+ * ~instantly via docx-preview so paying the ~2-3 s LibreOffice cold-
+ * start there is rarely worth it.
+ *
+ * When the gate is closed for a given format we fall through to the
+ * existing CDN/mammoth/slide-list pipeline so a misconfiguration
+ * doesn't break previews.
+ *
+ * Hardening:
+ * - Subprocess runs in an isolated temp directory (no shared profile, no
+ * access to the operator's home) and a stripped env (`PATH`, `HOME`,
+ * `TMPDIR` only). LibreOffice's `-env:UserInstallation` flag forces a
+ * fresh user profile per call so concurrent conversions can't collide
+ * on shared ~/.config/libreoffice locks.
+ * - 30-second timeout — soffice has been known to hang on malformed input;
+ * `SIGKILL` after the timer expires.
+ * - 50 MB PDF output cap — a runaway document or a deliberate pathology
+ * can't blow the server's disk.
+ * - Macros stay disabled (LibreOffice's default `--macro-security high`
+ * plus our `--norestore --invisible --nodefault` flags).
+ *
+ * What this module is NOT:
+ * - A LibreOffice service / pool. Each call spawns a fresh subprocess.
+ * If load grows we'd want to keep a long-lived `soffice` process and
+ * drive it via UNO, but that's a v2 concern.
+ * - A native PDF viewer integration. We tried that first — Chrome blocks
+ * `<iframe src="data:application/pdf">` AND `<iframe src="blob:...">`
+ * navigation in sandboxed iframes (the built-in viewer requires a
+ * top-level browsing context). pdf.js is the only thing that works
+ * in our iframe topology.
+ */
+
+/**
+ * Parse `OFFICE_PREVIEW_LIBREOFFICE` into the set of formats opted in.
+ * Three forms:
+ * - Truthy (`true`/`1`/`yes`): all formats
+ * - Falsy (`false`/`0`/`no`/empty/unset): no formats
+ * - Comma-separated list (`pptx`/`pptx,docx`): just those formats
+ *
+ * Returning `'all'` (vs. a Set containing every format) keeps the gate
+ * future-proof — adding a new format to the LibreOffice route doesnt
+ * require operators to re-enumerate their env value.
+ */
+type LibreOfficeFormatEnablement = 'all' | ReadonlySet<string> | null;
+
+function parseLibreOfficeEnablement(value: string | undefined): LibreOfficeFormatEnablement {
+ if (value == null) {
+ return null;
+ }
+ const trimmed = value.trim();
+ if (trimmed === '') {
+ return null;
+ }
+ if (/^(1|true|yes)$/i.test(trimmed)) {
+ return 'all';
+ }
+ if (/^(0|false|no)$/i.test(trimmed)) {
+ return null;
+ }
+ /* Comma-separated format list. Lowercased + trimmed; empty entries
+ * dropped so trailing commas / `pptx, ,docx` don't enable spurious
+ * formats. */
+ const formats = trimmed
+ .split(',')
+ .map((s) => s.trim().toLowerCase())
+ .filter(Boolean);
+ return formats.length > 0 ? new Set(formats) : null;
+}
+
+/**
+ * Whether the LibreOffice path is enabled for this specific format.
+ * Read at call time (not module load) so tests can flip the env
+ * without rebuilding.
+ *
+ * @param format extension token (`docx`, `pptx`, `xlsx`, `odt`, ...)
+ */
+export function isLibreOfficeEnabledFor(format: string): boolean {
+ const enabled = parseLibreOfficeEnablement(process.env.OFFICE_PREVIEW_LIBREOFFICE);
+ if (enabled === null) {
+ return false;
+ }
+ if (enabled === 'all') {
+ return true;
+ }
+ return enabled.has(format.toLowerCase());
+}
+
+/**
+ * Whether ANY format is enabled — kept for diagnostic / probe code that
+ * wants to short-circuit binary checks when the feature is fully off.
+ * Most production callers should use `isLibreOfficeEnabledFor(format)`.
+ */
+export function isLibreOfficeEnabled(): boolean {
+ return parseLibreOfficeEnablement(process.env.OFFICE_PREVIEW_LIBREOFFICE) !== null;
+}
+
+interface BinaryProbe {
+ available: boolean;
+ binary: string | null;
+ versionLine: string | null;
+ reason?: string;
+}
+
+let cachedProbe: BinaryProbe | null = null;
+
+/**
+ * Probe `soffice` then `libreoffice` once per process and cache the result.
+ * If the operator installs LibreOffice after server start, they need to
+ * restart to pick it up — simpler than invalidating the cache on filesystem
+ * events for a feature that's already opt-in.
+ */
+export async function probeLibreOfficeBinary(): Promise<BinaryProbe> {
+ if (cachedProbe) {
+ return cachedProbe;
+ }
+ for (const candidate of ['soffice', 'libreoffice']) {
+ try {
+ const versionLine = await runVersion(candidate);
+ cachedProbe = { available: true, binary: candidate, versionLine };
+ return cachedProbe;
+ } catch {
+ /* try next candidate */
+ }
+ }
+ cachedProbe = {
+ available: false,
+ binary: null,
+ versionLine: null,
+ reason: 'neither `soffice` nor `libreoffice` found on $PATH',
+ };
+ return cachedProbe;
+}
+
+function runVersion(binary: string): Promise<string> {
+ return new Promise((resolve, reject) => {
+ const proc = spawn(binary, ['--version'], {
+ stdio: ['ignore', 'pipe', 'pipe'],
+ env: { PATH: process.env.PATH ?? '', HOME: tmpdir() },
+ });
+ let stdout = '';
+ proc.stdout?.on('data', (chunk) => {
+ stdout += String(chunk);
+ });
+ const timer = setTimeout(() => {
+ proc.kill('SIGKILL');
+ reject(new Error('--version timed out'));
+ }, 5_000);
+ proc.once('error', (err) => {
+ clearTimeout(timer);
+ reject(err);
+ });
+ proc.once('exit', (code) => {
+ clearTimeout(timer);
+ if (code === 0) {
+ resolve(stdout.split('\n')[0]?.trim() ?? '');
+ } else {
+ reject(new Error(`exit ${code}`));
+ }
+ });
+ });
+}
+
+/** Reset the cached probe — for tests; never called from production code. */
+export function _resetLibreOfficeProbeCache(): void {
+ cachedProbe = null;
+}
+
+/** Maximum subprocess wall-time. Tuned for cold-start on a small DOCX. */
+export const LIBREOFFICE_TIMEOUT_MS = 30_000;
+
+/** Maximum PDF output size; refuse anything larger so a runaway doc can't fill the disk. */
+export const MAX_LIBREOFFICE_PDF_BYTES = 50 * 1024 * 1024;
+
+/** Tag-distinct error so callers can distinguish "binary missing" from "conversion failed". */
+export class LibreOfficeUnavailableError extends Error {
+ override readonly name = 'LibreOfficeUnavailableError';
+}
+
+/** Tag-distinct error so callers can swallow conversion-side failures and fall through. */
+export class LibreOfficeConversionError extends Error {
+ override readonly name = 'LibreOfficeConversionError';
+ constructor(
+ message: string,
+ readonly stderr?: string,
+ ) {
+ super(message);
+ }
+}
+
+/**
+ * Spawn a LibreOffice subprocess to convert `buffer` (in `extensionHint`
+ * format — `docx`, `pptx`, `xlsx`, `odt`, `odp`, `ods`) to PDF. Returns
+ * the PDF bytes.
+ *
+ * `extensionHint` is part of the temp filename so soffice infers the input
+ * format correctly. We never trust the extension to gate format — the
+ * caller (`html.ts` dispatcher) has already routed by MIME / magic bytes.
+ *
+ * Throws:
+ * - `LibreOfficeUnavailableError` when the binary isn't on $PATH
+ * - `LibreOfficeConversionError` for subprocess failures, timeout, or
+ * oversized output
+ */
+export async function convertOfficeToPdf(buffer: Buffer, extensionHint: string): Promise<Buffer> {
+ const probe = await probeLibreOfficeBinary();
+ if (!probe.available || !probe.binary) {
+ throw new LibreOfficeUnavailableError(probe.reason ?? 'LibreOffice binary unavailable');
+ }
+ const safeExt = extensionHint.replace(/[^a-z0-9]/gi, '').toLowerCase() || 'bin';
+ const tempDir = await mkdtemp(join(tmpdir(), 'lc-libreoffice-'));
+ try {
+ const inputPath = join(tempDir, `input.${safeExt}`);
+ await writeFile(inputPath, buffer);
+ await runConversion(probe.binary, inputPath, tempDir);
+ const outputPath = join(tempDir, 'input.pdf');
+ const pdf = await readFile(outputPath);
+ if (pdf.length > MAX_LIBREOFFICE_PDF_BYTES) {
+ throw new LibreOfficeConversionError(
+ `PDF output ${pdf.length} bytes exceeds cap ${MAX_LIBREOFFICE_PDF_BYTES}`,
+ );
+ }
+ return pdf;
+ } finally {
+ /* Best-effort cleanup. If unlink fails (rare — Linux tmpfs, no
+ * unmounts), the OS will reclaim the dir on next reboot. */
+ await rm(tempDir, { recursive: true, force: true }).catch(() => undefined);
+ }
+}
+
+function runConversion(binary: string, inputPath: string, tempDir: string): Promise<void> {
+ return new Promise((resolve, reject) => {
+ const userProfile = `file://${join(tempDir, 'userprof')}`;
+ const proc = spawn(
+ binary,
+ [
+ '--headless',
+ '--norestore',
+ '--invisible',
+ '--nodefault',
+ '--nofirststartwizard',
+ '--nolockcheck',
+ `-env:UserInstallation=${userProfile}`,
+ '--convert-to',
+ 'pdf',
+ '--outdir',
+ tempDir,
+ inputPath,
+ ],
+ {
+ stdio: ['ignore', 'pipe', 'pipe'],
+ /* Stripped env. PATH is required for soffice to find its own libs;
+ * HOME/TMPDIR pinned to the isolated temp dir so the subprocess
+ * can't read the operator's profile or scribble outside our tree. */
+ env: {
+ PATH: process.env.PATH ?? '',
+ HOME: tempDir,
+ TMPDIR: tempDir,
+ /* Suppress LibreOffice's "Recovery" dialog state on macOS. */
+ DBUS_SESSION_BUS_ADDRESS: 'disabled:',
+ },
+ },
+ );
+ let stderr = '';
+ proc.stderr?.on('data', (chunk) => {
+ stderr += String(chunk);
+ });
+ const timer = setTimeout(() => {
+ proc.kill('SIGKILL');
+ reject(
+ new LibreOfficeConversionError(
+ `LibreOffice timeout after ${LIBREOFFICE_TIMEOUT_MS}ms`,
+ stderr,
+ ),
+ );
+ }, LIBREOFFICE_TIMEOUT_MS);
+ proc.once('error', (err) => {
+ clearTimeout(timer);
+ reject(new LibreOfficeConversionError(err.message, stderr));
+ });
+ proc.once('exit', (code) => {
+ clearTimeout(timer);
+ if (code === 0) {
+ resolve();
+ } else {
+ reject(new LibreOfficeConversionError(`soffice exit ${code}`, stderr));
+ }
+ });
+ });
+}
+
+/**
+ * Build the iframe HTML that embeds the PDF for in-panel rendering. The
+ * PDF is base64-encoded as a `data:` URI and pointed to by an inner
+ * canvas via PDF.js (Mozilla's pdfjs-dist) loaded from CDN.
+ *
+ * Why PDF.js + canvas (vs. native browser PDF viewer):
+ * We tried `<iframe src="data:application/pdf;...">` first — Chrome
+ * blocks data: PDF navigation in sandboxed iframes since Chrome 76.
+ * We tried `<iframe src="blob:...">` next — Chrome ALSO blocks
+ * blob: PDFs in sandboxed iframes (the built-in PDF viewer requires
+ * a top-level browsing context). The Sandpack host iframe IS
+ * sandboxed, so neither works. Manual e2e on PR #12934 — both
+ * produced the "This page has been blocked by Chrome" interstitial.
+ *
+ * PDF.js renders to `<canvas>` which works in ANY context (sandboxed,
+ * embedded, restricted CSP) because it's pure JS — no plugin, no
+ * privileged viewer. ~1 MB CDN load is acceptable for a feature
+ * that's already env-gated and opt-in.
+ *
+ * Worker handling:
+ * PDF.js wants a Web Worker for parsing (offloads CPU from main
+ * thread). The worker URL is loaded from the same jsdelivr origin;
+ * CSP `worker-src https://cdn.jsdelivr.net blob:` allows it. blob:
+ * covers the case where pdf.js wraps the worker source in a Blob
+ * to dodge cross-origin worker restrictions.
+ */
+const PDF_JS_CDN = {
+ /* Pinned to v3.11.174 (last v3 release) — it's a single-file UMD
+ * bundle that loads via a plain `<script>` tag. v4+ uses ES modules
+ * (`pdf.min.mjs`) which complicates the load + SRI flow. v3 still
+ * receives security backports per Mozilla's policy.
+ *
+ * SRI hashes intentionally OMITTED here (unlike docx-preview /
+ * pptx-preview) because the LibreOffice preview path is opt-in and
+ * the operator has already chosen to trust the LibreOffice render
+ * pipeline. Adding SRI is a follow-up worth doing once this path
+ * is proven in production. */
+ pdf: 'https://cdn.jsdelivr.net/npm/[email protected]/build/pdf.min.js',
+ worker: 'https://cdn.jsdelivr.net/npm/[email protected]/build/pdf.worker.min.js',
+} as const;
+
+export function buildPdfEmbedDocument(pdfBase64: string): string {
+ /* CSP scoping:
+ * - `default-src 'none'`: lock everything down.
+ * - `script-src https://cdn.jsdelivr.net 'unsafe-inline'`: load
+ * pdf.js from CDN + run our bootstrap inline.
+ * - `worker-src https://cdn.jsdelivr.net blob:`: pdf.js spawns a
+ * parser worker; jsdelivr is the origin we loaded the worker
+ * script from, blob: covers pdf.js's same-origin worker wrap.
+ * - `connect-src 'none'`: pdf.js doesn't fetch anything — the
+ * PDF bytes are inline, fonts are subset-embedded by LibreOffice.
+ * - `style-src 'unsafe-inline'`: page chrome.
+ * - `img-src 'self' data: blob:`: pdf.js may rasterize embedded
+ * bitmaps via canvas (data:/blob: covers internal handoffs). */
+ const csp = [
+ "default-src 'none'",
+ "script-src https://cdn.jsdelivr.net 'unsafe-inline'",
+ 'worker-src https://cdn.jsdelivr.net blob:',
+ "style-src 'unsafe-inline'",
+ "img-src 'self' data: blob:",
+ "connect-src 'none'",
+ "base-uri 'none'",
+ "form-action 'none'",
+ ].join('; ');
+ return `<!DOCTYPE html>
+<html lang="en">
+<head>
+<meta charset="UTF-8">
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<meta http-equiv="Content-Security-Policy" content="${csp}">
+<title>Preview</title>
+<style>
+:root { color-scheme: light dark; --bg: #ffffff; --fg: #1f2937; --muted: #6b7280; }
+@media (prefers-color-scheme: dark) { :root { --bg: #1a1a2e; --fg: #e5e7eb; --muted: #9ca3af; } }
+html, body { margin: 0; padding: 0; min-height: 100vh; background: var(--bg); color: var(--fg); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
+#lc-render { padding: 16px; box-sizing: border-box; display: flex; flex-direction: column; align-items: center; gap: 16px; }
+#lc-render canvas { max-width: 100%; height: auto; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.18); border-radius: 4px; background: #ffffff; }
+#lc-loading { padding: 24px; color: var(--muted); font-size: 14px; text-align: center; }
+#lc-fallback { display: none; padding: 24px; font-size: 14px; line-height: 1.5; color: var(--muted); text-align: center; }
+#lc-fallback.visible { display: block; }
+#lc-fallback-reason { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; color: var(--muted); margin-top: 6px; word-break: break-word; }
+</style>
+<script src="${PDF_JS_CDN.pdf}"></script>
+</head>
+<body>
+<div id="lc-render"><div id="lc-loading">Loading preview…</div></div>
+<div id="lc-fallback">
+ <p>PDF preview unavailable. Please download the file to view it.</p>
+ <details style="margin-top: 8px;">
+ <summary style="cursor: pointer; font-size: 12px;">Diagnostic details</summary>
+ <div id="lc-fallback-reason"></div>
+ </details>
+</div>
+<script id="lc-pdf-data" type="application/octet-stream;base64">${pdfBase64}</script>
+<script>
+(function () {
+ var container = document.getElementById('lc-render');
+ var loading = document.getElementById('lc-loading');
+ var fallback = document.getElementById('lc-fallback');
+ var reasonEl = document.getElementById('lc-fallback-reason');
+ var settled = false;
+
+ function showFallback(reason) {
+ if (settled) { return; }
+ settled = true;
+ if (loading) { loading.remove(); }
+ if (container) { container.style.display = 'none'; }
+ if (fallback) { fallback.classList.add('visible'); }
+ var text = reason ? String(reason).slice(0, 500) : 'no reason reported';
+ if (reasonEl) { reasonEl.textContent = text; }
+ if (typeof console !== 'undefined' && console.error) {
+ console.error('[libreoffice-pdf] fallback fired:', text);
+ }
+ }
+ function markSuccess() { settled = true; }
+
+ /* pdf.js wraps async errors as unhandled rejections; catch them at
+ * the window level so we never silently fail. */
+ window.addEventListener('unhandledrejection', function (e) {
+ if (settled) { return; }
+ showFallback((e.reason && e.reason.message) || 'unhandled-rejection');
+ });
+ window.addEventListener('error', function (e) {
+ if (settled) { return; }
+ showFallback((e.error && e.error.message) || e.message || 'script-error');
+ });
+
+ if (typeof pdfjsLib === 'undefined' || typeof pdfjsLib.getDocument !== 'function') {
+ showFallback('renderer-not-loaded (pdf.js failed to load from jsdelivr)');
+ return;
+ }
+
+ /* Point the worker at the same CDN version we loaded the main
+ * script from. pdf.js fetches this URL and spawns a Worker; CSP
+ * worker-src covers it. */
+ pdfjsLib.GlobalWorkerOptions.workerSrc = '${PDF_JS_CDN.worker}';
+
+ try {
+ var b64 = document.getElementById('lc-pdf-data').textContent.trim();
+ var bytes = Uint8Array.from(atob(b64), function (c) { return c.charCodeAt(0); });
+
+ pdfjsLib.getDocument({ data: bytes }).promise.then(function (pdf) {
+ if (loading) { loading.remove(); }
+
+ /* Render each page sequentially. We pick a render scale based
+ * on the panel width and the first pages native dimensions so
+ * the canvas matches the panel — no upscaling required by CSS
+ * (which would blur it). DPR-aware so retina screens get crisp
+ * pixels. */
+ var dpr = window.devicePixelRatio || 1;
+ var panelWidth = (container.clientWidth || window.innerWidth) - 32;
+
+ function renderPage(pageNum) {
+ return pdf.getPage(pageNum).then(function (page) {
+ var unscaledViewport = page.getViewport({ scale: 1 });
+ var cssScale = Math.max(0.1, panelWidth / unscaledViewport.width);
+ var viewport = page.getViewport({ scale: cssScale * dpr });
+ var canvas = document.createElement('canvas');
+ canvas.width = viewport.width;
+ canvas.height = viewport.height;
+ /* CSS dimensions in logical pixels; canvas backing store at
+ * DPR multiplier for crisp rendering. */
+ canvas.style.width = (viewport.width / dpr) + 'px';
+ canvas.style.height = (viewport.height / dpr) + 'px';
+ container.appendChild(canvas);
+ var ctx = canvas.getContext('2d');
+ return page.render({ canvasContext: ctx, viewport: viewport }).promise;
+ });
+ }
+
+ var chain = Promise.resolve();
+ for (var i = 1; i <= pdf.numPages; i++) {
+ (function (pageNum) {
+ chain = chain.then(function () { return renderPage(pageNum); });
+ })(i);
+ }
+ return chain.then(markSuccess);
+ }).catch(function (err) {
+ showFallback((err && err.message) || 'pdf-render-failed');
+ });
+
+ /* Safety net: if 15 seconds in pdf.js hasnt rendered anything
+ * visible, accept whatever we have or fall through. PDF.js is
+ * usually fast (<1s for typical chat decks), but big PDFs with
+ * many slides + DPR=2 can take longer. */
+ setTimeout(function () {
+ if (settled) { return; }
+ if (container && container.querySelectorAll('canvas').length > 0) {
+ markSuccess();
+ return;
+ }
+ showFallback('pdf-render-timeout');
+ }, 15000);
+ } catch (err) {
+ showFallback((err && err.message) || 'bootstrap-error');
+ }
+})();
+</script>
+</body>
+</html>`;
+}
+
+/**
+ * Try the LibreOffice path. Returns the wrapped HTML on success, or `null`
+ * if LibreOffice isn't enabled / available / converted successfully — the
+ * caller falls through to the existing pipeline. Never throws.
+ *
+ * `extensionHint`: `docx` / `pptx` / `xlsx` / `odt` / `odp` / `ods` etc.
+ *
+ * Output-size guarantee: returns `null` if the embedded HTML would exceed
+ * the 512 KB `attachment.text` cache cap. Base64 inflates by ~33% so the
+ * effective PDF cap is ~380 KB; LibreOffice's PDF/A-1 default produces
+ * compact output for typical chat-emitted documents.
+ */
+export async function tryLibreOfficePreview(
+ buffer: Buffer,
+ extensionHint: string,
+ outputCap: number,
+): Promise<string | null> {
+ if (!isLibreOfficeEnabledFor(extensionHint)) {
+ return null;
+ }
+ try {
+ const pdf = await convertOfficeToPdf(buffer, extensionHint);
+ const base64 = pdf.toString('base64');
+ const html = buildPdfEmbedDocument(base64);
+ if (Buffer.byteLength(html, 'utf-8') > outputCap) {
+ return null;
+ }
+ return html;
+ } catch (err) {
+ /* Swallow both unavailable + conversion errors. The dispatcher
+ * pipeline is the authoritative renderer; LibreOffice is opportunistic
+ * augmentation. The caller's logger will surface the underlying
+ * pipeline if they care to instrument it. */
+ void err;
+ return null;
+ }
+}
diff --git a/packages/api/src/files/documents/zipSafety.spec.ts b/packages/api/src/files/documents/zipSafety.spec.ts
new file mode 100644
index 0000000000..473bb5715c
--- /dev/null
+++ b/packages/api/src/files/documents/zipSafety.spec.ts
@@ -0,0 +1,124 @@
+import JSZip from 'jszip';
+import { megabyte } from 'librechat-data-provider';
+import { assertSafeZipSize, ZipBombError } from './zipSafety';
+
+/**
+ * Build a ZIP archive whose entries inflate to exactly `decompressedBytes`
+ * each. The data is highly compressible (single repeated character) so
+ * compressed size stays small — roughly 0.5% of inflated size for runs
+ * of zero bytes. Used to simulate the zip-bomb attack pattern from the
+ * SEC validation PoC on PR #12934.
+ */
+const buildBombArchive = async (
+ entries: Array<{ name: string; decompressedBytes: number }>,
+): Promise<Buffer> => {
+ const zip = new JSZip();
+ for (const { name, decompressedBytes } of entries) {
+ zip.file(name, Buffer.alloc(decompressedBytes, 0));
+ }
+ return zip.generateAsync({
+ type: 'nodebuffer',
+ compression: 'DEFLATE',
+ compressionOptions: { level: 9 },
+ });
+};
+
+/** Build a small, well-formed ZIP for the happy-path tests. */
+const buildBenignArchive = async (): Promise<Buffer> => {
+ const zip = new JSZip();
+ zip.file('hello.txt', 'hello world');
+ zip.file('subdir/note.txt', 'second entry');
+ return zip.generateAsync({ type: 'nodebuffer' });
+};
+
+describe('assertSafeZipSize', () => {
+ test('passes a benign small archive', async () => {
+ const buffer = await buildBenignArchive();
+ await expect(assertSafeZipSize(buffer)).resolves.toBeUndefined();
+ });
+
+ test('passes an archive whose entries are all under both caps', async () => {
+ const buffer = await buildBombArchive([
+ { name: 'a.bin', decompressedBytes: 1 * megabyte },
+ { name: 'b.bin', decompressedBytes: 1 * megabyte },
+ ]);
+ await expect(
+ assertSafeZipSize(buffer, { maxTotalBytes: 10 * megabyte, maxEntryBytes: 5 * megabyte }),
+ ).resolves.toBeUndefined();
+ });
+
+ test('throws ZipBombError when a single entry exceeds the per-entry cap', async () => {
+ /* Single 5 MB inflated entry compresses to a few KB. Per-entry cap of
+ * 1 MB should fire mid-inflate. */
+ const buffer = await buildBombArchive([{ name: 'big.bin', decompressedBytes: 5 * megabyte }]);
+ await expect(assertSafeZipSize(buffer, { maxEntryBytes: 1 * megabyte })).rejects.toThrow(
+ ZipBombError,
+ );
+ });
+
+ test('throws ZipBombError when total decompressed size exceeds the total cap', async () => {
+ /* Many small-but-not-tiny entries that individually pass the per-entry
+ * cap but collectively bust the total cap. Catches the multi-entry
+ * variant of the attack. */
+ const buffer = await buildBombArchive(
+ Array.from({ length: 5 }, (_, i) => ({
+ name: `chunk${i}.bin`,
+ decompressedBytes: 1 * megabyte,
+ })),
+ );
+ await expect(
+ assertSafeZipSize(buffer, { maxTotalBytes: 3 * megabyte, maxEntryBytes: 2 * megabyte }),
+ ).rejects.toThrow(ZipBombError);
+ });
+
+ test('cap-violation error is a ZipBombError, not a generic Error', async () => {
+ const buffer = await buildBombArchive([{ name: 'big.bin', decompressedBytes: 5 * megabyte }]);
+ /* Distinguishing the bomb case from a generic parse failure lets
+ * the UI surface a meaningful "preview unavailable, file too large
+ * to inflate" message instead of a vague 500. */
+ await expect(assertSafeZipSize(buffer, { maxEntryBytes: 1 * megabyte })).rejects.toMatchObject({
+ name: 'ZipBombError',
+ code: 'ZIP_BOMB',
+ });
+ });
+
+ test('rejects a malformed zip', async () => {
+ /* Not a real zip — yauzl will throw a parse error (NOT a
+ * ZipBombError; that distinction matters to callers). */
+ const buffer = Buffer.from('not a real zip');
+ await expect(assertSafeZipSize(buffer)).rejects.toThrow();
+ await expect(assertSafeZipSize(buffer)).rejects.not.toBeInstanceOf(ZipBombError);
+ });
+
+ test('handles archives containing directory entries without crashing', async () => {
+ const zip = new JSZip();
+ zip.folder('emptydir');
+ zip.file('emptydir/file.txt', 'data');
+ const buffer = await zip.generateAsync({ type: 'nodebuffer' });
+ await expect(assertSafeZipSize(buffer)).resolves.toBeUndefined();
+ });
+
+ test('uses provided name in the error message for caller-side surfacing', async () => {
+ const buffer = await buildBombArchive([{ name: 'big.bin', decompressedBytes: 5 * megabyte }]);
+ await expect(
+ assertSafeZipSize(buffer, { maxEntryBytes: 1 * megabyte, name: 'evil.docx' }),
+ ).rejects.toThrow(/evil\.docx/);
+ });
+
+ test('re-PoC: catches the SEC-validation attack pattern (sub-1MB compressed → 100MB+ inflated)', async () => {
+ /* Mirrors the SEC validation PoC shape: a sub-1MB compressed
+ * archive whose entries inflate to many tens of MB. Tests that the
+ * default caps fire on this canonical attack without the caller
+ * needing to override anything. The PoC inflated to ~200MB across
+ * several entries; we use 50MB for test-suite speed (still well
+ * over both default caps). */
+ const buffer = await buildBombArchive([
+ { name: 'word/document.xml', decompressedBytes: 50 * megabyte },
+ ]);
+ /* Defense-in-depth check: the compressed payload IS small (proves
+ * the input would slip past a compressed-size gate). */
+ expect(buffer.length).toBeLessThan(1 * megabyte);
+ /* And the validator catches it on default caps. */
+ await expect(assertSafeZipSize(buffer)).rejects.toThrow(ZipBombError);
+ });
+});
diff --git a/packages/api/src/files/documents/zipSafety.ts b/packages/api/src/files/documents/zipSafety.ts
new file mode 100644
index 0000000000..1fba064f11
--- /dev/null
+++ b/packages/api/src/files/documents/zipSafety.ts
@@ -0,0 +1,165 @@
+import yauzl from 'yauzl';
+import { megabyte } from 'librechat-data-provider';
+
+/**
+ * Default per-archive total decompressed-size cap. Office documents in
+ * normal use rarely exceed a few MB inflated; 100 MB leaves generous
+ * headroom for image-heavy templates while still catching the
+ * pathological zip-bomb case (e.g. a 1 MB compressed XLSX that inflates
+ * to 200+ MB of XML — see SEC review on PR #12934).
+ */
+const DEFAULT_MAX_TOTAL_BYTES = 100 * megabyte;
+
+/**
+ * Default per-entry decompressed-size cap. A single inflated entry
+ * larger than this is essentially always either a bomb or content the
+ * downstream parser would balk at anyway.
+ */
+const DEFAULT_MAX_ENTRY_BYTES = 25 * megabyte;
+
+/**
+ * Tag-distinct error so callers (e.g. the office HTML producers and the
+ * RAG document parser) can distinguish a refused zip-bomb from generic
+ * parse failures and emit a sensible "file too large to preview" UI
+ * instead of silently degrading.
+ */
+export class ZipBombError extends Error {
+ readonly code = 'ZIP_BOMB';
+ constructor(message: string) {
+ super(message);
+ this.name = 'ZipBombError';
+ }
+}
+
+export interface ZipSafetyOptions {
+ /** Per-archive total decompressed-byte cap. */
+ maxTotalBytes?: number;
+ /** Per-entry decompressed-byte cap. */
+ maxEntryBytes?: number;
+ /** Filename for error messages — does not need to match disk. */
+ name?: string;
+}
+
+/**
+ * Validate that a ZIP-backed buffer (DOCX, XLSX, XLS-as-OOXML, ODS,
+ * ODT, PPTX, …) does not blow up under decompression beyond the given
+ * caps. Streams every entry through yauzl and counts real decompressed
+ * bytes mid-inflate — the central directory's `uncompressedSize` cannot
+ * be trusted (it can be falsified to lie about the payload, the
+ * specific bypass technique used in the SEC validation PoC for
+ * PR #12934).
+ *
+ * Drops the decompressed bytes immediately (only counts them), so the
+ * validator's own memory footprint is bounded by yauzl's stream
+ * buffer regardless of payload size. CPU is bounded by `maxTotalBytes`
+ * — once the cap fires, the underlying read stream is destroyed and
+ * decompression stops.
+ *
+ * Throws `ZipBombError` on cap violation; throws plain `Error` on a
+ * malformed ZIP. Resolves with `void` when the archive is fully walked
+ * within caps.
+ */
+export function assertSafeZipSize(buffer: Buffer, options: ZipSafetyOptions = {}): Promise<void> {
+ const maxTotalBytes = options.maxTotalBytes ?? DEFAULT_MAX_TOTAL_BYTES;
+ const maxEntryBytes = options.maxEntryBytes ?? DEFAULT_MAX_ENTRY_BYTES;
+ const label = options.name ?? 'archive';
+
+ return new Promise((resolve, reject) => {
+ yauzl.fromBuffer(buffer, { lazyEntries: true }, (err, zipfile) => {
+ if (err) {
+ return reject(err);
+ }
+ if (!zipfile) {
+ return reject(new Error('Failed to open zip buffer'));
+ }
+
+ let settled = false;
+ let totalDecompressed = 0;
+ const finish = (error: Error | null) => {
+ if (settled) {
+ return;
+ }
+ settled = true;
+ try {
+ zipfile.close();
+ } catch {
+ /* zipfile.close() is best-effort — yauzl will throw if a
+ * stream is mid-flight. We've already settled the outer
+ * promise, so swallow this. */
+ }
+ if (error) {
+ reject(error);
+ } else {
+ resolve();
+ }
+ };
+
+ zipfile.readEntry();
+
+ zipfile.on('entry', (entry: yauzl.Entry) => {
+ /* Directory entries (trailing slash) are zero-byte and don't
+ * need to be opened. Saves a stream allocation per directory
+ * in archives like .docx that have nested subfolders. */
+ if (/\/$/.test(entry.fileName)) {
+ zipfile.readEntry();
+ return;
+ }
+
+ zipfile.openReadStream(entry, (streamErr, readStream) => {
+ if (streamErr || !readStream) {
+ return finish(streamErr ?? new Error('Failed to open zip entry stream'));
+ }
+
+ let entryBytes = 0;
+ let entryCapped = false;
+
+ readStream.on('data', (chunk: Buffer) => {
+ if (entryCapped) {
+ return;
+ }
+ entryBytes += chunk.byteLength;
+ totalDecompressed += chunk.byteLength;
+ if (entryBytes > maxEntryBytes) {
+ entryCapped = true;
+ readStream.destroy();
+ finish(
+ new ZipBombError(
+ `${label}: entry "${entry.fileName}" exceeds the ${
+ maxEntryBytes / megabyte
+ }MB per-entry decompressed cap`,
+ ),
+ );
+ return;
+ }
+ if (totalDecompressed > maxTotalBytes) {
+ entryCapped = true;
+ readStream.destroy();
+ finish(
+ new ZipBombError(
+ `${label}: total decompressed size exceeds the ${
+ maxTotalBytes / megabyte
+ }MB cap (zip bomb suspected)`,
+ ),
+ );
+ return;
+ }
+ });
+
+ readStream.on('end', () => {
+ if (!settled) {
+ zipfile.readEntry();
+ }
+ });
+ readStream.on('error', (readErr: Error) => {
+ if (!entryCapped) {
+ finish(readErr);
+ }
+ });
+ });
+ });
+
+ zipfile.on('end', () => finish(null));
+ zipfile.on('error', (zipErr: Error) => finish(zipErr));
+ });
+ });
+}
diff --git a/packages/data-provider/src/types/files.ts b/packages/data-provider/src/types/files.ts
index 9e856d059f..2215e7c5b6 100644
--- a/packages/data-provider/src/types/files.ts
+++ b/packages/data-provider/src/types/files.ts
@@ -117,6 +117,15 @@ export type TFile = {
expiresAt?: string | Date;
preview?: string;
text?: string;
+ /**
+ * Format of the `text` field. `'html'` means the backend produced
+ * a sanitized full-document HTML preview the client may inject as
+ * `index.html` inside the office artifact iframe. `'text'` (or
+ * `undefined` for legacy records) is plain text and MUST NOT be
+ * injected as HTML — render through the markdown/escaping path.
+ * See Codex P1 review on PR #12934.
+ */
+ textFormat?: 'html' | 'text' | null;
metadata?: { fileIdentifier?: string };
createdAt?: string | Date;
updatedAt?: string | Date;
diff --git a/packages/data-schemas/src/schema/file.ts b/packages/data-schemas/src/schema/file.ts
index e483541bdb..4098c83eb9 100644
--- a/packages/data-schemas/src/schema/file.ts
+++ b/packages/data-schemas/src/schema/file.ts
@@ -54,6 +54,16 @@ const file: Schema<IMongoFile> = new Schema(
text: {
type: String,
},
+ textFormat: {
+ /* 'html' when the backend produced a sanitized HTML preview
+ * (office-type CDN/mammoth output), 'text' for plain-text
+ * extracts (RAG / pdf-parse / mammoth.extractRawText). Clients
+ * gate office-bucket routing on textFormat === 'html' to
+ * prevent injecting RAG-extracted plain text into the iframe
+ * as HTML. See Codex P1 review on PR #12934. */
+ type: String,
+ enum: ['html', 'text'],
+ },
context: {
type: String,
},
diff --git a/packages/data-schemas/src/types/file.ts b/packages/data-schemas/src/types/file.ts
index bbf9de3d3d..9541d5abec 100644
--- a/packages/data-schemas/src/types/file.ts
+++ b/packages/data-schemas/src/types/file.ts
@@ -8,6 +8,17 @@ export interface IMongoFile extends Omit<Document, 'model'> {
temp_file_id?: string;
bytes: number;
text?: string;
+ /**
+ * Format of the `text` field — `'html'` when the backend produced
+ * a sanitized full-document HTML preview (e.g. office types via
+ * `bufferToOfficeHtml`), `'text'` for plain-text extracts (e.g.
+ * RAG mammoth/pdf-parse output), `undefined` for legacy records
+ * that pre-date the field. Clients MUST treat `undefined` as
+ * `'text'` and refuse to inject the value into HTML contexts —
+ * otherwise plain document text containing `<script>` tags would
+ * become executable markup. See Codex P1 review on PR #12934.
+ */
+ textFormat?: 'html' | 'text';
filename: string;
filepath: string;
object: 'file';