I was skeptical that environment variables could be that bad. After all, does the twelve-factor app not require them?
Software deployments require configuration, some of it confidential, and we certainly don’t want sensitive values being stored in the application source code (and then on every developer’s workstation, a code forge, a USB stick, for all eternity). Modern web application deployments are typically discouraged by security folk from using environment variables to solve the configuration problem, particularly for passing sensitive values in to an application. Why, exactly? And what can be done about it?
What is an environment variable?
Environment variables had humble beginnings: Added in 1979’s Version 7 UNIX with DOS gaining support later in 1983’s MS-DOS 2.0 release, environment variables are a dictionary of key and value pairs that is present in a process’s operating system context. They are accessible to a process, and on many systems, they’re also automatically inherited by the children of a process (except some special cases where it’s insecure, like setting LD_PRELOAD before running a privileged program), allowing an “environment” to be incrementally built as part of the process tree. The init process can set up a global environment for all subsequent processes, the user may add or modify values in the environment their shell inherits from init, and applications may then read or modify their own environment based on the combined init and shell modifications, optionally passing those subsequent changes down to any processes launched from the application.
Here root prefers ed(1), the standard UNIX editor, whereas I’ve gone for something a little more modern:
# ps aux | fgrep /bin/bash
fincham 1203285 0.0 0.0 7076 3448 ttyS0 S 15:25 0:00 /bin/bash
root 1203927 0.0 0.0 9648 6568 tty2 S 15:33 0:00 /bin/bash
# tr '\0' '\n' < /proc/1203927/environ | fgrep EDITOR
EDITOR=ed
# tr '\0' '\n' < /proc/1203285/environ | fgrep EDITOR
EDITOR=natto
This is a convenient mechanism for ensuring that a small amount of hierarchically-assembled state may be made available to programs running on the system - values with a more generic system-wide use may be set early and automatically inherited by all processes (such as setting EDITOR to the path to a preferred text editor for the entire system), can be overridden by users (maybe they prefer to use vi instead, I don’t judge) and can be further used to pass configuration to programs through a standardised mechanism - particularly useful in cases where options are passed that function as de-facto defaults for all standard invocations of a particular program (for instance to specify an HTTP proxy to be used for all invocations of a web client), or to specify values that need to be used early-on in a program’s lifecycle, perhaps before it loads its own configuration from a disk file.
How did it all go so wrong?
Web applications.
As with many things, environment variables aren’t inherently bad. We just started to slowly change what we used them for until we accidentally arrived at a somewhat unsafe situation. As usual, the steps in isolation mostly make a kind of sense so it’s easy to see how the situation evolved.
In the forgotten times, computers were less numerous, larger and slower. Application code came in “releases” with a periodic cadence, and an operator would manually install and configure the latest release for the computer’s users to enjoy. Configuring the software was the operator’s job, and typically took place by editing configuration files on the host where the software was deployed, during the deployment process.
Over time the pace of software development has increased and the scale at which multi-user applications (typically nowadays through the web) are deployed has increased. This required a greater emphasis on deployment and configuration automation - every instance of an application needed to be updated more regularly, more consistently across multiple hosts and without the manual involvement of an operator at every step.
From this grew the cloud “platform as a service” paradigm, one where not only can an operator not really be expected to log in to every host and configure things, but where a developer could deploy an application directly to the platform without having any access or knowledge of how the underlying operating system was configured at all.
This raised a problem: if we can’t specify configuration in files that live outside the application, the platform has to manage this work somehow and hand it off to our application on our behalf. Thus was born the Twelve-Factor App in 2011, standardising the major points of integration between an idealised web application and the platform on which it is deployed. Point three addresses configuration, and recommends a mechanism: pass configuration as environment variables.
Environment variables had typically not been considered sensitive - some are, after all, exposed to every process in a system - and because they do specifically influence application behaviour, it was common for applications to return the contents of their process environment automatically as a debugging aid when something went wrong. Early web applications executing in a CGI environment typically only received environment variables containing user input anyway, there’s not much point in stopping a user from seeing their own input to the program.
This created a situation where web applications are still often a single bug or crash away from leaking the contents of their process environment, and any associated credentials that may have been passed to the application through the environment. The opportunities for defence-in-depth are rather more limited when a flaw in your application might just cause your web framework to spill the beans directly to an attacker.
This is bad enough when it’s only the web application holding the sensitive variables, but if the application is deployed inside a container there’s a good chance that actually every process in the container inherits the secrets too:
$ docker run -it -e SECRET=pulsepulsepulse debian:latest
root@91325dba7f14:/# set | fgrep SECRET
SECRET=pulsepulsepulse
Further confusing matters, user interfaces where environment variables are set in cloud environments often treat them as semi-secret, for instance Azure has a little ‘Show value’ button you have to click before each variable is displayed:
Which seems to imply they’re at least a bit secret in Microsoft’s opinion.
And to make matters worse, even when they’re not secrets, we have a poor track record at not just letting environment variables become attacker controlled inputs anyway. CVEs where an attacker can inject mostly-arbitrary environment variables in to a web application’s execution context turn up regularly, and this bug class goes back to at least the introduction of CGI around 1993. One of the more well-known instances of this was httpoxy in 2014, where it was viewed as something of an anachronism already, but of course we are still having similar issues more than a decade later.
Hopefully, your web application doesn’t rely on any environment variables called HTTP_something because this is probably all an attacker would need to do to set those to whatever value they wanted:
$ curl -s -H "Actually-Use-This-URL: https://pulsesecurity.co.nz/" http://localhost/debug.php | fgrep pulse
<tr><td class="e">HTTP_ACTUALLY_USE_THIS_URL</td><td class="v">https://pulsesecurity.co.nz/</td></tr>
Environment variables get set automatically in many HTTP request contexts just from an attacker’s supplied headers, with the caveat that there’s usually a mandatory HTTP_ prefix. Here I’ve used curl to add a header called Actually-Use-This-URL to the request, and when debug.php executes, surprise, this is now in the process environment with the name HTTP_ACTUALLY_USE_THIS_URL. Typically, this happens regardless of whether that variable was already set or intended to be used for something else. You can probably imagine the chaos this can cause.
How are we fixing it?
Some web frameworks, like Django, make an attempt at filtering out “sensitive looking” environment variables from debug output. This is distinctly best effort, the default list of keywords Django filters is just API, KEY, PASS, SECRET, SIGNATURE and TOKEN. Environment variables such as BACKEND_URL containing credentials still won’t be filtered:
Even if you’re not running your application in DEBUG mode (which, to be clear, you should definitely not be in production), error reports containing these secrets may still be logged or e-mailed. Django provides an additional sensitive_variables decorator that can mark other variables to be masked in errors, but this mostly falls in to the category of “ensuring complete and unerring developer vigilance” which has not historically proven to be foolproof.
Better Options
The gold standard is using a dedicated key vault and retrieving the credentials during application startup. This has several benefits, including audit logging, and ensuring that the secrets exist only in an in-memory variable within the program. While we don’t tend to trust applications not to dump the contents of their environment for debugging during error conditions, it’s much rarer that an application would dump arbitrary variables (outside of maybe sometimes showing the local variables in the current stack frame), significantly reducing this risk. It’s also less likely that an attacker can influence the value of a global variable in your application by just setting an HTTP header with their requests.
Open source solutions to this problem exist, such as OpenBao, but if you’re deploying your application in a big-tech cloud environment you’ve almost certainly got a platform solution available as well - Azure has Key Vault, AWS has Secrets Manager and Google Cloud has Secret Manager.
Even if you don’t go the whole way to a secrets management platform, just storing your application’s secrets in a file outside of the web root that is readable only by the application and reading these in to a variable at startup is an improvement over environment variables. Just make sure you don’t check this file in to the application’s source code repo.
$ ls -l secrets
-r--r----- 1 mycoolapp mycoolapp 6 Oct 21 14:52 secrets



