sanitization

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package sanitization provides utilities for cleaning and validating user input to prevent XSS attacks, injection attacks, and other security vulnerabilities.

Package sanitization provides utilities for cleaning and validating user input to prevent XSS attacks, injection attacks, and other security vulnerabilities.

Package sanitization provides utilities for cleaning and validating user input to prevent XSS attacks, injection attacks, and other security vulnerabilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsValidEmail

func IsValidEmail(s ServiceInterface, input string) bool

IsValidEmail checks if an email is valid after sanitization

func IsValidURL

func IsValidURL(s ServiceInterface, input string) bool

IsValidURL checks if a URL is valid after sanitization

Types

type SanitizeOptions

type SanitizeOptions struct {
	TrimWhitespace bool
	RemoveHTML     bool
	MaxLength      int
	AllowedTags    []string
}

SanitizeOptions provides advanced sanitization with options

type Service

type Service struct{}

Service provides sanitization functionality for various input types

func NewService

func NewService() *Service

NewService creates a new sanitization service

func (*Service) Alpha

func (s *Service) Alpha(input string, spaces bool) string

Alpha sanitizes to alpha characters only

func (*Service) AlphaNumeric

func (s *Service) AlphaNumeric(input string, spaces bool) string

AlphaNumeric sanitizes to alphanumeric characters only

func (*Service) Domain

func (s *Service) Domain(input string) (string, error)

Domain sanitizes a domain name

func (*Service) Email

func (s *Service) Email(input string) string

Email sanitizes an email address

func (*Service) HTML

func (s *Service) HTML(input string) string

HTML sanitizes HTML content

func (*Service) IPAddress

func (s *Service) IPAddress(input string) string

IPAddress sanitizes an IP address

func (*Service) Numeric

func (s *Service) Numeric(input string) string

Numeric sanitizes to numeric characters only

func (*Service) Path

func (s *Service) Path(input string) string

Path sanitizes a file path

func (*Service) SanitizeForLogging

func (s *Service) SanitizeForLogging(input string) string

SanitizeForLogging sanitizes a string specifically for safe logging This method prevents log injection attacks by removing newlines, null bytes, HTML tags, and HTML escaping the content

func (*Service) SanitizeFormData

func (s *Service) SanitizeFormData(data, fieldTypes map[string]string) map[string]string

SanitizeFormData sanitizes form data based on field types

func (*Service) SanitizeJSON

func (s *Service) SanitizeJSON(data any) any

SanitizeJSON sanitizes JSON data recursively

func (*Service) SanitizeMap

func (s *Service) SanitizeMap(data map[string]any)

SanitizeMap sanitizes a map of string keys to any values

func (*Service) SanitizeSlice

func (s *Service) SanitizeSlice(data []any)

SanitizeSlice sanitizes a slice of any values

func (*Service) SanitizeStruct

func (s *Service) SanitizeStruct(obj any)

SanitizeStruct sanitizes all string fields in a struct

func (*Service) SanitizeWithOptions

func (s *Service) SanitizeWithOptions(input string, opts SanitizeOptions) string

SanitizeWithOptions sanitizes a string with custom options

func (*Service) Scripts

func (s *Service) Scripts(input string) string

Scripts removes script tags

func (*Service) SingleLine

func (s *Service) SingleLine(input string) string

SingleLine removes newlines and extra whitespace

func (*Service) String

func (s *Service) String(input string) string

String sanitizes a string input using XSS protection

func (*Service) TrimAndSanitize

func (s *Service) TrimAndSanitize(input string) string

TrimAndSanitize trims whitespace and sanitizes a string

func (*Service) TrimAndSanitizeEmail

func (s *Service) TrimAndSanitizeEmail(input string) string

TrimAndSanitizeEmail trims whitespace and sanitizes an email

func (*Service) URI

func (s *Service) URI(input string) string

URI sanitizes a URI

func (*Service) URL

func (s *Service) URL(input string) string

URL sanitizes a URL

func (*Service) XML

func (s *Service) XML(input string) string

XML sanitizes XML content

type ServiceInterface

type ServiceInterface interface {
	// Basic string sanitization methods
	String(input string) string
	Email(input string) string
	URL(input string) string
	HTML(input string) string
	Path(input string) string
	IPAddress(input string) string
	Domain(input string) (string, error)
	URI(input string) string
	Alpha(input string, spaces bool) string
	AlphaNumeric(input string, spaces bool) string
	Numeric(input string) string
	SingleLine(input string) string
	Scripts(input string) string
	XML(input string) string
	TrimAndSanitize(input string) string
	TrimAndSanitizeEmail(input string) string

	// Log-specific sanitization
	SanitizeForLogging(input string) string

	// Complex data structure sanitization
	SanitizeMap(data map[string]any)
	SanitizeSlice(data []any)
	SanitizeStruct(obj any)
	SanitizeFormData(data map[string]string, fieldTypes map[string]string) map[string]string
	SanitizeJSON(data any) any
	SanitizeWithOptions(input string, opts SanitizeOptions) string
}

ServiceInterface defines the interface for sanitization operations

Jump to

Keyboard shortcuts

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