Skip to main content

Hard-Coded Secrets

About Hard-Coded Secrets

What are Hard-coded Secrets?

Hard-coded secrets refer to the practice of embedding sensitive information, such as passwords, encryption keys, or API keys, directly in the source code of an application.

This practice can make it easy for attackers to identify and exploit these secrets, leading to security vulnerabilities and other types of security threats.

Hard-coded secrets are a security risk because they are often stored in plain text, making it easy for attackers to extract them from the source code. They can also be inadvertently disclosed or exposed through other security vulnerabilities, such as code injection or data leaks.

Hard-coded secrets can affect various types of applications, such as web applications, mobile applications, and desktop applications. They can also be found in infrastructure components, such as scripts, configuration files, and server software.

At GuardRails, we differentiate between different kinds of secrets and cover the below CWEs.

What is the impact of Hard-Coded Secrets?

Hard-coded secrets can lead to various security threats and risks, such as:

  • Information disclosure: Hard-coded secrets can expose sensitive information, such as passwords, keys, or other types of confidential information, to unauthorized parties.
  • Unauthorized access: Hard-coded secrets can allow attackers to gain unauthorized access to applications, systems, or networks, perform unauthorized actions, or modify data.
  • Data modification: Hard-coded secrets can allow attackers to modify data or system configurations, leading to data loss or corruption, or other types of system instability.

This has led to many high-profile breaches.

How to prevent Hard-Coded Secrets?

To prevent hard-coded secrets, implement appropriate security measures, such as:

  • Use environment variables: Use environment variables or configuration files to store sensitive information, such as passwords or keys, instead of hard-coding them in the source code.
  • Use secure storage: Store sensitive information in secure storage, such as a secure key vault, that provides additional security features, such as access controls and encryption.
  • Use encryption: Use encryption to protect sensitive information in transit and at rest, such as using TLS for network traffic and encrypting storage media.
  • Apply access controls: Apply access controls to limit the privileges and actions that users or applications can perform on sensitive information, such as using role-based access controls or fine-grained access controls.
  • Use secure coding practices: Follow secure coding practices, such as code reviews, vulnerability scanning and testing, and threat modeling, to ensure that the source code is free of vulnerabilities and that sensitive information is properly protected.

A list of popular secure key vault software can be found below:

  1. HashiCorp Vault
  2. AWS Secrets Manager
  3. GCP Cloud KMS
  4. Microsoft Azure Key Vault
  5. Confidant
  6. Keywhiz
  7. Knox

References

Taxonomies

Explanation & Prevention

Training

Fixing Hard-coded Secrets

Use Environment Variables

  1. Go through the issues that GuardRails identified in the PR/MR.

  2. If there are false positives, you can mark them accordingly in the GuardRails dashboard or manage which files should be ignored.

  3. Install BFG filter.

  4. Remove the identified secrets from version control.

    # Warning: This is a destructive action.
    # To remove files containing sensitive data, run:
    bfg --delete-files YOUR-FILE-WITH-SENSITIVE-DATA
    # To replace all text listed in `passwords.txt` everywhere in your repository's history, run:
    bfg --replace-text passwords.txt
    # Warning, make sure that this is not a common word, that would replace other things.
  5. Rotate/change the identified passwords, API keys, or cryptographic keys.

  6. Replace the hard-coded secrets with an according environment variable placeholder. See below for references on how this can be done for common CI systems and use cases.

  7. Test it and ensure that the app is still working as expected.

  8. Ship it 🚢