input

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package input provides security-focused input validation and basic defense-in-depth sanitization utilities.

This package implements conservative validation functions for common input types including:

  • Email addresses with DNS-style domain label checks
  • Safe HTTP and HTTPS URLs plus safe absolute relative URL paths
  • Phone numbers with E.164-compatible digit bounds
  • HTTP tokens (RFC 7230)

The sanitizer helpers are not substitutes for context-aware HTML sanitizers, parameterized SQL queries, or output encoding.

Example usage:

import "github.com/spcent/plumego/security/input"

// Validate email
if !input.ValidateEmail("user@example.com") {
	// Invalid email
}

// Validate URL
if !input.ValidateURL("https://example.com") {
	// Invalid URL
}

// Validate phone number (E.164)
if !input.ValidatePhone("+1234567890") {
	// Invalid phone number
}

// Check if string contains dangerous characters
if input.ContainsDangerousChars(userInput) {
	// Contains dangerous characters
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BestEffortSanitizeHTML

func BestEffortSanitizeHTML(s string) string

BestEffortSanitizeHTML applies a lossy, best-effort HTML cleanup pass.

This helper removes a small set of common script and inline-handler patterns for defense-in-depth only. It is not a complete HTML parser or sanitizer and must not be used as the sole control for rendering untrusted HTML.

Example:

import "github.com/spcent/plumego/security/input"

cleaned := input.BestEffortSanitizeHTML(userInput)

func BestEffortSanitizeSQL

func BestEffortSanitizeSQL(s string) string

BestEffortSanitizeSQL applies a lossy, best-effort SQL text cleanup pass.

WARNING: This is NOT a substitute for parameterized queries. Always use parameterized queries for database operations. This helper removes a small set of common comment, separator, and keyword patterns for defense-in-depth scenarios only.

Example:

import "github.com/spcent/plumego/security/input"

// Still use parameterized queries!
cleaned := input.BestEffortSanitizeSQL(userInput)

func ContainsDangerousChars

func ContainsDangerousChars(s string) bool

ContainsDangerousChars checks for characters commonly used in injection attacks.

Returns true if the string contains potentially dangerous characters.

Example:

import "github.com/spcent/plumego/security/input"

if input.ContainsDangerousChars(userInput) {
	// Handle potentially dangerous input
}

func IsHeaderName

func IsHeaderName(value string) bool

IsHeaderName reports whether value is safe for use as an HTTP header name.

Header names must be valid HTTP tokens according to RFC 7230.

Example:

import "github.com/spcent/plumego/security/input"

if input.IsHeaderName("X-Custom-Header") {
	// Valid header name
}

func IsHeaderValue

func IsHeaderValue(value string) bool

IsHeaderValue reports whether value is safe for use as an HTTP header value. It rejects control characters that can lead to response splitting.

Header values must be valid UTF-8 and must not contain ASCII control characters except horizontal tab.

Example:

import "github.com/spcent/plumego/security/input"

if input.IsHeaderValue("some value") {
	// Safe to use as header value
}

func IsToken

func IsToken(value string) bool

IsToken reports whether value is a valid HTTP token (RFC 7230).

HTTP tokens are used in header names, media types, and other HTTP constructs. A token consists of ASCII letters, digits, and certain special characters.

Example:

import "github.com/spcent/plumego/security/input"

if input.IsToken("Content-Type") {
	// Valid HTTP token
}

Valid tokens include:

  • Letters (a-z, A-Z)
  • Digits (0-9)
  • Special characters: ! # $ % & ' * + - . ^ _ ` | ~

func StripControlChars

func StripControlChars(s string) string

StripControlChars removes ASCII control characters except newline and tab.

This function removes characters that could cause display issues or be used for terminal injection attacks.

Example:

import "github.com/spcent/plumego/security/input"

clean := input.StripControlChars(userInput)

func TrimWhitespace

func TrimWhitespace(s string) string

TrimWhitespace removes leading and trailing whitespace and normalizes internal whitespace.

This function collapses multiple consecutive whitespace characters into a single space and removes leading/trailing whitespace.

Example:

import "github.com/spcent/plumego/security/input"

normalized := input.TrimWhitespace("  hello    world  ")
// Returns: "hello world"

func ValidateEmail

func ValidateEmail(email string) bool

ValidateEmail performs security-focused email validation.

Returns true if the email appears valid. This is a basic check for security purposes, not a comprehensive email validation.

Example:

import "github.com/spcent/plumego/security/input"

if !input.ValidateEmail(userEmail) {
	// Reject invalid email
}

func ValidatePhone

func ValidatePhone(phone string) bool

ValidatePhone performs basic phone number validation.

Returns true if the phone number appears valid. This is a basic check that accepts E.164 format and common phone number patterns.

Example:

import "github.com/spcent/plumego/security/input"

if !input.ValidatePhone(userPhone) {
	// Reject invalid phone
}

func ValidatePublicURL

func ValidatePublicURL(rawURL string) bool

ValidatePublicURL checks whether rawURL is an absolute HTTP(S) URL whose literal host is suitable for server-side fetch allow-lists.

It rejects relative paths, localhost names, loopback, private, link-local, multicast, unspecified, and metadata-service IP targets. It does not perform DNS resolution; callers that resolve hostnames must still enforce the same checks on every resolved address.

func ValidateURL

func ValidateURL(rawURL string) bool

ValidateURL checks URL shape, allowed schemes, and unsafe relative forms.

Returns true if the URL is syntactically valid for common redirect/link use. This function rejects javascript:, data:, file:, vbscript:, embedded credentials, malformed URLs, and unsafe relative paths. It does not make a URL safe for server-side fetching because hostnames can resolve to private addresses. Use ValidatePublicURL for SSRF-sensitive fetch targets.

Example:

import "github.com/spcent/plumego/security/input"

if !input.ValidateURL(userURL) {
	// Reject invalid or unsafe URL
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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