Documentation
¶
Overview ¶
Package safeinput provides context-aware input sanitization for Go applications. It addresses MITRE CWE Top 25 injection vulnerabilities including:
- CWE-79: Cross-site Scripting (XSS)
- CWE-89: SQL Injection
- CWE-22: Path Traversal
- CWE-78: OS Command Injection
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrInputTooLong is returned when input exceeds maximum length. ErrInputTooLong = errors.New("input exceeds maximum length") // ErrUnknownContext is returned when an unknown sanitization context is provided. ErrUnknownContext = errors.New("unknown sanitization context") // ErrNullByte is returned when a null byte is detected in input. ErrNullByte = errors.New("null byte detected in input") )
Functions ¶
func SanitizeShellArg ¶
SanitizeShellArg sanitizes shell command arguments (CWE-78). Only allows alphanumeric characters, dash, underscore, period, and forward slash.
func StripNullBytes ¶
StripNullBytes removes null bytes from a string.
Types ¶
type Config ¶
type Config struct {
MaxInputLength int
AllowedHTMLTags []string
BasePath string
StrictMode bool
StripNullBytes bool
}
Config holds sanitizer configuration options.
type Context ¶
type Context int
Context defines the output context for sanitization.
const ( // HTMLBody sanitizes for HTML body content (CWE-79). HTMLBody Context = iota // HTMLAttribute sanitizes for HTML attribute values (CWE-79). HTMLAttribute // SQLIdentifier sanitizes SQL identifiers (CWE-89). SQLIdentifier // SQLValue validates values before queries (CWE-89). SQLValue // FilePath sanitizes filesystem paths (CWE-22). FilePath // URLPath sanitizes URL path components. URLPath // URLQuery sanitizes URL query parameters. URLQuery // ShellArg sanitizes shell command arguments (CWE-78). ShellArg )
type Sanitizer ¶
type Sanitizer struct {
// contains filtered or unexported fields
}
Sanitizer provides the main sanitization interface.
func (*Sanitizer) MustSanitize ¶
MustSanitize panics on error.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package analyzer provides an AST-based static analyser that detects HTTP handlers reading user input without go-safeinput sanitisation.
|
Package analyzer provides an AST-based static analyser that detects HTTP handlers reading user input without go-safeinput sanitisation. |
|
cmd
|
|
|
analyzer
command
Command analyzer runs the AgenticAnalyzer across a Go source tree, reports unsanitized HTTP input with CWE-mapped fix suggestions, and emits GitHub Actions workflow annotations so findings appear inline on PRs.
|
Command analyzer runs the AgenticAnalyzer across a Go source tree, reports unsanitized HTTP input with CWE-mapped fix suggestions, and emits GitHub Actions workflow annotations so findings appear inline on PRs. |
|
examples
|
|
|
agentic/after
Package after shows the same handlers from the "before" package, rewritten using go-safeinput to eliminate the vulnerabilities.
|
Package after shows the same handlers from the "before" package, rewritten using go-safeinput to eliminate the vulnerabilities. |
|
agentic/before
Package before shows common Go HTTP handlers that contain unsanitized input vulnerabilities.
|
Package before shows common Go HTTP handlers that contain unsanitized input vulnerabilities. |
|
Package html provides XSS prevention (CWE-79) using pure Go.
|
Package html provides XSS prevention (CWE-79) using pure Go. |
|
Package middleware provides SafeHTTP, a drop-in net/http middleware that automatically sanitizes all query parameters and form values before they reach the wrapped handler.
|
Package middleware provides SafeHTTP, a drop-in net/http middleware that automatically sanitizes all query parameters and form values before they reach the wrapped handler. |
|
Package path provides path traversal prevention (CWE-22).
|
Package path provides path traversal prevention (CWE-22). |
|
Package safedecode provides a safe JSON decoder with protection against CWE-502 (Deserialization of Untrusted Data).
|
Package safedecode provides a safe JSON decoder with protection against CWE-502 (Deserialization of Untrusted Data). |
|
Package safedeserialize provides secure deserialization utilities for Go that mitigate CWE-502: Deserialization of Untrusted Data.
|
Package safedeserialize provides secure deserialization utilities for Go that mitigate CWE-502: Deserialization of Untrusted Data. |
|
examples
command
Package main demonstrates usage of the safedeserialize package.
|
Package main demonstrates usage of the safedeserialize package. |
|
Package sql provides SQL injection prevention (CWE-89).
|
Package sql provides SQL injection prevention (CWE-89). |
Click to show internal directories.
Click to hide internal directories.