Documentation
¶
Overview ¶
Package security provides comprehensive security measures for the GoPCA toolkit. It implements defense-in-depth strategies to protect against common vulnerabilities including path traversal, command injection, and resource exhaustion attacks.
Input Validation ¶
The package provides validators for all user inputs:
- Numeric values with bounds checking
- String inputs with length and character restrictions
- File paths with traversal prevention
- Command arguments with injection prevention
Path Security ¶
File path operations include multiple layers of protection:
- Path traversal detection and prevention
- System directory write protection
- Jail/sandbox path enforcement
- Platform-specific validation (Windows reserved names, etc.)
Command Security ¶
External command execution is secured through:
- Command whitelisting
- Argument validation
- Special character escaping
- Environment variable sanitization
Resource Limits ¶
The package enforces limits to prevent resource exhaustion:
- Maximum file size: 500MB
- Maximum CSV rows: 1,000,000
- Maximum CSV columns: 10,000
- Maximum field length: 10,000 characters
- Maximum memory usage: 2GB for data matrices
Usage ¶
Input validation:
value, err := security.ValidateNumericInput(input, 0, 100, "parameter")
Path validation:
err := security.ValidateInputPath(filePath)
Command validation:
err := security.ValidateCommand(cmd, args)
Security Policy ¶
For vulnerability reporting and security policies, see SECURITY.md in the repository root.
Package security provides security utilities for input validation, path sanitization, and protection against common vulnerabilities.
Index ¶
- Constants
- Variables
- func IsValidEmail(email string) bool
- func JailPath(basePath, userPath string) (string, error)
- func ResolveSymlinks(path string) (string, error)
- func SanitizeFilename(filename string) string
- func SecureCommand(name string, args ...string) (*exec.Cmd, error)
- func SecureTempFile(pattern string) (*os.File, error)
- func ValidateCSVDelimiter(delimiter string) (rune, error)
- func ValidateCommand(cmd string, args []string) error
- func ValidateComponentCount(components, maxFeatures int) error
- func ValidateDataDimensions(rows, cols int) error
- func ValidateInputPath(path string) error
- func ValidateIntegerInput(input string, min, max int, paramName string) (int, error)
- func ValidateKernelParameters(kernelType string, gamma, degree float64, coef0 float64) error
- func ValidateNumericInput(input string, min, max float64, paramName string) (float64, error)
- func ValidateOutputPath(path string) error
- func ValidateStringInput(input string, maxLength int, allowedChars string, paramName string) (string, error)
Constants ¶
const ( MaxFileSize = 500 * 1024 * 1024 // 500MB max file size MaxCSVRows = 1000000 // 1M rows max MaxCSVColumns = 10000 // 10K columns max MaxFieldLength = 100000 // 100K chars per field MaxStringLength = 10000 // 10K chars for general strings MaxPathLength = 4096 // Standard PATH_MAX MaxComponents = 1000 // Max PCA components MinComponents = 1 // Min PCA components MaxKernelPCASamples = 10000 // Max samples for Kernel PCA (memory safety) MaxKernelMatrixVisualization = 1000 // Max samples for kernel matrix visualization MaxKernelGamma = 1e6 // Max kernel gamma value MinKernelGamma = 1e-6 // Min kernel gamma value MaxIterations = 10000 // Max iterations for algorithms MaxMemoryUsageMB = 2048 // 2GB max memory for operations )
Limits for various input types to prevent resource exhaustion
Variables ¶
var AllowedArguments = map[string]map[string]bool{ "open": { "-a": true, "-n": true, "--args": true, "--open": true, }, "tasklist": { "/FI": true, }, "pgrep": { "-x": true, }, }
AllowedArguments defines safe arguments for specific commands
var AllowedCommands = map[string]bool{ "open": true, "pgrep": true, "tasklist": true, "GoPCA": true, "GoCSV": true, "gopca-desktop": true, "gocsv": true, }
AllowedCommands defines the whitelist of commands that can be executed
var SystemDirectories = []string{
"/etc", "/bin", "/sbin", "/usr/bin", "/usr/sbin",
"/sys", "/proc", "/dev", "/boot", "/lib", "/lib64",
"/usr/lib", "/usr/local/bin", "/usr/local/sbin",
"/var/log", "/root", "/home/root",
}
SystemDirectories that should never be written to
var WindowsSystemDirectories = []string{
`C:\Windows`, `C:\Program Files`, `C:\Program Files (x86)`,
`C:\ProgramData`, `C:\System32`, `C:\SysWOW64`,
}
WindowsSystemDirectories that should never be written to
Functions ¶
func IsValidEmail ¶
IsValidEmail performs basic email validation
func ResolveSymlinks ¶
ResolveSymlinks safely resolves symbolic links
func SanitizeFilename ¶
SanitizeFilename removes potentially dangerous characters from filenames
func SecureCommand ¶
SecureCommand creates a secure exec.Cmd with validation
func SecureTempFile ¶
SecureTempFile creates a secure temporary file
func ValidateCSVDelimiter ¶
ValidateCSVDelimiter validates CSV delimiter character
func ValidateCommand ¶
ValidateCommand validates a command and its arguments for security
func ValidateComponentCount ¶
ValidateComponentCount validates PCA component count
func ValidateDataDimensions ¶
ValidateDataDimensions validates data matrix dimensions
func ValidateInputPath ¶
ValidateInputPath validates a path for reading operations
func ValidateIntegerInput ¶
ValidateIntegerInput validates integer input within bounds
func ValidateKernelParameters ¶
ValidateKernelParameters validates kernel PCA parameters
func ValidateNumericInput ¶
ValidateNumericInput validates and sanitizes numeric input within bounds
func ValidateOutputPath ¶
ValidateOutputPath validates a path for writing operations
Types ¶
This section is empty.