The best way to control Claude Code, Codex and more from any computer or phone
fix(web): preserve literal XML declarations in EPUB previews (#36624)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 17, 2026 at 09:11 UTC
6ec1d31b1ebade38fdc6bf88c7401d0ea35a1180
2 files changed
+39
-20
apps/web/src/components/files/FilePreviewPanel.test.ts
+18
-7
@@ -470,7 +470,7 @@ Third}`;
470
expect(result.pages[0]!.html).toBe(`<table>${html}</table>`);
471
});
472
473
- it.each(["odt", "odp"] as const)("reads %s text and rejects XML entities", async (kind) => {
473
+ it.each(["odt", "odp"] as const)("reads %s text and rejects unsupported XML", async (kind) => {
474
const archive = new JSZip().file(
475
"content.xml",
476
`<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0"><office:body>${kind === "odt" ? "<office:text><text:p>Field notes</text:p></office:text>" : '<draw:page draw:name="Overview"><text:h>Launch plan</text:h><presentation:notes><draw:frame><draw:text-box><text:p>Speaker only</text:p></draw:text-box></draw:frame></presentation:notes></draw:page>'}</office:body></office:document-content>`,
@@ -480,11 +480,19 @@ Third}`;
480
if (result.type !== "document") throw new Error("Expected document");
481
expect(result.pages[0]!.html).toContain(kind === "odt" ? "Field notes" : "Launch plan");
482
expect(result.pages[0]!.html).not.toContain("Speaker only");
483
- archive.file(
484
- "content.xml",
483
+ for (const source of [
484
'<!DOCTYPE x [<!ENTITY external SYSTEM "file:///etc/passwd">]><x>&external;</x>',
486
- );
487
- await expect(openDocument(archive, kind)).rejects.toThrow("unsupported XML");
485
+ '<html><body><p title="<!--">x</p><!DOCTYPE x [<!ENTITY e "boom">]><b title="-->">&e;</b></body></html>',
486
+ '<html><body><p title="<![CDATA[">x</p><!DOCTYPE x [<!ENTITY e "boom">]><b title="]]>">&e;</b></body></html>',
487
+ '<!DOC<!DOCTYPE a>TYPE x [<!EN<!DOCTYPE a>TITY e "boom">]><x>&e;</x>',
488
+ '<?test <![CDATA[ ?><!DOCTYPE html [<!ENTITY e "boom">]><html><body><pre><![CDATA[x]]></pre></body></html>',
489
+ '<?test <!-- ?><!DOCTYPE html [<!ENTITY e "boom">]><html><body><!-- x --></body></html>',
490
+ "<html><body>" + "<!--".repeat(80_000) + "</body></html>",
491
+ "<html><body>" + "<![CDATA[".repeat(80_000) + "</body></html>",
492
+ ]) {
493
+ archive.file("content.xml", source);
494
+ await expect(openDocument(archive, kind)).rejects.toThrow("unsupported XML");
495
+ }
496
});
497
498
it("reads EPUB chapters in spine order and removes active and remote content", async () => {
@@ -503,7 +511,7 @@ Third}`;
511
)
512
.file(
513
"Book/two&&&.xhtml",
506
- '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html><head><title>Second</title></head><body><h1>Second chapter</h1><p>A B © — ≂̸ &lt;</p><pre><![CDATA[Use & and < literally]]></pre><img src="cover.png" alt="© ≂̸"/></body></html>',
514
+ '<?xml-stylesheet href="a<b.css"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html><head><title>Second</title></head><body><h1>Second chapter</h1><p>A B © — ≂̸ &lt;</p><pre><![CDATA[Use & and < literally]]></pre><pre><![CDATA[<!DOCTYPE html><!ENTITY author "Ada">]]></pre><!-- <!ENTITY note "x"> --><img src="cover.png" alt="< © ≂̸"/></body></html>',
515
)
516
.file(
517
"Book/cover.png",
@@ -518,7 +526,10 @@ Third}`;
526
expect(result.pages.map((page) => page.title)).toEqual(["Second", "First"]);
527
expect(result.pages[0]!.html).toContain("A\u00a0B © — ≂̸ &lt;");
528
expect(result.pages[0]!.html).toContain("<pre>Use &amp; and &lt; literally</pre>");
521
- expect(result.pages[0]!.html).toContain('alt="© ≂̸"');
529
+ expect(result.pages[0]!.html).toContain(
530
+ "<pre><!DOCTYPE html><!ENTITY author "Ada"></pre>",
531
+ );
532
+ expect(result.pages[0]!.html).toContain('alt="< © ≂̸"');
533
expect(result.pages[1]!.html).toContain('src="data:image/png;base64,');
534
expect(result.pages[1]!.html).not.toMatch(/https:|script|alert/);
535
});
apps/web/src/components/files/extendedDocumentPreview.ts
+21
-13
@@ -35,13 +35,17 @@ const namespacePrefixes: Record<string, string> = {
35
"urn:oasis:names:tc:opendocument:xmlns:container": "",
36
};
37
function parseXml(source: string): XmlNode {
38
- // EPUB XHTML commonly declares an external DTD. Discard it without resolving it.
39
- source = source.replaceAll(/<!DOCTYPE\s+[^<>[\]]*>/gi, "");
40
- if (
41
- source.length > 8 * 1024 * 1024 ||
42
- /<!\s*(DOCTYPE|ENTITY)/i.test(source) ||
43
- XMLValidator.validate(source) !== true
44
- )
38
+ // EPUB XHTML commonly declares an external DTD: discard it without resolving it. Any other
39
+ // declaration is rejected; CDATA, comments and processing instructions pass through untouched.
40
+ let declares = false;
41
+ source = source.replaceAll(
42
+ /<\?[\s\S]*?(?:\?>|$)|<!\[CDATA\[[\s\S]*?(?:]]>|$)|<!--[\s\S]*?(?:-->|$)|(<!DOCTYPE\s+[^<>[\]]*>)|(<!\s*(?:DOCTYPE|ENTITY))/gi,
43
+ (markup, externalDoctype?: string, declaration?: string) => {
44
+ if (declaration) declares = true;
45
+ return externalDoctype ? " " : markup;
46
+ },
47
+ );
48
+ if (source.length > 8 * 1024 * 1024 || declares || XMLValidator.validate(source) !== true)
49
throw new Error("This document contains invalid or unsupported XML.");
50
const parser = new XMLParser({
51
preserveOrder: true,
@@ -71,10 +75,13 @@ function parseXml(source: string): XmlNode {
75
return { name: "#text", attrs: {}, children: [], text: literal };
76
}
77
const attrs = Object.fromEntries(
74
- Object.entries((item[":@"] ?? {}) as Record<string, string>).map(([key, value]) => [
75
- key,
76
- decodeEntities(value),
77
- ]),
78
+ Object.entries((item[":@"] ?? {}) as Record<string, string>).map(([key, value]) => {
79
+ // fast-xml-parser accepts raw '<' in element attributes, which can mask declarations
80
+ // above. Processing instructions end at their first '?>', so their data may hold '<'.
81
+ if (!name.startsWith("?") && value.includes("<"))
82
+ throw new Error("This document contains invalid or unsupported XML.");
83
+ return [key, decodeEntities(value)];
84
+ }),
85
);
86
const namespaces = Object.create(inherited) as Record<string, string>;
87
for (const [key, value] of Object.entries(attrs)) {
@@ -419,8 +426,9 @@ export async function openEpub(archive: JSZip): Promise<OfficeWorkerResult> {
426
const chapter = parseXml(await entry.async("string"));
427
pages.push({
428
title:
422
- text(find(chapter, "title")[0] ?? find(chapter, "h1")[0] ?? chapter).slice(0, 120) ||
423
- `Chapter ${pages.length + 1}`,
429
+ text(find(chapter, "title")[0] ?? find(chapter, "h1")[0] ?? chapter)
430
+ .trim()
431
+ .slice(0, 120) || `Chapter ${pages.length + 1}`,
432
html: await epubHtml(chapter, archive, path),
433
});
434
}