handle-secrets-safely
Decide how a credential is stored, rotated and revoked, starting from what kind of secret it is, because encrypting a password is the recurring mistake.
Save as .claude/skills/handle-secrets-safely/SKILL.md in a repository, or under ~/.claude/skills/handle-secrets-safely/SKILL.md to have it everywhere. The copy button takes the whole file, frontmatter included.
--- name: handle-secrets-safely description: Decide how a credential should be stored, passed, rotated and removed, and what to do when one leaks. Use when adding an API key or password, reviewing code that touches credentials, choosing a hashing or encryption approach, or responding to a secret found in source control. --- # Handle secrets safely Most secret handling failures are not cryptographic. They are a credential in the wrong place, living longer than it should, with no way to rotate it. Get the boring parts right before reaching for an algorithm. ## First: what kind of secret is this The right handling follows entirely from the answer, and conflating these is the most common mistake. | Kind | Example | Correct handling | |---|---|---| | Something you verify | user password | **Hash** with a password hash. Never decrypt. | | Something you must read back | third-party API key | **Encrypt**, or store in a secret manager | | Something you prove authorship with | webhook signature | **HMAC** with a shared secret | | Something that proves identity for a while | session token | Random, opaque, revocable, expiring | The error that recurs: encrypting passwords. If you can decrypt it, so can whoever takes the database. Passwords are hashed, one way, always. ## Passwords Use a memory-hard password hash. In rough order of preference: **Argon2id**, **scrypt**, **bcrypt**. All three are acceptable; bcrypt's 72-byte input limit is worth knowing if you pre-hash. Never use a general-purpose hash. SHA-256, SHA-512 and MD5 are designed to be fast, which is exactly the wrong property. A single GPU tries billions of SHA-256 candidates per second. - Salt per password, stored alongside. Every modern library does this inside the hash string; do not invent your own. - Tune the cost parameter to your hardware, aiming for roughly 100 to 250 milliseconds per hash, and revisit it. A cost chosen in 2018 is too low now. - Compare with the library's verify function, which is constant time. - On successful login, re-hash if the stored parameters are below current settings. - Check new passwords against a breach corpus. A long password that has already leaked is worthless, and this catches more real risk than any composition rule. Length and a breach check beat character class requirements. ## API keys and credentials you must read back These need to be recoverable, so they are encrypted or held in a secret manager, not hashed. Order of preference: 1. A secret manager or the platform's encrypted environment variables. The application receives the value at runtime and never stores it. 2. Encrypted at rest with a key held somewhere the database backup does not reach. Use an authenticated mode, AES-GCM or ChaCha20-Poly1305, never AES-CBC without a MAC. 3. Plaintext in the database. Only with a written reason. Never: in source control, in a container image, in a build log, in an error message, in a URL query string, in a client-side bundle, or in an analytics event. URLs in particular end up in server logs, referrer headers and browser history. ## Secrets you generate - Use a cryptographically secure random source: `crypto.randomBytes`, `secrets.token_urlsafe`, `crypto/rand`. Never `Math.random`, never a timestamp, never a UUID v1, never a sequential ID. - 128 bits of entropy is the floor for anything long-lived; 256 for anything that cannot be rotated easily. - Make them identifiable. A prefix such as `sk_live_` lets scanners spot a leak and lets you tell environments apart at a glance. - Store only a hash of the key where you can. Show the full value once at creation, verify by hash afterwards, like a password. This makes a database leak much less severe. ## Rotation, which is the part people skip A secret with no rotation path is a future incident with a long outage. Before shipping, answer three questions: - How is it rotated, specifically, as a sequence of steps. - Can old and new be valid at once. If not, rotation means downtime, and the design needs to support two active values. - How long does a leaked one stay useful. Short-lived credentials are worth more than strong ones. Prefer short-lived tokens issued against a long-lived credential held in one place, over long-lived credentials distributed widely. ## When one leaks Order matters and people get it backwards. 1. **Revoke first.** Before investigating, before writing anything up. A revoked key is a non-event; an investigated key is still live. 2. Rotate everywhere it was used. 3. Then work out exposure: what it could reach, for how long, and whether logs show use you cannot account for. 4. Remove it from the repository history if it was committed — and note that **rewriting history does not undo the exposure.** Anyone could have cloned, and forks, caches and mirrors persist. The rewrite is housekeeping. The revocation was the fix. 5. Write down how it got there, because the cause is usually a missing `.gitignore` entry or a convenience that will recur. ## Reviewing code that touches secrets Look for, in order of severity: - A credential literal in source, a test fixture, or a config file that is committed. - A secret logged, including inside an object that is logged whole, and inside an exception that includes request context. - A secret in a URL rather than a header or body. - `==` comparing a token or signature. Use a constant-time compare. - A general-purpose hash used for a password. - An encryption mode without authentication. - A secret read at import time, which makes rotation require a restart and makes the value harder to scope. - A `.env` file not in `.gitignore`. ## Reporting Name the specific credential, where it is exposed, and what an attacker reaches with it. Rank by what the secret unlocks, not by how it was stored. Say plainly when you have not verified whether a key is live, and never include the value itself in the report.
what it needs
Nothing. This skill is instructions only, so it works in any agent that reads SKILL.md with nothing installed and no account. Save the file and it is live in the next session.
the instructions, in full
Handle secrets safely
Most secret handling failures are not cryptographic. They are a credential in the wrong place, living longer than it should, with no way to rotate it. Get the boring parts right before reaching for an algorithm.
First: what kind of secret is this
The right handling follows entirely from the answer, and conflating these is the most common mistake.
| Kind | Example | Correct handling |
|---|---|---|
| Something you verify | user password | Hash with a password hash. Never decrypt. |
| Something you must read back | third-party API key | Encrypt, or store in a secret manager |
| Something you prove authorship with | webhook signature | HMAC with a shared secret |
| Something that proves identity for a while | session token | Random, opaque, revocable, expiring |
The error that recurs: encrypting passwords. If you can decrypt it, so can whoever takes the database. Passwords are hashed, one way, always.
Passwords
Use a memory-hard password hash. In rough order of preference: Argon2id, scrypt, bcrypt. All three are acceptable; bcrypt's 72-byte input limit is worth knowing if you pre-hash.
Never use a general-purpose hash. SHA-256, SHA-512 and MD5 are designed to be fast, which is exactly the wrong property. A single GPU tries billions of SHA-256 candidates per second.
- Salt per password, stored alongside. Every modern library does this inside the hash string; do not invent your own.
- Tune the cost parameter to your hardware, aiming for roughly 100 to 250 milliseconds per hash, and revisit it. A cost chosen in 2018 is too low now.
- Compare with the library's verify function, which is constant time.
- On successful login, re-hash if the stored parameters are below current settings.
- Check new passwords against a breach corpus. A long password that has already leaked is worthless, and this catches more real risk than any composition rule. Length and a breach check beat character class requirements.
API keys and credentials you must read back
These need to be recoverable, so they are encrypted or held in a secret manager, not hashed.
Order of preference:
- A secret manager or the platform's encrypted environment variables. The application receives the value at runtime and never stores it.
- Encrypted at rest with a key held somewhere the database backup does not reach. Use an authenticated mode, AES-GCM or ChaCha20-Poly1305, never AES-CBC without a MAC.
- Plaintext in the database. Only with a written reason.
Never: in source control, in a container image, in a build log, in an error message, in a URL query string, in a client-side bundle, or in an analytics event. URLs in particular end up in server logs, referrer headers and browser history.
Secrets you generate
- Use a cryptographically secure random source:
crypto.randomBytes,secrets.token_urlsafe,crypto/rand. NeverMath.random, never a timestamp, never a UUID v1, never a sequential ID. - 128 bits of entropy is the floor for anything long-lived; 256 for anything that cannot be rotated easily.
- Make them identifiable. A prefix such as
sk_live_lets scanners spot a leak and lets you tell environments apart at a glance. - Store only a hash of the key where you can. Show the full value once at creation, verify by hash afterwards, like a password. This makes a database leak much less severe.
Rotation, which is the part people skip
A secret with no rotation path is a future incident with a long outage. Before shipping, answer three questions:
- How is it rotated, specifically, as a sequence of steps.
- Can old and new be valid at once. If not, rotation means downtime, and the design needs to support two active values.
- How long does a leaked one stay useful. Short-lived credentials are worth more than strong ones.
Prefer short-lived tokens issued against a long-lived credential held in one place, over long-lived credentials distributed widely.
When one leaks
Order matters and people get it backwards.
- Revoke first. Before investigating, before writing anything up. A revoked key is a non-event; an investigated key is still live.
- Rotate everywhere it was used.
- Then work out exposure: what it could reach, for how long, and whether logs show use you cannot account for.
- Remove it from the repository history if it was committed — and note that rewriting history does not undo the exposure. Anyone could have cloned, and forks, caches and mirrors persist. The rewrite is housekeeping. The revocation was the fix.
- Write down how it got there, because the cause is usually a missing
.gitignoreentry or a convenience that will recur.
Reviewing code that touches secrets
Look for, in order of severity:
- A credential literal in source, a test fixture, or a config file that is committed.
- A secret logged, including inside an object that is logged whole, and inside an exception that includes request context.
- A secret in a URL rather than a header or body.
==comparing a token or signature. Use a constant-time compare.- A general-purpose hash used for a password.
- An encryption mode without authentication.
- A secret read at import time, which makes rotation require a restart and makes the value harder to scope.
- A
.envfile not in.gitignore.
Reporting
Name the specific credential, where it is exposed, and what an attacker reaches with it. Rank by what the secret unlocks, not by how it was stored. Say plainly when you have not verified whether a key is live, and never include the value itself in the report.
check its answers by hand
A skill that reasons well about wrong data is still wrong. For the first few cases, run the same question through a tool that cannot be mistaken.
Check how strong a password really is: entropy, character variety, and estimated time to crack.
Open the toolGenerate SHA-1, SHA-256, SHA-384, and SHA-512 hashes of any text, live as you type.
Open the toolCompute HMAC-SHA256, SHA-1, or SHA-512 signatures from a message and secret key.
Open the toolEncrypt text with a password using AES-256-GCM, and decrypt it back. Real encryption, fully in your browser.
Open the tool