Safe Rendering for Untrusted Content
Problem
User-submitted or model-generated Markdown arrives: it may contain script tags, javascript: links, entity-encoded scheme tricks, or megabytes of nesting. Display it without ever executing, ever hanging, or ever throwing out of the render path.
Solution
ts
import { parseProse, renderProseDom, ScribeError } from '@vielzeug/scribe';
function renderProseSafely(container: HTMLElement, source: string): void {
try {
container.replaceChildren(renderProseDom(parseProse(source)));
} catch (error) {
if (error instanceof ScribeError) {
// Oversized or pathologically nested: show the raw source as plain text.
container.replaceChildren(document.createTextNode(source));
} else {
throw error; // never swallow real bugs
}
}
}What the pipeline already handles before you catch anything:
ts
const doc = parseProse(
'<script>alert(1)</script>\n' +
'[click](javascript:alert(1))\n' +
'[sneaky](:javascript:alert(1))\n' +
'[ok](/relative/page)',
);
// <script>…</script> → escaped text (raw HTML is not in the profile)
// javascript: links → label text "click", dev warning, no anchor ever created
// :javascript: → entities are decoded BEFORE the policy check: same degradation
// /relative/page → allowed: relative URLs always render as linksNarrow the policy for stricter surfaces (custom allowlist) — anything not listed degrades the same way:
ts
const strict = parseProse(source, { linkProtocols: ['https'] }); // no mailto, no exceptionsPitfalls
- Degradation keeps the link label: content is never silently dropped, only un-linked.
data:cannot be allowed even vialinkProtocols—data:image/svg+xmlis a script vector.- The dev warning is compiled out in production builds; if you need reporting, wrap the input scan yourself — the policy decision itself never throws.
- On a
ScribeLimitErrorfallback, render the source withtextContent(as above), neverinnerHTML.
Related
- Usage Guide
- API Reference
- Refine: Prose component — the same pipeline as
ore-prose, themed with Refine tokens