diff --git a/packages/app-bundle/manifest.json b/packages/app-bundle/manifest.json index f61f9eff2..d9db67b3a 100644 --- a/packages/app-bundle/manifest.json +++ b/packages/app-bundle/manifest.json @@ -527,7 +527,8 @@ "packages/session-ui/src/components/group-parts.test.ts": "a31ce19009e1d62e75e83b682190a4613da0eed334420409ed73a9e21834ae84", "packages/session-ui/src/components/image-lightbox.tsx": "d6bddb0db8736f8284568d74ae2a33b1c3b1acf51e8627a711ae6637386a0b23", "packages/session-ui/src/components/line-comment-styles.ts": "2077836c13bd3c3f1c39c67722119e448a2dfa123377f94bfd3676634e955d00", - "packages/session-ui/src/components/markdown-file-refs.test.ts": "2172d17a0ee5e49636c8bd323f4fb797a7f41e469d0098864b82d374f0c10144", + "packages/session-ui/src/components/markdown-cache.tsx": "fae72b94c2a6149c44451f8046e13e74c7b2c34c8c7983813ff04eb53e7eead3", + "packages/session-ui/src/components/markdown-file-refs.test.ts": "815753da251b1da23ba08749eeea46eecd55b4671f5a8900a1e0c14593bdc40c", "packages/session-ui/src/components/markdown-file-refs.ts": "417417f3660fe18f04a13ca4765b155b5479d7ce41b3638a57ba1abaa5830054", "packages/session-ui/src/components/markdown-local-images.test.ts": "f614e986656d128f5b72ca772a7f3c8ea607cbed69559227feb3d9286f4110be", "packages/session-ui/src/components/markdown-local-images.ts": "17fd69cdda8056711227601c695d7419bc8147433f7700eb3ed301b1803ce188", @@ -1104,7 +1105,7 @@ "packages/ui/src/v2/components/text-shimmer-v2.tsx" ], "exceptions": [], - "extracted_at": "2026-11-08T15:21:50.674Z", + "extracted_at": "2026-11-23T21:55:54.079Z", "per_package": { "packages/app": { "A": 314, @@ -1143,7 +1144,7 @@ }, "packages/session-ui": { "A": 56, - "M": 32, + "M": 33, "D": 0 }, "packages/ui": { @@ -1153,7 +1154,7 @@ } }, "counts": { - "overlay_total": 1082, + "overlay_total": 1083, "deletions": 7, "server_coupled": 0 }, @@ -1677,6 +1678,7 @@ "packages/session-ui/src/components/group-parts.test.ts": "A", "packages/session-ui/src/components/image-lightbox.tsx": "A", "packages/session-ui/src/components/line-comment-styles.ts": "M", + "packages/session-ui/src/components/markdown-cache.tsx": "M", "packages/session-ui/src/components/markdown-file-refs.test.ts": "A", "packages/session-ui/src/components/markdown-file-refs.ts": "A", "packages/session-ui/src/components/markdown-local-images.test.ts": "A", diff --git a/packages/app-bundle/overlay/packages/session-ui/src/components/markdown-cache.tsx b/packages/app-bundle/overlay/packages/session-ui/src/components/markdown-cache.tsx new file mode 100644 index 000000000..b36455b45 --- /dev/null +++ b/packages/app-bundle/overlay/packages/session-ui/src/components/markdown-cache.tsx @@ -0,0 +1,76 @@ +import { checksum } from "@opencode-ai/core/util/encode" +import DOMPurify from "dompurify" +import { parseMarkdown } from "./markdown-worker" + +export type MarkdownCacheEntry = { + raw: string + hash: string + html: string +} + +const max = 200 +const cache = new Map() +const config = { + USE_PROFILES: { html: true, mathMl: true }, + SANITIZE_NAMED_PROPS: true, + FORBID_TAGS: ["style"], + FORBID_CONTENTS: ["style", "script"], + ADD_TAGS: ["svg", "path"], + ADD_ATTR: ["d", "viewBox", "preserveAspectRatio", "xmlns", "target"], + // AMICODE (#2117): DOMPurify's default URI allow-list plus `file:`. Without + // it an authored `[name](file:///abs/path)` link lost its href and the + // click went nowhere. file:// is the open-file bridge's scheme; the + // extension validates the path (absolute, bounded, on disk or + // posture-resolved) before opening anything. + ALLOWED_URI_REGEXP: + /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp|matrix|file):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i, +} + +if (typeof window !== "undefined" && DOMPurify.isSupported) { + DOMPurify.addHook("afterSanitizeAttributes", (node: Element) => { + if (!(node instanceof HTMLAnchorElement)) return + if (node.target !== "_blank") return + + const rel = node.getAttribute("rel") ?? "" + const set = new Set(rel.split(/\s+/).filter(Boolean)) + set.add("noopener") + set.add("noreferrer") + node.setAttribute("rel", Array.from(set).join(" ")) + }) +} + +export function sanitizeMarkdown(html: string) { + if (!DOMPurify.isSupported) return "" + return DOMPurify.sanitize(html, config) +} + +export function getCachedMarkdown(key: string) { + return cache.get(key) +} + +export function touchCachedMarkdown(key: string, value: MarkdownCacheEntry) { + cache.delete(key) + cache.set(key, value) + + if (cache.size <= max) return + + const first = cache.keys().next().value + if (!first) return + cache.delete(first) +} + +export async function preloadMarkdown(text: string, cacheKey: string) { + const key = `${cacheKey}:0:full` + const cached = getCachedMarkdown(key) + if (cached?.raw === text) { + touchCachedMarkdown(key, cached) + return + } + const hash = checksum(text) + if (!hash) return + touchCachedMarkdown(key, { + raw: text, + hash, + html: sanitizeMarkdown(await parseMarkdown(text)), + }) +} diff --git a/packages/app-bundle/overlay/packages/session-ui/src/components/markdown-file-refs.test.ts b/packages/app-bundle/overlay/packages/session-ui/src/components/markdown-file-refs.test.ts index df7b2a6e9..08ded5a81 100644 --- a/packages/app-bundle/overlay/packages/session-ui/src/components/markdown-file-refs.test.ts +++ b/packages/app-bundle/overlay/packages/session-ui/src/components/markdown-file-refs.test.ts @@ -1,6 +1,8 @@ import { beforeEach, describe, expect, test } from "bun:test" import { readFileSync } from "node:fs" import { resolve } from "node:path" +import DOMPurify from "dompurify" +import { sanitizeMarkdown } from "./markdown-cache" import { cachedFileRef, clearFileRefCache, @@ -116,3 +118,26 @@ describe("registration handles (#1845)", () => { expect(fileRefResolverVersion()).toBe(before + 2) }) }) + +// #2117: an authored `[name](file:///abs/path)` link rendered as an anchor with +// NO href. DOMPurify's default URI allow-list drops the file: scheme, so the +// click handler (setupExternalLinks) saw an empty href and did nothing: no +// open, no toast, no error. file:// is the open-file bridge's own scheme (the +// resolver's rewritten links use it too), so the chat sanitizer must keep it. +// happy-dom's tree walker never reaches attributes during sanitize(), so the +// URI policy is asserted through DOMPurify.isValidAttribute, which reads the +// config the chat sanitizer just applied. +describe("chat sanitizer keeps file:// links (#2117)", () => { + test("file:// hrefs survive; script schemes stay stripped", () => { + sanitizeMarkdown("

x

") + expect(DOMPurify.isValidAttribute("a", "href", "file:///home/u/proj/deck.html")).toBe(true) + expect(DOMPurify.isValidAttribute("a", "href", "file:///home/u/My%20Project/report.pdf")).toBe(true) + expect(DOMPurify.isValidAttribute("a", "href", "https://example.com/x")).toBe(true) + expect(DOMPurify.isValidAttribute("a", "href", "mailto:a@b.c")).toBe(true) + expect(DOMPurify.isValidAttribute("a", "href", "plans/foo.md")).toBe(true) + expect(DOMPurify.isValidAttribute("a", "href", "#heading")).toBe(true) + expect(DOMPurify.isValidAttribute("a", "href", "javascript:alert(1)")).toBe(false) + expect(DOMPurify.isValidAttribute("a", "href", "vbscript:x")).toBe(false) + expect(DOMPurify.isValidAttribute("a", "href", "data:text/html,")).toBe(false) + }) +})