Skip to content

Security Overview

Environment variables often store sensitive information, making secure handling critical. This document provides an overview of 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 │
├──────────────────────────────────────────────────────────────┤
│                         Parsing Layer                        │
├──────────────────────────────────────────────────────────────┤
│  Format Detection │  Expansion Check │    Path Validation   │
└──────────────────────────────────────────────────────────────┘

Core Security Features

FeatureDescriptionDocumentation
SecureValueSensitive value memory protection, auto-zeroingSecureValue API
Forbidden KeysPrevent modification of critical system variablesConstants & Errors
Sensitive Key DetectionAutomatic identification of sensitive config keysConstants & Errors
Value ValidationDetect 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.Reveal()  // Call only when plaintext is needed

Core capabilities:

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

Full API

See SecureValue API for details.

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
  • Maximum length of MaxKeyLength

Forbidden Keys

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

CategoryExamplesRisk
System PathsPATH, 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
# Read/write for owner 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 CaseCharacteristics
DevelopmentConfig()DevelopmentRelaxed restrictions, YAML syntax support
TestingConfig()TestingOverride existing variables, test isolation
ProductionConfig()ProductionStrict validation + audit logging, no override of existing variables
go
// Recommended production configuration
cfg := env.ProductionConfig()
cfg.RequiredKeys = []string{"DB_HOST", "API_KEY"}
cfg.AllowedKeys = []string{"APP_NAME", "PORT", "DB_HOST", "API_KEY"}