Summary
Caddy's FastCGI transport incorrectly parses request paths containing non-ASCII bytes, allowing certain non-script files to be misclassified as executable scripts.
In environments where attackers can write content to files served via FastCGI, this misclassification can be abused to execute arbitrary code via crafted URLs, turning benign content into a remote code execution vector.
Affected context
This vulnerability occurs in Caddy deployments using the FastCGI transport for dynamic content handling, typically for PHP or similar backends. It becomes relevant when request paths are split into script and path-info components and when non-ASCII bytes appear in the URL path. Risk is highest in setups that allow user-controlled files (such as uploads or shared storage) to be placed in directories serviced by FastCGI, because incorrect splitting can cause Caddy to treat those files as executable scripts rather than static resources.
Exploit preconditions
- Attack surface: HTTP(S) endpoints served by Caddy and forwarded via FastCGI to a backend (e.g., PHP-FPM).
- Required access: Ability to create or modify files reachable by the FastCGI handler (e.g., file uploads, user-writable storage paths).
- Trigger point: A crafted URL whose path contains specific non-ASCII bytes and matches a pattern that causes splitPos() to misidentify the script portion.
- Why exploitation is possible: The path splitting logic misuses case-insensitive search on non-ASCII data and contains fallback flaws, causing non-script files lacking the configured script extension to be treated as executable scripts, thereby delegating attacker-controlled content to the FastCGI engine for execution.
Why this happens
The underlying bug stems from improper input validation and path processing in the FastCGI splitPos() function. By relying on golang.org/x/text/search with search.IgnoreCase on paths containing non-ASCII bytes, the splitting logic enters flawed fallback code paths that misinterpret the boundary between script name and path-info. This behavior maps to CWE-20: Improper Input Validation, combined with CWE-176/CWE-178: Improper Encoding/Handling of Unicode and Case Sensitivity, because the code assumes ASCII-like behavior on inputs that can include arbitrary bytes and ignores edge cases in case-insensitive matching. As a result, the system breaks the invariant that only files with configured script extensions (like .php) are treated as FastCGI scripts, allowing attacker-controlled content to be executed.
Potential impact
- Attackers may execute arbitrary code via FastCGI by turning uploaded or stored files into interpreted scripts.
- They could run server-side commands with the privileges of the FastCGI backend (e.g., PHP interpreter).
- Attackers can potentially access or modify sensitive application data handled by the backend.
- They may escalate from simple file upload abuse to full application or server compromise.
- Remote attackers can bypass expected file-type and extension-based execution controls.
General mitigation guidance
Mitigation centers on tightening path splitting and reducing trust in extension-based execution decisions. Secure patterns include:
- Ensuring path parsing logic treats URLs as opaque data and rigorously validates script boundaries, especially when handling non-ASCII or mixed-encoding paths.
- Applying strict rules that only files with explicitly whitelisted script extensions and locations are ever passed to FastCGI for execution.
- Separating upload and user-writable directories from those used for executable scripts so that user-controlled content cannot be interpreted by FastCGI.
- Normalizing and validating paths before any splitting, avoiding ambiguous case-insensitive or Unicode-dependent comparisons.
- Running FastCGI backends with least privilege and minimizing what they can access or modify, so that even misclassified content has reduced impact.
How teams detect issues like this
Detection generally involves reviewing both configuration and code paths that influence FastCGI routing:
- Static analysis or grep-based checks for custom FastCGI path splitting or case-insensitive matching on URL paths, particularly where non-ASCII input may appear.
- Code review focused on modules that decide which files are treated as scripts, looking for assumptions about file extensions, encoding, and case handling.
- Configuration audits to identify deployments where uploaded or user-controlled files are served from directories routed through FastCGI.
- Runtime logging and security monitoring for unexpected execution of non-.php or non-configured extensions via FastCGI endpoints.
- Integrating automated scanning tools that flag inconsistent path normalization, Unicode handling, or case-insensitive matching in routing logic as policy violations.

