{"id":"CVE-2026-72695","aliases":["GHSA-jq29-c7v8-rg55"],"title":"Grav: Path Traversal in MediaUploadTrait::deleteFile() Allows Arbitrary File Deletion","summary":"Grav: Path Traversal in MediaUploadTrait::deleteFile() Allows Arbitrary File Deletion","severity":"high","cvss":8.1,"cwe":["CWE-22"],"vendor":"getgrav","product":"getgrav/grav","ecosystem":"composer","affected":["getgrav/grav <= 2.0.15"],"patched":["getgrav/grav 2.0.16"],"published":"2026-09-17","updated":"2026-09-17","sourceUpdated":"2026-09-17T20:34:07Z","source":"GHSA","sourceUrl":"https://github.com/advisories/GHSA-jq29-c7v8-rg55","references":[{"url":"https://github.com/getgrav/grav/security/advisories/GHSA-jq29-c7v8-rg55"},{"url":"https://nvd.nist.gov/vuln/detail/CVE-2026-72695"},{"url":"https://www.vulncheck.com/advisories/grav-before-path-traversal-via-mediauploadtrait-deletefile"},{"url":"https://github.com/advisories/GHSA-jq29-c7v8-rg55"}],"tags":["ghsa","composer"],"epss":0.00567,"epssPercentile":0.45535,"ingestedAt":"2026-09-17T21:29:16.995Z","slug":"CVE-2026-72695","body":"## Overview\n\n# Path Traversal in MediaUploadTrait::deleteFile() Allows Arbitrary File Deletion\n\n## Summary\n\nA path traversal vulnerability in `MediaUploadTrait::deleteFile()` allows an authenticated user with media management permissions to delete arbitrary files on the server. The method validates only the basename portion of the filename using `Utils::checkFilename()`, while the directory path (which may contain `../` sequences) is preserved and passed unvalidated to `unlink()`. This enables directory escape from the intended media storage path.\n\n## Severity\n\n**High (8.1)** - CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:N/I:H/A:H\n\n## CWE\n\nCWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')\n\n## Details\n\nIn `system/src/Grav/Common/Media/Traits/MediaUploadTrait.php`, the `deleteFile()` method (lines 332-365) performs filename validation only on the basename, not the full path:\n\n```php\npublic function deleteFile(string $filename, ?array $settings = null): void\n{\n    $settings = $this->getUploadSettings($settings);\n    $filesystem = Filesystem::getInstance(false);\n\n    // Line 339-340: Only the BASENAME is validated\n    $basename = $filesystem->basename($filename);  // e.g. \"evil.jpg\" from \"../../evil.jpg\"\n    if (!Utils::checkFilename($basename)) {         // passes - no traversal in basename\n        throw new RuntimeException(/* ... */);\n    }\n\n    $path = $settings['destination'] ?? $this->getPath();\n    // ...\n\n    // Line 353: Full pathname (with traversal) is preserved\n    $pathname = $filesystem->pathname($filename);   // \"../../\"\n\n    // Line 356-357: Traversal path reconstructed\n    [$base, $ext,,] = $this->getFileParts($basename);\n    $name = \"{$pathname}{$base}.{$ext}\";            // \"../../evil.jpg\"\n\n    // Line 360: Passed to doRemove()\n    $this->doRemove($name, $path);\n}\n```\n\n`doRemove()` (line 521-582) then calls:\n\n```php\n// Line 538\nunlink(\"{$folder}/{$filename}\");\n// e.g. unlink(\"/var/www/grav/user/pages/mypage/../../config/system.yaml\")\n```\n\n`Utils::checkFilename()` (lines 1022-1044) properly checks for `/`, `\\`, and `..`, but it is applied to `$filesystem->basename($filename)` (the last path component only), so traversal sequences in the directory portion are never validated.\n\n### Data flow from user input\n\nThe vulnerability is reachable through the Flex media handling pipeline:\n\n1. `FlexMediaTrait::setUpdatedMedia()` (line 386) iterates form flash data where `$filename` is the array key - user-controlled\n2. For file deletions (`$file` is null, line 396), NO upload validation is performed (the `checkUploadedFile()` call at line 401 only executes when `$file` is truthy)\n3. The raw filename is stored in `$this->_uploads` at line 414\n4. `saveUpdatedMedia()` (line 499) calls `$media->deleteFile($filename, $settings)` with the unsanitized filename\n\n### Sibling: renameFile()\n\nThe same pattern exists in `renameFile()` (lines 374-405) which has even weaker validation - it performs NO `checkFilename()` call at all. While `renameFile()` currently has no callers in the core codebase, it is part of the public `MediaUploadInterface` and should be fixed as defense-in-depth.\n\n## Proof of Concept\n\n**Environment**: Grav CMS 2.0.16 with admin plugin\n\nThe attack requires an authenticated admin user with page/media editing permissions (not super-admin).\n\n1. Create a target file:\n```bash\necho \"DELETE_ME\" > /var/www/grav/user/data/target.txt\n```\n\n2. Submit a Flex object form (e.g. page edit) with a crafted media deletion where the filename key contains path traversal:\n\n```\nPOST /admin/pages/mypage/task:save\nContent-Type: multipart/form-data\n\n# The form flash data includes a media deletion entry with key:\n# \"../../data/target.txt\" -> null (deletion marker)\n```\n\n3. When `saveUpdatedMedia()` processes the deletion queue:\n   - `$filename` = `../../data/target.txt`\n   - `deleteFile(\"../../data/target.txt\")` is called\n   - `$basename` = `target.txt` (passes `checkFilename()`)\n   - `$pathname` = `../../data/`\n   - `$name` = `../../data/target.txt`\n   - `doRemove()` calls `unlink(\"/var/www/grav/user/pages/mypage/../../data/target.txt\")`\n   - Which resolves to `unlink(\"/var/www/grav/user/data/target.txt\")`\n\n4. The file is deleted outside the intended media directory.\n\n### Impact\n\nAn authenticated user with media management permissions can:\n- Delete configuration files (`user/config/system.yaml`, `user/config/security.yaml`)\n- Delete other pages' content files\n- Delete authentication-related files (user account YAML files)\n- Cause denial of service by removing critical application files\n- Potentially escalate privileges by removing security configuration\n\n## Suggested Fix\n\nApply `Utils::checkFilename()` to the full `$filename` parameter before decomposing it, or reject any filename containing directory separators or `..` sequences:\n\n```php\npublic function deleteFile(string $filename, ?array $settings = null): void\n{\n    $settings = $this->getUploadSettings($settings);\n    $filesystem = Filesystem::getInstance(false);\n\n    // Validate the FULL filename, not just the basename\n    if (!Utils::checkFilename($filename)) {\n        throw new RuntimeException(/* ... */);\n    }\n\n    // ... rest unchanged\n}\n```\n\nThe same fix should be applied to `renameFile()` for both `$from` and `$to` parameters.\n\n## References\n\n- Vulnerable file: `system/src/Grav/Common/Media/Traits/MediaUploadTrait.php` lines 332-365, 521-582\n- Caller: `system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php` lines 386-414, 490-499\n- Sibling: `system/src/Grav/Common/Media/Traits/MediaUploadTrait.php` lines 374-405 (renameFile)\n- Related GHSA: GHSA-g6j3-8jv9-ch5f (path traversal in PagesController::batchCopy - different file, same bug class)\n\n## Disclosure\n\nThis vulnerability was discovered using AI-assisted security research tools.\n\n## Affected packages\n\n- `getgrav/grav <= 2.0.15`\n\n## Remediation\n\nUpgrade to a patched release:\n\n- `getgrav/grav 2.0.16`","depth":"twilight","depthScore":45,"depthScoreParts":{"impact":44.6,"likelihood":0.1,"exploitation":0,"ransomware":0},"changes":[]}