Summary
A scope-validation flaw in the Grav API plugin allows a narrowly scoped API key to bypass authorization checks for Twig processing when the key was created on a super-account context. Under specific Twig configuration settings, saving a page may enable server-side template execution, potentially resulting in remote code execution.
Affected context
The vulnerability occurs in the Grav API plugin's page-saving authorization flow, specifically where Twig content processing is guarded by a super-administrator check rather than the API key's declared scopes. It is relevant when a least-privilege key is minted on a super-account context, Twig content processing is enabled, and editor-based Twig controls are disabled. Plugin releases before the corrected release may be affected.
Exploit preconditions
- Attack surface: Grav API page-writing functionality
- Required access: A valid API key with page-write permission, such as
api.pages.write - Account condition: The key must be associated with a super-account context whose administrative status is accepted by the guard
- Trigger point: A page save that reaches the Twig-content processing authorization check
- Configuration condition:
security.twig_content.process_enabled=trueandeditor_enabled=false - Why exploitation is possible: The authorization path relies on a bare
isSuperAdmin()check and does not consistently enforce the API key'sapi_key_scopes, allowing access beyond the key's intended privilege boundary
Why this happens
The underlying defect is an authorization-scope enforcement failure mapped to CWE-862: Missing Authorization. The page controller checks whether the associated account is a super administrator but fails to verify whether the API key itself has the separate scope required to enable Twig processing. This confuses account-level authority with delegated token authority, permitting a lower-privilege credential to invoke functionality intentionally excluded from its scope. If attacker-controlled or attacker-influenced page content is subsequently processed as Twig, the authorization bypass can become server-side template injection.
Potential impact
- Server-side template injection may occur during page processing.
- Arbitrary server-side code execution could result.
- The Grav application and its stored content may be compromised.
- Confidential data accessible to the application process could be exposed.
- The host environment may be affected according to the application's runtime privileges.
- Attackers could modify content or use the application as a foothold for further activity.
General mitigation guidance
Mitigation generally focuses on enforcing authorization at the API-key scope boundary rather than relying only on the privileges of the account that minted the key.
Defensive patterns include:
- Require the specific administrative scope for any operation that enables or processes Twig content.
- Evaluate token scopes independently from account-level administrator status.
- Apply deny-by-default authorization for sensitive content-processing features.
- Keep server-side template processing disabled unless it is explicitly required and tightly governed.
- Separate content editing permissions from template execution permissions.
- Run the application with least-privilege operating-system and service-account permissions.
- Add authorization tests covering super-account-issued keys with restricted scopes.
How teams detect issues like this
Detection can combine code review, automated analysis, and runtime policy checks.
- Search for authorization branches that use account or role checks without validating delegated token scopes.
- Review controller methods that process page content or toggle template features for missing scope checks.
- Flag security-sensitive operations gated only by
isSuperAdmin()or equivalent identity checks. - Add tests that verify restricted API keys cannot invoke administrative functionality merely because their issuing account is privileged.
- Monitor API activity for page-write requests followed by changes to template-processing settings or unusual server-side template errors.
- Use automated code-review or static-analysis tools to identify inconsistent authorization checks across related endpoints.

