Documentation
¶
Overview ¶
Package sanitize provides HTML/XSS filtering helpers built on top of bluemonday. It offers several canned policies ranging from a plain-text strict mode to a permissive UGC mode that keeps a safe subset of HTML tags, plus utilities for escaping, stripping tags and sanitizing URLs.
Basic usage:
// Remove dangerous tags but keep safe ones
safe := sanitize.Clean(`<a href="https://x.com">ok</a><script>x</script>`)
// UGC mode: allow b/i/em/strong/a/p/br/ul/ol/li/code/pre ...
body := sanitize.CleanUGC(userContent)
// Strict mode: plain text only
text := sanitize.CleanStrict(`<b>hi</b>`) // "hi"
// URL safety
if sanitize.IsSafeURL(u) {
u = sanitize.SanitizeURL(u)
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clean ¶
Clean performs a basic HTML cleanup: it keeps safe formatting tags and removes dangerous ones (script/style/iframe), event handler attributes (on*) and javascript:/data: URLs.
func CleanMarkdown ¶
CleanMarkdown sanitizes HTML embedded inside Markdown source while preserving Markdown syntax. Inline HTML blocks/tags are passed through CleanUGC; Markdown markup (#, *, >, [], etc.) is left untouched.
func CleanStrict ¶
CleanStrict removes every HTML tag and returns plain, escaped text.
func CleanUGC ¶
CleanUGC sanitizes user generated content using UGCPolicy. It keeps a safe subset of tags (b/i/em/strong/a/p/br/ul/ol/li/code/pre/...) and removes script/style/iframe, on* attributes and dangerous URLs.
func EscapeHTML ¶
EscapeHTML escapes special HTML characters (<, >, &, ", ') to their corresponding entities.
func IsSafeURL ¶
IsSafeURL reports whether the URL uses a safe scheme (http, https, mailto, tel, ftp) or is a relative reference. javascript:, data:, vbscript:, file: and about: URLs are considered unsafe.
func SanitizeURL ¶
SanitizeURL returns the URL with dangerous schemes (javascript:, data:, vbscript:, file:, about:) removed. If the URL is unsafe an empty string is returned. Relative URLs and http(s) URLs are returned unchanged.
func StripTags ¶
StripTags removes all HTML tags but keeps the inner text content. The contents of <script>, <style> and <iframe> blocks are dropped entirely.
func UGCPolicy ¶
func UGCPolicy() *bluemonday.Policy
UGCPolicy returns the bluemonday policy used by CleanUGC. It allows a safe subset of tags commonly used in user generated content (b, i, em, strong, a, p, br, ul, ol, li, code, pre, blockquote, h1-h6, hr, img, span, div, table, thead, tbody, tr, th, td) while stripping script/style/iframe and all on* event handler attributes. The returned policy can be further customized before use.
Types ¶
This section is empty.