How Secrets Leak
Almost no secret is stolen by breaking encryption. They leak through ordinary engineering habits: a debug flag, an environment dump, a plan file uploaded as a CI artifact, a layer in a container image. Knowing the paths is the first half of closing them.
| Path | Typical example |
|---|---|
| Git | a .env committed "temporarily", a key in a test fixture, a Kubernetes Secret YAML (base64 is not encryption) |
| Logs | set -x in a deploy script, a connection string in an error message, request logging of Authorization headers |
| Environment variables | inherited by every child process, readable in /proc/<pid>/environ, printed by crash reporters and env debugging steps |
| CI/CD | secrets echoed by a step, masked values transformed (base64, URL-encoded) so masking misses them, fork PRs with access to secrets |
| Images and artifacts | ENV/ARG in a Dockerfile, a file copied and "deleted" in a later layer, a plan file in an artifact store |
| Terraform | state and saved plans hold every attribute in plain text, including sensitive ones |
| People and tools | secrets pasted in chat, tickets and AI assistants; shell history; screenshots |
Scanning catches some of these after the fact (Secret Detection with gitleaks). The rest of this course is about not creating them in the first place.
Shell tracing and the environment
set -x prints every command after expansion, which includes the secret you just expanded. And an exported variable is visible to every process the script starts:
cat > deploy.sh <<'SH'
#!/bin/bash
set -euo pipefail
set -x
DB_PASSWORD="hunter2-prod"
export DB_PASSWORD
psql_url="postgres://linkly:${DB_PASSWORD}@db:5432/linkly"
set +x
# a "harmless" helper the script calls: it inherits the whole environment
bash -c 'echo "helper sees DB_PASSWORD=${DB_PASSWORD}"'
# any process running as the same user can read another one's environment
sleep 5 &
tr '\0' '\n' < /proc/$!/environ | grep '^DB_'
kill $!
SH
bash deploy.sh 2>&1+ DB_PASSWORD=hunter2-prod
+ export DB_PASSWORD
+ psql_url=postgres://linkly:hunter2-prod@db:5432/linkly
+ set +x
helper sees DB_PASSWORD=hunter2-prod
DB_PASSWORD=hunter2-prod
Four leaks in eleven lines: the assignment and the URL in the trace (which in CI goes straight into the job log), the inherited variable in the helper, and the variable in /proc/<pid>/environ of an unrelated child. Prefer passing secrets as files with tight permissions (0400, a tmpfs mount, a Kubernetes Secret volume), read them only where they are needed, and never trace sections that touch them.
CI masking is a safety net, not a control
GitHub Actions and GitLab CI replace the exact value of a registered secret with *** in logs. Anything that transforms the value defeats that: base64, URL-encoding, JSON-escaping, splitting across lines, printing one character at a time. Here is the idea, modelled:
import base64, urllib.parse
SECRET = "p@ss/w0rd+42"
def mask(log: str) -> str:
"""What a CI runner does: replace exact occurrences of registered secrets."""
return log.replace(SECRET, "***")
lines = [
f"connecting with password {SECRET}",
f"Authorization: Basic {base64.b64encode(f'linkly:{SECRET}'.encode()).decode()}",
f"url=postgres://linkly:{urllib.parse.quote(SECRET, safe='')}@db/linkly",
f"debug: {list(SECRET[:4])}",
]
for line in lines:
print(mask(line))connecting with password ***
Authorization: Basic bGlua2x5OnBAc3MvdzByZCs0Mg==
url=postgres://linkly:p%40ss%2Fw0rd%2B42@db/linkly
debug: ['p', '@', 's', 's']
Only the first line was masked. The Basic-auth header decodes to the password, and the URL-encoded form is the password with three characters escaped. Register derived values as secrets too (::add-mask:: in GitHub Actions), but mainly: don't print them.
Terraform plans and state
sensitive = true hides a value in plan output. It does not remove it from the saved plan or the state:
mkdir tf && cd tf
cat > main.tf <<'HCL'
variable "db_password" {
type = string
sensitive = true
}
resource "terraform_data" "db" {
input = { user = "linkly", password = var.db_password }
}
HCL
tofu init > /dev/null
TF_VAR_db_password=hunter2-prod tofu plan -out=tfplan | grep -A3 'input'
tofu show -json tfplan > plan.json
echo "--- plan.json"
jq -c '.resource_changes[0].change | {after_sensitive, after}' plan.json + input = {
+ password = (sensitive value)
+ user = "linkly"
}
--- plan.json
{"after_sensitive":{"input":{"password":true},"output":{}},"after":{"input":{"password":"hunter2-prod","user":"linkly"},"triggers_replace":null}}
The human-readable plan prints (sensitive value); the JSON plan, which policy tools, cost tools and PR bots read, contains the password. Treat saved plans and state like the secrets inside them: encrypted backends, short artifact retention, no uploads to public places. Newer providers avoid the problem with write-only arguments (password_wo) and ephemeral variables, which never reach the plan or state; Conftest on Terraform Plans uses them.
Images
A Dockerfile that copies a key, uses it and deletes it in a later RUN still ships the key in the earlier layer, and ARG/ENV values are stored in the image config (docker history, docker inspect). Use BuildKit secret mounts, which never land in a layer:
# syntax=docker/dockerfile:1
FROM golang:1.27 AS build
RUN --mount=type=secret,id=netrc,target=/root/.netrc \
go mod download # private modules; the .netrc exists only during this RUNdocker build --secret id=netrc,src=$HOME/.netrc -t linkly:dev .The Docker course covers layers in depth.
Go further: Leaked Access Keys: Scanning and Response walks through the response to a leaked AWS key, step by step.
Test yourself
A CI runner masks the registered secret p@ss. Which log line still exposes it?
password p@ssAuthorization: Basic bGlua2x5OnBAc3M=(base64 of user:password)- Neither
- Both are masked
Where does a Terraform variable marked sensitive = true still appear in plain text?
- Nowhere
- In the saved plan file /
show -jsonoutput and in the state - Only in the console output
- Only in the lock file
Why are environment variables a weaker way to pass secrets than a file with mode 0400?
- They are slower
- Every child process inherits them and they are readable via /proc and in crash dumps or debug output
- They have a size limit of 10 bytes
- Files are encrypted automatically