Repository Sanitization Skill
This skill provides systematic approaches for identifying and removing sensitive information from git repositories, including credentials, API keys, tokens, and other secrets.
When to Use This Skill
- Preparing a private repository for public release
- Auditing repositories for accidentally committed secrets
- Cleaning up after a credential leak
- Removing sensitive data from git history
- Compliance and security reviews
Critical Understanding
Repository sanitization involves two distinct scopes:
- Working Directory Sanitization: Replacing secrets in current file contents
- Git History Sanitization: Removing secrets from all historical commits
Simply editing files in the working directory does NOT remove secrets from git history. The .git/objects directory retains all historical versions of files.
Approach
Phase 1: Comprehensive Secret Detection
Before making any changes, perform exhaustive detection using multiple strategies:
1.1 Pattern-Based Detection
Search for common secret patterns. Refer to references/secret_patterns.md for comprehensive regex patterns covering:
- API keys (AWS, GCP, Azure, GitHub, GitLab, Hugging Face, OpenAI, etc.)
- Authentication tokens (JWT, OAuth, Bearer tokens)
- Database credentials (connection strings, passwords)
- Private keys (RSA, SSH, PGP)
- Environment variable assignments containing secrets
- Base64-encoded secrets
- Webhook URLs with embedded tokens
1.2 File-Based Detection
Check files commonly containing secrets:
.env, .env.* files
- Configuration files:
*.yaml, *.yml, *.json, *.toml, *.ini, *.cfg
- Docker files:
Dockerfile, docker-compose.yml
- CI/CD configs:
.github/workflows/*, .gitlab-ci.yml, Jenkinsfile
- Cloud configs:
terraform.tfvars, *.tfstate
- Credential files:
credentials, secrets, *.pem, *.key
1.3 Entropy-Based Detection
High-entropy strings often indicate secrets. Look for:
- Strings with mixed case, numbers, and special characters
- Strings longer than 20 characters without dictionary words
- Base64-encoded blobs in unexpected locations
Phase 2: Systematic Verification
After initial detection:
- Document all findings before making changes
- Categorize secrets by type (API key, password, token, etc.)
- Identify false positives (environment variable references vs actual values)
- Check all files in directories where secrets are found (not just the first match)
- Examine binary files that might contain embedded secrets
Phase 3: Sanitization Strategy
3.1 Placeholder Format
Use consistent placeholder formats:
<your-aws-access-key> for AWS keys
<your-api-key> for generic API keys
<your-database-password> for passwords
${ENV_VAR_NAME} for values that should come from environment
3.2 Working Directory Sanitization
- Create a backup branch before modifications
- Replace secrets systematically, one type at a time
- Verify each replacement maintains file validity (especially JSON/YAML)
- Run syntax validation on modified configuration files
3.3 Git History Sanitization
For complete sanitization, the git history must also be cleaned:
Option A: BFG Repo-Cleaner (Recommended)
# Remove specific strings from history
bfg --replace-text secrets.txt repo.git
Option B: git filter-repo
# Remove file containing secrets from all history
git filter-repo --path sensitive-file.txt --invert-paths
Option C: git filter-branch (Legacy)
# Use only if other tools unavailable
git filter-branch --tree-filter 'command' HEAD
After history rewriting:
- Force push to remote (coordinate with team)
- All collaborators must re-clone
- Invalidate/rotate all exposed credentials
Phase 4: Verification
4.1 Post-Sanitization Checks
- Re-run all detection patterns to confirm no secrets remain
- Verify file syntax (JSON, YAML, etc.) is still valid
- Check that placeholder format is consistent throughout
- Confirm application can start (may fail due to missing secrets - expected)
4.2 Git History Verification
# Search git history for secret patterns
git log -p --all -S 'secret_pattern' --source
Common Pitfalls
Incomplete Detection
- Pitfall: Searching for only specific patterns (e.g.,
ghp_ for GitHub tokens)
- Solution: Use comprehensive pattern list; account for older token formats and variations
Ignoring File Types
- Pitfall: Only checking text files, missing secrets in JSON, notebooks, or binary files
- Solution: Check all file types; use specialized tools for binary inspection
Forgetting Git History
- Pitfall: Only sanitizing working directory, leaving secrets in git history
- Solution: Always warn about git history; use BFG or git-filter-repo for complete removal
Inconsistent Placeholders
- Pitfall: Using different placeholder formats (
<token>, YOUR_TOKEN, xxx)
- Solution: Define placeholder convention upfront; use search-replace consistently
Missing Related Secrets
- Pitfall: Finding one secret in a file but not checking for related secrets
- Solution: When a secret is found, thoroughly examine the entire file and directory
Partial Directory Scanning
- Pitfall: Checking only the first matching file in a directory
- Solution: Systematically check all files matching patterns in directories where secrets are found
Encoded Secrets
- Pitfall: Missing Base64-encoded or otherwise obfuscated secrets
- Solution: Decode suspicious Base64 strings; check for common encoding patterns
Verification Checklist
Before declaring sanitization complete:
1---2name: sanitize-git-repo3description: Guidance for sanitizing git repositories by identifying and removing sensitive credentials, API keys, tokens, and other secrets. This skill should be used when tasks involve cleaning repositories of secrets, preparing code for public release, auditing for credential exposure, or removing sensitive data from version control history.4---5
6# Repository Sanitization Skill
7
8This skill provides systematic approaches for identifying and removing sensitive information from git repositories, including credentials, API keys, tokens, and other secrets.
9
10## When to Use This Skill
11
12- Preparing a private repository for public release
13- Auditing repositories for accidentally committed secrets
14- Cleaning up after a credential leak
15- Removing sensitive data from git history
16- Compliance and security reviews
17
18## Critical Understanding
19
20Repository sanitization involves two distinct scopes:
21
221. **Working Directory Sanitization**: Replacing secrets in current file contents
232. **Git History Sanitization**: Removing secrets from all historical commits
24
25Simply editing files in the working directory does NOT remove secrets from git history. The `.git/objects` directory retains all historical versions of files.
26
27## Approach
28
29### Phase 1: Comprehensive Secret Detection
30
31Before making any changes, perform exhaustive detection using multiple strategies:
32
33#### 1.1 Pattern-Based Detection
34
35Search for common secret patterns. Refer to `references/secret_patterns.md` for comprehensive regex patterns covering:
36
37- API keys (AWS, GCP, Azure, GitHub, GitLab, Hugging Face, OpenAI, etc.)
38- Authentication tokens (JWT, OAuth, Bearer tokens)
39- Database credentials (connection strings, passwords)
40- Private keys (RSA, SSH, PGP)
41- Environment variable assignments containing secrets
42- Base64-encoded secrets
43- Webhook URLs with embedded tokens
44
45#### 1.2 File-Based Detection
46
47Check files commonly containing secrets:
48
49- `.env`, `.env.*` files
50- Configuration files: `*.yaml`, `*.yml`, `*.json`, `*.toml`, `*.ini`, `*.cfg`
51- Docker files: `Dockerfile`, `docker-compose.yml`
52- CI/CD configs: `.github/workflows/*`, `.gitlab-ci.yml`, `Jenkinsfile`
53- Cloud configs: `terraform.tfvars`, `*.tfstate`
54- Credential files: `credentials`, `secrets`, `*.pem`, `*.key`
55
56#### 1.3 Entropy-Based Detection
57
58High-entropy strings often indicate secrets. Look for:
59
60- Strings with mixed case, numbers, and special characters
61- Strings longer than 20 characters without dictionary words
62- Base64-encoded blobs in unexpected locations
63
64### Phase 2: Systematic Verification
65
66After initial detection:
67
681. **Document all findings** before making changes
692. **Categorize secrets** by type (API key, password, token, etc.)
703. **Identify false positives** (environment variable references vs actual values)
714. **Check all files in directories** where secrets are found (not just the first match)
725. **Examine binary files** that might contain embedded secrets
73
74### Phase 3: Sanitization Strategy
75
76#### 3.1 Placeholder Format
77
78Use consistent placeholder formats:
79
80- `<your-aws-access-key>` for AWS keys
81- `<your-api-key>` for generic API keys
82- `<your-database-password>` for passwords
83- `${ENV_VAR_NAME}` for values that should come from environment
84
85#### 3.2 Working Directory Sanitization
86
871. Create a backup branch before modifications
882. Replace secrets systematically, one type at a time
893. Verify each replacement maintains file validity (especially JSON/YAML)
904. Run syntax validation on modified configuration files
91
92#### 3.3 Git History Sanitization
93
94For complete sanitization, the git history must also be cleaned:
95
96**Option A: BFG Repo-Cleaner (Recommended)**
97```bash
98# Remove specific strings from history
99bfg --replace-text secrets.txt repo.git
100```
101
102**Option B: git filter-repo**
103```bash
104# Remove file containing secrets from all history
105git filter-repo --path sensitive-file.txt --invert-paths
106```
107
108**Option C: git filter-branch (Legacy)**
109```bash
110# Use only if other tools unavailable
111git filter-branch --tree-filter 'command' HEAD
112```
113
114After history rewriting:
115- Force push to remote (coordinate with team)
116- All collaborators must re-clone
117- Invalidate/rotate all exposed credentials
118
119### Phase 4: Verification
120
121#### 4.1 Post-Sanitization Checks
122
1231. Re-run all detection patterns to confirm no secrets remain
1242. Verify file syntax (JSON, YAML, etc.) is still valid
1253. Check that placeholder format is consistent throughout
1264. Confirm application can start (may fail due to missing secrets - expected)
127
128#### 4.2 Git History Verification
129
130```bash
131# Search git history for secret patterns
132git log -p --all -S 'secret_pattern' --source
133```
134
135## Common Pitfalls
136
137### Incomplete Detection
138
139- **Pitfall**: Searching for only specific patterns (e.g., `ghp_` for GitHub tokens)
140- **Solution**: Use comprehensive pattern list; account for older token formats and variations
141
142### Ignoring File Types
143
144- **Pitfall**: Only checking text files, missing secrets in JSON, notebooks, or binary files
145- **Solution**: Check all file types; use specialized tools for binary inspection
146
147### Forgetting Git History
148
149- **Pitfall**: Only sanitizing working directory, leaving secrets in git history
150- **Solution**: Always warn about git history; use BFG or git-filter-repo for complete removal
151
152### Inconsistent Placeholders
153
154- **Pitfall**: Using different placeholder formats (`<token>`, `YOUR_TOKEN`, `xxx`)
155- **Solution**: Define placeholder convention upfront; use search-replace consistently
156
157### Missing Related Secrets
158
159- **Pitfall**: Finding one secret in a file but not checking for related secrets
160- **Solution**: When a secret is found, thoroughly examine the entire file and directory
161
162### Partial Directory Scanning
163
164- **Pitfall**: Checking only the first matching file in a directory
165- **Solution**: Systematically check all files matching patterns in directories where secrets are found
166
167### Encoded Secrets
168
169- **Pitfall**: Missing Base64-encoded or otherwise obfuscated secrets
170- **Solution**: Decode suspicious Base64 strings; check for common encoding patterns
171
172## Verification Checklist
173
174Before declaring sanitization complete:
175
176- [ ] All secret patterns from `references/secret_patterns.md` searched
177- [ ] All common secret-containing file types checked
178- [ ] All files in directories with findings examined
179- [ ] Placeholder format is consistent
180- [ ] Configuration file syntax validated
181- [ ] Git history addressed (cleaned or documented as still containing secrets)
182- [ ] Comprehensive re-scan confirms no remaining secrets
183- [ ] Credential rotation recommended to user for any exposed secrets