gosanitizer

package module
v0.0.0-...-76a0893 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package gosanitizer provides production-grade input sanitization: HTML, SQL, path traversal, null-byte injection, Unicode normalization, struct-tag-driven bulk sanitization, and XSS prevention.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllowURLSchemes

func AllowURLSchemes(schemes ...string)

AllowURLSchemes overrides the default allowed scheme set.

func CollapseWhitespace

func CollapseWhitespace(s string) string

CollapseWhitespace reduces internal runs of whitespace to a single space.

func ContainsSQLInjection

func ContainsSQLInjection(s string) bool

ContainsSQLInjection heuristically detects SQL injection patterns.

func EscapeHTML

func EscapeHTML(s string) string

EscapeHTML encodes HTML special characters, preventing XSS.

func EscapeSQL

func EscapeSQL(s string) string

EscapeSQL escapes single quotes and removes obvious SQL injection patterns. Prefer parameterised queries; use this as a last-resort defence-in-depth layer.

func IsEmail

func IsEmail(s string) bool

IsEmail reports whether s is a syntactically valid e-mail address.

func IsPathTraversal

func IsPathTraversal(p string) bool

IsPathTraversal reports whether the path contains traversal sequences.

func IsValidUTF8

func IsValidUTF8(s string) bool

IsValidUTF8 reports whether s is valid UTF-8.

func NormalizeUnicode

func NormalizeUnicode(s string) string

NormalizeUnicode converts the string to NFC form and removes control characters.

func RemoveNullBytes

func RemoveNullBytes(s string) string

RemoveNullBytes strips null bytes (0x00) from s.

func Sanitize

func Sanitize(v interface{})

Sanitize walks a struct pointer and applies sanitization based on the `sanitize` struct tag. Supported tag values (comma-separated):

trim         – TrimWhitespace
html         – StripHTML
escape       – EscapeHTML
lower        – strings.ToLower
upper        – strings.ToUpper
slug         – SanitizeSlug
alphanum     – SanitizeAlphaNum
email        – TrimWhitespace + lower
phone        – SanitizePhone
url          – SanitizeURL
filename     – SanitizeFilename
nullbyte     – RemoveNullBytes
collapse     – CollapseWhitespace
display      – SanitizeForDisplay

func SanitizeAlphaNum

func SanitizeAlphaNum(s string) string

SanitizeAlphaNum removes all characters that are not letters or digits.

func SanitizeFilename

func SanitizeFilename(s string) string

SanitizeFilename removes characters illegal in file names across all OSes.

func SanitizeForDisplay

func SanitizeForDisplay(s string) string

SanitizeForDisplay sanitises user content for safe HTML display. It strips tags, escapes HTML entities, and removes event handlers.

func SanitizePath

func SanitizePath(p string) string

SanitizePath cleans a file path and prevents directory traversal. The result is always relative (no leading separator) and cannot escape root.

func SanitizePhone

func SanitizePhone(s string) string

SanitizePhone strips non-phone characters and preserves +, -, (, ), digits, spaces.

func SanitizeSlug

func SanitizeSlug(s string) string

SanitizeSlug removes all characters that are not letters, digits, hyphens, or underscores.

func SanitizeURL

func SanitizeURL(raw string) string

SanitizeURL validates and normalises a URL. Returns an empty string if the URL is malformed or uses a disallowed scheme.

func StripHTML

func StripHTML(s string) string

StripHTML removes all HTML tags and returns plain text.

func TrimWhitespace

func TrimWhitespace(s string) string

TrimWhitespace trims all leading/trailing Unicode whitespace.

func Truncate

func Truncate(s string, maxRunes int) string

Truncate returns at most maxRunes runes from s (safe for multibyte strings).

Types

This section is empty.

Jump to

Keyboard shortcuts

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