Chapter 18Lesson 02~100 minutes

Secrets, Credentials, and Process-Table Exposure

Secrets can leak without ever being printed intentionally. Process metadata, environment inheritance, tracing, files, history, and diagnostics all create exposure surfaces that production Bash must minimize.

IntermediateSecurity & portabilityHands-on lab

Learning objectives

By the end of this lesson

  • Recognize common secret exposure channels.
  • Avoid credentials in command arguments.
  • Scope secret-bearing environment variables narrowly.
  • Protect secret files and tracing boundaries.
  • Respond correctly to credential exposure.

1. Secrets leak through more channels than stdout

Credentials can appear in process arguments, environment variables, shell history, xtrace output, temporary files, core dumps, CI logs, diagnostic bundles, or inherited child environments.

Common secret exposure surfaces
flowchart TD
  S["secret"] --> A["argv"]
  S --> E["environment"]
  S --> F["files"]
  S --> L["logs / traces"]
  S --> H["history / debug bundles"]

2. Command-line arguments can be observable

# Risky pattern:
# some_cli --token "$API_TOKEN"

# Prefer a supported stdin/file/provider mechanism:
printf '%s' "$API_TOKEN" |
some_cli login --token-stdin

On many systems, process arguments may be visible to other users, monitoring agents, or diagnostic tools.

3. Environment variables are convenient, not invisible

export API_TOKEN
run_child_process

Child processes inherit exported environment variables. Depending on OS and permissions, process environments may also be inspectable. Scope secret-bearing variables to the smallest command possible.

4. Prefer narrow command-scoped environment injection

API_TOKEN="$API_TOKEN" \
DEPLOY_ENV=prod \
./deploy-helper.sh

Avoid exporting credentials globally when only one subprocess needs them.

5. Disable tracing before secret expansion

set +x
response=$(secure_login "$API_TOKEN")
status=$?
set -x
Tracing hazard

A secret can leak even if the command later fails. The exposure occurs when Bash prints the expanded trace line.

6. Secret files require restrictive permissions

umask 077
credential_file=$(mktemp) || exit 1
trap 'rm -f -- "$credential_file"' EXIT

printf '%s\n' "$PRIVATE_KEY" > "$credential_file"
chmod 0600 -- "$credential_file"

7. Interactive history is another risk

Avoid typing long-lived secrets directly into interactive command lines. Prefer credential helpers, protected prompts, files with correct permissions, or short-lived identity mechanisms.

8. Log state, not secret value

if [[ -n ${API_TOKEN:-} ]]; then
  printf 'api_token=present\n' >&2
else
  printf 'api_token=missing\n' >&2
  exit 65
fi

Do not log hashes or lengths unless they are genuinely needed; even derived metadata can reveal information.

9. Short-lived credentials reduce blast radius

Workload identity, federated identity, instance roles, managed identities, and short-lived tokens are generally preferable to static secrets embedded in files or project settings.

10. Cleanup cannot revoke an already-exposed secret

Deleting a temporary credential file is necessary hygiene, but once a secret appears in logs or leaves the trust boundary, rotation or revocation is the recovery action.

11. Hands-on lab: compare secret scopes

mkdir -p "$HOME/devops-academy/bash/chapter18/lesson02"
cd "$HOME/devops-academy/bash/chapter18/lesson02"

cat > inspect-env.sh <<'EOF'
#!/usr/bin/env bash
set -u

if [[ -n ${DEMO_SECRET:-} ]]; then
  printf 'child_secret=present\n'
else
  printf 'child_secret=missing\n'
fi
EOF
chmod u+x inspect-env.sh

DEMO_SECRET='temporary-value' ./inspect-env.sh

printf 'parent_exported=%s\n' \
  "$([[ -v DEMO_SECRET ]] && printf yes || printf no)"

Verification checklist

12. Knowledge check

Question 1. Can command-line arguments expose secrets?

Question 2. Do exported variables propagate to children?

Question 3. What should happen after a secret is exposed?

Question 4. Why are short-lived credentials preferable?

13. Summary

Secret-safe Bash minimizes lifetime, scope, copies, and observability. Prefer supported credential providers, avoid argv and logs, scope environment variables narrowly, protect temporary files, disable tracing, and rotate credentials after exposure.

14. Further reading

  • OWASP Secrets Management Cheat Sheet.
  • OWASP Logging Cheat Sheet.
  • GNU Bash Reference Manual — environment and tracing.
  • Your operating system documentation for process inspection permissions.
Next lesson

Secure Temporary Files, Permissions, and Cleanup

Continue Chapter 18 by strengthening Bash trust boundaries, security assumptions, and portability guarantees.

Keep the academy open

Support free, practical DevOps education.

Every lesson is designed to remain readable in a browser, downloadable from GitHub, and usable without a paid learning platform. Contributions help expand and maintain the curriculum.

Ethereum / ERC-20
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0 Send only Ethereum/ERC-20 compatible assets to this address.