Documentation
¶
Overview ¶
Package isl provides a Go Input Sanitization Library (goisl).
goisl is inspired by WordPress's escaping and sanitization system and is designed to help developers securely process user input by providing modular, testable sanitization and escaping functions for common use cases like emails, file names, URLs, and HTML. Custom hooks are supported to support just about any sanitization scenario imagineable.
See README.md for examples and usage.
Index ¶
- Constants
- Variables
- func EscapePlainText(input string, hook EscapePlainTextHook) string
- func EscapeURL(input string, context string, hook URLHook) (string, error)
- func HTMLSanitize(content string, allowedHTML map[string][]string) string
- func HTMLSanitizeBasic(content string) string
- func IsAllowedProtocol(scheme string, allowedProtocols []string) bool
- func MustHTMLSanitizeBasic(content string) string
- func MustSanitizeEmailBasic(input string) string
- func MustSanitizeFileNameBasic(input string) string
- func MustSanitizeURLBasic(input string) string
- func SafeEscapeHTML(input string) string
- func SanitizeEmail(input string, hook EmailHook) (string, error)
- func SanitizeEmailBasic(input string) (string, error)
- func SanitizeFileName(input string, hook FileNameHook) (string, error)
- func SanitizeFileNameBasic(input string) (string, error)
- func SanitizeURL(input string) (string, error)
- func SanitizeURLBasic(input string) (string, error)
- type EmailHook
- type EscapePlainTextHook
- type FileNameHook
- type SanitizedStringFlag
- type SanitizedTextFlag
- type URLHook
Constants ¶
const (
MaxFileNameLength = 255
)
Constants for sanitization
Variables ¶
var AllowedHTML = map[string][]string{ "b": nil, "a": {"href"}, "img": {"src", "alt"}, }
AllowedHTML defines allowed tags and their permitted attributes.
var EscapeAllowedProtocols = []string{"http", "https", "mailto", "ftp"}
EscapeAllowedProtocols defines the list of acceptable URL schemes for escaping.
var SanitizeAllowedProtocols = []string{
"http", "https", "mailto", "ftp", "ftps", "news", "irc", "irc6",
"ircs", "gopher", "nntp", "feed", "telnet", "mms", "rtsp", "sms",
"svn", "tel", "fax", "xmpp", "webcal", "urn",
}
SanitizeAllowedProtocols defines the list of acceptable URL schemes for sanitization.
Functions ¶
func EscapePlainText ¶
func EscapePlainText(input string, hook EscapePlainTextHook) string
EscapePlainText sanitizes plain text by removing unwanted characters. It allows customization through an optional hook to permit additional characters.
func EscapeURL ¶
EscapeURL sanitizes and escapes a URL, applying an optional custom hook. This is a known and accepted complexity (gocyclo > 15).
func HTMLSanitize ¶
HTMLSanitize sanitizes content by removing unwanted HTML tags, attributes, and protocols.
func HTMLSanitizeBasic ¶ added in v1.1.0
HTMLSanitizeBasic sanitizes HTML using the default allowed HTML map.
func IsAllowedProtocol ¶
IsAllowedProtocol checks if a URL scheme is in the provided allowed list.
func MustHTMLSanitizeBasic ¶ added in v1.1.0
MustHTMLSanitizeBasic runs HTMLSanitize using the default AllowedHTML rules.
func MustSanitizeEmailBasic ¶ added in v1.1.0
MustSanitizeEmailBasic is a fail-fast wrapper that panics if SanitizeEmailBasic returns an error.
func MustSanitizeFileNameBasic ¶ added in v1.1.0
MustSanitizeFileNameBasic is a fail-fast wrapper that panics if SanitizeFileNameBasic returns an error.
func MustSanitizeURLBasic ¶ added in v1.1.0
MustSanitizeURLBasic is a fail-fast wrapper that panics if SanitizeURLBasic returns an error.
func SafeEscapeHTML ¶
SafeEscapeHTML escapes only specific characters, excluding '%'.
func SanitizeEmail ¶
SanitizeEmail sanitizes an email address with optional hooks for custom behavior.
func SanitizeEmailBasic ¶ added in v1.1.0
SanitizeEmailBasic sanitizes the email input using default behavior (no hook).
func SanitizeFileName ¶
func SanitizeFileName(input string, hook FileNameHook) (string, error)
SanitizeFileName sanitizes a filename by removing unwanted characters, handling multiple extensions, preventing directory traversal, normalizing Unicode characters, and enforcing filename length constraints. An optional custom hook can be applied for additional validation or transformation.
func SanitizeFileNameBasic ¶ added in v1.1.0
SanitizeFileNameBasic sanitizes a filename with default settings and no custom hook.
func SanitizeURL ¶
SanitizeURL sanitizes the input URL using the "display" context.
func SanitizeURLBasic ¶ added in v1.1.0
SanitizeURLBasic sanitizes the URL using the default display context and no custom hook.
Types ¶
type EscapePlainTextHook ¶
type EscapePlainTextHook func() []rune
EscapePlainTextHook defines a function signature for custom behavior.
type FileNameHook ¶
FileNameHook defines a function signature for custom filename validation or transformation. It receives the sanitized filename and can perform additional checks or modifications.
type SanitizedStringFlag ¶ added in v1.1.0
type SanitizedStringFlag struct {
// contains filtered or unexported fields
}
SanitizedStringFlag represents a sanitized string flag bound to pflag.
func BindSanitizedFlag ¶ added in v1.1.0
func BindSanitizedFlag(name, defaultValue, usage string, sanitizer func(string) (string, error)) *SanitizedStringFlag
BindSanitizedFlag binds a string flag and attaches a sanitizer function.
func (*SanitizedStringFlag) Get ¶ added in v1.1.0
func (f *SanitizedStringFlag) Get() (string, error)
Get returns the sanitized value or an error.
func (*SanitizedStringFlag) MustGet ¶ added in v1.1.0
func (f *SanitizedStringFlag) MustGet() string
MustGet returns the sanitized value or panics on error.
type SanitizedTextFlag ¶ added in v1.1.0
type SanitizedTextFlag struct {
// contains filtered or unexported fields
}
SanitizedTextFlag represents a plain text flag bound to pflag and auto-sanitized.
func BindSanitizedTextFlag ¶ added in v1.1.0
func BindSanitizedTextFlag(name, defaultValue, usage string, hook EscapePlainTextHook) *SanitizedTextFlag
BindSanitizedTextFlag registers a flag that will be sanitized using EscapePlainText. The hook argument can be nil for default sanitization.
func (*SanitizedTextFlag) Get ¶ added in v1.1.0
func (f *SanitizedTextFlag) Get() string
Get returns the sanitized text value using EscapePlainText and the optional hook.
func (*SanitizedTextFlag) MustGet ¶ added in v1.1.0
func (f *SanitizedTextFlag) MustGet() string
MustGet is an alias for Get to match other sanitized flag helpers.