Documentation
¶
Overview ¶
Package security provides HTTP middleware for NornicDB security validation.
Package security provides security validation utilities for NornicDB.
Index ¶
- Constants
- Variables
- func EnsureRootedParent(path string, perm os.FileMode) error
- func KeyedDigest(namespace string, parts ...string) [32]byte
- func KeyedDigestHex(namespace string, parts ...string) string
- func ReadRootedDir(path string) ([]os.DirEntry, error)
- func RemoveAllRootedPath(path string) error
- func RemoveRootedPath(path string) error
- func RenameRootedFile(oldPath, newPath string) error
- func RootedStat(path string) (os.FileInfo, error)
- func SanitizeString(input string) string
- func ValidateHeaderValue(value string) error
- func ValidateToken(token string) error
- func ValidateURL(rawURL string, isDevelopment, allowHTTP bool) error
- func WriteRootedFile(path string, data []byte, perm os.FileMode) error
- type RootedFile
- type SecurityConfig
- type SecurityMiddleware
Constants ¶
const ( MaxTokenLength = 8192 MaxURLLength = 2048 MaxHeaderLength = 4096 )
Variables ¶
var ( ErrTokenInvalidChars = fmt.Errorf("token contains invalid characters (possible injection attack)") ErrTokenTooLong = fmt.Errorf("token exceeds maximum length of %d characters", MaxTokenLength) ErrTokenEmpty = fmt.Errorf("token must be a non-empty string") ErrURLInvalidProtocol = fmt.Errorf("only HTTP/HTTPS protocols are allowed") ErrURLPrivateIP = fmt.Errorf("private IP addresses are not allowed") ErrURLLocalhost = fmt.Errorf("localhost is not allowed in production") ErrURLHTTPNotAllowed = fmt.Errorf("only HTTPS URLs are allowed in production") ErrURLTooLong = fmt.Errorf("URL exceeds maximum length of %d characters", MaxURLLength) ErrURLInvalid = fmt.Errorf("invalid URL format") )
Functions ¶
func EnsureRootedParent ¶ added in v1.2.1
EnsureRootedParent creates the parent directory of a canonical path through an os.Root capability.
func KeyedDigest ¶ added in v1.2.1
KeyedDigest returns a deterministic process-local HMAC-SHA256 digest for the supplied namespace and parts. The namespace provides domain separation so the same value used in different contexts does not yield the same digest.
func KeyedDigestHex ¶ added in v1.2.1
KeyedDigestHex is the hex-encoded form of KeyedDigest.
func ReadRootedDir ¶ added in v1.2.1
ReadRootedDir reads a canonical directory through an os.Root capability.
func RemoveAllRootedPath ¶ added in v1.2.1
RemoveAllRootedPath removes a canonical path through an os.Root capability.
func RemoveRootedPath ¶ added in v1.2.1
RemoveRootedPath removes a canonical file or empty directory through an os.Root capability.
func RenameRootedFile ¶ added in v1.2.1
RenameRootedFile atomically renames canonical paths in the same directory through a single os.Root capability.
func RootedStat ¶ added in v1.2.1
RootedStat returns information about a canonical path through an os.Root capability.
func SanitizeString ¶
SanitizeString removes dangerous characters from user input.
func ValidateHeaderValue ¶
ValidateHeaderValue validates HTTP header values to prevent injection attacks.
func ValidateToken ¶
ValidateToken validates OAuth/API token format to prevent injection attacks.
func ValidateURL ¶
ValidateURL validates URLs to prevent SSRF attacks.
Types ¶
type RootedFile ¶ added in v1.2.1
RootedFile retains the root capability for the lifetime of an open file.
func CreateRootedFile ¶ added in v1.2.1
func CreateRootedFile(path string, perm os.FileMode) (*RootedFile, error)
CreateRootedFile creates or truncates a canonical path through os.Root.
func OpenRootedFile ¶ added in v1.2.1
OpenRootedFile opens a canonical file path through an os.Root rooted at its parent directory. Traversal components and paths whose cleaned form differs from the supplied path are rejected before any filesystem operation.
func (*RootedFile) Close ¶ added in v1.2.1
func (f *RootedFile) Close() error
Close closes both the file and its root capability.
type SecurityConfig ¶
type SecurityConfig struct {
Environment string // "development", "production"
AllowHTTP bool // Allow non-TLS connections
}
SecurityConfig holds security middleware configuration. This is passed from the main config to avoid direct env var access.
type SecurityMiddleware ¶
type SecurityMiddleware struct {
// contains filtered or unexported fields
}
SecurityMiddleware wraps HTTP handlers with security validations.
func NewSecurityMiddleware ¶
func NewSecurityMiddleware() *SecurityMiddleware
NewSecurityMiddleware creates a new security middleware instance. Use NewSecurityMiddlewareWithConfig for production code.
func NewSecurityMiddlewareWithConfig ¶
func NewSecurityMiddlewareWithConfig(cfg SecurityConfig) *SecurityMiddleware
NewSecurityMiddlewareWithConfig creates a security middleware with explicit config.
func (*SecurityMiddleware) SetLocalizer ¶ added in v1.3.1
func (m *SecurityMiddleware) SetLocalizer(manager *localization.Manager)
SetLocalizer sets the immutable message catalog used for HTTP errors.
func (*SecurityMiddleware) ValidateRequest ¶
func (m *SecurityMiddleware) ValidateRequest(next http.Handler) http.Handler
ValidateRequest performs comprehensive security validation on incoming requests.