๐ง Secure Handling of Secrets
Shell scripts frequently work with sensitive data such as passwords, API tokens, SSH keys, and access credentials. However, the shell environment is inherently insecure: variables leak, commands appear in process lists, and logs may expose secrets. This module explains how to handle secrets safely in POSIXโcompatible scripts.
๐ Who This Is For
- DevOps/SRE engineers managing CI/CD pipelines
- Developers writing automation that interacts with APIs or cloud services
- Sysadmins handling credentials, tokens, or private keys
- Anyone needing to avoid accidental secret leakage in shell scripts
๐งฉ Role in the Ecosystem
Secrets appear in many contexts:
- API tokens
- Passwords
- SSH keys
- Cloud credentials
- Database connection strings
- OAuth/JWT tokens
Shell scripts often orchestrate tools that require these secrets, but the shell itself provides no builtโin secure storage. Safe handling requires defensive practices and external tools.
๐งฉ Key Concepts
1. Shell Environments Are Not Secure
Environment variables leak through:
ps aux/proc/$PID/environ- debug tracing (
set -x) - CI/CD logs
- crash dumps
- history files
2. Arguments Are Visible
Passing secrets as CLI arguments is unsafe:
1 | |
3. Stdout vs Stderr
Secrets printed to stdout may be captured by pipelines or logs.
4. Filesystem Exposure
Secrets stored in plain text files are readable by other users unless permissions are restricted.
๐ง Techniques
Read Secrets Without Echoing
1 | |
Pass Secrets via Stdin
1 | |
Use Environment Files with Restricted Permissions
1 | |
Use External Secret Stores
pass(GPGโbased)gpgencrypted filesageencrypted filessops(YAML/JSON secrets)- cloud secret managers (AWS/GCP/Azure)
Avoid Logging Secrets
Disable tracing around sensitive code:
1 2 3 | |
Use Temporary Files Safely
1 2 | |
Clear Variables After Use
1 | |
โ ๏ธ Limitations & Pitfalls
- Environment variables are visible to other processes
- CLI arguments leak through
ps set -xexposes secrets in logs- CI/CD systems often log environment variables
- Shell history may capture commands containing secrets
- Temporary files may persist after crashes
/procexposes process environments
๐ง When to Use These Techniques
- Passing API tokens to CLI tools
- Running scripts in CI/CD pipelines
- Handling credentials for cloud services
- Managing SSH keys or encrypted files
- Automating deployments or provisioning
โ When Not to Use Shell for Secrets
- Longโterm secret storage
- Highโsecurity environments
- Multiโuser systems without isolation
- Handling large or complex secret structures
- Applications requiring cryptographic operations
Use a real secret manager or a higherโlevel language.
โ Best Practices
- Never pass secrets as CLI arguments
- Never store secrets in shell history
- Never print secrets to stdout
- Use
read -sfor interactive input - Use stdin instead of arguments
- Use external secret stores
- Restrict file permissions (
chmod 600) - Disable tracing around sensitive operations
- Clear variables after use
- Avoid exporting secrets unless necessary
๐งช Testing Secret Safety
Check for leaks:
1 | |
Check environment exposure:
1 | |
Check history:
1 | |
Test safe input:
1 | |
๐ง Summary
Shell scripts are not inherently secure, but careful handling of secrets can minimize risk. Avoid passing secrets via arguments, avoid logging them, use stdin or external secret stores, and restrict file permissions. These practices ensure that automation remains safe, predictable, and productionโready.