Secrets management
API keys, database credentials, cloud access permissions, SSH keys and certificates all have to be stored somewhere, handed to the programs that use them, and replaced or withdrawn over their lifetime. Secrets management is the practice of controlling those steps for every secret an application holds. The usual failure is a secret written in plain text into code or a configuration file.1
Storage and delivery
A secret written in source code reaches everyone who can read the code, and it stays in the Git history after the line is deleted (see Exposed credentials in source code repositories). Credentials belong outside the code, in an encrypted configuration file or a database that outsiders cannot read.2 In web applications the usual carrier is the environment variable, set where the application runs and read by name. Next.js loads .env files into process.env, and the create-next-app template lists every .env file in .gitignore so that none of them is committed.3
# .env (listed in .gitignore)
DB_PASS=replace-with-the-real-password
// app/api/route.js: runs on the server
export async function GET() {
const db = await myDB.connect({ password: process.env.DB_PASS })
}Vercel encrypts environment variables at rest and offers two types: a Config value stays readable to team members after saving, while a Secret value can be written but never read back. When a Secret of 32 characters or more appears in a build log, Vercel replaces it with [REDACTED].45 GitHub Actions encrypts a secret with a Libsodium sealed box before it reaches GitHub, and a workflow receives it only when the workflow file names it. GitHub masks secret values printed to job logs and states that this masking is not guaranteed.6
Other processes on the same machine can usually read environment variables, and their values can end up in logs and memory dumps. A secret does not belong in a container image through the Docker ENV or ARG instructions, and an encryption key does not belong next to the secrets it protects unless the key is itself encrypted.1
The browser boundary
In Next.js, a variable without a prefix exists only in the Node.js server process. A variable whose name starts with NEXT_PUBLIC_ is copied into the JavaScript bundle when the app is built and reaches every visitor.3 A server key given that prefix by mistake is published with the site (see Privileged key in client-side code and Client-side code exposure).
Lifecycle
A secret passes through four stages: creation, rotation, revocation and expiration. A new secret receives only the permissions its task needs, following the principle of least privilege. Rotation replaces it on a schedule so that a stolen copy stops working after a short time. User passwords are excluded and changed only when there are signs of compromise. Revocation withdraws a secret that is no longer needed or may have leaked; expiration ends it at a set time without anyone acting.1
Rotation changes the credential in two places, the service that accepts it and the store the application reads it from.7 Vercel's procedure avoids downtime: create a new credential at the provider while the old one still works, save it in the environment variable, redeploy, check the deployment, and only then invalidate the old credential. A changed variable has no effect on existing deployments.4 A team-level variable linked to several projects needs each of them redeployed first.8
AWS Secrets Manager rotates database passwords in one of two ways. The single-user strategy changes one user's password, and connections can be refused for a short time while the change takes effect. The alternating-users strategy keeps two database users in one secret and changes their passwords in turn, so the previous credentials stay valid during the switch.9 A short-lived credential is requested by the application when it starts and expires with its lease; a stolen copy stays usable until the lease ends or the credential is revoked.1
Access and audit
Every person or system that can read a secret is a route by which it can leak, so access rules are set per secret and production and development keep separate stores. An audit log records who requested and used each secret and when it changed. If a build system's credentials reach the secrets manager from an unfamiliar address, the secret is treated as leaked.1
Build pipelines
Secrets stored in a CI tool are available to its jobs and to project maintainers, which makes the tool a poor home for long-lived, broadly scoped credentials. A job can instead prove its identity with a short-lived OpenID Connect token and receive temporary cloud credentials, so no cloud key has to be stored on the CI platform. Runtime secrets are better kept out of the pipeline altogether: the running service reads its own secrets from the secrets manager under its platform identity.1
Notable incidents
On 4 January 2023 the build service CircleCI told customers to rotate every secret stored on the platform.10 Its incident report states that malware on an engineer's laptop, present since 16 December 2022, had stolen a single sign-on session protected by two-factor authentication. With the engineer's right to generate production access tokens, the attacker copied customer environment variables, tokens and keys on 22 December 2022. The data was encrypted at rest, but the attacker also took the encryption keys from a running process. CircleCI revoked all project API tokens on 5 January, told customers to assume that any secret stored during the period had been accessed, and recommended OIDC tokens so that long-lived credentials need not be stored on the platform.11
Response to a leak
Containing a leak means revoking the exposed secret, rotating to a new one, and deleting the old value from code and logs.1 Steps for each provider are in Responding to a leaked API key, and tools that find secrets in code in Secret scanning.
In SecFlash
SecFlash reports a key written in the source code or its Git history as SF-SEC-702 on the Security page. A secret key in JavaScript that the site serves is SF-EXP-301, and one written into the HTML of a page is SF-EXP-304, both on the Exposure page. A downloadable environment file is SF-EXP-307 on the Exposure page and SF-SEC-617 on the Security page. A key inside the base image a Dockerfile builds on is SF-CLD-912 on the Cloud page.
See also
- Exposed credentials in source code repositories
- Privileged key in client-side code
- Responding to a leaked API key
- Secret scanning
- Secret exposure in CI/CD workflows
- Principle of least privilege
- Encryption at rest
- Category: Secrets
References
- a b c d e f g "Secrets Management Cheat Sheet". OWASP Cheat Sheet Series.
- back "CWE-798: Use of Hard-coded Credentials". Common Weakness Enumeration. MITRE.
- a b "How to use environment variables in Next.js". Next.js Docs. Vercel.
- a b Vercel (updated 17 September 2026). "Environment variables". Vercel Docs.
- back Vercel (updated 28 August 2026). "Config and Secret environment variables". Vercel Docs.
- back GitHub. "Secrets". GitHub Docs.
- back Amazon Web Services. "Rotate AWS Secrets Manager secrets". AWS Secrets Manager User Guide.
- back Vercel. "Rotating environment variables". Vercel Docs.
- back Amazon Web Services. "Rotation strategies". AWS Secrets Manager User Guide.
- back Abrams, Lawrence (14 January 2023). "CircleCI's hack caused by malware stealing engineer's 2FA-backed session". BleepingComputer.
- back CircleCI (January 2023). "CircleCI Jan 4, 2023 security incident report". CircleCI blog.
External links
- Key Management Cheat Sheet, OWASP Cheat Sheet Series
- CI/CD Security Cheat Sheet, OWASP Cheat Sheet Series