Getting Secrets Out of Your Code: .env Files, Secret Management, and Rotation

When we take over a project or run a security review, one of the most common problems we find is a database password, a payment provider key, or a cloud service credential sitting inside the code. Sometimes a .env file has accidentally been committed to the repository; sometimes the key has been written straight into a class. In this post we walk through a practical way to get secrets out of the code, manage them, and rotate them regularly.

What counts as a secret, and where it should never live

Passwords, API keys, signing keys, database connection details, and webhook secrets are all secrets. The rule is simple: secrets don't go into version control, don't get written to logs, don't appear in error messages, and don't get embedded in client apps. The most critical item on that list is version control, because Git history doesn't forget; even if you delete a secret later, it stays in the old commits.

The right way to use .env in Laravel

In Laravel, environment values live in the .env file, and that file must be listed in .gitignore. In the repository, we commit a .env.example with the values left blank, so a new developer can see which variables are needed. One more important detail: use the env() call only in config/ files, and in application code access values with something like config('services.odeme.anahtar'). When php artisan config:cache runs in production, the configuration is cached, and env() calls outside of config can unexpectedly return null.

Mobile apps can't keep secrets

This is a frequently misunderstood topic in Flutter projects. Supplying a value at build time with --dart-define separates the key from the source code, but it doesn't stop it from being extracted from the app; anyone who unpacks the package can find the value. So only put identifiers into the client that are harmless if they become public. Values like a payment provider's secret key or an AI service key must always stay in your own backend: the app should talk to that backend, and the backend should call the external service.

A key supplied at build time can be extracted from the app; secret keys stay in the backend, and the app talks to the backend.
Secret keys should always live in your own backend.

Layers of protection in the repository, the pipeline, and on the server

  • Run a scanner like gitleaks as a pre-commit hook before every commit.
  • Turn on your repository platform's secret scanning and push protection features.
  • In your CI/CD pipeline, store secrets in the platform's secrets area, and make sure they are masked in the logs.
  • On the server, keep the .env file with permissions that only the application user can read, for example chmod 600.
  • Use a separate key per environment; development, test, and production should never share the same key.
  • As the team grows, move to a central secrets manager (your cloud provider's service or a solution like Vault).

When a leak happens, and regular rotation

If a secret has made it into the repository, the first step isn't cleaning up the history; it's revoking that secret and generating a new one. Rewriting history comes later, because once a secret has spread, it can't be considered safe. Set up a regular schedule too, so that rotation doesn't depend on a leak. The trick to painless rotation is allowing the old and new values to be valid side by side for a short time: generate the new key first, switch the system over to it, watch the old one for a while, and revoke it once you're sure it's no longer in use. In Laravel, changing APP_KEY affects encrypted cookies and data, so before you change it, check the framework's setting for supporting previous keys.

Don't try to do it all in one day. First run a secret scan of the repository, history included, and replace whatever you find; then add the scanner to the pre-commit pipeline. After that, gather which secret is used where into an inventory; you can't rotate a secret that isn't in an inventory. For each secret, it's enough to record its owner, where it's used, and the date it was last changed; even a simple table saves hours when the first leak happens.

First the repository is scanned, any secrets found are replaced, a pre-commit scanner is added, and then a secrets inventory is built.
You can't rotate a secret that isn't in an inventory.

If you'd like to review secret management in your current project, run a security scan, or harden your deployment pipeline in this respect, write to us through our contact page. The picture we've seen in the projects we've taken over through the years is clear: most leaks don't come from sophisticated attacks, but from a single file that accidentally made it into the repository.