GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(app): sanitize markdown -> html · ayc/opencode@d7a1c26

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • bun.lock
  • package.json
  • packages/ui
    • package.json
    • src/components
      • markdown.tsx
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎bun.lock‎

Copy file name to clipboardExpand all lines: bun.lock
+6Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎package.json‎

Copy file name to clipboardExpand all lines: package.json
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@solid-primitives/storage": "4.3.3",
3737
"@tailwindcss/vite": "4.1.11",
3838
"diff": "8.0.2",
39+
"dompurify": "3.3.1",
3940
"ai": "5.0.119",
4041
"hono": "4.10.7",
4142
"hono-openapi": "1.1.2",
Collapse file

‎packages/ui/package.json‎

Copy file name to clipboardExpand all lines: packages/ui/package.json
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"fuzzysort": "catalog:",
5252
"katex": "0.16.27",
5353
"luxon": "catalog:",
54+
"dompurify": "catalog:",
5455
"marked": "catalog:",
5556
"marked-katex-extension": "5.1.6",
5657
"marked-shiki": "catalog:",
Collapse file

‎packages/ui/src/components/markdown.tsx‎

Copy file name to clipboardExpand all lines: packages/ui/src/components/markdown.tsx
+32-2Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { useMarked } from "../context/marked"
2+
import DOMPurify from "dompurify"
23
import { checksum } from "@opencode-ai/util/encode"
34
import { ComponentProps, createResource, splitProps } from "solid-js"
5+
import { isServer } from "solid-js/web"
46

57
type Entry = {
68
hash: string
@@ -10,6 +12,31 @@ type Entry = {
1012
const max = 200
1113
const cache = new Map<string, Entry>()
1214

15+
if (typeof window !== "undefined" && DOMPurify.isSupported) {
16+
DOMPurify.addHook("afterSanitizeAttributes", (node: Element) => {
17+
if (!(node instanceof HTMLAnchorElement)) return
18+
if (node.target !== "_blank") return
19+
20+
const rel = node.getAttribute("rel") ?? ""
21+
const set = new Set(rel.split(/\s+/).filter(Boolean))
22+
set.add("noopener")
23+
set.add("noreferrer")
24+
node.setAttribute("rel", Array.from(set).join(" "))
25+
})
26+
}
27+
28+
const config = {
29+
USE_PROFILES: { html: true, mathMl: true },
30+
SANITIZE_NAMED_PROPS: true,
31+
FORBID_TAGS: ["style"],
32+
FORBID_CONTENTS: ["style", "script"],
33+
}
34+
35+
function sanitize(html: string) {
36+
if (!DOMPurify.isSupported) return ""
37+
return DOMPurify.sanitize(html, config)
38+
}
39+
1340
function touch(key: string, value: Entry) {
1441
cache.delete(key)
1542
cache.set(key, value)
@@ -34,6 +61,8 @@ export function Markdown(
3461
const [html] = createResource(
3562
() => local.text,
3663
async (markdown) => {
64+
if (isServer) return ""
65+
3766
const hash = checksum(markdown)
3867
const key = local.cacheKey ?? hash
3968

@@ -46,8 +75,9 @@ export function Markdown(
4675
}
4776

4877
const next = await marked.parse(markdown)
49-
if (key && hash) touch(key, { hash, html: next })
50-
return next
78+
const safe = sanitize(next)
79+
if (key && hash) touch(key, { hash, html: safe })
80+
return safe
5181
},
5282
{ initialValue: "" },
5383
)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.