API keys, access tokens, database passwords, and service credentials are often required for applications to connect to other services. Problems arise when these secret values are written directly into code or uploaded to Git repositories. If repositories are visible to others, those credentials can be misused to access data, consume API quotas, or generate unexpected charges.
One common pattern is separating configuration from code using environment variables. In local projects, the .env file is frequently used to store these values. However, this file is not an encrypted vault. Protection still depends on how the file is stored, shared, and used.
What are .env files and API keys?
A .env file contains name-value configuration pairs that applications read at runtime. For example, an application can read database hostnames or API tokens from the environment without hardcoding values in source code. An API key is a credential that identifies or authorizes an application to access a service. Treat these values like passwords: don’t display them in documentation, screenshots, logs, or public conversations.
Format and loading methods for .env files vary by programming language and framework. Ensure the library you’re using actually reads the file; Git and operating systems do not automatically load it into applications.
1. Store secret values in local .env
In your project directory, create a file named .env and enter the required values. The following example uses placeholder values, not real credentials:
APP_ENV=development
API_BASE_URL=https://api.example.com
SERVICE_API_KEY=replace_with_local_key
DATABASE_URL=postgres://user:password@localhost:5432/appdb
Don’t put production values in code examples. Also avoid putting secrets in files meant to be shared. If your project requires a library to read .env, follow your framework’s official documentation and ensure production configuration doesn’t depend on individual developers’ local files.
2. Exclude .env from Git
Add secret file patterns to .gitignore in your repository root before committing. A simple example:
.env
.env.*
!.env.example
This rule excludes .env files and their variants while still allowing .env.example templates. Adjust patterns if your project has other configuration files that are safe to track. Afterward, check your changes before committing and ensure secret files aren’t staged.
Important: .gitignore only prevents untracked files from being added normally. If .env was previously committed, adding it to .gitignore won’t remove copies from Git history. Remove the file from tracking as needed, and assume any uploaded credentials are already exposed.
3. Provide .env.example without secrets
Teams still need a list of required configuration. Create a .env.example file containing only variable names and placeholder values, for example:
APP_ENV=development
API_BASE_URL=https://api.example.com
SERVICE_API_KEY=
DATABASE_URL=
This template can be shared with code since it contains no sensitive values. Include instructions in your README about copying the template to .env and obtaining credentials through official channels. Don’t copy production values into templates just to simplify project setup.
4. Use secret managers for deployment
For applications running on servers, hosting platforms, or CI/CD pipelines, use your platform’s secret management features when available. For example, GitHub Actions supports secrets at repository, organization, or environment levels. Store values in secret settings, then grant access only to workflows and stages that genuinely need them. Official guidance is available in GitHub Actions documentation on secrets.
Limit who can read or modify secrets, separate development and production credentials, and avoid printing values to logs. Environment variables are practical but not automatically secure: other processes with access rights, diagnostic dumps, or poor logging configuration can leak them. For high-risk systems, consider dedicated secret management services and follow your hosting provider’s security guidelines.
5. Apply least privilege and rotation
Create API keys with the narrowest permissions possible. If services offer restrictions by project, IP address, endpoint, or quota, enable them according to application needs. Don’t use a single administrator key for all tasks. Separate keys per application or environment so incidents in one place don’t immediately compromise entire services.
Rotation means replacing credentials on schedule, not just renaming variables. Ensure applications are using new keys before revoking old ones, then check usage logs for unrecognized activity. Principles of management, access restriction, and secret revocation are also covered in the OWASP Secrets Management Cheat Sheet.
What if an API key already leaked?
Don’t just delete the visible line. Immediately revoke or disable the key through the provider’s dashboard, create a replacement, then update configuration in all legitimate environments. Check usage logs, billing, and account activity for misuse. If secrets entered a repository, restrict repository access and clean history if necessary, but remember that copies or forks may already exist elsewhere. Credential replacement remains the primary step.
Don’t paste real keys into support tickets, messages, or online scanning tools when asking for help. If investigating incidents, use redacted versions and keep evidence with restricted access.
Mistakes to avoid
- Writing API keys directly in code, configuration examples, or deployment scripts.
- Treating private repositories or .env files as perfect protection.
- Printing entire configuration to logs during debugging.
- Using the same key for development, testing, and production.
- Relying on .gitignore after secrets already entered Git history.
Quick checklist before sharing projects
Ensure .env isn’t tracked by Git, .env.example contains only placeholders, and repository scanning finds no credentials. Also check secret settings on hosting and pipelines, access rights for each key, and rotation procedures for leaks. Automated scanning and push protection can help, but still require manual review; see GitHub’s guide on secret scanning.
Conclusion
.env files help separate configuration from source code, but they’re not leak-proof secret storage. Combine environment variables with .gitignore, credential-free templates, secret managers for deployment, least privilege access, and scheduled rotation. If keys are ever exposed, revoke and replace them immediately. These simple habits reduce API key leak risks without complicating development workflows. Windows Terminal v1.0 illustration by Microsoft; source: Wikimedia Commons; Expat/MIT license.

Komentar (0)
Belum ada komentar. Jadilah yang pertama berkomentar.