Vulnerabilities

What is Hardcoded Secrets?

Hardcoded secrets are credentials, API keys, or cryptographic keys embedded directly in source code — creating persistent exposure risk whenever code is accessed, shared, or committed to version control.

By the Hyrax team·5 min read·May 1, 2026
TL;DR
  1. 1.Definition
  2. 2.Why Secrets End Up in Code
  3. 3.The Version Control Problem
  4. 4.Impact
  5. 5.Prevention

Definition

Hardcoded secrets are sensitive credentials — API keys, passwords, database connection strings, cryptographic keys, tokens, certificates — embedded directly in source code rather than loaded from a secure external source. When secrets are in code, every person who can read the code can access the secret, and every copy of the code (in version control history, backups, forks, logs, or leaks) exposes the secret indefinitely.

Hardcoded secrets appear under CWE-798 (Use of Hard-coded Credentials) and CWE-259 (Use of Hard-coded Password), both consistently present in the CWE/SANS Top 25. They are among the most commonly found vulnerability classes in real-world security assessments and red team operations.

Why Secrets End Up in Code

Hardcoded secrets are almost always unintentional. Common scenarios:

  • A developer adds a credential for testing and forgets to remove it
  • A configuration file with secrets is committed alongside application code
  • A credential is added "temporarily" during debugging and never rotated
  • A script or tool includes a credentials constant for convenience
  • An .env file is committed to a repository that was mistakenly initialized as public

The Version Control Problem

Once a secret is committed to a git repository, removing it from the current branch does not eliminate the risk. The secret remains in the commit history and is accessible to anyone with read access to the repository. GitHub, GitLab, and Bitbucket repositories leak secrets this way regularly — even repositories that are later made private or have the secret "removed" in a subsequent commit.

Secrets in version control history require: secret rotation (the compromised credential must be replaced), and history rewriting (git filter-branch or BFG Repo Cleaner to remove the commit from history) — after which all team members must re-clone.

Impact

The impact of a hardcoded secret depends on what it grants access to:

  • Database credentials — direct access to all application data
  • Cloud provider API keys — administrative access to cloud infrastructure
  • Third-party service keys — access to payment processors, communication APIs, data providers
  • Internal service tokens — access to internal APIs and services
  • Cryptographic keys — ability to sign tokens, decrypt data, impersonate services

Prevention

Use environment variables

The baseline pattern: secrets live in environment variables or secret management systems, never in code. The application reads process.env.DATABASE_URL, os.environ["API_KEY"], or equivalent. Code contains the key name, not the value.

Use secret management systems

For production systems, use a dedicated secret manager: AWS Secrets Manager, GCP Secret Manager, HashiCorp Vault, Azure Key Vault. Secrets are stored encrypted, access is audited, rotation is automated.

Secret scanning in CI/CD

Tools like git-secrets, truffleHog, Gitleaks, and GitHub Secret Scanning detect secrets in commits before they are pushed (pre-commit hooks) or after (CI checks). Fail the build on detected secrets.

Never commit .env files

Add .env, .env.*, *.local to .gitignore. Use .env.example with placeholder values to communicate required configuration without committing actual secrets.

OptionSecurityAuditabilityRotation Support
Hardcoded in sourceNone — exposed to all readersNoneManual, requires code change
Environment variablesModerate — process-level isolationLimitedManual, requires restart
Secret manager (Vault, AWS SM)High — encrypted, access-controlledFull audit logAutomated rotation supported
Sealed secrets (Kubernetes)High — encrypted at rest in repoGit history + K8s auditRequires re-sealing

Hardcoded Secrets and Autonomous Code Governance

Secret detection is a core capability of Hydra. The system applies entropy analysis, pattern matching (API key formats, connection string patterns, token formats for major services), and structural analysis to identify hardcoded secrets across the entire codebase and git history. When a secret is found, Hydra generates a fix that: replaces the hardcoded value with an environment variable read, adds the variable name to the .env.example with a placeholder, and flags the credential for rotation. Because the secret is already compromised by its presence in code, Hydra surfaces rotation as a required human action alongside the code fix.

Frequently Asked Questions

If I delete a secret from my code and push, is it safe?

No. The secret remains in git commit history. Anyone with access to the repository can check out the previous commit and read the secret. You must rotate the credential (invalidate the old one, generate a new one) and if necessary rewrite git history to remove the commit.

What counts as a hardcoded secret?

Any credential with a value embedded in code: API keys, database passwords, OAuth client secrets, JWT signing keys, encryption keys, webhook secrets, SMTP credentials, private certificates, SSH private keys, and cloud provider access keys. Even "low-value" credentials like internal API keys can be the first step in a privilege escalation chain.

Are secrets in Docker environment variables safe?

Better than in code, but not ideal. Docker environment variables are visible in docker inspect, in container logs, and to other processes on the host. For production deployments, use Docker secrets (for Swarm), Kubernetes secrets (ideally sealed or backed by a secret manager), or a runtime secret injection mechanism rather than environment variables in Docker commands.

Stop flagging. Start fixing.

Hyrax reviews your pull requests, remediates issues autonomously, and closes the ticket.

Join the waitlist