SAASMENT

Security skill · Find and fix

Secrets in repo: scan and rotation.

Scan a repository and its git history for committed secrets (API keys, tokens, private keys, passwords), rotate and revoke anything exposed, remove secrets from code, and add prevention. Use when asked to audit a repo for leaked credentials, respond to an exposed key, or set up secret scanning.

Download .zipInstall via MCPFind and fixMIT
  • Secrets
  • Code review

Facts

Category
Find and fix
License
MIT
Tags
  • Secrets
  • Code review
Version
1.0.0
Last updated
2026-12-02
Folder
secrets-in-repo-scan-and-rotation/
MCP resource
skill://secrets-in-repo-scan-and-rotation/SKILL.md
Files
  • SKILL.md
  • LICENSE
  • references/rotation-runbook.md
Standards

Original Saasment content. Saasment is in early access; this skill does not require joining it.

Install via MCP or as files

Connect your client to https://www.saasment.com/api/mcp (see client setup) and call get_skill:

{
  "name": "get_skill",
  "arguments": {
    "slug": "secrets-in-repo-scan-and-rotation"
  }
}

Or download the .zip and unpack it into your agent's skills directory:

Terminal, from your project root (user-level: ~/.cursor/skills)

mkdir -p .cursor/skills
curl -fsSL https://www.saasment.com/ai-security/skills/secrets-in-repo-scan-and-rotation/download.zip -o /tmp/secrets-in-repo-scan-and-rotation.zip
unzip -o /tmp/secrets-in-repo-scan-and-rotation.zip -d .cursor/skills
# result: .cursor/skills/secrets-in-repo-scan-and-rotation/SKILL.md

Review SKILL.md before enabling a skill. Skills are instructions your agent will follow.

SKILL.md

---
name: secrets-in-repo-scan-and-rotation
description: Scan a repository and its git history for committed secrets (API keys, tokens, private keys, passwords), rotate and revoke anything exposed, remove secrets from code, and add prevention. Use when asked to audit a repo for leaked credentials, respond to an exposed key, or set up secret scanning.
license: MIT
metadata:
  author: Saasment
  version: "1.0.0"
  category: both
  tags: "secrets, code-review"
---

# Secrets in repo: scan and rotation (find and fix)

License: MIT. Copyright (c) 2026 Saasment. Source: https://www.saasment.com/ai-security/skills/secrets-in-repo-scan-and-rotation

A secret that was ever committed must be treated as exposed, even if the commit was later deleted: it can live on in git history, forks, clones, CI caches, build logs, and package artifacts. Deleting the file does not fix the exposure. Rotation does. This skill finds exposed secrets, rotates them in the right order, and prevents repeats.

## When to use

