Hardcoded Secrets
Learn how to prevent hardcoded secrets in your code with real examples and best practices. Protect your applications from exposing sensitive credentials and API keys.
Tools recognizing this:
Opengrep Fortify Checkmarx SonarQube Snyk Semgrep CodeQL
What are Hardcoded Secrets and How Do They Impact Security?
Hardcoded secrets are sensitive credentials, API keys, or passwords that are directly embedded in source code. This is a significant security risk as it can expose sensitive information to unauthorized users, especially when code is shared or made public.
The risks of hardcoded secrets include:
Exposure of sensitive credentials in version control systems
Difficulty in rotating or changing credentials
Potential unauthorized access to systems and services
Compliance violations and security audit failures
This guide covers hardcoded secrets, examples, prevention methods, and how to properly manage sensitive credentials in your applications.
One Simple Hardcoded Secret Example
Consider this common example of hardcoded credentials:
const dbConnection = mysql.createConnection({
host: 'localhost',
user: 'admin',
password: 'secretP@ssw0rd123!'
});This exposes the database credentials directly in the code, making them:
Visible to anyone with code access
The credentials should instead be stored in:
Environment variables Secret management systems Configuration files (outside version control)
Hardcoded Secrets Prevention Methods: How to Fix Your Code
The most effective way to fix hardcoded secrets is to externalize them from the code using environment variables or secure secret management systems.
Environment variables and secret managers provide a secure way to store and access sensitive credentials while keeping them separate from the application code.
Code Samples
Vulnerable Code
Fixed Code
Fix Explanation
The vulnerable code contains hardcoded API credentials directly in the source code. The fix uses environment variables to retrieve sensitive credentials at runtime. Added null checks to ensure credentials are properly configured. Credentials can now be easily rotated without code changes.
Vulnerable Code
Fixed Code
Fix Explanation
The vulnerable code exposes API credentials directly in the source code. The fix uses dotenv to load environment variables. Credentials are accessed through process.env. Added validation to ensure required credentials are present.
Vulnerable Code
Fixed Code
Fix Explanation
The vulnerable code contains hardcoded database credentials. The fix uses environment variables loaded through python-dotenv. Added validation to ensure all required credentials are available. Credentials can be managed separately from the code.
Vulnerable Code
Fixed Code
Fix Explanation
The vulnerable code contains hardcoded payment API credentials. The fix uses ASP.NET Core's configuration system. Credentials are injected through dependency injection. Added null checks to ensure credentials are configured.
Vulnerable Code
Fixed Code
Fix Explanation
The vulnerable code contains hardcoded AWS credentials. The fix uses environment variables to store credentials. Added validation to ensure credentials are available. Follows AWS security best practices for credential management.
Vulnerable Code
Fixed Code
Fix Explanation
The vulnerable code contains hardcoded API credentials. The fix uses environment variables through std::getenv. Added validation to ensure credentials are available. Credentials can be managed externally from the application.
Need more help in preventing Hardcoded Secrets?
Mobb supports fixing many forms of Hardcoded Secrets vulnerabilities, and can mitigate your issues in batch.
Start now for free at app.mobb.ai
We'd love your feedback!
We're excited to hear your thoughts and ideas about fixing vulnerabilities.
Book a meeting or Contact us if you have any corrections, questions or suggestions. Start now for free at https://app.mobb.ai
Last updated
Was this helpful?