Learning objectives
After reading this article you will be able to:
- Compare .env files, platform variables, CI secrets and secrets vaults
- Weigh team size, rotation, auditing and who can read each secret
- Choose where each secret lives, from a personal bot to a system run by several teams
Getting the token out of your code is the first step, covered in how to keep secrets out of your code. The next question is where the secret goes. There are four common answers: a .env file, your hosting platform's environment variables, your CI provider's secrets and a dedicated secrets vault.
None of them is best in every case. Each one fits a stage (development, pipeline, production) and a team size. This guide compares the four on the criteria that matter and ends with a recommendation per scenario.
Which criteria decide where a secret should live?#
- Who can read it. People (anyone with access to the repository, the hosting dashboard or CI) and processes (anything running with access to the app's environment). Fewer people and fewer processes is better.
- Rotation. How much work it takes to change the secret. If changing it hurts, nobody does it, and an old leaked secret keeps working. OWASP recommends regular rotation and, where possible, dynamic short-lived credentials.
- Auditing. Knowing who read or changed each secret, and when. Among the things to record, OWASP lists who requested each secret, for which system and role, and who changed it and when.
- Team size and number of services. A secret used by one bot is not the same as a database password shared by ten services and fifteen people.
- Operational effort. Every extra layer is another system to run, pay for and protect.
When does a .env file make sense?#
A .env file is a text file with one KEY=value line per secret. Node.js loads it with node --env-file=.env, and in Python python-dotenv does the same. The syntax is covered in what a .env file is.
Pros: zero infrastructure, works offline, every developer has their own.
Cons: plain text on disk, with no access control beyond file permissions, no history and no auditing. It is easy to commit, to forget inside a deploy ZIP or to send to a teammate over chat, and every copy is a possible leak.
Use .env for local development, with the file in .gitignore and a versioned .env.example holding fake values instead. Outside your machine, it is not the right place.
Are platform variables safe enough?#
In production, the standard is to set secrets in your hosting platform's settings, through its dashboard, CLI or API. The platform injects the values into the process when it starts, and the code reads process.env or os.environ. This is the 12-factor model, explained in environment variables.
Pros: the secret stays out of the code and the repository, each app and environment gets its own set, and there is no extra infrastructure. Changing a value means editing it and restarting.
Cons: OWASP is blunt: environment variables are generally accessible to all processes and may end up in logs or system dumps. Child processes inherit the environment, and on Linux the /proc/<pid>/environ file exposes the initial environment to anyone who passes the kernel's permission check. Anyone with access to the hosting account can read the values too. Rotation is usually manual, and auditing depends on the platform.
That is why OWASP prefers mounted files or a sidecar that fetches the secret from a vault, when the environment allows it. On application hosting platforms, environment variables are usually the mechanism available, and they work well with four habits:
- Never print values in logs, error messages or debug pages.
- Give each app only the credentials it uses, with the least privilege possible.
- Use a different secret per environment, as shown in development, staging and production.
- Do not mix up runtime and build-time variables: a secret baked into a frontend bundle becomes public, as explained in build-time vs runtime variables.
What are CI secrets for?#
CI providers store secrets for pipeline jobs: running tests against a database, publishing a package, deploying. In GitHub Actions, secrets live at the organization, repository or environment level, are encrypted with libsodium sealed boxes before they reach GitHub, and are redacted in logs, although GitHub warns that redaction is not guaranteed. Workflows triggered from a fork do not receive secrets, except GITHUB_TOKEN, and environment secrets can require approval from a reviewer.
# Part of a job: the secret becomes a variable for this step only
- run: npm run migrate
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}The limit is the role: CI serves the pipeline, not the app in production. OWASP advises against storing high-value, long-lived, widely scoped credentials there. For a pipeline that reaches a cloud, GitHub itself recommends OIDC, which swaps the fixed key for a short-lived token. A full pipeline example is in deploying with GitHub Actions.
When is a secrets vault worth it?#
A vault is a central service that stores secrets encrypted, controls who reads each one, logs every access and, in many cases, rotates values on its own. The app authenticates to the vault and fetches the secret at runtime. The best-known options:
- HashiCorp Vault: besides storing values, it generates dynamic database credentials, unique per service and with an expiry you set, and rotates the passwords of static users on a schedule.
- OpenBao: an open source fork of Vault, a project of the OpenSSF at the Linux Foundation.
- AWS Secrets Manager: automatic rotation through a Lambda function, or built in for some AWS services. It charges per stored secret and per volume of API calls; see its pricing page.
- Google Cloud Secret Manager: versions for each secret and a rotation schedule that sends notifications to Pub/Sub. Your code does the actual rotation.
- Azure Key Vault: the equivalent role on Azure.
- SOPS: a middle ground with no server. It encrypts YAML, JSON or ENV files with KMS, age or PGP keys, and the encrypted file goes into Git, reviewed in pull requests like the rest of the code.
A vault does not remove the environment variable: the app still needs a credential to talk to it. OWASP flags this, since the primary secret of the solution must also be protected somewhere else. With AWS, for example, the SDK reads the access key from AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY and fetches the rest at startup:
// npm install @aws-sdk/client-secrets-manager
import {
GetSecretValueCommand,
SecretsManagerClient,
} from "@aws-sdk/client-secrets-manager";
// Region and credentials come from AWS_REGION, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
const client = new SecretsManagerClient({});
const { SecretString } = await client.send(
new GetSecretValueCommand({ SecretId: process.env.DB_SECRET_ID }),
);
const { username, password } = JSON.parse(SecretString);The gain is that this credential can read only the secrets its policy allows, and every read is logged. The cost is one more critical system: if the vault is down, the app does not start.
How do the four options compare?#
.env |
Platform variables | CI secrets | Vault | |
|---|---|---|---|---|
| Where it lives | file on disk | hosting settings | CI provider | central service |
| Who reads it | whoever has the file | whoever accesses the account and the process | pipeline jobs | whoever has permission, per secret |
| Rotation | manual | manual, with a restart | manual | automatic or short-lived credentials |
| Auditing | none | depends on the platform | limited | a record of every read |
| Effort | none | low | low | high |
| Best for | local development | production for small and medium projects | tests, builds and deploys | teams, many services, compliance |
Which one should you pick for each scenario?#
| Scenario | Where to store |
|---|---|
| Personal bot or API | local .env and platform variables in production |
| Small team, few services | platform variables per environment, CI secrets for deploys and a password manager for sharing, never chat |
| Several teams and shared credentials | a vault, with its access credential in a platform variable |
| Payments, sensitive data or audit requirements | a vault with automatic rotation or dynamic credentials |
| Configuration that needs pull request review | SOPS, with the key outside the repository |
In every scenario, rotate the secrets a person had access to when they leave the project. And if something leaked, revoke first: the steps are in API key exposed on GitHub.
How to do this on Square Cloud#
On Square Cloud, production secrets live in each app's environment variables, set from the dashboard, the API, the CLI (app env set KEY=VALUE) or the VS Code extension. The dashboard imports your .env and exports the variables as KEY=VALUE, so your local file and production stay aligned without copying values one by one, and the .env does not need to ship in the ZIP or the repository.
The variables are isolated: one app cannot read another app's variables or files, even in the same account, so your bot never sees your API's credentials. A change takes effect on the next restart, with no rebuild, and the variables already exist while dependencies are installed.
Square Cloud does not have a secrets vault with automatic rotation. If your scenario calls for rotation, dynamic credentials or per-read auditing, use an external vault and keep only the credential that unlocks it in the app's variables. To find a secret that ended up written in the code, the check-up from Cubic, the AI agent in the dashboard, flags credentials written in the code, with file and line, never the value. See everything on the environment variables page.
Infrastructure
L3/L4/L7 DDoS mitigation and a WAF on every plan
FAQs
Are environment variables a safe place for secrets?
They are much better than secrets in code, and they are the standard on hosting platforms. OWASP points out the risks: environment variables are generally accessible to all processes and may end up in logs or dumps. Never print the values and give each app only what it uses.
Can I use a .env file in production?
It works, but it is plain text on disk with no access control, history or rotation, and it leaks easily inside a ZIP or a commit. Prefer the platform's variables and keep the .env out of Git and out of your deploy package.
When is a secrets vault worth it?
When many people or services share credentials, when you need to know who read each secret, or when you want automatic rotation or short-lived credentials. For a bot or an API run by one person, platform variables are usually enough.
Should GitHub Actions secrets hold production credentials?
They serve the pipeline: tests, builds and deploys. OWASP advises against storing high-value, long-lived credentials in CI variables and, for pipelines that reach a cloud, prefers short-lived OIDC tokens.
More in Security
See all 24 articles- Authentication vs authorization: the difference in practiceAuthentication confirms who you are; authorization decides what you can do. Learn 401 vs 403, roles, scopes and the bug of checking only the login, with code.
- JWT vs cookie sessions: which to use for loginWith sessions, state lives on the server and the cookie holds an ID; with JWT, the data travels in the token. Compare logout, CSRF, XSS and multiple instances.
- What is SQL injection and how to prevent itSQL injection happens when user input becomes part of a SQL query. See how the attack works and how to prevent it with parameterized queries in Node.js and Python.
- What is a brute force attack and how to defend against itA brute force attack tests passwords in bulk until one works. Learn the types (spraying, credential stuffing), how they work and the defenses, with code that runs.