Documentation
¶
Overview ¶
Package render converts Markdown or raw HTML into a sanitized HTML fragment safe to embed in a trusted page template.
It owns the reusable, document-format-agnostic part of that security boundary: Markdown-to-HTML conversion, the explicit allowlist sanitizer, dangerous URL scheme rejection, and native audio/video/source/track handling. Markdown and HTML both return a fragment, not a complete HTML document; routing, local asset serving and the trusted outer page template stay the hosting application's own job, since those are inherently specific to how each application is built and served. ContentSecurityPolicy and SecurityHeaders provide a ready-to-use reference policy for the one full-page concern that pairs directly with what the sanitizer allows through, so a consumer is not forced to invent an equivalent CSP from scratch, but using them is optional.
This package has no dependency on OCIDoc registry, store or CLI packages; it depends on github.com/ocidoc/ocidoc-go/spec only for spec.ValidateBundlePath, the same portable bundle-path rule OCIDoc artifacts already use, so a local URL is judged "valid" by the exact rule that decided whether the file it points to could exist in an OCIDoc artifact in the first place.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContentSecurityPolicy ¶
func ContentSecurityPolicy(mode ExternalAssets) string
ContentSecurityPolicy returns the reference deny-by-default CSP for a page that embeds this package's sanitized fragment output, scoped by mode: under ExternalAssetsBlock, image and media sources are same-origin only matching the fragment itself never containing an external subresource URL in that mode.
A caller is free to design its own full-page policy instead - this is a ready-to-use default paired with what the sanitizer actually allows through, not a requirement.
func HTML ¶
HTML sanitizes raw HTML source to a safe HTML fragment: the explicit document allowlist, then URL classification and media/link normalization.
WithUnsafe (Markdown's own raw-HTML passthrough) does not make its output trusted - this is the actual sanitization step, and every caller must run it before that output reaches anything.
Example ¶
fragment, err := HTML([]byte(
`<p>Read <a href="https://example.com">the guide</a>.</p><img src="https://example.com/logo.svg">`),
Options{
DocumentPath: "README.md",
ExternalAssets: ExternalAssetsBlock,
},
)
if err != nil {
panic(err)
}
fmt.Print(string(fragment))
Output: <p>Read <a href="https://example.com" rel="noopener noreferrer">the guide</a>.</p><img/>
func Markdown ¶
Markdown renders Markdown source to a sanitized HTML fragment: Goldmark conversion (raw HTML passthrough enabled) followed by the same sanitizer and URL/media policy HTML applies directly.
Example ¶
fragment, err := Markdown([]byte("# Guide\n\n<script>alert(1)</script>"), Options{
DocumentPath: "docs/README.md",
})
if err != nil {
panic(err)
}
fmt.Print(string(fragment))
Output: <h1>Guide</h1>
func SecurityHeaders ¶
func SecurityHeaders(header http.Header, mode ExternalAssets)
SecurityHeaders sets the reference response headers for a page that embeds this package's sanitized fragment output: ContentSecurityPolicy plus Referrer-Policy, X-Content-Type-Options and X-Frame-Options.
As with ContentSecurityPolicy itself, a caller may set these as-is, adapt them or build an entirely different policy - nothing else in this package depends on them being used.
Types ¶
type ExternalAssets ¶
type ExternalAssets string
ExternalAssets controls whether HTTP/HTTPS subresources (images, audio, video, source, track, poster) outside the document itself are left in place or stripped. It never affects navigation links (<a href>), only subresource loads.
const ( // ExternalAssetsAllow preserves HTTP(S) document subresources. ExternalAssetsAllow ExternalAssets = "allow" // ExternalAssetsBlock removes HTTP(S) document subresources. ExternalAssetsBlock ExternalAssets = "block" )
type Options ¶
type Options struct {
// DocumentPath is the source document's own bundle-relative path,
// used to resolve relative local URLs (images, links, media) against its directory.
// Required: local URL resolution has no other frame of reference.
DocumentPath string
// ExternalAssets defaults to ExternalAssetsAllow when empty.
ExternalAssets ExternalAssets
}
Options controls Markdown and HTML.