Skip to content

Commit 7f130ad

Browse files
fix(web): allow slow Office transfers and legacy CFB sizes
1 parent 0caabcd commit 7f130ad

4 files changed

Lines changed: 281 additions & 5 deletions

File tree

‎apps/web/src/components/files/officeDocumentClient.test.ts‎

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { afterEach, describe, expect, it, vi } from "vite-plus/test";
55
import {
66
createOfficeDocumentClient,
77
OFFICE_PREVIEW_MAX_BYTES,
8+
OFFICE_PREVIEW_TRANSFER_TIMEOUT_MS,
89
OFFICE_PREVIEW_TIMEOUT_MS,
910
readOfficeDocumentBytes,
1011
} from "./officeDocumentClient";
@@ -30,11 +31,46 @@ afterEach(() => {
3031
});
3132

3233
const source = { name: "book.xlsx", format: "xlsx" as const, file: new Blob(["bytes"]) };
34+
const remoteSource = {
35+
name: "book.xlsx",
36+
format: "xlsx" as const,
37+
src: "https://environment.test/signed",
38+
};
3339
const workerPosted = () =>
3440
new Promise<TestWorker>((resolve) => {
3541
TestWorker.onPosted = resolve;
3642
});
3743

44+
function controlledRemoteBody() {
45+
let controller: ReadableStreamDefaultController<Uint8Array> | undefined;
46+
let reading: (() => void) | undefined;
47+
const readStarted = new Promise<void>((resolve) => {
48+
reading = resolve;
49+
});
50+
const cancel = vi.fn();
51+
const stream = new ReadableStream<Uint8Array>({
52+
start(value) {
53+
controller = value;
54+
},
55+
pull() {
56+
reading?.();
57+
},
58+
cancel,
59+
});
60+
vi.stubGlobal(
61+
"fetch",
62+
vi.fn(async () => new Response(stream)),
63+
);
64+
return {
65+
readStarted,
66+
cancel,
67+
complete() {
68+
controller?.enqueue(new Uint8Array([1, 2, 3]));
69+
controller?.close();
70+
},
71+
};
72+
}
73+
3874
describe("bounded Office document loading", () => {
3975
it("enforces the limit on actual chunked HTTP bytes without Content-Length", async () => {
4076
const server = NodeHttp.createServer((_request, response) => {
@@ -76,6 +112,7 @@ describe("bounded Office document loading", () => {
76112
});
77113

78114
it("cancels a stalled reader when the document closes", async () => {
115+
vi.useFakeTimers();
79116
let cancelled: (() => void) | undefined;
80117
const cancellation = new Promise<void>((resolve) => {
81118
cancelled = resolve;
@@ -108,9 +145,157 @@ describe("bounded Office document loading", () => {
108145
await cancellation;
109146
expect(cancel).toHaveBeenCalledOnce();
110147
expect(vi.mocked(fetch).mock.calls[0]?.[1]?.signal?.aborted).toBe(true);
148+
expect(vi.getTimerCount()).toBe(0);
149+
});
150+
151+
it("allows a transfer beyond fifteen seconds before starting the parser's full watchdog", async () => {
152+
vi.useFakeTimers();
153+
vi.stubGlobal("Worker", TestWorker);
154+
const response = controlledRemoteBody();
155+
const client = createOfficeDocumentClient(remoteSource);
156+
const posted = workerPosted();
157+
const unsubscribe = client.subscribe(vi.fn());
158+
await response.readStarted;
159+
await vi.advanceTimersByTimeAsync(OFFICE_PREVIEW_TIMEOUT_MS + 1000);
160+
expect(client.getSnapshot()).toEqual({ status: "loading" });
161+
expect(TestWorker.instances).toHaveLength(0);
162+
expect(vi.mocked(fetch).mock.calls[0]?.[1]?.signal?.aborted).toBe(false);
163+
response.complete();
164+
const worker = await posted;
165+
expect(new Uint8Array(worker.postMessage.mock.calls[0]![0].buffer)).toEqual(
166+
new Uint8Array([1, 2, 3]),
167+
);
168+
expect(vi.getTimerCount()).toBe(1);
169+
await vi.advanceTimersByTimeAsync(OFFICE_PREVIEW_TIMEOUT_MS - 1);
170+
expect(client.getSnapshot()).toEqual({ status: "loading" });
171+
expect(worker.terminate).not.toHaveBeenCalled();
172+
await vi.advanceTimersByTimeAsync(1);
173+
expect(client.getSnapshot()).toMatchObject({
174+
status: "error",
175+
message: expect.stringContaining("preview timed out"),
176+
});
177+
expect(worker.terminate).toHaveBeenCalledOnce();
178+
expect(vi.getTimerCount()).toBe(0);
179+
unsubscribe();
180+
});
181+
182+
it("aborts a stalled transfer at its finite deadline without starting a parser", async () => {
183+
vi.useFakeTimers();
184+
vi.stubGlobal("Worker", TestWorker);
185+
const response = controlledRemoteBody();
186+
const client = createOfficeDocumentClient(remoteSource);
187+
const unsubscribe = client.subscribe(vi.fn());
188+
await response.readStarted;
189+
await vi.advanceTimersByTimeAsync(OFFICE_PREVIEW_TRANSFER_TIMEOUT_MS - 1);
190+
expect(client.getSnapshot()).toEqual({ status: "loading" });
191+
await vi.advanceTimersByTimeAsync(1);
192+
expect(client.getSnapshot()).toMatchObject({
193+
status: "error",
194+
message: expect.stringContaining("download timed out"),
195+
});
196+
expect(response.cancel).toHaveBeenCalledOnce();
197+
expect(vi.mocked(fetch).mock.calls[0]?.[1]?.signal?.aborted).toBe(true);
198+
expect(TestWorker.instances).toHaveLength(0);
199+
expect(vi.getTimerCount()).toBe(0);
200+
unsubscribe();
201+
});
202+
203+
it.each(["success", "worker error"])(
204+
"releases the parser deadline on %s",
205+
async (result: string) => {
206+
vi.useFakeTimers();
207+
vi.stubGlobal("Worker", TestWorker);
208+
const client = createOfficeDocumentClient(source);
209+
const listener = vi.fn();
210+
const posted = workerPosted();
211+
const unsubscribe = client.subscribe(listener);
212+
const worker = await posted;
213+
expect(vi.getTimerCount()).toBe(1);
214+
if (result === "success") {
215+
const document = { kind: "spreadsheet", sheets: [], warnings: [] };
216+
worker.dispatchEvent(
217+
new MessageEvent("message", {
218+
data: { id: worker.postMessage.mock.calls[0]![0].id, ok: true, document },
219+
}),
220+
);
221+
expect(client.getSnapshot()).toEqual({ status: "success", document });
222+
} else {
223+
worker.dispatchEvent(new Event("error"));
224+
expect(client.getSnapshot()).toMatchObject({
225+
status: "error",
226+
message: expect.stringContaining("Unable to read"),
227+
});
228+
}
229+
expect(worker.terminate).toHaveBeenCalledOnce();
230+
expect(vi.getTimerCount()).toBe(0);
231+
const calls = listener.mock.calls.length;
232+
await vi.advanceTimersByTimeAsync(OFFICE_PREVIEW_TRANSFER_TIMEOUT_MS);
233+
expect(listener).toHaveBeenCalledTimes(calls);
234+
unsubscribe();
235+
},
236+
);
237+
238+
it("releases the transfer deadline after an HTTP failure", async () => {
239+
vi.useFakeTimers();
240+
const cancel = vi.fn();
241+
vi.stubGlobal(
242+
"fetch",
243+
vi.fn(async () => new Response(new ReadableStream({ cancel }), { status: 403 })),
244+
);
245+
let complete: (() => void) | undefined;
246+
const completed = new Promise<void>((resolve) => {
247+
complete = resolve;
248+
});
249+
const client = createOfficeDocumentClient(remoteSource);
250+
const unsubscribe = client.subscribe(() => {
251+
if (client.getSnapshot().status === "error") complete?.();
252+
});
253+
await completed;
254+
expect(client.getSnapshot()).toMatchObject({
255+
status: "error",
256+
message: expect.stringContaining("HTTP 403"),
257+
});
258+
expect(cancel).toHaveBeenCalledOnce();
259+
expect(vi.getTimerCount()).toBe(0);
260+
unsubscribe();
261+
});
262+
263+
it("ignores an earlier transfer completed after unsubscribe and a new subscription", async () => {
264+
vi.useFakeTimers();
265+
vi.stubGlobal("Worker", TestWorker);
266+
let resolveEarlier: ((response: Response) => void) | undefined;
267+
const earlier = new Promise<Response>((resolve) => {
268+
resolveEarlier = resolve;
269+
});
270+
vi.stubGlobal(
271+
"fetch",
272+
vi
273+
.fn()
274+
.mockReturnValueOnce(earlier)
275+
.mockResolvedValueOnce(new Response(new Uint8Array([3, 4]))),
276+
);
277+
const client = createOfficeDocumentClient(remoteSource);
278+
const stopEarlier = client.subscribe(vi.fn());
279+
stopEarlier();
280+
expect(vi.getTimerCount()).toBe(0);
281+
const posted = workerPosted();
282+
const stopCurrent = client.subscribe(vi.fn());
283+
const worker = await posted;
284+
resolveEarlier?.(new Response(new Uint8Array([1, 2])));
285+
await vi.advanceTimersByTimeAsync(0);
286+
expect(TestWorker.instances).toHaveLength(1);
287+
expect(new Uint8Array(worker.postMessage.mock.calls[0]![0].buffer)).toEqual(
288+
new Uint8Array([3, 4]),
289+
);
290+
expect(client.getSnapshot()).toEqual({ status: "loading" });
291+
expect(vi.getTimerCount()).toBe(1);
292+
stopCurrent();
293+
expect(worker.terminate).toHaveBeenCalledOnce();
294+
expect(vi.getTimerCount()).toBe(0);
111295
});
112296

113297
it("transfers bounded bytes and ignores results after termination", async () => {
298+
vi.useFakeTimers();
114299
vi.stubGlobal("Worker", TestWorker);
115300
const client = createOfficeDocumentClient(source);
116301
const listener = vi.fn();
@@ -127,6 +312,7 @@ describe("bounded Office document loading", () => {
127312
);
128313
expect(worker.terminate).toHaveBeenCalledOnce();
129314
expect(listener).toHaveBeenCalledTimes(calls);
315+
expect(vi.getTimerCount()).toBe(0);
130316
});
131317

132318
it("terminates a stuck parser at the watchdog deadline", async () => {
@@ -142,6 +328,7 @@ describe("bounded Office document loading", () => {
142328
message: expect.stringContaining("timed out"),
143329
});
144330
expect(TestWorker.instances[0]!.terminate).toHaveBeenCalledOnce();
331+
expect(vi.getTimerCount()).toBe(0);
145332
unsubscribe();
146333
});
147334
});

‎apps/web/src/components/files/officeDocumentClient.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
} from "./officeDocumentParser";
66

77
export const OFFICE_PREVIEW_MAX_BYTES = 5 * 1024 * 1024;
8+
export const OFFICE_PREVIEW_TRANSFER_TIMEOUT_MS = 120_000;
89
export const OFFICE_PREVIEW_TIMEOUT_MS = 15_000;
910

1011
export interface OfficeDocumentSource {
@@ -106,12 +107,12 @@ export function createOfficeDocumentClient(source: OfficeDocumentSource, initial
106107
cleanup();
107108
publish(next);
108109
};
109-
const timeout = setTimeout(() => {
110+
let timeout = setTimeout(() => {
110111
finish({
111112
status: "error",
112-
message: "Office preview timed out. Save this file to open it in another app.",
113+
message: "Office document download timed out. Save this file to open it in another app.",
113114
});
114-
}, OFFICE_PREVIEW_TIMEOUT_MS);
115+
}, OFFICE_PREVIEW_TRANSFER_TIMEOUT_MS);
115116
stop = () => {
116117
finished = true;
117118
generation++;
@@ -122,6 +123,13 @@ export function createOfficeDocumentClient(source: OfficeDocumentSource, initial
122123
void readOfficeDocumentBytes(source, controller.signal)
123124
.then((buffer) => {
124125
if (finished || current !== generation) return;
126+
clearTimeout(timeout);
127+
timeout = setTimeout(() => {
128+
finish({
129+
status: "error",
130+
message: "Office preview timed out. Save this file to open it in another app.",
131+
});
132+
}, OFFICE_PREVIEW_TIMEOUT_MS);
125133
worker = new Worker(new URL("./officeDocument.worker.ts", import.meta.url), {
126134
type: "module",
127135
});

‎apps/web/src/components/files/officeDocumentParser.test.ts‎

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,13 +700,93 @@ describe("parseOfficeDocument presentation outlines", () => {
700700
});
701701

702702
describe("Office archive boundaries", () => {
703+
it("reads legacy version 3 workbook values despite uninitialized high stream sizes", () => {
704+
const bytes = workbookBytes("biff8");
705+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
706+
expect(view.getUint16(26, true)).toBe(3);
707+
const directoryOffset = (view.getUint32(48, true) + 1) * 512;
708+
for (let offset = directoryOffset; offset < directoryOffset + 512; offset += 128) {
709+
if (bytes[offset + 66] === 2 || bytes[offset + 66] === 5)
710+
view.setUint32(offset + 124, 0x12345678, true);
711+
}
712+
const document = parseOfficeDocument(bytes, "xls");
713+
if (document.kind !== "spreadsheet") throw new Error("Expected spreadsheet");
714+
expect(document.sheets[0]?.name).toBe("売上");
715+
expect(document.sheets[0]?.rows).toEqual([
716+
["品名", "価格"],
717+
["りんご", "125"],
718+
]);
719+
});
720+
721+
it("rejects full 64-bit version 4 stream sizes while reading the bounded baseline", () => {
722+
const rows = [
723+
["品名", "価格"],
724+
["りんご", 125],
725+
...Array.from({ length: 30 }, (_, row) => [`商品${row}`, "日本語".repeat(30)]),
726+
];
727+
const original = workbookBytes("biff8", XLSX.utils.aoa_to_sheet(rows));
728+
const cfb = XLSX.CFB.read(original, { type: "array" }) as {
729+
FileIndex: { name: string; content: Uint8Array }[];
730+
};
731+
const workbook = cfb.FileIndex.find((entry) => entry.name === "Workbook")!.content;
732+
expect(workbook.length).toBeGreaterThanOrEqual(4096);
733+
const streamSectors = Math.ceil(workbook.length / 4096);
734+
const bytes = new Uint8Array((3 + streamSectors) * 4096);
735+
bytes.set(original.subarray(0, 512));
736+
const view = new DataView(bytes.buffer);
737+
view.setUint16(26, 4, true);
738+
view.setUint16(30, 12, true);
739+
for (const [offset, value] of [
740+
[40, 1],
741+
[44, 1],
742+
[48, 0],
743+
[60, 0xfffffffe],
744+
[64, 0],
745+
[68, 0xfffffffe],
746+
[72, 0],
747+
])
748+
view.setUint32(offset!, value!, true);
749+
for (let index = 0; index < 109; index++)
750+
view.setUint32(76 + index * 4, index ? 0xffffffff : 1, true);
751+
for (let index = 0; index < 32; index++) {
752+
const offset = 4096 + index * 128;
753+
for (const edge of [68, 72, 76]) view.setUint32(offset + edge, 0xffffffff, true);
754+
}
755+
for (const [index, name] of ["Root Entry", "Workbook"].entries()) {
756+
const offset = 4096 + index * 128;
757+
for (let char = 0; char < name.length; char++)
758+
view.setUint16(offset + char * 2, name.charCodeAt(char), true);
759+
view.setUint16(offset + 64, (name.length + 1) * 2, true);
760+
bytes[offset + 66] = index ? 2 : 5;
761+
bytes[offset + 67] = 1;
762+
view.setUint32(offset + 116, index ? 2 : 0xfffffffe, true);
763+
view.setUint32(offset + 120, index ? workbook.length : 0, true);
764+
}
765+
view.setUint32(4096 + 76, 1, true);
766+
for (let index = 0; index < 1024; index++) view.setUint32(8192 + index * 4, 0xffffffff, true);
767+
view.setUint32(8192, 0xfffffffe, true);
768+
view.setUint32(8196, 0xfffffffd, true);
769+
for (let index = 0; index < streamSectors; index++)
770+
view.setUint32(8200 + index * 4, index + 1 === streamSectors ? 0xfffffffe : index + 3, true);
771+
bytes.set(workbook, 3 * 4096);
772+
const document = parseOfficeDocument(bytes, "xls");
773+
if (document.kind !== "spreadsheet") throw new Error("Expected spreadsheet");
774+
expect(document.sheets[0]?.name).toBe("売上");
775+
expect(document.sheets[0]?.rows.slice(0, 2)).toEqual([
776+
["品名", "価格"],
777+
["りんご", "125"],
778+
]);
779+
view.setUint32(4096 + 128 + 124, 1, true);
780+
expect(() => parseOfficeDocument(bytes, "xls")).toThrow("streams exceed preview limits");
781+
});
782+
703783
it("rejects unbounded binary workbook stream sizes before invoking SheetJS", () => {
704784
const bytes = workbookBytes("biff8");
705785
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
706786
const directoryOffset = (view.getUint32(48, true) + 1) * 512;
707787
for (let offset = directoryOffset; offset < directoryOffset + 512; offset += 128) {
708788
if (bytes[offset + 66] === 2) {
709-
view.setUint32(offset + 124, 1, true);
789+
view.setUint32(offset + 120, bytes.length + 1, true);
710790
break;
711791
}
712792
}

‎apps/web/src/components/files/officeDocumentParser.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,8 @@ function validateCompoundWorkbook(bytes: Uint8Array) {
621621
if (
622622
kind !== 0 &&
623623
(directory.length >= MAX_ENTRIES ||
624-
u32(offset + 124) !== 0 ||
624+
// Legacy version 3 writers may leave the unused high size DWORD uninitialized.
625+
(major === 4 && u32(offset + 124) !== 0) ||
625626
size > bytes.length ||
626627
(total += size) > ARCHIVE_MAX_BYTES)
627628
)

0 commit comments

Comments
 (0)