Deleted, but still there
In January a developer put the shop's payments key in a file, pushed, then noticed. The next day they replaced it with a line that reads the key from the environment.
The file looks clean now. Is the key gone?
By the end of this lesson
Note
I can find a secret in a repository's history and stop the next one.
Key words
| Word | What it means |
|---|---|
| Secret | a key or password that grants access |
| History | every commit a repository has ever had |
| Entropy | how random a string looks |
| Rotate | replace a key and cancel the old one |
| Fingerprint | the id of one finding, to accept it |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsScan the files, then the history
Gitleaks is an open-source secret scanner. gitleaks dir reads the files as they are now. gitleaks git reads every commit in the history.
--redact hides the value in the output, so the scan does not leak the secret a second time.
Bring the lab's copy up to date
docker exec gitleaks git -C /work/tuckshop pull -qShown here, not run. Press Start lab to type it into your own lab’s terminal.
List the commits
docker exec gitleaks git -C /work/tuckshop log --onelineShown here, not run. Press Start lab to type it into your own lab’s terminal.
Scan the files as they are now
docker exec gitleaks gitleaks dir /work/tuckshop --no-banner --redactShown here, not run. Press Start lab to type it into your own lab’s terminal.
Scan every commit
docker exec gitleaks gitleaks git /work/tuckshop --no-banner --redact -vShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA common mistake
Warning
Many people think deleting the line in a new commit removes the secret. Every clone still holds the old commit. Once a key is pushed, rotate it first: make a new one and cancel the old. Rewriting history comes after, if at all.
How a scanner spots a key
Gitleaks has rules for many providers' key formats. Its generic rule looks for a name such as KEY or TOKEN and a value with high entropy.
Entropy measures how mixed the characters are. A word scores low; a random key scores high. The shop's key scored about 4.9.
Try it: measure entropy
import math
def entropy(text):
counts = {}
for ch in text:
counts[ch] = counts.get(ch, 0) + 1
total = len(text)
return sum(n / total * math.log2(total / n) for n in counts.values())
for value in ["password1", "aaaaaaaaaaaa", "7Gq2Rk9Xw4Lm8Tz1Vb5Nc3Hd"]:
print(round(entropy(value), 2), value)
# Add a value of your own. Which kind of string scores highest?
Make a free account to save your work and track your progress.
A rule for your own key format
The generic rule can miss a key, for example one whose value contains the word "fake". Your own keys have a known shape, so describe it.
The shop's keys start tuck_live_ and 24 letters or digits follow. The rule extends the built-in ones.
Write the shop's own rule
docker exec -i gitleaks sh -c 'cat > /work/rules/gitleaks.toml' <<'EOF'
[extend]
useDefault = true
[[rules]]
id = "tuckshop-payments-key"
description = "Tuck shop payments key"
regex = '''tuck_live_[A-Za-z0-9]{24}'''
keywords = ["tuck_live_"]
EOFShown here, not run. Press Start lab to type it into your own lab’s terminal.
Scan the history with it
docker exec gitleaks gitleaks git /work/tuckshop --config /work/rules/gitleaks.toml --no-banner --redact -vShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsAccept a rotated key, stop a new one
The old key was rotated, so the team accepts that finding. Its fingerprint goes in an ignore file, and the scan passes.
Then a pre-commit hook scans what you are about to commit. A hook can be skipped, so the pipeline scans again on the server.
Accept the old finding by its fingerprint
docker exec gitleaks sh -c 'gitleaks git /work/tuckshop --no-banner -v 2>/dev/null | grep Fingerprint | cut -d" " -f2 > /work/rules/.gitleaksignore'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Scan again with the ignore file
docker exec gitleaks gitleaks git /work/tuckshop --gitleaks-ignore-path /work/rules/.gitleaksignore --no-bannerShown here, not run. Press Start lab to type it into your own lab’s terminal.
Clone the shop in your terminal
test -d tuckshop || git clone -q http://student:student@gitea:3000/student/tuckshop.gitShown here, not run. Press Start lab to type it into your own lab’s terminal.
Add a pre-commit hook
printf '#!/bin/sh\ngit diff --cached | docker exec -i gitleaks gitleaks stdin --no-banner --redact\n' > tuckshop/.git/hooks/pre-commit && chmod +x tuckshop/.git/hooks/pre-commitShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try to commit a made-up key
echo 'PAYMENTS_KEY = "tuck_live_Zz9Yy8Xx7Ww6Vv5Uu4Tt3Ss2"' >> tuckshop/settings.py && git -C tuckshop -c user.name=Student -c [email protected] commit -qam 'Try a key'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Throw the change away
git -C tuckshop checkout settings.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Assignment: a small secret scanner
Write a small secret scanner in three functions.
entropy(text) returns the Shannon entropy of a string in bits per character, rounded to 2 places, and 0 for an empty string: for each character, take its share p of the string and add up -p * log2(p). is_suspect(line) returns True when the line has an =, the name to its left contains KEY, TOKEN or PASSWORD (in any case), and the value to its right, with spaces and double quotes stripped, is at least 16 characters long with an entropy of at least 3.5. redact(line) returns the line with that value replaced by REDACTED inside the quotes, keeping the first 4 characters, such as KEY = "tuckREDACTED".
import math
def entropy(text):
return 0
def is_suspect(line):
return False
def redact(line):
return line
Make a free account to save your work and track your progress.
Exit quiz
Have a go