Summary
Caddy's vars_regexp matcher incorrectly performs a second round of placeholder expansion on user-controlled input, causing attacker-supplied values to be interpreted as Caddy template placeholders.
Under these conditions, a crafted HTTP header can cause the server to resolve {env.*}, {file.*}, or similar placeholders, leaking environment variables, sensitive files, and internal system information.
Affected context
This vulnerability affects Caddy deployments using the vars_regexp matcher in configurations where:
- Request-derived data (such as HTTP headers) is matched or processed through Caddy's replacer system, and
- The vars_regexp matcher can see and act on user-controlled values that may contain placeholder-like syntax (e.g.,
{env.VAR},{file./path}).
It is most relevant when environment variables, configuration secrets, or file paths accessible to the Caddy process contain sensitive data, and when administrators rely on placeholders in configuration to dynamically fetch such values.
Exploit preconditions
- Attack surface: Exposed HTTP endpoints handled by a Caddy instance configured to use
vars_regexp(or features that internally rely on it). - Required access: Ability to send HTTP requests that include attacker-controlled headers or other inputs that the configuration maps into placeholders, such as
{http.request.header.X-Input}. - Trigger point: vars_regexp evaluates a placeholder derived from user input, resolves it once, and then passes the resolved value through
repl.ReplaceAll()again, treating any placeholder syntax in the header value as an additional template to evaluate. - Why exploitation is possible: The double expansion breaks the expected trust boundary between user input and internal placeholder evaluation, effectively allowing external input to invoke internal expansion functions (e.g.,
env,file) that can read secrets and files.
Why this happens
The bug stems from improper handling of user-controlled data in Caddy's templating and replacement system.
The vars_regexp matcher is intended to resolve placeholders like {http.request.header.X-Input} once, substituting them with their actual runtime values.
However, due to a logic error in vars.go:337, the resolved value is then passed through repl.ReplaceAll() a second time.
This double-expansion means that if an attacker places template-like content such as {env.DATABASE_URL} or {file./etc/passwd} in the relevant request header, that content is no longer just data; it is interpreted as an instruction to read environment variables or files.
Conceptually, this maps to CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component, where untrusted input is allowed to influence template processing, combined with CWE-200: Exposure of Sensitive Information, since the expanded placeholders can reveal secrets.
Potential impact
- Leakage of environment variables (e.g., database URLs, API keys, credentials) via
{env.*}placeholders. - Exposure of local file contents readable by the Caddy process using
{file./path/to/secret}. - Disclosure of system and configuration information such as hostnames, paths, or runtime metadata.
- Indirect compromise of backend systems if leaked secrets are reused (e.g., database compromise, service account takeover).
- Enhanced attacker reconnaissance, enabling more targeted follow-on attacks against the application and infrastructure.
General mitigation guidance
Effective mitigation focuses on preventing user-controlled input from being treated as template directives and tightening the trust boundary around placeholder expansion:
- Ensure that templating and placeholder expansion functions run only on trusted configuration data, not on raw, user-supplied values.
- Avoid passing already-resolved values back through template expansion APIs; data derived from requests should be treated as opaque values, not re-templated strings.
- Design configuration so that sensitive placeholders (e.g.,
{env.*},{file.*}) are used only in static configuration or in contexts that are not influenced by request parameters, headers, or query strings. - Apply defense-in-depth by limiting which environment variables and files are accessible to the Caddy process, reducing the blast radius if a similar issue arises.
- Use a principle-of-least-privilege approach for runtime secrets: keep database URLs, API keys, and tokens in dedicated secret stores and avoid broad environment exposure when possible.
How teams detect issues like this
Organizations can look for this class of issue using a combination of configuration review and code-level analysis:
- Static configuration review: Examine Caddy configuration files for use of
vars_regexpwhere variables reference{http.request.*},{header.*}, or similar request-driven placeholders, and verify that these values are not later reprocessed by placeholder expansion. - Code and template analysis: Use static analysis or manual review to identify flows where untrusted input is combined with templating engines, template replacement utilities, or placeholder resolvers.
- Centralized policy checks: Enforce policies in CI/CD that flag configurations or code paths that apply template expansion functions more than once or apply them to user-controlled strings.
- Runtime monitoring: Log unusual header values containing placeholder-like syntax (
{env.,{file., etc.) and alert on requests that appear to be probing for template expansion behavior. - Automated scanners and configuration linters can be extended with rules to detect double-expansion patterns and the mixing of template syntax with request-derived data.

