security

package
v1.1.8 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2025 License: MIT Imports: 10 Imported by: 0

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

View Source
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

View Source
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

View Source
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

View Source
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

View Source
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

func IsValidEmail(email string) bool

IsValidEmail performs basic email validation

func JailPath

func JailPath(basePath, userPath string) (string, error)

JailPath ensures a path stays within a jail directory

func ResolveSymlinks(path string) (string, error)

ResolveSymlinks safely resolves symbolic links

func SanitizeFilename

func SanitizeFilename(filename string) string

SanitizeFilename removes potentially dangerous characters from filenames

func SecureCommand

func SecureCommand(name string, args ...string) (*exec.Cmd, error)

SecureCommand creates a secure exec.Cmd with validation

func SecureTempFile

func SecureTempFile(pattern string) (*os.File, error)

SecureTempFile creates a secure temporary file

func ValidateCSVDelimiter

func ValidateCSVDelimiter(delimiter string) (rune, error)

ValidateCSVDelimiter validates CSV delimiter character

func ValidateCommand

func ValidateCommand(cmd string, args []string) error

ValidateCommand validates a command and its arguments for security

func ValidateComponentCount

func ValidateComponentCount(components, maxFeatures int) error

ValidateComponentCount validates PCA component count

func ValidateDataDimensions

func ValidateDataDimensions(rows, cols int) error

ValidateDataDimensions validates data matrix dimensions

func ValidateInputPath

func ValidateInputPath(path string) error

ValidateInputPath validates a path for reading operations

func ValidateIntegerInput

func ValidateIntegerInput(input string, min, max int, paramName string) (int, error)

ValidateIntegerInput validates integer input within bounds

func ValidateKernelParameters

func ValidateKernelParameters(kernelType string, gamma, degree float64, coef0 float64) error

ValidateKernelParameters validates kernel PCA parameters

func ValidateNumericInput

func ValidateNumericInput(input string, min, max float64, paramName string) (float64, error)

ValidateNumericInput validates and sanitizes numeric input within bounds

func ValidateOutputPath

func ValidateOutputPath(path string) error

ValidateOutputPath validates a path for writing operations

func ValidateStringInput

func ValidateStringInput(input string, maxLength int, allowedChars string, paramName string) (string, error)

ValidateStringInput validates and sanitizes string input

Types

This section is empty.

Jump to

Keyboard shortcuts

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