Skip to content
Back to log

.env files and secret management

Chainabit's Chao · · 7 min read

In short

Secret storage, environment variables risks, access boundaries, and leak response, based on guidance from OWASP and GitHub.

The scope of secrets

Secret management covers where the credentials an application needs are stored, who can access them, and how they are changed. OWASP includes API keys, database credentials, IAM permissions, SSH keys, and certificates in this scope. This information may be present in plain text in application code or configuration files.

According to OWASP, sharing the same secret among multiple services makes it harder to determine where a leak or unauthorized access began. Centralizing secret management covers the management of storage, access, auditing, and rotation.

A user who can read or update a secret can cause it to leak either directly or through the system they use. OWASP therefore states that not everyone should have access to all secrets and that the Least Privilege principle should be applied. To do this, the management system must be able to define detailed access rules for each object and component.

Environment variables and container risk

OWASP lists the use of environment variables as one way to deliver secrets to an application in a Docker container. However, environment variables are generally accessible to all processes. They can also appear in logs or system dumps. OWASP does not recommend using environment variables unless other methods are not possible.

It is also possible to provide secrets directly in Docker container configuration. However, OWASP states that secrets should not be hardcoded using Docker ENV or Docker ARG commands. These values can easily leak along with container definitions. In the method described by OWASP, the orchestrator replaces the environment variable value, which is not hardcoded, with the actual secret.

In another method that uses files, the secret is stored in a configuration or secret file and attached to the environment where the application runs as a mounted volume. OWASP states that the orchestrator should make this attachment when needed, rather than including it in the container definition. Including the file in the container definition can cause the secret to leak along with the definition.

In another method described by OWASP, a sidecar application or container retrieves the secret directly from a secrets manager service. This method allows dynamically generated secrets to be used without concerns about their being visible through the file system or the Docker container's environment variables.

Application access through a sidecar

OWASP describes a Kubernetes setup in which a Pod contains the main application container and a sidecar container. The sidecar authenticates with the secrets manager, retrieves the secret, and writes it to a shared in-memory volume. The main application container then reads the information from this volume.

In this setup, the sidecar can refresh the secret regularly. The application container can read it from the shared volume without being directly tied to the details of the secret management solution. OWASP gives the use of a Kubernetes Service Account as an example of a method for the sidecar to authenticate with the secrets manager.

OWASP also describes a short-lived sidecar method. In this method, the sidecar retrieves secrets through a remote endpoint and places them in a shared volume attached to the main container. It terminates after retrieving the information. The containers in the Pod can use the information through this volume without being aware of the secrets manager.

If the orchestrator stores secrets internally, OWASP states that the storage infrastructure should be encrypted and its keys managed. OWASP also states that secrets should be exposed to the communication mechanisms between the container and the deployment representation, and not through external access mechanisms shared between deployments or orchestrators.

Access, lifetime, and logs

According to OWASP, a newly created secret should have sufficient cryptographic strength for its intended use. It should be granted only the minimum privileges required for the necessary use or role. Credentials must also be transmitted securely; OWASP states that secrets should not be transmitted in plain text.

The lifecycle of secrets consists of creation, rotation, revocation, and expiration. A secret that is no longer needed or may have been compromised must be revoked to limit access. OWASP also states that a specific validity period should be defined for secrets where possible.

Turn this insight into action.

Use the mobile app to continue with guided workflows.

Dynamic secrets can be used in this lifecycle. In OWASP's example, the application requests database credentials when it starts and receives newly created credentials for that session. OWASP states that dynamic secrets should be used where possible, the lease duration should be kept short, and credentials should be revoked when they are no longer needed.

Restarting the application does not automatically revoke stolen credentials. According to OWASP, these credentials can be used until they expire or are revoked by the service they belong to. Long-lived static secrets are also often required to generate dynamic credentials.

Static secrets also have uses. OWASP gives examples of keys that must live longer than a deployment, such as storage encryption keys and TLS PKI keys. Credentials used to connect to services that do not support the creation of temporary roles or credentials are also among the uses for static secrets.

OWASP states that audit logs in secret management should be kept resistant to attempts to modify or delete them. The logs should record who requested a secret, for which system and role, whether the request was approved or denied, and who used the secret and when. Attempts to reuse expired credentials, as well as authentication and authorization failures, should also be recorded.

The secret itself should not be written to logs. OWASP states that encryption or masking should be applied to prevent plain-text secrets from entering logs. Audit logs should also record who updated the secret and management operations.

Detecting a leak

According to GitHub, committing credentials such as API keys and passwords as hardcoded values to repositories makes them targets for unauthorized access. GitHub secret scanning scans the Git history across all branches of a repository for known hardcoded credentials. These include API keys, passwords, and tokens.

GitHub secret scanning is not limited to scanning code history. GitHub states that it also scans issue descriptions and comments; titles, descriptions, and comments of open and closed issues; and titles, descriptions, and comments in pull requests and GitHub Discussions. Wikis and secret gists are also covered by automatic scanning.

When a credential leak is detected, GitHub creates an alert in the repository's Security and quality tab with details about the exposed information. GitHub states that the affected credential should be rotated immediately when an alert is received. It is also possible to remove the secret from Git history; however, GitHub says this can be time-consuming and is often unnecessary if the credential has already been revoked.

GitHub states that the default detection scope can be extended with generic patterns and custom patterns. Generic patterns cover information such as private keys, connection strings, and general API keys that are not tied to a particular service provider. Custom patterns allow regular expressions to be defined to detect organization-specific secrets that are not covered by the default patterns.

Validity checks help prioritize which finding to address first by determining whether a detected secret is still active. According to GitHub, when this feature is enabled, secret scanning can contact the service that issued the credential to determine whether it has been revoked.

Actions and limits after a leak

OWASP states that exposed keys should be revoked immediately and that a new secret should be created and applied quickly. Revoked or rotated secrets should be removed immediately from the system where they were exposed, including code and logs. A process is also needed to preserve the integrity of logs when removing secrets from them.

Rewriting code history can break some links: OWASP states that changing commit history back to a point before the secret was added will break links to the affected commits. During the response, records of who accessed the secret, when they used it, and when it was previously changed are also useful.

Protection in memory is a separate boundary. OWASP states that reducing the time a secret remains in memory and access to the memory area can provide additional security. However, this can be difficult to implement. According to OWASP, if an attacker has already accessed the memory of the process handling the secret, a security breach may have occurred by that point.

Ready to start?

Download the app and continue from your phone.

End of Entry