learning-it by example

#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.

1. If an API key is ever exposed, for example pushed to a public repository, what does the video say you must do?
2. Six fake keys were hidden in a small project in six different habits, with a file hash as a decoy. What did the two scanners find?
3. How does the video's second scanner decide that a file may hold a secret?
4. The automatic fixer rewrites a line like name = 'value' to read from an environment variable. What did it get wrong in the secret scanner demo?
5. The randomness scanner flags a file when a long word has an entropy score above 3.2. What does the video say about that limit?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. 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.

  2. 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.

  3. 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.

  4. 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

All AI security and governance videos

Related videos