API keys are the weakest credential type that still guards real data: long lived, rarely rotated, often over-scoped, and frequently pasted into places they should never reach. Managing them is a lifecycle problem, not a generation problem. This skill covers the full lifecycle on systems you are authorized to assess: issuance with least privilege, rotation that actually retires old keys, storage that survives a breach, revocation that takes effect immediately, and detection when a key leaks.
Quick Workflow
- Inventory keys: where they are issued, who holds them, and what each one can do, from the admin panel and the docs.
- Test issuance: can a low privilege user create a key, and can they choose its scope or expiry.
- Test scoping: use a read-only key for a write, and a key scoped to one tenant for another tenant's data.
- Test rotation: rotate a key and confirm the old value stops working, immediately, on every route.
- Test revocation: revoke a key and confirm it fails at the gateway, not just in the admin UI listing.
- Test storage: check for keys in client code, repos, logs, error messages, and mobile app bundles you are authorized to review.
- Test leak detection: plant a canary key where a leak would be caught, or review whether the provider scans for exposed keys.
- Report per key class, with scope, expiry, rotation, and revocation verdicts side by side.
1. Issuance and scoping tests
# Issuance: what does a fresh key look like, and what can it do
curl -s "https://target.example/api/v1/keys" \
-H 'Authorization: Bearer user-token-value' \
-H 'Content-Type: application/json' \
-d '{"name": "audit-key", "scopes": ["read"]}' | jq .
echo "record whether expiry is set by default, whether scopes are"
echo "free text the caller controls, and whether the full secret is"
echo "shown more than once after creation (it must not be)"
# Scoping: a read-only key must fail writes, and a tenant key must
# fail on another tenant, at the gateway and at the service
echo "== read key attempting a write"
curl -s -o /dev/null -w '%{http_code}\n' \
-H 'X-API-Key: read-only-key-value' \
-H 'Content-Type: application/json' \
-d '{"name": "should-fail"}' \
"https://target.example/api/v1/users"
echo "== tenant A key reading tenant B data"
curl -s -o /dev/null -w '%{http_code}\n' \
-H 'X-API-Key: tenant-a-key-value' \
"https://target.example/api/v1/tenants/B/users"
echo "want 403 for both; a 200 on either is a scoping failure"
2. Rotation and revocation
#!/usr/bin/env python3
"""Rotation proof: rotate a test key, then prove the old value dies.
Uses only a test key created for this audit. A rotation that leaves
the old key working is not rotation, it is duplication.
"""
import subprocess
BASE = "https://target.example/api/v1"
OLD = "test-key-old-value"
NEW = "test-key-new-value"
def status(key):
out = subprocess.run(
["curl", "-s", "-o", "/dev/null", "-w", "%{http_code}",
"-H", f"X-API-Key: {key}", f"{BASE}/users/me"],
capture_output=True, text=True).stdout.strip()
return out
print("old key before rotation:", status(OLD))
print("rotate the key in the admin panel, then re-run this script")
print("old key after rotation: want 401, got", status(OLD))
print("new key after rotation: want 200, got", status(NEW))
print("if the old key still returns 200, rotation is broken or")
print("cached; note the delay if it dies only after some minutes")
# Revocation: revoke, then test immediately and again after a delay
curl -s -X DELETE "https://target.example/api/v1/keys/test-key-id" \
-H 'Authorization: Bearer admin-token-value'
echo "== revoked key, immediate test"
curl -s -o /dev/null -w '%{http_code}\n' \
-H 'X-API-Key: revoked-key-value' \
"https://target.example/api/v1/users/me"
sleep 300
echo "== revoked key, test after 5 minutes (cache window check)"
curl -s -o /dev/null -w '%{http_code}\n' \
-H 'X-API-Key: revoked-key-value' \
"https://target.example/api/v1/users/me"
echo "a key that works after revocation, even briefly, means the"
echo "gateway caches auth decisions; report the window in seconds"
3. Storage and leak detection
# Storage audit on material you are authorized to review: repos,
# client bundles, and CI logs for key-shaped strings
grep -RInE '(sk-[A-Za-z0-9_-]{16,}|api[_-]?key["'"'"' ]*[:=][^ ]{16,})' \
--exclude-dir=.git ./client-code/ 2>/dev/null | head -20
echo "every hit needs triage: live key, test key, or example value"
echo "from documentation. Only live keys are findings, but example"
echo "values that look real still train developers to copy them"
#!/usr/bin/env python3
"""Leak canary: a distinctive test key whose use is alarmed.
Create the canary through the normal issuance flow, give it a
narrow read-only scope, then deliberately place it only where a
leak detector should find it. Any use of the canary in production
traffic proves the detector missed it or the alert went nowhere.
"""
canary = {
"name": "canary-do-not-use",
"scopes": ["read"],
"expires_in_days": 7,
"alert_on_use": True,
}
print("canary key record:", canary)
print("steps: issue, place in the monitored location, wait, then")
print("check the alert channel. No alert means detection is absent")
print("or the alert route is broken; both are findings.")
# Log hygiene: keys must never appear in logs or error responses
curl -s "https://target.example/api/v1/users/me" \
-H 'X-API-Key: wrong-key-value' | jq .
echo "check the response and the gateway log line for this request:"
echo "the rejected key value itself must never be echoed back"
Edge cases
- Keys are per environment but share a prefix. Then a staging key that works in production is a finding about environment separation, not just about the key.
- Rotation is scheduled but old keys have a grace period. A grace period is legitimate only if it is documented, short, and enforced; an undocumented grace period is a failed rotation.
- Keys are stored hashed server side but shown in full at creation and in the admin UI afterwards. Re-display after creation defeats hashing; only the creation response may show the secret.
- Service-to-service keys cannot rotate without downtime. That is a design constraint to solve with dual-key support, not a reason to keep one eternal key.
- A leaked key is revoked but the data it exposed is not reviewed. Revocation stops future use; the incident review must cover what the key already accessed.
Key references
- OWASP API Security Top 10, broken authentication and security misconfiguration sections.
- OWASP guidance on secrets management and on credential rotation.
- Related skills: api-gateway-security, api-security-testing, openapi-security-review.
