#90 AI security and governance 5 min
Stop Leaking API Keys: Build a Secret Scanner in Python
I hid six fake keys in a project. My first scanner found three.
Read what the fixer did
Copy the code folder first. Run python compare.py, which writes a small project folder next to the script and scans it. Then open project/app.py and project/check.py. Predict first what each file starts with now. You should see import os on the first line of both, and the key line in app.py now reading os.environ. check.py held a hash, not a key, yet it was rewritten too. Write one sentence on why you should always read the changes an automatic fix makes.
A hint
The fixer rewrites any line of the form name = 'value' in a Python file. It does not know what the value is.
Add a fourth key shape
In scan.py add one more pattern to SHAPES for keys that start with tok_ followed by 40 hex characters: r"tok_[0-9a-f]{40}". Predict how many of the six keys the known shapes scanner finds, then run python compare.py. You should see 4 of 6 with no false alarms. Which file does it find now? Then say why adding a shape for every habit you can think of is not a full answer.
A hint
The deploy.sh file holds a key with the tok_ prefix. Remember that the other scanner works without any shapes.
Move the entropy limit
In scan.py change the default limit in by_randomness from 3.2 to 3.5, then to 3.8, then to 4.0, running python compare.py each time. Predict first what happens to the number of keys found and to the false alarm. You should see 4 of 6 with a false alarm at 3.5, 4 of 6 with no false alarm at 3.8, and 0 of 6 at 4.0. The video says its limit of 3.2 was a guess. What does this show about picking a limit?
A hint
Compare what each limit finds and what it wrongly flags. The check file holds a hash.
Find the split key
After running python compare.py, open project/split.py. Predict first why the known shapes scanner missed it but the randomness scanner found it. The key is written as two quoted pieces joined with a plus, so no single quoted string holds the whole key. Then, using QUOTED and entropy in scan.py, work out which of the two pieces is long enough to be scored, and write down whether the scanner finds the key because it recognises its shape.
A hint
The scanner takes every run of 20 or more letters, digits, underscores and hyphens and scores each one for randomness.
These tasks are for you to try on your own machine. Nothing is checked or saved.
What this does not show
Six habits I chose and fake keys from a seeded generator; the three shapes are simplified and the entropy limit of 3.2 is a guess; real scanners were not compared; a key that was already pushed stays in the history and must be revoked and replaced.
Up next in AI security and governance
More in AI security and governance
Related videos
- #163 Secrets in ML Apps: Scan, Move Out, Rotate (Python) MLOps and deployment
- #281 How Sites Store Passwords: Hash, Salt and Slow Hashes How it works, by example
- #282 5 Password Attacks and the Defence for Each How it works, by example
- #89 Jailbreak-Test Your Own App: Eight Wrappers, Counted AI security and governance
