CWE-798: Use of Hard-coded Credentials
A secret, an API key, password, token, or connection string, is written directly into source code instead of being supplied at runtime, so anyone who can read the code can use it.
Also known as: Hardcoded secrets, Embedded credentials, Secrets in source, Inline API keys
Risk
Typically high to critical. A single leaked credential can grant direct, authenticated access to production systems, cloud accounts, or payment infrastructure, with no exploit chain required.
Definition
What it is
CWE-798 is the use of a credential that is embedded in the application rather than provided from a secure external source at runtime. The secret may be an API key, a database password, a signing key, an OAuth client secret, a cloud access key, or a service token. Because it lives in the source, it is visible to everyone with repository access, it is copied into every clone and fork, and it is permanently recorded in version-control history even after it is deleted. Rotating it means changing code and redeploying, so in practice it rarely gets rotated at all.
Use-cases
How it shows up in real code
A payment key shipped to the browser
A developer wires up Stripe and pastes the secret key directly into a client-side file so the checkout 'just works' in testing. The bundle ships to production; anyone who opens dev tools can read the key and issue charges or refunds against the account.
A cloud access key in a committed config
An AWS access key and secret are hard-coded into a settings file to get a deploy script running. The repository is later made public, or a contractor is offboarded but keeps a clone. The key still works, granting access to the whole cloud account.
A database password in the connection string
The production database URL, password included, is written inline in the data-access layer. It is now in every branch, every CI log that prints the config, and every developer laptop that cloned the repo.
A secret deleted from code but alive in git history
A key is spotted and removed in a later commit, so the current file looks clean. But the original commit still contains it; a one-line `git log -p` recovers it. The credential must be rotated, not just deleted.
AI risk
Why AI-generated code has increased the exposure
- AI code assistants optimize for a snippet that runs immediately, not for secure configuration. Asked to 'connect to Stripe' or 'call the OpenAI API', they frequently emit the key inline (`const apiKey = "sk-..."`) because that is the shortest path to working code, rather than reading from an environment variable or a secrets manager.
- Models were trained on millions of public repositories, a large share of which contain hard-coded secrets. The pattern is well represented in the training data, so assistants reproduce it by default unless explicitly steered away.
- Autocomplete can suggest a real-looking, correctly formatted credential in the exact position where one belongs, making it one keystroke to accept a secret straight into the file.
- AI-scaffolded projects often omit the surrounding hygiene, no `.env`, no `.gitignore` entry for it, no secrets-manager wiring, so even when a key is meant to be external, there is nothing stopping it from being committed.
- The volume of generated code raises the exposure linearly: more code produced per hour means more places a secret can land, and less human attention per line to catch it.
Vibe coding
How vibe coding amplifies the risk
- Vibe coding, building an app by prompting and accepting the generated result without reading the diff, is the single biggest amplifier of CWE-798. The person asks for a feature, the model writes the key into the file, and because nobody reads the change line by line, the secret ships.
- The speed that makes vibe coding attractive is exactly what removes the review step where a hard-coded secret would normally be caught. Code review, linting, and a security pass are the usual backstops; prompt-and-ship skips all three.
- Prototypes built this way are routinely promoted straight to production. A key that was 'just for the demo' becomes the live credential, still sitting in the source.
- Secrets pasted from an AI chat window or a tutorial, placeholder keys the author meant to replace, get swapped for real ones in the same inline position, so the real key inherits the insecure placement.
- Public 'build in public' repos and AI-shared snippets mean generated code, secrets included, is pushed to GitHub quickly. Automated scrapers harvest exposed keys within minutes of a public commit, often faster than the author notices.
Detection
How to find it
- Scan the working tree and the full git history (not just the current HEAD) for high-entropy strings and known key formats; a secret removed in a later commit is still live until rotated.
- Grep for the obvious anchors, `apiKey`, `secret`, `password`, `token`, `BEGIN PRIVATE KEY`, provider prefixes like `sk_`, `AKIA`, `ghp_`, across all file types, including config, infrastructure, and client bundles.
- Check what reaches the browser: anything in a client-side bundle is public by definition, so a secret there is already exposed.
- Treat any match as compromised the moment it exists in a shared or public repository, and assume it has been scraped.
Remediation
How to fix it
- Move every secret out of source and supply it at runtime from the environment or a secrets manager; reference it by name (for example `process.env.STRIPE_SECRET_KEY`), never by value.
- Rotate any credential that has ever been committed. Removing it from the current file is not enough; the old value is in history and must be invalidated at the provider.
- Add the secret-bearing files to `.gitignore` and provide a committed `.env.example` with names and empty values so the shape is documented without the secrets.
- Keep secret keys server-side only; the browser should talk to your server, which holds the key, not to the third-party API directly.
- Add a pre-commit secret scanner and a CI gate so a hard-coded credential fails the build before it can merge.
Frequently asked questions
- What is CWE-798?
- CWE-798, Use of Hard-coded Credentials, is when a secret such as an API key, password, or token is written directly into source code instead of being provided securely at runtime. Anyone who can read the code, or its git history, can use the credential.
- Why is CWE-798 so dangerous?
- A hard-coded credential usually grants direct, authenticated access to a real system with no further exploitation needed. It is copied into every clone and every commit in history, is rarely rotated because rotating means changing code, and is harvested by automated scrapers within minutes of a public push.
- How have AI assistants and vibe coding increased the risk?
- AI assistants optimize for code that runs immediately and were trained on repositories full of inline secrets, so they frequently emit keys directly in the file. Vibe coding, accepting generated code without reading the diff, removes the review step that would normally catch the secret, and promotes prototypes straight to production. More code generated per hour means more secrets landing in more places with less human attention per line.
- How do I fix a hard-coded credential?
- Move the secret to an environment variable or secrets manager and reference it by name, rotate the exposed value at the provider (deleting it from the file is not enough because it stays in git history), ignore the secret file in version control, keep keys server-side, and add a pre-commit and CI secret scanner so it cannot happen again.
Related weaknesses
Catch CWE-798 before you ship
Tethlyn Audit scans your codebase and its git history for CWE-798 and the rest of the secret & credential exposure class, then ranks each finding by real exploitability and data sensitivity.