safety

package
v1.0.28 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2026 License: MPL-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInsecureDirectory = errors.New("target directory has insecure permissions (world-writable)")

ErrInsecureDirectory is returned when target directory has insecure permissions

View Source
var ErrPathTraversal = errors.New("path traversal detected")

ErrPathTraversal is returned when path traversal is detected

View Source
var ErrSymlinkDetected = errors.New("symlink detected in path (TOCTOU protection)")

ErrSymlinkDetected is returned when a symlink is detected in the path

Functions

func FormatBytes

func FormatBytes(bytes int64) string

FormatBytes converts bytes to human-readable format

func InitCPU

func InitCPU(lim Limits)

InitCPU sets GOMAXPROCS based on config This prevents the Go server from consuming all CPU cores

func InitMemory

func InitMemory(lim Limits)

InitMemory sets memory limit based on config Uses runtime/debug SetMemoryLimit (Go 1.19+)

func IsSymlink(path string) (bool, error)

IsSymlink checks if a path is a symlink without following it

func ReloadMemoryLimits added in v1.0.28

func ReloadMemoryLimits()

ReloadMemoryLimits reloads limits from environment (for testing/config changes)

func SafeAppendFile added in v1.0.28

func SafeAppendFile(path string, data []byte, perm os.FileMode) error

SafeAppendFile appends data to a file with TOCTOU protection

func SafeAppendFlags added in v1.0.28

func SafeAppendFlags() int

SafeAppendFlags returns secure flags for appending to files

func SafeCreate added in v1.0.28

func SafeCreate(path string, perm os.FileMode) (*os.File, error)

SafeCreate creates a file with TOCTOU protection - Validates path for symlinks and traversal - Uses O_NOFOLLOW to prevent symlink following - Validates parent directory permissions

func SafeFileFlags added in v1.0.28

func SafeFileFlags() int

SafeFileFlags returns secure flags for file creation O_NOFOLLOW prevents following symlinks (TOCTOU protection) O_EXCL ensures file is created (not opened if exists)

func SafeMkdirAll added in v1.0.28

func SafeMkdirAll(path string, perm os.FileMode) error

SafeMkdirAll creates directories with TOCTOU protection Validates each path component is not a symlink

func SafeOpenFile added in v1.0.28

func SafeOpenFile(path string, flag int, perm os.FileMode) (*os.File, error)

SafeOpenFile opens a file with TOCTOU protection

func SafeWriteFile added in v1.0.28

func SafeWriteFile(path string, data []byte, perm os.FileMode) error

SafeWriteFile writes data to a file with TOCTOU protection Uses atomic write pattern: write to temp, then rename

func ValidateDirectory added in v1.0.28

func ValidateDirectory(dir string) error

ValidateDirectory checks if a directory is safe for file creation Returns error if directory is world-writable (o+w)

func ValidatePath added in v1.0.28

func ValidatePath(path string) error

ValidatePath performs security checks on a file path - Rejects symlinks anywhere in the path - Rejects path traversal attempts (../) - Rejects world-writable directories

Types

type Limits

type Limits struct {
	// GOMAXPROCS limit (CPU cores)
	GoMaxProcs int // default: 2

	// Connection limits
	MaxConcurrentConns int // default: 100
	MaxConnsPerIP      int // default: 10

	// Request limits
	RequestTimeoutSec   int   // default: 30
	MaxRequestBodyMB    int   // default: 10
	MaxRequestBodyBytes int64 // computed from MB

	// Rate limiting
	RateLimitPerMin int // default: 60 requests per minute per IP

	// Memory limits
	MaxMemoryPercent int   // default: 20% of available
	MaxMemoryBytes   int64 // default: 512 MiB

	// Logging
	EnableMetrics bool // default: true
}

Limits holds all safety thresholds for the GUI server

func FromEnv

func FromEnv() Limits

FromEnv returns sane defaults that can be overridden via environment variables This matches the pattern from go-feeds/internal/safety/config.go

type MemAvail

type MemAvail struct {
	Total         int64
	Avail         int64
	CgroupLimit   int64
	CgroupCurrent int64
}

MemAvail holds available memory info (cgroup-aware) This matches the pattern from go-feeds/internal/safety/mem.go

func AvailableMem

func AvailableMem() MemAvail

AvailableMem returns available memory (cgroup-aware for containers) This is critical for running in Docker/Kubernetes where cgroup limits apply

type MemoryLimits added in v1.0.28

type MemoryLimits struct {
	// Scorer limits (pkg/suricata/scorer.go)
	ScorerMaxIPs         int // Max unique IPs tracked in scorer (default: 50000)
	ScorerMaxEventsPerIP int // Max event timestamps per IP (default: 100)

	// Analytics limits (pkg/analytics/state.go)
	AnalyticsMaxIPOrigins     int // Max IPs in ipOrigins map (default: 100000)
	AnalyticsMaxIPsPerCountry int // Max IPs per country (default: 10000)

	// Stats cache limits (pkg/suricata/stats/cache.go)
	StatsMaxSIDs          int // Max SIDs tracked (default: 10000)
	StatsMaxSourcesPerSID int // Max unique sources per SID (default: 1000)

	// Queue limits (cli/lib/nftban/helpers/nftban_task_queue.sh)
	QueueMaxPending       int // Max pending tasks (default: 10000)
	QueueDLQAutoRetention int // Auto-purge DLQ entries older than N days (default: 7)
}

MemoryLimits holds caps and TTLs to prevent unbounded memory growth (CWE-400) All limits are configurable via environment variables with sane defaults.

func DefaultMemoryLimits added in v1.0.28

func DefaultMemoryLimits() MemoryLimits

DefaultMemoryLimits returns production-safe defaults

func GetMemoryLimits added in v1.0.28

func GetMemoryLimits() MemoryLimits

GetMemoryLimits returns the global memory limits

type SystemIPs

type SystemIPs struct {
	ServerIPs     []net.IP    // All server interface IPs
	CurrentUserIP net.IP      // IP of current SSH connection
	GatewayIPs    []net.IP    // Default gateway
	DNSServers    []net.IP    // DNS servers from /etc/resolv.conf
	LoopbackCIDRs []net.IPNet // 127.0.0.0/8, ::1/128
}

SystemIPs holds all critical IPs that must NEVER be blocked

func DetectSystemIPs

func DetectSystemIPs() (*SystemIPs, error)

DetectSystemIPs auto-detects all critical IPs that must be whitelisted

func (*SystemIPs) GetAllIPs

func (s *SystemIPs) GetAllIPs() []net.IP

GetAllIPs returns all IPs as a flat list

func (*SystemIPs) GetAllIPsWithCIDRs

func (s *SystemIPs) GetAllIPsWithCIDRs() ([]net.IP, []net.IPNet)

GetAllIPsWithCIDRs returns all IPs including loopback CIDRs

func (*SystemIPs) PrintSystemIPs

func (s *SystemIPs) PrintSystemIPs()

PrintSystemIPs displays all detected IPs

Jump to

Keyboard shortcuts

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