Skip to content

Commit 6349b59

Browse files
yaacovcorcosclaude
andcommitted
fix(web): Word previews no longer cut ordinary documents short
The Word preview bounds a document before the @betteroffice/docx engine opens it, but the bounds were sized for a renderer on the page's main thread. Ordinary documents lost parts: a table over about 2,000 tags or 128 KB ended the preview there, a body stopped at 5,000 paragraphs, and styles past 1,024 definitions or lists past 2,000 tags were dropped, which unstyled the whole document. The engine lays out in its own worker, so the limits are now measured against it and kept in one WORD_LIMITS table: - a paragraph's text, 32K characters (line breaking grows with the square of one paragraph's text), replacing the 128 KB element limit that mostly measured picture markup; - one body element, 100,000 tags (a table is laid out in one go; a 4,000-row table takes about 2 s); - a header or footer, 128 KB (its layout grows with its own length); - body 4 MB, and styles, lists, notes and expansion totals raised to where they no longer bind real documents. Archive, entry and nesting limits are unchanged. The existing limit tests size their inputs from WORD_LIMITS so each still crosses the limit it checks. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6577c6b commit 6349b59

3 files changed

Lines changed: 210 additions & 42 deletions

File tree

‎apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx‎

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ describe("bounded binary previews", () => {
482482
});
483483
});
484484
import { OFFICE_PREVIEW_MAX_COLUMNS, type OfficeWorkerResult } from "./officePreviewProtocol";
485+
import { WORD_LIMITS } from "./officeArchive";
485486

