Sensitive Data Masking
A git diff can contain API Keys, tokens, or emails added by mistake. Sending that diff to an external LLM API hands the secrets to a third-party server. ai-gitgen's --safe-mode finds such values with regex and hides them before sending.
What Gets Masked
| Target | Example pattern |
|---|---|
| OpenAI/Anthropic-style keys | sk-..., rk-... |
| GitHub tokens | ghp_, gho_, ghu_, ghs_, ghr_ |
| AWS Access Key | AKIA... |
| Slack tokens | xoxb-, xoxa-, etc. |
| Google API Key | AIza... |
| Authorization header | Authorization: Bearer ... |
| JWT | three dot-separated parts starting with eyJ... |
name@example.com | |
.env secrets | values on lines whose variable name contains KEY, SECRET, TOKEN, PASSWORD |
Matches are replaced with the type name kept, like [MASKED:GitHub token]. The LLM still knows "a token changed here" without seeing the value.
Keep the Context, Hide Only the Value
SECRET_KEY=abc123 → SECRET_KEY=[MASKED:secret setting]
Authorization: Bearer x.y → Authorization: [MASKED:Authorization/Bearer header]For .env lines and Authorization headers, regex groups keep the prefix (SECRET_KEY=, Authorization: ) and replace only the value. That way the commit message can still say something like "change secret key setting". Each run reports how many were masked, e.g. masked: GitHub token 1.
Order: Mask, Then Cut
safe-mode also limits the size (10 files, 200 lines by default). The order is fixed as mask first, cut later.
masked, found = mask_sensitive_text(diff)
limited, note = limit_diff(masked, max_files, max_lines)If you cut first, part of a key can survive at the boundary. For example, if sk-abcdefgh... is cut short, it may no longer match a "at least 8 chars" pattern and leak partially. Masking the full original first leaves no such gap.
The file limit splits the diff on diff --git headers and cuts by file; the line limit is applied after that.
Limitations
- It only finds known shapes. Passwords without a prefix or new key formats can slip through.
- Conversely, broad patterns like email also mask addresses in example docs.
- It is off by default. It only runs with
--safe-mode. For safety, on-by-default would be better. - So the principle is never commit secrets in the first place; safe-mode is one more guard against mistakes.
Questions That Could Come Up in an Interview
Q. How did you prevent sensitive data from going to an external AI?
This checks whether you understand the risk of external transmission and the limits of your safeguard.
In
--safe-mode, keys, tokens, JWTs, emails, and.envsecrets are found with regex and replaced with[MASKED:type]before sending. For.envlines I kept the variable name and hid only the value so the AI keeps the context. I mask before cutting the size so no partial key remains at a cut boundary. But it only catches defined patterns and is off by default, so not committing secrets comes first.
Follow-up Questions
- What would you change if you rebuilt it? → Turn safe-mode on by default, and add detection of high-randomness (entropy) strings alongside patterns. I haven't implemented the entropy approach myself.
Source: Codyssey-B1/B3-2
