Summary
Grav can interpret attacker-controlled image metadata and file names as Twig template syntax when rendering media. A media-only account or anonymous uploader may be able to inject content that runs for page visitors, potentially exposing unpublished content or serving scripts to visitors, including super admins.
Affected context
The issue affects Grav installations using versions from 2.0.0 up to, but not including, 2.2.18. It arises when media metadata—such as image titles, alt text, class or id attributes, or file names—is processed as Twig rather than treated solely as data. The risk depends on whether an attacker can submit or change media and whether the resulting content is rendered in pages.
Exploit preconditions
- Attack surface: Media upload and the rendering of uploaded media in pages
- Required access: A media-only account or, where enabled, anonymous upload access
- Trigger point: A page renders media containing attacker-controlled metadata or a file name
- Why exploitation is possible: Attacker-supplied Twig delimiters can be interpreted during rendering, allowing injected template content to affect visitors
Why this happens
The underlying bug is that untrusted media fields are treated as template syntax instead of inert data. When user-controlled values reach a Twig evaluation or rendering context without appropriate handling, an attacker may influence the output seen by other users. This is associated with CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting').
Potential impact
- Injected scripts could run in visitors' browsers in the context of the Grav site.
- Attackers could target privileged visitors, including super admins.
- Unpublished page content could be exposed.
- Site content or visitor sessions could be affected, depending on application behavior and browser protections.
General mitigation guidance
Defensive approaches focus on keeping untrusted media data separate from template logic:
- Treat titles, alt text, attributes, and file names as data; avoid evaluating user-controlled values as templates.
- Apply context-appropriate output encoding and strict validation when rendering user-supplied fields.
- Prefer secure-by-default template patterns that do not permit dynamic template evaluation of uploaded content.
- Limit who can upload media and reduce anonymous upload capabilities where they are not needed.
- Use least-privilege permissions for accounts that manage media, and consider browser-side defenses such as a restrictive Content Security Policy as an additional layer.
How teams detect issues like this
- Use static analysis to identify dynamic template evaluation or untrusted values passed into Twig rendering paths.
- Review media upload and rendering code for user-controlled metadata or file names used in template contexts.
- Test rendered pages with representative untrusted media fields and verify output remains data rather than executable template content.
- Enforce policies that prohibit evaluating user-supplied content as templates, and include these checks in code review and CI workflows.

