← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Key Management & Rotation

Security CWE-320 OWASP A2:2021 Advanced
debt(d7/e7/b7/t7)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7), slightly better than d9 because tools like trufflehog, semgrep, and vault exist and can catch some patterns (keys hardcoded in source, keys in .env committed to VCS), but many misuses — like using the same key for all purposes, never rotating, or storing keys in the DB — require architectural review or runtime audit rather than automated scanning. The detection coverage is partial and inconsistent, so d7 is appropriate.

e7 Effort Remediation debt — work required to fix once spotted

Closest to 'cross-cutting refactor across the codebase' (e7). The quick_fix mentions migrating to AWS KMS or HashiCorp Vault, but getting there from a state where keys are hardcoded in source, stored in the DB, or never rotated requires updating every encryption call site, provisioning and integrating a secrets manager, migrating existing encrypted data, and establishing rotation policies. This spans infrastructure, application code, and deployment configuration — clearly cross-cutting.

b7 Burden Structural debt — long-term weight of choosing wrong

Closest to 'strong gravitational pull' (e7). Key management applies broadly across web and CLI contexts per applies_to, and is tagged as cryptography + infrastructure. Every feature that touches encrypted data, secrets, or authenticated tokens must work within whatever key management scheme is chosen. A poor choice (e.g., DB-stored keys, no rotation) shapes every encryption decision going forward and demands constant awareness from all maintainers.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap (contradicts how a similar concept works elsewhere)' (t7). The canonical misconception is that environment variables are fully secure — this is a well-documented but widely believed falsehood. Developers familiar with 12-factor app patterns naturally reach for env vars as 'the secure place,' but as the misconception field states, env vars are accessible to all processes, sometimes logged, and exposed via /proc on Linux. This directly contradicts common developer intuition shaped by widely taught best practices.

About DEBT scoring →

Also Known As

secret management key rotation cryptographic key management

TL;DR

The policies and practices for generating, storing, distributing, rotating, and retiring cryptographic keys securely.

Explanation

Poor key management is responsible for many real-world cryptographic failures despite using strong algorithms. Key management principles include: generate keys using a CSPRNG, never hardcode keys in source code, store keys in a dedicated secrets manager (HashiCorp Vault, AWS KMS, Azure Key Vault), enforce least-privilege access to keys, rotate keys on a regular schedule and immediately after suspected compromise, and maintain key versioning so old data can still be decrypted during rotation. In PHP, use environment variables or a secrets SDK — never php.ini or config files committed to git.

Common Misconception

Storing keys in environment variables is fully secure. Env vars are accessible to all processes, sometimes logged by frameworks, and exposed in /proc on Linux. A dedicated secrets manager provides proper access control and audit logging.

Why It Matters

The strongest encryption is useless if the key is stored alongside the ciphertext or is never rotated — key management is where most cryptographic systems fail in practice.

Common Mistakes

  • Storing encryption keys in the database alongside the data they protect — breach exposes both.
  • Hardcoding encryption keys in source code — committed to version control, permanent exposure.
  • Never rotating keys — a compromised key remains usable indefinitely.
  • Using a single key for all purposes — one compromised key breaks all encrypted data.

Code Examples

✗ Vulnerable
// Key stored next to ciphertext in the same DB table:
$row = $db->query('SELECT data, encryption_key FROM secrets WHERE id = 1');
$plaintext = openssl_decrypt($row['data'], 'AES-256-CBC', $row['encryption_key'], ...);
✓ Fixed
// Use a KMS or secrets manager — not plaintext files

// AWS Secrets Manager — fetch at startup, cache in memory
function getSecret(string $name): string {
    static $cache = [];
    return $cache[$name] ??= fetchFromAws($name); // cached per process
}

$dbPassword = getSecret('prod/db/password');

// Laravel — APP_KEY drives all encryption
$encrypted = Crypt::encryptString($sensitive); // uses APP_KEY

// Key rotation:
$ php artisan key:generate  // new APP_KEY
// Re-encrypt existing data with old key before switching

// Hierarchy: master key (in KMS) → data encryption keys → encrypted data
// Never hardcode keys. Rotate on any suspected exposure.

Added 15 Mar 2026
Edited 22 Mar 2026
Views 63
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 1 ping T 1 ping F 1 ping S 0 pings S 1 ping M 0 pings T 0 pings W 2 pings T 1 ping F 1 ping S 3 pings S 1 ping M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 4 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Amazonbot 7 ChatGPT 7 Scrapy 7 Perplexity 6 SEMrush 5 Google 4 Ahrefs 4 Unknown AI 3 Bing 3 Claude 2 Majestic 1 Meta AI 1 Sogou 1
crawler 45 crawler_json 5 pre-tracking 1
DEV INTEL Tools & Severity
🔴 Critical ⚙ Fix effort: High
⚡ Quick Fix
Store encryption keys in AWS KMS, HashiCorp Vault, or at minimum environment variables — never in the database next to the data they encrypt, and never in source code
📦 Applies To
any web cli
🔗 Prerequisites
🔍 Detection Hints
Encryption key stored in same DB as encrypted data; APP_KEY in committed .env; key derivation from predictable seed; no key rotation mechanism
Auto-detectable: ✓ Yes trufflehog semgrep vault
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✗ Manual fix Fix: High Context: File
CWE-321 CWE-320


✓ schema.org compliant