|
| 1 | +import DOMPurify from "/vendor/dompurify/purify.es.mjs"; |
| 2 | +import { marked } from "/vendor/marked/marked.esm.js"; |
| 3 | +import { addBlankTargetsToLinks } from "/js/html-links.js"; |
| 4 | + |
| 5 | +const GITHUB_REPO_ROUTE_PREFIXES = new Set([ |
| 6 | + "actions", |
| 7 | + "blob", |
| 8 | + "branches", |
| 9 | + "commit", |
| 10 | + "commits", |
| 11 | + "compare", |
| 12 | + "discussions", |
| 13 | + "issues", |
| 14 | + "labels", |
| 15 | + "milestones", |
| 16 | + "packages", |
| 17 | + "projects", |
| 18 | + "pulls", |
| 19 | + "raw", |
| 20 | + "releases", |
| 21 | + "security", |
| 22 | + "tags", |
| 23 | + "tree", |
| 24 | + "wiki", |
| 25 | +]); |
| 26 | + |
| 27 | +const DOMPURIFY_CONFIG = Object.freeze({ |
| 28 | + USE_PROFILES: { html: true }, |
| 29 | + FORBID_TAGS: ["script", "iframe", "object", "embed", "svg", "math"], |
| 30 | +}); |
| 31 | + |
| 32 | +function parseGithubRepoContext(githubUrl) { |
| 33 | + if (!githubUrl || typeof githubUrl !== "string") return null; |
| 34 | + |
| 35 | + let repoUrl; |
| 36 | + try { |
| 37 | + repoUrl = new URL(githubUrl.trim().replace(/\.git$/i, "")); |
| 38 | + } catch { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + if (repoUrl.hostname !== "github.com") return null; |
| 43 | + |
| 44 | + const [owner, repo] = repoUrl.pathname |
| 45 | + .replace(/^\/+|\/+$/g, "") |
| 46 | + .split("/"); |
| 47 | + if (!owner || !repo) return null; |
| 48 | + |
| 49 | + return { owner, repo }; |
| 50 | +} |
| 51 | + |
| 52 | +function shouldSkipRebase(value) { |
| 53 | + return ( |
| 54 | + !value || |
| 55 | + value.startsWith("#") || |
| 56 | + value.startsWith("//") || |
| 57 | + /^[a-zA-Z][a-zA-Z\d+.-]*:/.test(value) |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +function resolveRepoPath(value) { |
| 62 | + if (shouldSkipRebase(value)) return null; |
| 63 | + try { |
| 64 | + const resolved = new URL(value, "https://repo-root.invalid/"); |
| 65 | + return `${resolved.pathname.replace(/^\/+/, "")}${resolved.search}${resolved.hash}`; |
| 66 | + } catch { |
| 67 | + return null; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +function isGithubRepoRoutePath(repoPath) { |
| 72 | + const pathOnly = repoPath |
| 73 | + .split(/[?#]/, 1)[0] |
| 74 | + .replace(/^\/+|\/+$/g, ""); |
| 75 | + if (!pathOnly) return false; |
| 76 | + const firstSegment = pathOnly.split("/")[0].toLowerCase(); |
| 77 | + return GITHUB_REPO_ROUTE_PREFIXES.has(firstSegment); |
| 78 | +} |
| 79 | + |
| 80 | +function isSafeUrlValue(value, attributeName) { |
| 81 | + const normalized = String(value || "").trim(); |
| 82 | + if (!normalized) return true; |
| 83 | + if ( |
| 84 | + normalized.startsWith("#") || |
| 85 | + normalized.startsWith("/") || |
| 86 | + normalized.startsWith("./") || |
| 87 | + normalized.startsWith("../") || |
| 88 | + normalized.startsWith("?") |
| 89 | + ) { |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + try { |
| 94 | + const url = new URL(normalized, "https://sanitizer.invalid/"); |
| 95 | + if (url.origin === "https://sanitizer.invalid") { |
| 96 | + return true; |
| 97 | + } |
| 98 | + |
| 99 | + const protocol = url.protocol.toLowerCase(); |
| 100 | + if (protocol === "http:" || protocol === "https:") return true; |
| 101 | + if (attributeName === "href" && (protocol === "mailto:" || protocol === "tel:")) { |
| 102 | + return true; |
| 103 | + } |
| 104 | + } catch { |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + return false; |
| 109 | +} |
| 110 | + |
| 111 | +function stripUnsafeUrlAttributes(html) { |
| 112 | + const doc = new DOMParser().parseFromString(html, "text/html"); |
| 113 | + |
| 114 | + doc.querySelectorAll("[href], [src]").forEach((element) => { |
| 115 | + for (const attributeName of ["href", "src"]) { |
| 116 | + if (!element.hasAttribute(attributeName)) continue; |
| 117 | + const value = element.getAttribute(attributeName) || ""; |
| 118 | + if (!isSafeUrlValue(value, attributeName)) { |
| 119 | + element.removeAttribute(attributeName); |
| 120 | + } |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + return doc.body.innerHTML; |
| 125 | +} |
| 126 | + |
| 127 | +export function sanitizeHtml(html) { |
| 128 | + if (!html || typeof html !== "string") return ""; |
| 129 | + const sanitized = DOMPurify.sanitize(html, DOMPURIFY_CONFIG); |
| 130 | + return stripUnsafeUrlAttributes(sanitized); |
| 131 | +} |
| 132 | + |
| 133 | +export function rebaseGithubReadmeHtml(html, githubUrl, branch) { |
| 134 | + if (!html || typeof html !== "string" || !branch) return html; |
| 135 | + |
| 136 | + const repoContext = parseGithubRepoContext(githubUrl); |
| 137 | + if (!repoContext) return html; |
| 138 | + |
| 139 | + const { owner, repo } = repoContext; |
| 140 | + const repoWebBase = `https://github.com/${owner}/${repo}`; |
| 141 | + const repoBlobBase = `${repoWebBase}/blob/${branch}`; |
| 142 | + const repoRawBase = `https://raw.githubusercontent.com/${owner}/${repo}/${branch}`; |
| 143 | + const doc = new DOMParser().parseFromString(html, "text/html"); |
| 144 | + |
| 145 | + // Single-segment links like "releases" are ambiguous, so README rebasing |
| 146 | + // needs an explicit GitHub repo-route allowlist instead of a single base URL. |
| 147 | + doc.querySelectorAll("a[href]").forEach((anchor) => { |
| 148 | + const href = (anchor.getAttribute("href") || "").trim(); |
| 149 | + const repoPath = resolveRepoPath(href); |
| 150 | + if (!repoPath) return; |
| 151 | + const base = isGithubRepoRoutePath(repoPath) ? repoWebBase : repoBlobBase; |
| 152 | + anchor.setAttribute("href", `${base}/${repoPath}`); |
| 153 | + }); |
| 154 | + |
| 155 | + doc.querySelectorAll("img[src]").forEach((image) => { |
| 156 | + const src = (image.getAttribute("src") || "").trim(); |
| 157 | + const repoPath = resolveRepoPath(src); |
| 158 | + if (!repoPath) return; |
| 159 | + image.setAttribute("src", `${repoRawBase}/${repoPath}`); |
| 160 | + }); |
| 161 | + |
| 162 | + return doc.body.innerHTML; |
| 163 | +} |
| 164 | + |
| 165 | +export function renderSafeMarkdown(markdown, options = {}) { |
| 166 | + if (!markdown) return ""; |
| 167 | + |
| 168 | + const { githubUrl = "", branch = "", openExternalLinksInNewTab = true } = options; |
| 169 | + |
| 170 | + let html = marked.parse(markdown, { breaks: true }); |
| 171 | + if (githubUrl && branch) { |
| 172 | + html = rebaseGithubReadmeHtml(html, githubUrl, branch); |
| 173 | + } |
| 174 | + |
| 175 | + html = sanitizeHtml(html); |
| 176 | + |
| 177 | + if (openExternalLinksInNewTab) { |
| 178 | + html = addBlankTargetsToLinks(html); |
| 179 | + } |
| 180 | + |
| 181 | + return html; |
| 182 | +} |
0 commit comments