Overview
This vulnerability involves embedding cryptographic keys (used for encryption, decryption, signing, etc.) directly within the application’s source code or configuration files. Similar to hard-coded passwords (CWE-259), these keys often end up in version control systems, exposing them to anyone with repository access. 🔑💻
Business Impact
Hard-coded keys completely undermine the security provided by cryptography. If an attacker obtains the key, they can decrypt sensitive data, forge signatures, or bypass authentication mechanisms that rely on that key. This can lead to data breaches, unauthorized access, and loss of data integrity.Reference Details
CWE ID: CWE-321
OWASP Top 10 (2021): A02:2021 - Cryptographic Failures
Severity: High
Framework-Specific Analysis and Remediation
Like hard-coded passwords, this is a framework-agnostic developer practice issue. The solution is identical: externalize the keys.- Remove the hard-coded key from code/config.
- Store the key securely using environment variables, secrets management services (Vault, AWS Secrets Manager, Azure Key Vault), or hardware security modules (HSMs).
- Load the key into the application at runtime.
- Python
- Java
- .NET(C#)
- PHP
- Node.js
- Ruby
Framework Context
Hard-coding encryption keys (e.g., for Fernet, PyCryptodome) insettings.py or application logic.Vulnerable Scenario 1: Hard-coded Fernet Key
Vulnerable Scenario 2: JWT Secret Key in Settings
Mitigation and Best Practices
Load keys from environment variables (os.environ.get()) or a secrets manager. Ensure keys have sufficient entropy and are rotated periodically.
