sanitize

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 5 Imported by: 0

README

sanitize

HTML/XSS filtering helpers built on top of bluemonday. Provides 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.

Key functions

  • Clean(s string) string — basic HTML cleanup (keeps safe formatting tags, removes script/style/iframe, on* attrs and javascript:/data: URLs)
  • CleanStrict(s string) string — strict mode: plain text only, all HTML tags removed
  • CleanUGC(s string) string — UGC mode: allows b/i/em/strong/a/p/br/ul/ol/li/code/pre/..., removes script/style/iframe, on* attrs and dangerous URLs
  • EscapeHTML(s string) string — HTML entity escaping
  • StripTags(s string) string — remove all HTML tags but keep text content (drops <script>/<style>/<iframe> bodies)
  • SanitizeURL(s string) string — drop javascript:/data:/vbscript:/file:/about: URLs
  • IsSafeURL(s string) bool — check whether a URL uses a safe scheme
  • CleanMarkdown(s string) string — sanitize HTML embedded in Markdown while preserving Markdown syntax
  • UGCPolicy() *bluemonday.Policy — the reusable UGC policy

Quick start

import "github.com/LingByte/ling-base/common/sanitize"

safe := sanitize.Clean(`<a href="https://x.com">ok</a><script>x</script>`)
// <a href="https://x.com" rel="nofollow">ok</a>

text := sanitize.CleanStrict(`<b>hi</b>`) // "hi"

body := sanitize.CleanUGC(userContent)

if !sanitize.IsSafeURL(u) {
    return errors.New("unsafe url")
}
u = sanitize.SanitizeURL(u)

Dependencies

  • github.com/microcosm-cc/bluemonday v1.0.27

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

func Clean(s string) string

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

func CleanMarkdown(s string) string

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

func CleanStrict(s string) string

CleanStrict removes every HTML tag and returns plain, escaped text.

func CleanUGC

func CleanUGC(s string) string

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

func EscapeHTML(s string) string

EscapeHTML escapes special HTML characters (<, >, &, ", ') to their corresponding entities.

func IsSafeURL

func IsSafeURL(s string) bool

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

func SanitizeURL(s string) string

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

func StripTags(s string) string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL