Skip to content

Commit 787c59c

Browse files
yaacovcorcosclaudejuliusmarminge
authored
fix(web): Word previews no longer cut ordinary documents short (#32040)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent d0c7147 commit 787c59c

3 files changed

Lines changed: 224 additions & 40 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: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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 R = 'xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"';
9+
const relationship = (type: string, target: string) =>
10+
`<Relationship Id="${type}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/${type}" Target="${target}"/>`;
11+
const paragraph = (text: string, properties = "") =>
12+
`<w:p>${properties}<w:r><w:t xml:space="preserve">${text}</w:t></w:r></w:p>`;
13+
14+
async function wordFile(
15+
body: string,
16+
parts: Partial<Record<"styles" | "numbering" | "header" | "footer" | "footnotes", string>> = {},
17+
section = "",
18+
) {
19+
const zip = new JSZip();
20+
zip.file(
21+
"[Content_Types].xml",
22+
`<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>`,
23+
);
24+
zip.file(
25+
"_rels/.rels",
26+
`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">${relationship("officeDocument", "word/document.xml")}</Relationships>`,
27+
);
28+
zip.file(
29+
"word/_rels/document.xml.rels",
30+
`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">${Object.keys(
31+
parts,
32+
)
33+
.map((part) => relationship(part, `${part}.xml`))
34+
.join("")}</Relationships>`,
35+
);
36+
for (const [part, xml] of Object.entries(parts)) zip.file(`word/${part}.xml`, xml);
37+
zip.file(
38+
"word/document.xml",
39+
`<w:document ${W} ${V} ${R}><w:body>${body}<w:sectPr>${section}<w:pgSz w:w="12240" w:h="15840"/></w:sectPr></w:body></w:document>`,
40+
);
41+
return zip.generateAsync({ type: "arraybuffer" });
42+
}
43+
44+
async function prepared(bytes: ArrayBuffer) {
45+
const result = await prepareOffice(bytes, "docx");
46+
const zip = await JSZip.loadAsync(result.bytes);
47+
return { truncated: result.truncated, part: (path: string) => zip.file(path)?.async("string") };
48+
}
49+
50+
// regression: the budgets sized for a main-thread renderer cut ordinary documents short.
51+
describe("Word preview limits", () => {
52+
it("keeps a document whose table follows a long one", async () => {
53+
const rows = Array.from(
54+
{ length: 500 },
55+
(_, row) =>
56+
`<w:tr>${Array.from({ length: 5 }, (_, cell) => `<w:tc>${paragraph(`r${row}c${cell}`)}</w:tc>`).join("")}</w:tr>`,
57+
).join("");
58+
const result = await prepared(
59+
await wordFile(`<w:tbl>${rows}</w:tbl>${paragraph("After the table")}`),
60+
);
61+
expect(result.truncated).toBe(false);
62+
expect(await result.part("word/document.xml")).toContain("After the table");
63+
});
64+
65+
it("keeps the list and style definitions of a long document", async () => {
66+
const levels = Array.from(
67+
{ length: 9 },
68+
(_, level) =>
69+
`<w:lvl w:ilvl="${level}"><w:numFmt w:val="decimal"/><w:lvlText w:val="%${level + 1}."/></w:lvl>`,
70+
).join("");
71+
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>`;
72+
const styles = `<w:styles ${W}>${Array.from(
73+
{ length: 1_500 },
74+
(_, style) =>
75+
`<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>`,
76+
).join("")}</w:styles>`;
77+
const result = await prepared(
78+
await wordFile(
79+
Array.from({ length: 6_000 }, (_, index) =>
80+
paragraph(
81+
`Item ${index}`,
82+
`<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>`,
83+
),
84+
).join(""),
85+
{ styles, numbering },
86+
),
87+
);
88+
expect(result.truncated).toBe(false);
89+
expect(await result.part("word/numbering.xml")).toBe(numbering);
90+
expect(await result.part("word/styles.xml")).toBe(styles);
91+
expect(await result.part("word/document.xml")).toContain("Item 5999");
92+
});
93+
94+
const text = "x".repeat(140_000);
95+
it.each([
96+
["on its own", `<w:p><w:r><w:t>${text}</w:t></w:r></w:p>`],
97+
[
98+
"after an empty paragraph in a text box",
99+
`<w:p><w:r><w:pict><v:shape><v:textbox><w:txbxContent><w:p/></w:txbxContent></v:textbox></v:shape></w:pict></w:r><w:r><w:t>${text}</w:t></w:r></w:p>`,
100+
],
101+
["written as CDATA", `<w:p><w:r><w:t><![CDATA[${text}]]></w:t></w:r></w:p>`],
102+
])("stops before a paragraph too long to lay out %s", async (_, long) => {
103+
const result = await prepared(
104+
await wordFile(paragraph("Kept") + long + paragraph("Not reached")),
105+
);
106+
expect(result.truncated).toBe(true);
107+
const document = await result.part("word/document.xml");
108+
expect(document).toContain("Kept");
109+
expect(document).not.toContain("Not reached");
110+
});
111+
112+
it("counts only the text a paragraph lays out", async () => {
113+
const formatted = `<w:p>${" ".repeat(140_000)}<w:r><w:instrText>${"x".repeat(140_000)}</w:instrText></w:r><w:r><w:t>${"x".repeat(40_000)}</w:t></w:r></w:p>`;
114+
const result = await prepared(await wordFile(formatted + paragraph("After")));
115+
expect(result.truncated).toBe(false);
116+
expect(await result.part("word/document.xml")).toContain("After");
117+
});
118+
119+
it("keeps a long header", async () => {
120+
const header = `<w:hdr ${W}>${Array.from({ length: 3_000 }, (_, line) => paragraph(`Header line ${line}`)).join("")}</w:hdr>`;
121+
const footer = `<w:ftr ${W}>${paragraph("Footer")}</w:ftr>`;
122+
const footnotes = `<w:footnotes ${W}><w:footnote w:id="1">${paragraph("Note")}</w:footnote></w:footnotes>`;
123+
const result = await prepared(
124+
await wordFile(
125+
`<w:p><w:r><w:t>Body</w:t></w:r><w:r><w:footnoteReference w:id="1"/></w:r></w:p>`,
126+
{ header, footer, footnotes },
127+
`<w:headerReference w:type="default" r:id="header"/><w:footerReference w:type="default" r:id="footer"/>`,
128+
),
129+
);
130+
expect(result.truncated).toBe(false);
131+
expect(await result.part("word/header.xml")).toBe(header);
132+
expect(await result.part("word/footer.xml")).toBe(footer);
133+
expect(await result.part("word/footnotes.xml")).toBe(footnotes);
134+
expect(await result.part("word/document.xml")).toContain("Body");
135+
});
136+
});

0 commit comments

Comments
 (0)