- You are asked to audit a repository (or an organization's repositories) for committed credentials.
- A key or token was pasted into a commit, issue, log, or chat and you must respond.
- You want to add secret scanning to local development and CI.

## Safe by default

- Only scan repositories you own or are authorized to review.
- Never print full secret values into chat, tickets, or logs. Show only the type, file, line, commit, and the first and last 2 to 4 characters when needed to identify a key.
- Do not "test" a found credential by logging in to the service with it unless the owner has explicitly authorized that. Checking whether a key is active should be done through the provider's console or by the key owner.
- Do not commit found secrets into a report file. Store reports outside the repository.
- Do not rewrite shared history (force push) without agreement from the repository owners.

## Procedure

1. **Scope.** List the repositories, branches, and also non-code places that often leak secrets: CI configuration, Dockerfiles, `.env` files, notebooks, mobile app configs, and published artifacts such as npm packages or container images.
2. **Scan the working tree and full history.** Use a dedicated scanner (see `references/rotation-runbook.md` for commands): gitleaks, TruffleHog, or your platform's native secret scanning. Include all branches and tags. Add a manual search for high-signal patterns: `-----BEGIN .* PRIVATE KEY-----`, `AKIA[0-9A-Z]{16}`, `xox[abprs]-`, `ghp_`, `sk_live_`, `password\s*=`, connection strings with embedded passwords.
3. **Triage each hit.** Classify as: real and live, real but already revoked, test or example value, or false positive. Record owner, service, scope of the credential, and where it was exposed (public repo, private repo, shared logs).
4. **Rotate before you clean up.** For each real secret, in this order:
   1. Create a replacement credential with the narrowest scope needed.
   2. Deploy the replacement to every consumer (apps, CI, jobs) and confirm they work.
   3. Revoke the old credential at the provider.
   4. Review the provider's audit logs for use of the old credential during the exposure window. Escalate to incident response if there is unexpected use.
5. **Remove the secret from code.** Replace with environment variables or a secret manager reference. Commit the change with a message that does not contain the secret.
6. **Decide on history rewrite.** If the secret is revoked, a rewrite is usually optional. If you must purge it (for example, regulated data or a private key that cannot be rotated), coordinate a rewrite with a tool such as `git filter-repo`, then have all collaborators re-clone, and ask the host to clear cached views and forks where possible.
7. **Prevent recurrence.** Enable the host's push protection or secret scanning. Add a pre-commit hook and a CI job running the same scanner. Add `.env*` patterns to `.gitignore`. Use short-lived credentials (OIDC from CI to the cloud provider) where supported. Maintain an allowlist file for reviewed false positives.

## Verification

- Re-run the scanner on the full history and confirm that every remaining finding is documented as a false positive or revoked credential.
- Confirm each rotated credential: new one works in production, old one is rejected (check in the provider console, or ask the key owner to confirm).
- Confirm CI fails when a test secret pattern is committed to a scratch branch, then delete that branch.
- Confirm the application starts using only environment or secret-manager values in a clean environment.

## Reporting

Provide a table with: finding id, secret type, location, exposure scope, rotated (yes or no, date), old credential revoked (yes or no), provider logs reviewed (yes or no). Do not include secret values.

## References

- Companion skills: `iac-misconfig-review`, `dependency-cve-triage-and-patch`.
- GitHub documentation on removing sensitive data from a repository: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository
- CWE-798 (Use of Hard-coded Credentials): https://cwe.mitre.org/data/definitions/798.html

Helper files

LICENSE
MIT License

Copyright (c) 2026 Saasment

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
references/rotation-runbook.md
# Secret scanning commands and rotation runbook

License: MIT. Copyright (c) 2026 Saasment.

Check each tool's current documentation before use, because flags change between versions. These examples run locally and send nothing to third parties unless you enable verification features.

## Scan commands

```bash
# gitleaks: full history of the current repo, redacted output, report kept outside the repo
gitleaks git --redact --report-format json --report-path /tmp/gitleaks-report.json .

# gitleaks: scan only files in the working directory (no history)
gitleaks dir --redact .

# TruffleHog: git history of a local repo, only report verified-live findings
# (verification makes network calls to the credential provider; skip it if not authorized)
trufflehog git file://. --results=verified

# TruffleHog: do not call providers, list candidates only
trufflehog git file://. --no-verification
```

Manual high-signal search of the working tree:

```bash
git grep -nE -- '-----BEGIN [A-Z ]*PRIVATE KEY-----|AKIA[0-9A-Z]{16}|xox[abprs]-[0-9A-Za-z-]+|gh[pousr]_[0-9A-Za-z]{30,}|sk_live_[0-9A-Za-z]+'
```

Search history for a specific string you already know is exposed (without printing it again in shared logs):

```bash
git log --all --oneline -S'<fragment>' -- .
```

## Rotation order

1. Create new credential (narrow scope, expiry if the provider supports it).
2. Update secret store, CI variables, and running services.
3. Verify services healthy with the new credential.
4. Revoke the old credential.
5. Review provider audit logs for the exposure window.
6. Record the rotation date and owner.

## Provider quick notes

- Cloud access keys: create a second key, switch consumers, deactivate then delete the first key. Prefer role-based or OIDC access over long-lived keys.
- Database passwords: create a new user or password, update consumers, then drop or change the old one. Check for connection pools that hold old credentials.
- OAuth client secrets and webhooks: rotate the secret at the provider and update signature verification.
- SSH or signing private keys: generate a new key pair, replace the public key everywhere it is trusted, remove the old one. A private key that signed releases may require announcing a key change.
- Personal access tokens: revoke and re-issue with fine-grained, repository-scoped permissions.

## History rewrite (only when required and agreed)

```bash
# Example using git filter-repo with a file of replacements kept outside the repo
git filter-repo --replace-text /tmp/replacements.txt
```

After a rewrite, all collaborators must re-clone. Forks and cached copies may still contain the old data, so rotation remains mandatory.

## Prevention snippets

```bash
# .gitignore
.env
.env.*
!.env.example
```

```yaml
# Example CI job idea: run the scanner on every pull request and fail on findings
# Use your CI system's syntax and pin the scanner version.
```

Related

Companion skills

Questions

How do I install the Secrets in repo: scan and rotation skill?

Download https://www.saasment.com/ai-security/skills/secrets-in-repo-scan-and-rotation/download.zip and unzip it into your agent's skills directory (for example .cursor/skills, .claude/skills, .agents/skills, or .github/skills) so that secrets-in-repo-scan-and-rotation/SKILL.md exists. Or connect to https://www.saasment.com/api/mcp and call the get_skill tool with the slug secrets-in-repo-scan-and-rotation.

What license is the Secrets in repo: scan and rotation skill under?

It is original Saasment content under the MIT license, stated in the SKILL.md frontmatter and in the LICENSE file in the skill folder.

Is it safe to run this skill?

A skill is a set of instructions your agent follows, so read SKILL.md first. It tells the agent to test only systems you own or are authorized to test, to use test environments and synthetic data, and to avoid destructive probes. Downloading it does not run any code on your machine.

← All security skills

Try the platform.

Open the demo workspace with mock data. No login. Register from the banner when you want early access. No security team. No call.

Try the platform