|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import vm from "node:vm"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | +import { describe, expect, it } from "vitest"; |
| 6 | +import { parseHTML } from "linkedom"; |
| 7 | + |
| 8 | +type SessionEntry = { |
| 9 | + id: string; |
| 10 | + parentId: string | null; |
| 11 | + timestamp: string; |
| 12 | + type: string; |
| 13 | + message?: unknown; |
| 14 | + summary?: string; |
| 15 | + content?: unknown; |
| 16 | + display?: boolean; |
| 17 | + customType?: string; |
| 18 | + provider?: string; |
| 19 | + modelId?: string; |
| 20 | + thinkingLevel?: string; |
| 21 | +}; |
| 22 | + |
| 23 | +type SessionData = { |
| 24 | + header: { id: string; timestamp: string }; |
| 25 | + entries: SessionEntry[]; |
| 26 | + leafId: string; |
| 27 | + systemPrompt: string; |
| 28 | + tools: unknown[]; |
| 29 | +}; |
| 30 | + |
| 31 | +const exportHtmlDir = path.dirname(fileURLToPath(import.meta.url)); |
| 32 | +const templateHtml = fs.readFileSync(path.join(exportHtmlDir, "template.html"), "utf8"); |
| 33 | +const templateJs = fs.readFileSync(path.join(exportHtmlDir, "template.js"), "utf8"); |
| 34 | +const markedJs = fs.readFileSync(path.join(exportHtmlDir, "vendor", "marked.min.js"), "utf8"); |
| 35 | +const highlightJs = fs.readFileSync(path.join(exportHtmlDir, "vendor", "highlight.min.js"), "utf8"); |
| 36 | + |
| 37 | +function renderTemplate(sessionData: SessionData) { |
| 38 | + const html = templateHtml |
| 39 | + .replace("{{CSS}}", "") |
| 40 | + .replace("{{SESSION_DATA}}", Buffer.from(JSON.stringify(sessionData), "utf8").toString("base64")) |
| 41 | + .replace("{{MARKED_JS}}", "") |
| 42 | + .replace("{{HIGHLIGHT_JS}}", "") |
| 43 | + .replace("{{JS}}", ""); |
| 44 | + |
| 45 | + const { document, window } = parseHTML(html); |
| 46 | + if (window.HTMLElement?.prototype) { |
| 47 | + window.HTMLElement.prototype.scrollIntoView = () => {}; |
| 48 | + } |
| 49 | + |
| 50 | + const immediateTimeout = (fn: (...args: unknown[]) => void) => { |
| 51 | + fn(); |
| 52 | + return 0; |
| 53 | + }; |
| 54 | + const runtime: Record<string, unknown> = { |
| 55 | + document, |
| 56 | + console, |
| 57 | + clearTimeout: () => {}, |
| 58 | + setTimeout: immediateTimeout, |
| 59 | + URLSearchParams, |
| 60 | + TextDecoder, |
| 61 | + atob: (s: string) => Buffer.from(s, "base64").toString("binary"), |
| 62 | + btoa: (s: string) => Buffer.from(s, "binary").toString("base64"), |
| 63 | + navigator: { clipboard: { writeText: async () => {} } }, |
| 64 | + history: { replaceState: () => {} }, |
| 65 | + location: { href: "http://localhost/export.html", search: "" }, |
| 66 | + }; |
| 67 | + runtime.window = runtime; |
| 68 | + runtime.self = runtime; |
| 69 | + runtime.globalThis = runtime; |
| 70 | + |
| 71 | + vm.createContext(runtime); |
| 72 | + vm.runInContext(markedJs, runtime); |
| 73 | + vm.runInContext(highlightJs, runtime); |
| 74 | + vm.runInContext(templateJs, runtime); |
| 75 | + return { document }; |
| 76 | +} |
| 77 | + |
| 78 | +function now() { |
| 79 | + return new Date("2026-02-24T00:00:00.000Z").toISOString(); |
| 80 | +} |
| 81 | + |
| 82 | +describe("export html security hardening", () => { |
| 83 | + it("escapes raw HTML from markdown blocks", () => { |
| 84 | + const attack = "<img src=x onerror=alert(1)>"; |
| 85 | + const session: SessionData = { |
| 86 | + header: { id: "session-1", timestamp: now() }, |
| 87 | + entries: [ |
| 88 | + { |
| 89 | + id: "1", |
| 90 | + parentId: null, |
| 91 | + timestamp: now(), |
| 92 | + type: "message", |
| 93 | + message: { role: "user", content: attack }, |
| 94 | + }, |
| 95 | + { |
| 96 | + id: "2", |
| 97 | + parentId: "1", |
| 98 | + timestamp: now(), |
| 99 | + type: "branch_summary", |
| 100 | + summary: attack, |
| 101 | + }, |
| 102 | + { |
| 103 | + id: "3", |
| 104 | + parentId: "2", |
| 105 | + timestamp: now(), |
| 106 | + type: "custom_message", |
| 107 | + customType: "x", |
| 108 | + display: true, |
| 109 | + content: attack, |
| 110 | + }, |
| 111 | + ], |
| 112 | + leafId: "3", |
| 113 | + systemPrompt: "", |
| 114 | + tools: [], |
| 115 | + }; |
| 116 | + |
| 117 | + const { document } = renderTemplate(session); |
| 118 | + const messages = document.getElementById("messages"); |
| 119 | + expect(messages).toBeTruthy(); |
| 120 | + expect(messages?.querySelector("img[onerror]")).toBeNull(); |
| 121 | + expect(messages?.innerHTML).toContain("<img src=x onerror=alert(1)>"); |
| 122 | + }); |
| 123 | + |
| 124 | + it("escapes tree and header metadata fields", () => { |
| 125 | + const attack = "<img src=x onerror=alert(9)>"; |
| 126 | + const baseEntries: SessionEntry[] = [ |
| 127 | + { |
| 128 | + id: "1", |
| 129 | + parentId: null, |
| 130 | + timestamp: now(), |
| 131 | + type: "message", |
| 132 | + message: { role: "user", content: "ok" }, |
| 133 | + }, |
| 134 | + { |
| 135 | + id: "2", |
| 136 | + parentId: "1", |
| 137 | + timestamp: now(), |
| 138 | + type: "message", |
| 139 | + message: { |
| 140 | + role: "assistant", |
| 141 | + model: attack, |
| 142 | + provider: "p", |
| 143 | + content: [{ type: "text", text: "assistant" }], |
| 144 | + }, |
| 145 | + }, |
| 146 | + { |
| 147 | + id: "3", |
| 148 | + parentId: "2", |
| 149 | + timestamp: now(), |
| 150 | + type: "message", |
| 151 | + message: { role: "toolResult", toolName: attack }, |
| 152 | + }, |
| 153 | + { |
| 154 | + id: "4", |
| 155 | + parentId: "3", |
| 156 | + timestamp: now(), |
| 157 | + type: "model_change", |
| 158 | + provider: "p", |
| 159 | + modelId: attack, |
| 160 | + }, |
| 161 | + { |
| 162 | + id: "5", |
| 163 | + parentId: "4", |
| 164 | + timestamp: now(), |
| 165 | + type: "thinking_level_change", |
| 166 | + thinkingLevel: attack, |
| 167 | + }, |
| 168 | + { |
| 169 | + id: "6", |
| 170 | + parentId: "5", |
| 171 | + timestamp: now(), |
| 172 | + type: attack, |
| 173 | + }, |
| 174 | + ]; |
| 175 | + |
| 176 | + const headerSession: SessionData = { |
| 177 | + header: { id: "session-2", timestamp: now() }, |
| 178 | + entries: baseEntries, |
| 179 | + leafId: "6", |
| 180 | + systemPrompt: "", |
| 181 | + tools: [], |
| 182 | + }; |
| 183 | + |
| 184 | + const { document } = renderTemplate(headerSession); |
| 185 | + const tree = document.getElementById("tree-container"); |
| 186 | + const header = document.getElementById("header-container"); |
| 187 | + expect(tree).toBeTruthy(); |
| 188 | + expect(header).toBeTruthy(); |
| 189 | + expect(tree?.querySelector("img[onerror]")).toBeNull(); |
| 190 | + expect(header?.querySelector("img[onerror]")).toBeNull(); |
| 191 | + expect(tree?.innerHTML).toContain("<img src=x onerror=alert(9)>"); |
| 192 | + expect(header?.innerHTML).toContain("<img src=x onerror=alert(9)>"); |
| 193 | + |
| 194 | + const modelLeafSession: SessionData = { |
| 195 | + header: { id: "session-2-model", timestamp: now() }, |
| 196 | + entries: baseEntries, |
| 197 | + leafId: "4", |
| 198 | + systemPrompt: "", |
| 199 | + tools: [], |
| 200 | + }; |
| 201 | + const modelLeaf = renderTemplate(modelLeafSession).document; |
| 202 | + expect(modelLeaf.getElementById("tree-container")?.querySelector("img[onerror]")).toBeNull(); |
| 203 | + expect(modelLeaf.getElementById("tree-container")?.innerHTML).toContain( |
| 204 | + "<img src=x onerror=alert(9)>", |
| 205 | + ); |
| 206 | + |
| 207 | + const thinkingLeafSession: SessionData = { |
| 208 | + header: { id: "session-2-thinking", timestamp: now() }, |
| 209 | + entries: baseEntries, |
| 210 | + leafId: "5", |
| 211 | + systemPrompt: "", |
| 212 | + tools: [], |
| 213 | + }; |
| 214 | + const thinkingLeaf = renderTemplate(thinkingLeafSession).document; |
| 215 | + expect(thinkingLeaf.getElementById("tree-container")?.querySelector("img[onerror]")).toBeNull(); |
| 216 | + expect(thinkingLeaf.getElementById("tree-container")?.innerHTML).toContain( |
| 217 | + "<img src=x onerror=alert(9)>", |
| 218 | + ); |
| 219 | + }); |
| 220 | + |
| 221 | + it("sanitizes image MIME types used in data URLs", () => { |
| 222 | + const session: SessionData = { |
| 223 | + header: { id: "session-3", timestamp: now() }, |
| 224 | + entries: [ |
| 225 | + { |
| 226 | + id: "1", |
| 227 | + parentId: null, |
| 228 | + timestamp: now(), |
| 229 | + type: "message", |
| 230 | + message: { |
| 231 | + role: "user", |
| 232 | + content: [ |
| 233 | + { |
| 234 | + type: "image", |
| 235 | + data: "AAAA", |
| 236 | + mimeType: 'image/png" onerror="alert(7)', |
| 237 | + }, |
| 238 | + ], |
| 239 | + }, |
| 240 | + }, |
| 241 | + ], |
| 242 | + leafId: "1", |
| 243 | + systemPrompt: "", |
| 244 | + tools: [], |
| 245 | + }; |
| 246 | + |
| 247 | + const { document } = renderTemplate(session); |
| 248 | + const img = document.querySelector("#messages .message-image"); |
| 249 | + expect(img).toBeTruthy(); |
| 250 | + expect(img?.getAttribute("onerror")).toBeNull(); |
| 251 | + expect(img?.getAttribute("src")).toBe("data:application/octet-stream;base64,AAAA"); |
| 252 | + }); |
| 253 | +}); |
0 commit comments