sanitize

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package sanitize supplies immutable HTML fragment allowlists backed by bluemonday. Output is for HTML body content, not scripts, CSS, URLs or attributes.

Index

Constants

View Source
const MaxInputBytes = 1 << 20
View Source
const MaxOutputBytes = 4 << 20

Variables

This section is empty.

Functions

func StripTags

func StripTags(ctx context.Context, input string) (string, error)

StripTags returns escaped HTML text with all tags removed. Active-element content is discarded. Calls share one immutable empty-allowlist policy with the default bounds; it is built on first use.

Types

type Config

type Config struct {
	AllowedTags []string
	InputBytes  int
	OutputBytes int

	// AdditionalURLSchemes accepts only URLSchemeTel. Unknown values and
	// duplicates are invalid; the default HTTP, HTTPS and mailto schemes cannot be added.
	AdditionalURLSchemes []URLScheme

	// AllowRelativeURLs permits local paths, queries and fragments. Protocol-relative
	// URLs (//host) and backslash authority prefixes remain forbidden.
	AllowRelativeURLs bool
}

Config configures allowed HTML tags, URLs and byte budgets.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns default tags and byte budgets, with only HTTP, HTTPS and mailto URLs allowed and relative URLs disabled.

type Policy

type Policy struct {
	// contains filtered or unexported fields
}

Policy never exposes the mutable third-party builder. It is safe for concurrent calls after construction. No CSS, event handlers, data attributes, forms, embedded documents, SVG/MathML, data URLs or script-capable tags are admitted.

func New

func New(config Config) (*Policy, error)

New validates and snapshots configuration into a sanitization policy with restricted attributes and URL schemes.

func (*Policy) HTML

func (p *Policy) HTML(ctx context.Context, input string) (string, error)

HTML sanitizes valid UTF-8 HTML within input and output budgets, honoring context cancellation.

type URLScheme added in v0.12.0

type URLScheme string

URLScheme is an additional URL scheme supported by a sanitization policy.

const URLSchemeTel URLScheme = "tel"

URLSchemeTel permits telephone links in addition to HTTP, HTTPS and mailto.

Jump to

Keyboard shortcuts

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