Sub topic 3
How keys leak
This chapter assumes you know basic Git (commit, push, .gitignore), can read short Python or JavaScript snippets, and know roughly what an HTTP API is. It explains where keys escape so the later chapters on storage make sense.
Why leaks are usually accidents
Almost nobody leaks a key on purpose. A key leaks when a normal workflow copies it somewhere you didn't intend. Those places are often public, searchable, or shared with more people than you expected. Automated scanners watch public code hosts for key patterns, so a pushed key can be found and abused within minutes.
The common leak paths
Commits
The most common path is a key hard-coded in a file, then committed:
# Do not do this
api_key = "sk-example-not-a-real-key"Deleting the line in a later commit does not help. Git keeps history, and anyone who clones the repository can read old commits. Forks and cached copies also keep the key. Config files such as .env leak the same way when they are not ignored.
Client-side code
Anything shipped to a browser or phone can be inspected. Bundled JavaScript, mobile app packages, and network requests in the browser's developer tools all expose the key. Environment variables baked into a front-end build (for example, variables with a public prefix in many bundlers) end up in the bundle as plain text.
Logs and error messages
Code that logs a full request prints the Authorization header with it:
import logging
logging.basicConfig(level=logging.INFO)
headers = {"Authorization": "Bearer sk-example-not-a-real-key"}
logging.info("Sending request with headers: %s", headers) # leaks the keyLog files are often copied to shared dashboards, and stack traces get pasted into tickets. Debug-level logging in HTTP libraries can also print headers.
Screenshots, chats, and shared files
Keys leak when people share a terminal screenshot, paste a config file into a chat or forum post, record a screen during a demo, or attach a notebook with saved cell output. Notebooks are a frequent source because the output of print(api_key) is stored in the file.
Other places
- Shell history, if you typed the key inline in a command.
- Container images, when the key is copied in with
COPYor set withENVin a Dockerfile. - Backups and shared drives that include project folders.
- Third-party tools, such as AI coding assistants or plugins, that read your files.
Spotting your own exposure
Search your project for strings that look like keys before you share it:
git log -p | grep -n "sk-"The pattern sk- is only an example. Use the prefix your provider documents. If this finds a match in history, treat the key as exposed.
Pattern to remember
Each path above comes from the key being somewhere other than the running server's memory and environment. The next chapters fix this in order. First you move the key into the environment, then into a git-ignored file, and finally you keep it off the client entirely.
Exercise
A teammate removes a hard-coded key from app.py and commits the fix. The earlier commit with the key was already pushed to a shared repository. Is the key safe now? Name one other place in this chapter where a key could have leaked in the same project.
Show answer
No. The key is still in Git history, so anyone with access to the repository can read it, and it must be revoked. Other places include logs that print the Authorization header, a front-end bundle, a notebook's saved output, or a pasted screenshot.