Skip to content

Security Overview

Environment variables often store sensitive information, making secure handling critical. This document outlines the env library's security architecture and core features.

Security Architecture

text
┌──────────────────────────────────────────────────────────────┐
│                        Application Layer                      │
├──────────────────────────────────────────────────────────────┤
│   SecureValue   │   Masking   │   Zeroing   │  Memory Lock   │
├──────────────────────────────────────────────────────────────┤
│                          Loader Layer                         │
├──────────────────────────────────────────────────────────────┤
│    Key Validation  │  Value Validation  │  Forbidden Keys  │  Size Limits  │
├──────────────────────────────────────────────────────────────┤
│                          Parser Layer                         │
├──────────────────────────────────────────────────────────────┤
│    Format Detection  │  Expansion Check  │   Path Validation │
└──────────────────────────────────────────────────────────────┘

Core Security Features

FeatureDescriptionDocumentation
SecureValueMemory-protected sensitive values with auto-zeroingSecureValue API
Forbidden KeysPrevents modification of critical system variablesConstants & Errors
Sensitive Key DetectionAuto-identifies sensitive configuration keysConstants & Errors
Value ValidationDetects control characters, null bytes, etc.Config API
Audit LoggingComplete operation trackingComponent Factory

SecureValue Overview

For sensitive data, use GetSecure instead of GetString:

go
// Not recommended
password := env.GetString("DB_PASSWORD")

// Recommended
secret := env.GetSecure("DB_PASSWORD")
defer secret.Close()
password := secret.String()

Core capabilities:

  • Memory Locking - Prevents swapping to disk (Linux/macOS/FreeBSD)
  • Auto-Zeroing - Securely erases memory on Close()
  • Masked Display - Masked() for log output
  • Thread Safety - Supports concurrent reads

Full API

See SecureValue API.

Key/Value Validation

Key Validation

Default key name rule: ^[A-Za-z][A-Za-z0-9_]*$

  • Must start with a letter
  • Only letters, digits, and underscores
  • Length must not exceed MaxKeyLength

Forbidden Keys

Built-in forbidden keys prevent modification of critical system variables:

CategoryExamplesRisk
System pathPATH, LD_LIBRARY_PATHCommand/library hijacking
Dynamic linkingLD_PRELOAD, DYLD_INSERT_LIBRARIESMalicious library injection
ShellSHELL, IFS, BASH_ENVShell hijacking
Language runtimesPYTHONPATH, NODE_PATHModule hijacking

Full List

See DefaultForbiddenKeys for the complete forbidden keys list.

Value Validation

Enable value validation to detect potential dangers:

go
cfg := env.ProductionConfig()
cfg.ValidateValues = true  // Detect control characters, null bytes, etc.

File Security Basics

File Permissions

bash
# Owner read/write only
chmod 600 .env

# Or stricter (read-only)
chmod 400 .env

Git Ignore

bash
.env
.env.local
.env.*.local
*.pem
*.key

Configuration Security Levels

PresetUse CaseFeatures
DevelopmentConfig()DevelopmentRelaxed limits, YAML syntax support
TestingConfig()TestingOverwrite existing, test isolation
ProductionConfig()ProductionStrict validation + audit logging, no overwrite
go
// Recommended production configuration
cfg := env.ProductionConfig()
cfg.RequiredKeys = []string{"DB_HOST", "API_KEY"}
cfg.AllowedKeys = []string{"APP_NAME", "PORT", "DB_HOST", "API_KEY"}