CI secret scan for AI provider API keys in code, notebooks and fixtures
A dependency-free Python scanner and GitHub Actions workflow that blocks pull requests containing Anthropic, OpenAI, AWS, GitHub, Google, Slack or Hugging Face keys, or private keys, with masked output and an allowlist file.
Automation in Security by AISkillHub Library
What it does
AI projects leak keys in places code review skims past: a notebook cell that printed the environment, an `.env.example` someone filled in, a curl command pasted into a prompt fixture, an eval log that echoed request headers. This check scans every file tracked by git on each pull request and fails if it finds a well-known key format.
**What it detects**
| Name | Pattern | |---|---| | `anthropic-api-key` | `sk-ant-...` | | `openai-api-key` | `sk-proj-...`, `sk-svcacct-...`, `sk-admin-...`, legacy 48-character `sk-` keys | | `aws-access-key-id` | `AKIA` or `ASIA` plus 16 characters | | `github-token` | `ghp_`, `gho_`, `ghu_`, `ghs_`, `ghr_`, `github_pat_` | | `google-api-key` | `AIza` plus 35 characters | | `slack-token` | `xoxb-`, `xoxp-` and related | | `huggingface-token` | `hf_` tokens | | `private-key` | PEM private key headers |
Matches are masked in the output (`sk-ant-a...0123`), so the CI log does not re-leak the key. Binary files, model weights and files over 5 MB are skipped.
**Suppressing false positives**
Add `secretscan:allow` on the same line, or list a glob in `.secretscan-ignore`. Only do this for values you have confirmed are fake.
**What it will not do**
- Scan git history. A key removed in a later commit is still exposed; rotate it. - Catch generic secrets such as database passwords or custom tokens without a recognizable prefix. - Replace your platform's secret scanning or a dedicated scanner. It is a fast, transparent first line of defense you can read in five minutes.
If it fires on a real key: remove it, **rotate the key with the provider immediately**, and if it was ever pushed, assume it is compromised.
Exit codes: 0 nothing found, 1 possible secrets found, 2 not a git repository or unreadable ignore file.
How to use it
1. Copy `scripts/scan_ai_keys.py`, `.github/workflows/secret-scan.yml` and `.secretscan-ignore` into your repository. 2. Run it locally once to clean up existing findings: ``` python3 scripts/scan_ai_keys.py # all files tracked by git python3 scripts/scan_ai_keys.py --all-files # walk the folder instead (also untracked files) python3 scripts/scan_ai_keys.py notebooks/x.ipynb ``` 3. For confirmed fake values in test fixtures, add the file glob to `.secretscan-ignore` or put `secretscan:allow` on the line. 4. Commit. The workflow runs on every pull request and push to `main`. 5. Optional local hook: add `python3 scripts/scan_ai_keys.py` to `.git/hooks/pre-commit` (make it executable) to catch keys before they are committed.
Python 3.8+ and git. No secrets or environment variables are needed.
Files
Save each file at the path shown, relative to your project root.