Summary
Grav's Form plugin allows attackers to write arbitrary files by abusing the process.save.filename parameter, which is only validated before Twig template rendering.
When user-controlled data with path traversal sequences passes through Twig and is not re-validated, it can result in files such as PHP webshells being written into the web root or other sensitive directories under vulnerable configurations.
Affected context
The vulnerability occurs in Grav installations that use the Form plugin with the process.save feature and dynamic filename values influenced by user input or form data.
Risk is elevated in environments where the application has write permissions to the web root or critical directories and Twig templates are used to construct file paths without performing a second round of path traversal validation after rendering.
Exploit preconditions
- Attack surface: HTTP form submissions to pages using the Grav Form plugin with
process.saveenabled. - Required access: Ability to submit or modify form data (typically unauthenticated or low-privilege web access, depending on site configuration).
- Trigger point: Processing of the
process.save.filenameparameter through Twig templates, followed by file creation or saving. - Why exploitation is possible: Path traversal checks are applied only to the pre-Twig filename value, while Twig rendering can reintroduce traversal sequences and arbitrary paths that are never re-validated before the file is written.
Why this happens
This issue stems from improper neutralization of alternate data streams and file paths when constructing file system locations from user-controlled input, mapping to CWE-73: External Control of File Name or Path.
The Form plugin validates the initial filename parameter for path traversal but then passes it through Twig templates, which can reintroduce attacker-controlled sequences into the rendered path.
Because the rendered filename is not subjected to a second validation step, the application treats Twig output as trusted, allowing externally controlled paths to be used for file writes.
Potential impact
- Attackers may write arbitrary files to directories writable by the web application.
- They could plant PHP webshells or other executable scripts in the web root, leading to remote code execution.
- They can overwrite or tamper with existing application or configuration files, potentially causing denial of service or data corruption.
- They may establish persistent backdoors or modify site content for defacement or phishing.
General mitigation guidance
Mitigation typically focuses on removing external control over file paths and enforcing strict, safe-by-default patterns for file writes.
Common defensive approaches include:
- Ensuring that any filename derived from user input is constrained to an allowed directory and sanitized after all template or macro processing.
- Performing path traversal checks and canonicalization on the final, rendered filename before writing files.
- Avoiding direct use of user-controlled data in path construction; instead, mapping user choices to predefined identifiers or using server-generated names.
- Running the application with least-privilege file system permissions so that it cannot write to the web root or other sensitive directories.
- Designing form-processing pipelines so that templating engines like Twig cannot reintroduce unvalidated data into file paths.
How teams detect issues like this
Detection efforts can focus on both code and runtime behavior:
- Static analysis rules that flag file write operations where paths are derived from HTTP request data or template-rendered values without subsequent validation or canonicalization.
- Code reviews that look for patterns like
process.save.filenameor similar constructs where user input flows into Twig or other templating logic and then into file system APIs. - Security policies that require all file writes to use whitelisted directories and centralized helper functions that enforce path safety.
- Runtime monitoring and file integrity checks to detect unexpected creations or modifications of files in the web root or configuration directories.
- Optional use of automated security scanning tools that can probe form endpoints for path traversal behaviors and unexpected file creation.

