safe-fragment
@johnhenry/safe-fragment is a set of framework-agnostic, dependency-light Web Components for rendering HTML-like source
into live DOM only after applying an explicit, versioned security profile: an allowlist of elements, attributes, and URL
schemes. The first parse is done by the native
HTML Sanitizer API or a locked-down
DOMPurify fallback, and a shared pass then re-derives the allowlist from the profile and
rebuilds the output from fresh nodes. The same pipeline is available without the element as sanitizeToFragment().
This is not a generic <inner-html> wrapper. <safe-fragment> never assigns untrusted strings through innerHTML,
outerHTML, insertAdjacentHTML, setHTMLUnsafe, or any equivalent unsafe sink, and there is no “unsafe”, “trusted” or
“allowScripts” escape hatch anywhere in the API.
<safe-fragment profile="article-v1"> <template> <p>Hello <strong>world</strong>. <img src="x" onerror="alert(1)" /></p> </template></safe-fragment><!-- Renders: <p>Hello <strong>world</strong>. <img src="x"></p> The onerror attribute is gone; nothing executes. -->
<script type="module"> import { registerSafeFragment } from "@johnhenry/safe-fragment"; registerSafeFragment(); // once, from browser-executed code</script>Importing the package never touches window, document, HTMLElement or customElements, so it is safe to import from
Node or SSR code; nothing is defined until you call registerSafeFragment().
What is in the box
Section titled “What is in the box”| Piece | What it is |
|---|---|
<safe-fragment> |
The element. One markup source, one named profile, sanitized render, events. |
| Profiles | plain-text-v1, article-v1, ui-v1, component-template-v1 (for a web component’s template: <slot>, part, and ids kept inside a shadow root on request) and the email-v1 scaffold; versioned, frozen, and extensible by deriving your own. |
sanitizeToFragment() |
The same pipeline without the element: a detached, profile-conformant DocumentFragment plus a report. |
src fetch |
Optional remote markup, disabled by default, GET-only, origin-allowlisted, size- and time-capped. |
<example-sandbox> |
A separate component that runs application-authored, executable code in an allow-scripts-only iframe. Never for untrusted input. |
Where to go next
Section titled “Where to go next”- Getting started: register the element, render something, and the no-bundler import map.
- The
<safe-fragment>element: attributes, properties, methods, events and therender()result. - Profiles: the built-ins, versioning, and deriving your own.
- Security model and Limitations and traps: what is guaranteed and what is still yours.
- API reference: every export.
The source is on GitHub. There is no npm link because nothing has been published.
Family
Section titled “Family”safe-fragment is one of a few browser-stack packages that each own one concern. None is a dependency of another.
- html-modules wires
sanitizeToFragment()in as the template sanitizer for HTML modules from less-trusted origins, through asanitizehook and an adapter that derives a profile fromcomponent-template-v1(Sanitizing templates). A<safe-fragment>inside a trusted component’s template does the same job declaratively for untrusted text a page hands the component. - window-algebra: its
htmlSurface(element)hosts any element, sohtmlSurface(safeFragmentEl)puts sanitized content in a window. It only callsmountandunmount, so neither package knows the other. With the defaultscope="light"the rendered wrapper is an ordinary child of the host, so it moves with the element into a pop-out document. - mport is the CDN router that compiles to an import map. safe-fragment’s DOMPurify fallback needs a
dompurifyimport-map entry on pages with no bundler; on raw-file CDNs list it explicitly (mport build @johnhenry/safe-fragment@0 [email protected]) or let mport add it from this package’sdependencieswith--dependencies(build(specs, { dependencies: true })); see No bundler: the import map. - Untrusted Desk: the Orrery planet that renders untrusted notes with
<safe-fragment>, loads one module through thesanitizeadapter, and proves it again in a strict-CSP, Trusted Types frame.
The one real runtime dependency is DOMPurify (exact-pinned), used only as the fallback sanitization engine (ADR 0002), never vendored and never used with its permissive defaults.