486487
describe("large text preview recovery", () => {
487488
it("retains the workspace viewer across URL renewals and reloads revisions and manual refreshes", async () => {
@@ -1190,7 +1191,7 @@ it.each([
11901191
: scenario === "runs"
11911192
? `<w:p>${"<w:r><w:t>x</w:t></w:r>".repeat(50_001)}</w:p>`
11921193
: scenario === "tables"
1193-
? "<w:tbl/>".repeat(50_001)
1194+
? "<w:tbl/>".repeat(2 * WORD_LIMITS.bodyElements)
11941195
: scenario === "XML bytes"
11951196
? `<w:p><w:r><w:t>${"x".repeat(2 * 1024 * 1024)}</w:t></w:r></w:p>`
11961197
: "<w:p/>";
@@ -1203,7 +1204,7 @@ it.each([
12031204
if (scenario === "auxiliary XML" || scenario === "renamed XML")
12041205
archive.file(
12051206
scenario === "renamed XML" ? "word/footnotes.dat" : "word/footnotes.xml",
1206-
`<w:footnotes>${"<w:p/>".repeat(50_001)}</w:footnotes>`,
1207+
`<w:footnotes>${"<w:p/>".repeat(WORD_LIMITS.auxiliaryMarkup + 1)}</w:footnotes>`,
12071208
);
12081209
const file = await archive.generateAsync({ type: "blob", compression: "DEFLATE" });
12091210
const src = URL.createObjectURL(file);
@@ -1254,8 +1255,8 @@ it.each([
12541255
);
12551256
}
12561257
expect(markup.length).toBeLessThan(body.length);
1257-
expect(markup.length).toBeLessThanOrEqual(3 * 1024 * 1024);
1258-
expect(markup.match(/<(?![!?/])/g)!.length).toBeLessThanOrEqual(100_000);
1258+
expect(markup.length).toBeLessThanOrEqual(WORD_LIMITS.bodyBytes);
1259+
expect(markup.match(/<(?![!?/])/g)!.length).toBeLessThanOrEqual(WORD_LIMITS.bodyTags);
12591260
}
12601261
} finally {
12611262
worker.terminate();
@@ -1286,15 +1287,15 @@ it.each([
12861287
);
12871288
archive.file(
12881289
"word/_rels/document.xml.rels",
1289-
`<Relationships>${scenario === "content type" ? "" : `<Relationship Id="style" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="${path.slice(5)}"/>`}${scenario === "metadata type" ? "<unused/>".repeat(50_001) : ""}</Relationships>`,
1290+
`<Relationships>${scenario === "content type" ? "" : `<Relationship Id="style" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="${path.slice(5)}"/>`}${scenario === "metadata type" ? "<unused/>".repeat(WORD_LIMITS.auxiliaryMarkup + 1) : ""}</Relationships>`,
12901291
);
12911292
archive.file(
12921293
path,
12931294
scenario === "relationship bytes"
1294-
? `<styles>${"x".repeat(1024 * 1024)}</styles>`
1295+
? `<styles>${"x".repeat(WORD_LIMITS.auxiliaryPartBytes)}</styles>`
12951296
: scenario === "deep XML"
12961297
? `${"<styles>".repeat(65)}${"</styles>".repeat(65)}`
1297-
: `<styles>${"<style/>".repeat(50_001)}</styles>`,
1298+
: `<styles>${"<style/>".repeat(WORD_LIMITS.auxiliaryMarkup + 1)}</styles>`,
12981299
);
12991300
const bytes = await archive.generateAsync({ type: "arraybuffer", compression: "DEFLATE" });
13001301
const worker = new Worker(new URL("./officePreview.worker.ts", import.meta.url), {
@@ -1348,35 +1349,38 @@ async function expandedWordFixture(role: (typeof EXPANDED_WORD_ROLES)[number]) {
13481349
'<Relationships><Relationship Id="doc" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/></Relationships>',
13491350
);
13501351
const text = (value: string) => `<w:p><w:r><w:t>${value}</w:t></w:r></w:p>`;
1351-
// Each copy of the 2,000-paragraph payload costs 8,000 markup units, so 20
1352-
// references (or pages) exceed the 100,000-unit expansion budget.
1352+
// Each copy of the 2,000-paragraph payload costs 6,000 markup units, so enough
1353+
// references (or pages) exceed the expansion budget.
1354+
const copies = Math.ceil(WORD_LIMITS.expandedMarkup / 6_000) + 1;
13531355
archive.file(
13541356
"word/document.xml",
1355-
`<w:document xmlns:w="${ns}" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><w:body>${text("body retained")}${Array.from({ length: 20 }, () => `<w:p><w:r><w:footnoteReference w:id="1"/><w:endnoteReference w:id="1"/><w:commentReference w:id="1"/><w:br w:type="page"/></w:r></w:p>`).join("")}<w:sectPr><w:headerReference w:type="default" r:id="aux"/><w:footerReference w:type="default" r:id="aux"/></w:sectPr></w:body></w:document>`,
1357+
`<w:document xmlns:w="${ns}" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><w:body>${text("body retained")}${Array.from({ length: copies }, () => `<w:p><w:r><w:footnoteReference w:id="1"/><w:endnoteReference w:id="1"/><w:commentReference w:id="1"/><w:br w:type="page"/></w:r></w:p>`).join("")}<w:sectPr><w:headerReference w:type="default" r:id="aux"/><w:footerReference w:type="default" r:id="aux"/></w:sectPr></w:body></w:document>`,
13561358
);
13571359
archive.file(
13581360
"word/_rels/document.xml.rels",
13591361
`<Relationships><Relationship Id="aux" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/${type}" Target="media/aux.xml"/></Relationships>`,
13601362
);
13611363
const xml =
13621364
role === "style chain" || role === "style count" || role === "style visits"
1363-
? `<w:styles xmlns:w="${ns}">${Array.from({ length: role === "style count" ? 3_000 : 1_000 }, (_, i) => `<w:style w:styleId="s${i}" w:type="paragraph">${i ? `<w:basedOn w:val="s${role === "style visits" && i >= 30 ? 29 : i - 1}"/>` : ""}<w:pPr><w:spacing w:after="120"/></w:pPr></w:style>`).join("")}</w:styles>`
1365+
? `<w:styles xmlns:w="${ns}">${Array.from({ length: role === "style count" ? WORD_LIMITS.styles + 1 : role === "style visits" ? Math.ceil(WORD_LIMITS.styleVisits / 30) + 1 : 1_000 }, (_, i) => `<w:style w:styleId="s${i}" w:type="paragraph">${i ? `<w:basedOn w:val="s${role === "style visits" && i >= 30 ? 29 : i - 1}"/>` : ""}<w:pPr><w:spacing w:after="120"/></w:pPr></w:style>`).join("")}</w:styles>`
13641366
: role === "styles"
13651367
? `<w:styles xmlns:w="${ns}">${Array.from({ length: 100 }, (_, i) => `<w:style w:styleId="${"s".repeat(2_000)}${i}" w:type="paragraph"><w:basedOn w:val="s0"/><w:pPr><w:spacing w:after="120"/></w:pPr></w:style>`).join("")}</w:styles>`
13661368
: role.includes("numbering")
1367-
? `<w:numbering xmlns:w="${ns}">${role === "picture numbering" ? '<w:numPicBullet w:numPicBulletId="1"/>' : ""}<w:abstractNum w:abstractNumId="1">${Array.from({ length: 9 }, (_, i) => `<w:lvl w:ilvl="${i}"><w:lvlText w:val="%1."/></w:lvl>`).join("")}</w:abstractNum>${Array.from({ length: 100 }, (_, i) => `<w:num w:numId="${i}"><w:abstractNumId w:val="1"/></w:num>`).join("")}</w:numbering>`
1369+
? `<w:numbering xmlns:w="${ns}">${role === "picture numbering" ? '<w:numPicBullet w:numPicBulletId="1"/>' : ""}<w:abstractNum w:abstractNumId="1">${Array.from({ length: 9 }, (_, i) => `<w:lvl w:ilvl="${i}"><w:lvlText w:val="%1."/></w:lvl>`).join("")}</w:abstractNum>${Array.from({ length: WORD_LIMITS.numberingReferences + 1 }, (_, i) => `<w:num w:numId="${i}"><w:abstractNumId w:val="1"/></w:num>`).join("")}</w:numbering>`
13681370
: role === "header" || role === "footer"
13691371
? `<w:${role === "header" ? "hdr" : "ftr"} xmlns:w="${ns}">${text("omitted payload").repeat(2_000)}</w:${role === "header" ? "hdr" : "ftr"}>`
13701372
: `<w:${role} xmlns:w="${ns}"><w:${role.slice(0, -1)} w:id="1">${text("omitted payload").repeat(2_000)}</w:${role.slice(0, -1)}></w:${role}>`;
13711373
archive.file("word/media/aux.xml", xml);
13721374
if (role === "note roots") {
1375+
// Each note's XML repeats the 32 KB root, which the expansion budget charges twice.
1376+
const notes = Math.ceil(WORD_LIMITS.expandedBytes / 64_000) + 1;
13731377
archive.file(
13741378
"word/document.xml",
1375-
`<w:document xmlns:w="${ns}"><w:body>${text("body retained")}${Array.from({ length: 200 }, (_, id) => `<w:p><w:r><w:footnoteReference w:id="${id}"/></w:r></w:p>`).join("")}</w:body></w:document>`,
1379+
`<w:document xmlns:w="${ns}"><w:body>${text("body retained")}${Array.from({ length: notes }, (_, id) => `<w:p><w:r><w:footnoteReference w:id="${id}"/></w:r></w:p>`).join("")}</w:body></w:document>`,
13761380
);
13771381
archive.file(
13781382
"word/media/aux.xml",
1379-
`<w:footnotes xmlns:w="${ns}" padding="${"x".repeat(32_000)}">${Array.from({ length: 200 }, (_, id) => `<w:footnote w:id="${id}">${text("omitted payload")}</w:footnote>`).join("")}</w:footnotes>`,
1383+
`<w:footnotes xmlns:w="${ns}" padding="${"x".repeat(32_000)}">${Array.from({ length: notes }, (_, id) => `<w:footnote w:id="${id}">${text("omitted payload")}</w:footnote>`).join("")}</w:footnotes>`,
13801384
);
13811385
}
13821386
return archive.generateAsync({ type: "blob", compression: "DEFLATE" });
@@ -1401,7 +1405,7 @@ it.each(EXPANDED_WORD_ROLES)(
14011405
(sum, [chunk]) => sum + (typeof chunk === "string" ? chunk.length : 0),
14021406
0,
14031407
),
1404-
).toBeLessThan(4 * 1024 * 1024);
1408+
).toBeLessThan(WORD_LIMITS.expandedBytes + 4 * 1024 * 1024);
14051409
const bounded = await JSZip.loadAsync(prepared.bytes);
14061410
const markup = await bounded.file("word/document.xml")!.async("string");
14071411
expect(markup).toContain("body retained");
@@ -1622,7 +1626,8 @@ it.each([false, true])(
16221626
},
16231627
);
16241628

1625-
it.each([1, 145])(
1629+
// A body just over the read limit once each paragraph repeats its text this often.
1630+
it.each([1, Math.ceil(WORD_LIMITS.bodyBytes / 5_000 / 4)])(
16261631
"counts shared Word settings once with %s text repetitions",
16271632
async (repetitions) => {
16281633
const { default: JSZip } = await import("jszip");
@@ -1658,7 +1663,8 @@ it.each([1, 145])(
16581663
const document = await bounded.file("word/document.xml")!.async("string");
16591664
const retainedSettings = await bounded.file("word/settings.xml")!.async("string");
16601665
expect(document.match(/<w:p>/g)!.length).toBeGreaterThan(100);
1661-
expect(document.length + retainedSettings.length).toBeLessThanOrEqual(3 * 1024 * 1024);
1666+
expect(document.length).toBeLessThanOrEqual(WORD_LIMITS.bodyBytes);
1667+
expect(retainedSettings).toBe(settings);
16621668
} finally {
16631669
worker.terminate();
16641670
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import JSZip from "jszip";
2+
import { describe, expect, it } from "vite-plus/test";
3+
4+
import { prepareOffice } from "./officeArchive";
5+
6+
const W = 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"';
7+
const V = 'xmlns:v="urn:schemas-microsoft-com:vml"';
8+
const relationship = (type: string, target: string) =>
9+
`<Relationship Id="${type}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/${type}" Target="${target}"/>`;
10+
const paragraph = (text: string, properties = "") =>
11+
`<w:p>${properties}<w:r><w:t xml:space="preserve">${text}</w:t></w:r></w:p>`;
12+
13+
async function wordFile(body: string, parts: { styles?: string; numbering?: string } = {}) {
14+
const zip = new JSZip();
15+
zip.file(
16+
"[Content_Types].xml",
17+
`<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/></Types>`,
18+
);
19+
zip.file(
20+
"_rels/.rels",
21+
`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">${relationship("officeDocument", "word/document.xml")}</Relationships>`,
22+
);
23+
zip.file(
24+
"word/_rels/document.xml.rels",
25+
`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">${Object.keys(
26+
parts,
27+
)
28+
.map((part) => relationship(part, `${part}.xml`))
29+
.join("")}</Relationships>`,
30+
);
31+
for (const [part, xml] of Object.entries(parts)) zip.file(`word/${part}.xml`, xml);
32+
zip.file(
33+
"word/document.xml",
34+
`<w:document ${W} ${V}><w:body>${body}<w:sectPr><w:pgSz w:w="12240" w:h="15840"/></w:sectPr></w:body></w:document>`,
35+
);
36+
return zip.generateAsync({ type: "arraybuffer" });
37+
}
38+
39+
async function prepared(bytes: ArrayBuffer) {
40+
const result = await prepareOffice(bytes, "docx");
41+
const zip = await JSZip.loadAsync(result.bytes);
42+
return { truncated: result.truncated, part: (path: string) => zip.file(path)?.async("string") };
43+
}
44+
45+
// regression: the budgets sized for a main-thread renderer cut ordinary documents short.
46+
describe("Word preview limits", () => {
47+
it("keeps a document whose table follows a long one", async () => {
48+
const rows = Array.from(
49+
{ length: 500 },
50+
(_, row) =>
51+
`<w:tr>${Array.from({ length: 5 }, (_, cell) => `<w:tc>${paragraph(`r${row}c${cell}`)}</w:tc>`).join("")}</w:tr>`,
52+
).join("");
53+
const result = await prepared(
54+
await wordFile(`<w:tbl>${rows}</w:tbl>${paragraph("After the table")}`),
55+
);
56+
expect(result.truncated).toBe(false);
57+
expect(await result.part("word/document.xml")).toContain("After the table");
58+
});
59+
60+
it("keeps the list and style definitions of a long document", async () => {
61+
const levels = Array.from(
62+
{ length: 9 },
63+
(_, level) =>
64+
`<w:lvl w:ilvl="${level}"><w:numFmt w:val="decimal"/><w:lvlText w:val="%${level + 1}."/></w:lvl>`,
65+
).join("");
66+
const numbering = `<w:numbering ${W}>${Array.from({ length: 60 }, (_, list) => `<w:abstractNum w:abstractNumId="${list}">${levels}</w:abstractNum>`).join("")}${Array.from({ length: 60 }, (_, list) => `<w:num w:numId="${list + 1}"><w:abstractNumId w:val="${list}"/></w:num>`).join("")}</w:numbering>`;
67+
const styles = `<w:styles ${W}>${Array.from(
68+
{ length: 1_500 },
69+
(_, style) =>
70+
`<w:style w:type="paragraph" w:styleId="S${style}">${style % 10 ? `<w:basedOn w:val="S${style - 1}"/>` : ""}<w:pPr><w:spacing w:after="${style}"/></w:pPr><w:rPr><w:sz w:val="22"/></w:rPr></w:style>`,
71+
).join("")}</w:styles>`;
72+
const result = await prepared(
73+
await wordFile(
74+
Array.from({ length: 6_000 }, (_, index) =>
75+
paragraph(
76+
`Item ${index}`,
77+
`<w:pPr><w:pStyle w:val="S${index % 1_500}"/><w:numPr><w:ilvl w:val="0"/><w:numId w:val="${(index % 60) + 1}"/></w:numPr></w:pPr>`,
78+
),
79+
).join(""),
80+
{ styles, numbering },
81+
),
82+
);
83+
expect(result.truncated).toBe(false);
84+
expect(await result.part("word/numbering.xml")).toBe(numbering);
85+
expect(await result.part("word/styles.xml")).toBe(styles);
86+
expect(await result.part("word/document.xml")).toContain("Item 5999");
87+
});
88+
89+
it.each([
90+
["on its own", ""],
91+
[
92+
"after an empty paragraph in a text box",
93+
"<w:r><w:pict><v:shape><v:textbox><w:txbxContent><w:p/></w:txbxContent></v:textbox></v:shape></w:pict></w:r>",
94+
],
95+
])("stops before a paragraph too long to lay out %s", async (_, before) => {
96+
const long = `<w:p>${before}<w:r><w:t>${"x".repeat(40_000)}</w:t></w:r></w:p>`;
97+
const result = await prepared(
98+
await wordFile(paragraph("Kept") + long + paragraph("Not reached")),
99+
);
100+
expect(result.truncated).toBe(true);
101+
const document = await result.part("word/document.xml");
102+
expect(document).toContain("Kept");
103+
expect(document).not.toContain("Not reached");
104+
});
105+
106+
it("counts only the text a paragraph lays out", async () => {
107+
const formatted = `<w:p>${" ".repeat(40_000)}<w:r><w:instrText>${"x".repeat(40_000)}</w:instrText></w:r><w:r><w:t>Short</w:t></w:r></w:p>`;
108+
const result = await prepared(await wordFile(formatted + paragraph("After")));
109+
expect(result.truncated).toBe(false);
110+
expect(await result.part("word/document.xml")).toContain("After");
111+
});
112+
});

0 commit comments

Comments
 (0)