Day 9: Protection Against Plain Text
Goal: A pre-commit hook keeps private keys and unencrypted secrets out of the repo, and you know what to do if something was pushed anyway. · Duration: approx. 60 min · Prerequisite: Day 8
Briefing
- The most common sops mishap is not a crypto failure but a
git add .before thesops encrypt -i. - Two checks complement each other: gitleaks detects known patterns (age keys, tokens, Kubernetes Secrets in plain text). A structure check requires files in the encrypted folders to have sops metadata.
- A Git hook runs only locally and can be bypassed with
--no-verify. The same check therefore also belongs in CI or on the server. - Once something is pushed, it is burned: change the secret, then clean up.
git commit ──► .git/hooks/pre-commit
├─ gitleaks git --pre-commit --staged (patterns)
├─ sops metadata in dev/ prod/ …? (structure)
└─ exit ≠ 0 ──► no commit
Drill 1: Install gitleaks and check the history
gitleaks calls amd64 x64 – a case line
translates it.
# on argo
mkdir -p ~/sops/tag09 && cd ~/sops/tag09
ARCH=$(dpkg --print-architecture)
case $ARCH in amd64) GA=x64;; arm64) GA=arm64;; esac
G=https://github.com/gitleaks/gitleaks/releases/download/v8.30.1
curl -fsSLO $G/gitleaks_8.30.1_linux_$GA.tar.gz
curl -fsSLO $G/gitleaks_8.30.1_checksums.txt
grep " gitleaks_8.30.1_linux_$GA.tar.gz$" gitleaks_8.30.1_checksums.txt \
| sha256sum -c
tar -xzf gitleaks_8.30.1_linux_$GA.tar.gz gitleaks
sudo install -m 0755 gitleaks /usr/local/bin/gitleaks
gitleaks version
cd ~/sops/repo
gitleaks git --no-banner --no-color . 2>&1 | tail -3Expected:
gitleaks_8.30.1_linux_x64.tar.gz: OK
8.30.1
4:38PM ERR [git] fatal: unable to read files to diff
4:38PM INF 0 commits scanned.
4:38PM INF no leaks found
“no leaks found” – but 0 commits scanned? The error
message before it gives it away: gitleaks reads the history with
git log -p, and that triggers your textconv driver from Day
8. For old states sops decrypt fails, git aborts, gitleaks
checks nothing and still gives the all-clear.
# on argo
cd ~/sops/repo
gitleaks git --no-banner --no-color --log-opts="--no-textconv" . 2>&1 | tail -2Expected:
4:38PM INF scanned ~17524 bytes (17.52 KB) in 705ms
4:38PM INF no leaks found
Now all commits were checked. Remember: a scanner that scanned
nothing is worse than no scanner – always read the scanned
line.
Save your progress? Sign up for free
Drill 2: The hook
There are no --log-opts for --pre-commit;
there, the environment variables GIT_CONFIG_* switch off
the driver for this one call.
# on argo
cd ~/sops/repo
cat > .git/hooks/pre-commit <<'EOF'
#!/bin/sh
# tag09: kein Klartext ins Repo
# 1) bekannte Schlüssel- und Token-Muster (textconv aus, sonst sieht gitleaks Klartext)
export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=diff.sopsdiffer.textconv
export GIT_CONFIG_VALUE_0=cat
gitleaks git --pre-commit --staged --redact --no-banner --no-color -l warn . || exit 1
# 2) Dateien in verschlüsselten Ordnern brauchen sops-Metadaten
rc=0
for f in $(git diff --cached --name-only --diff-filter=ACM); do
case "$f" in
dev/*|prod/*|notes/*|plattform/*|tresor/*)
git show ":$f" | grep -qE '^(sops:|sops_mac=|\[sops\])|"sops": \{' || {
echo "KLARTEXT: $f ist nicht mit sops verschlüsselt"; rc=1; } ;;
esac
done
exit $rc
EOF
chmod +x .git/hooks/pre-commit
ls -l .git/hooks/pre-commit | cut -c1-10Expected:
-rwxr-xr-x
The hook checks the staged content
(git show ":$f"), not the file on disk – otherwise you
could stage plain text and encrypt the local file afterwards. The German
comments and message in the hook say: no plain text in the repo; (1)
known key and token patterns; (2) files in encrypted folders need sops
metadata. With textconv, gitleaks would see the decrypted
values and report every encrypted file as a leak; hence cat
as the driver.
Save your progress? Sign up for free
Drill 3: A private key wants into the repo
Break it:
# on argo
cd ~/sops/repo
cp ~/sops/tag06/anna.key tresor/anna.key
git add tresor/anna.keySymptom:
# on argo
cd ~/sops/repo
git commit -q -m "tag09: versehentlich"; echo "rc=$?"Expected:
4:40PM WRN leaks found: 2
rc=1
Investigate:
# on argo
cd ~/sops/repo
GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=diff.sopsdiffer.textconv GIT_CONFIG_VALUE_0=cat \
gitleaks git --pre-commit --staged --redact --no-banner --no-color -v . 2>&1 \
| grep -E 'RuleID|File'Expected:
RuleID: generic-api-key
File: tresor/anna.key
RuleID: age-secret-key
File: tresor/anna.key
Cause: gitleaks knows the pattern
AGE-SECRET-KEY-1… (and even finds the public key comment
suspicious).
Fix and verify:
# on argo
cd ~/sops/repo
git restore --staged tresor/anna.key && rm tresor/anna.key
printf '*.key\n*.token\nkeys.txt\n' > .gitignore
git add .gitignore && git commit -q -m "tag09: Schlüsseldateien ignorieren"
git log --oneline | head -1Expected:
48eca5a tag09: Schlüsseldateien ignorieren
.gitignore is the first line of defense: what is ignored
does not even reach the index with git add ..
Save your progress? Sign up for free
Drill 4: A secret in plain text
Break it: Two new files – a Kubernetes Secret and a custom configuration, both unencrypted.
# on argo
cd ~/sops/repo
kubectl create secret generic neu -n notes-dev \
--from-literal=password=lab-db-neu-123 --dry-run=client -o yaml > dev/neu.yaml
printf 'db_password: lab-db-123\n' > notes/klar.yaml
git add notes/klar.yaml
git commit -q -m "tag09: Konfiguration"; echo "rc=$?"
git restore --staged notes/klar.yaml && rm notes/klar.yaml
git add dev/neu.yaml
git commit -q -m "tag09: neues Secret"; echo "rc=$?"Expected
(ist nicht mit sops verschlüsselt means “is not encrypted
with sops”):
KLARTEXT: notes/klar.yaml ist nicht mit sops verschlüsselt
rc=1
4:40PM WRN leaks found: 2
rc=1
lab-db-123 has too little randomness for gitleaks to
take it for a password – the structure check catches it. gitleaks
recognizes the Kubernetes Secret on its own (rule
kubernetes-secret-yaml).
Fix and verify:
# on argo
cd ~/sops/repo
sops encrypt -i dev/neu.yaml
git add dev/neu.yaml && git commit -q -m "tag09: neues Secret" && echo "rc=$?"
git push -qExpected:
rc=0
Save your progress? Sign up for free
Drill 5: The hook is voluntary
# on argo
cd ~/sops/repo
git switch -q -c leck
printf 'password: lab-leck-123\n' > dev/leck.yaml
git add dev/leck.yaml
git commit -q --no-verify -m "tag09: Leck"; echo "rc=$?"
git rev-parse HEAD > ~/sops/tag09/leck.sha
git push -q origin leck 2>&1 | grep -c pullExpected:
rc=0
1
--no-verify skips all hooks, and a fresh clone has none
at all – .git/hooks is not cloned. The leak is now on the
server; Forgejo kindly offers to open a pull request for it.
Save your progress? Sign up for free
Drill 6: Pushed is pushed
The reflex: delete the branch, done?
# on argo
cd ~/sops/repo
git switch -q main && git branch -D leck >/dev/null
git push -q origin --delete leck
SHA=$(cat ~/sops/tag09/leck.sha)
curl -s -u lab:lab-forgejo-123 \
http://172.16.0.182:30300/lab/secrets/raw/commit/$SHA/dev/leck.yamlExpected:
password: lab-leck-123
The branch is gone, the commit is not: anyone who knows the commit ID (from an e-mail notification, a CI log, a fork, a clone) can still read it. Git servers remove unreachable objects only during garbage collection after a grace period – and nobody cleans up clones and backups.
The order of steps in an incident:
| Step | Why |
|---|---|
| 1. Change the secret (database, token, key) | the old one is burned |
| 2. Check access during the window (audit logs) | was it used? |
| 3. Commit it again, encrypted | the new value belongs in the repo |
4. Clean up the history (git filter-repo, server
GC) |
optional, cosmetic |
Save your progress? Sign up for free
Drill 7: Check the whole history and all branches
A leak on someone else’s branch only shows up if you check all refs. Locally the branch no longer exists – nor on the server. What gitleaks finds across all local refs:
# on argo
cd ~/sops/repo
gitleaks git --no-banner --no-color --log-opts="--no-textconv --all" . 2>&1 \
| tail -2
git cat-file -t $(cat ~/sops/tag09/leck.sha)Expected:
4:41PM INF scanned ~19316 bytes (19.32 KB) in 693ms
4:41PM INF no leaks found
commit
--all finds nothing anymore because no ref points to the
commit – but the object is still in your local repo
(git cat-file -t says commit). And gitleaks
would not have recognized lab-leck-123 as a secret anyway.
Pattern scanners find known formats; only you know that “what is in
plain text here used to be secret”.
Save your progress? Sign up for free
Exercise: Speed Drills
- Show which file patterns
.gitignoreexcludes. Target time: 10 s - Check whether
prod/notes-db.yamlis encrypted from the hook’s point of view (structure rule by hand). Target time: 30 s
Solution
# on argo
cd ~/sops/repo
cat .gitignore
git show :prod/notes-db.yaml | grep -cE '^(sops:|sops_mac=|\[sops\])'Expected:
*.key
*.token
keys.txt
1
Save your progress? Sign up for free
Checkpoint
# on argo
cd ~/sops/repo && git status -sb
git ls-remote --heads origin | wc -l
test -x .git/hooks/pre-commit && echo "Hook aktiv"Expected (Hook aktiv means “hook
active”):
## main...origin/main
1
Hook aktiv
Challenge
The hook only protects your clone. Make a fresh clone in
~/sops/tag09/klon, check that no hook is active there, and
set it up: put the hook file into the repo as
hooks/pre-commit and point Git to it with
core.hooksPath.
Hint: core.hooksPath is a local setting
– everyone has to set it once too, but the file itself is then
versioned.
Solution
# on argo
cd ~/sops/repo
mkdir -p hooks && cp .git/hooks/pre-commit hooks/pre-commit
git add hooks/pre-commit && git commit -q -m "tag09: Hook versioniert" && git push -q
git clone -q http://172.16.0.182:30300/lab/secrets.git ~/sops/tag09/klon
cd ~/sops/tag09/klon
ls .git/hooks | grep -c '^pre-commit$'
git config core.hooksPath hooks
printf 'pw: lab-x-123\n' > dev/x.yaml && git add dev/x.yaml
git commit -q -m test; echo "rc=$?"Expected:
0
KLARTEXT: dev/x.yaml ist nicht mit sops verschlüsselt
rc=1
Save your progress? Sign up for free
Cleanup
# on argo
rm -rf ~/sops/tag09/klon ~/sops/tag09/gitleaks_8.30.1_linux_*.tar.gz
cd ~/sops/repo && git config core.hooksPath hooks && git status -sbExpected:
## main...origin/main
From now on your main clone uses the versioned hook as well.
Interview Note
- How do you prevent plain-text secrets in the repo?
.gitignorefor key files, a pre-commit hook (gitleaks + structure check), and the same check in CI or as a server hook, because local hooks are voluntary. - A secret was pushed – what do you do first? Change the secret. Rewriting history does not help against clones, forks, caches and backups.
Enterprise Note
- The pre-commit framework (
.pre-commit-config.yaml) distributes hooks consistently, including gitleaks and custom checks. - Server side: secret scanning with push protection (Git platform),
otherwise gitleaks as the first CI step of every pull request with
--log-optscovering the new commits. - Custom gitleaks rules (
.gitleaks.toml,[extend] useDefault = true) for in-house token formats. - A leak runbook with owners per type of secret: who can change the DB password, the cloud key, the age key within one hour?