Admin plugin "Save As" writes .md files outside the pages tree via unvalidated language code
Description
- Summary
The Grav admin "Save As" action (taskSaveas) builds the destination page-file path from the attacker-controlled POST field data[lang] without validation. determineFilenameIncludingLanguage() embeds the raw lang into <name>.<lang>.md, concatenated onto the page directory and written via RocketTheme Toolbox File::save() / Page::save(). A lang value containing ../ traverses out of the pages tree, and the page content is fully attacker-controlled via preparePage() (frontmatter+content) → an authenticated arbitrary-location write of a .md file with attacker-controlled bytes.
- Affected Version & Scope
Repositories: github.com/
- Recon → Discovery (Methodology)
3a. Recon. NEXT-TARGETS2 CMS seam (Grav = flat-file PHP; admin file-write/upload/YAML/SSTI paths are the classic bug source). Enumerated the admin task* handlers and their file-writing sinks. 3b. Discovery. A sink-sweep found that taskFilesUpload/taskAddmedia/taskDelmedia/taskBackup all run their filename through Utils::checkFilename (which rejects ../slashes/dangerous extensions), but taskSaveas does not. Tracing data[lang] → determineFilenameIncludingLanguage showed the raw lang is embedded into the filename with only an in_array($lang, languages_enabled) check that merely decides suffix-stripping — it does not reject a traversal value. preparePage confirmed the written content is fully attacker-controlled.
- Environment Setup (Reproduce From Zero)
git clone --depth=1 github.com/
serve Grav locally (php -S), create an admin user, log in to /admin, obtain a valid admin-nonce, then send §6.
- Technical Details & Root Cause
// admin/classes/plugin/AdminController.php — taskSaveas() 1728: if (!$this->authorizeTask('save as', ['admin.pages','admin.pages.create','admin.super'])) return false; 1736: $lang = $data['lang'] ?? null; // ATTACKER POST, UNVALIDATED 1746: $this->preparePage($obj, false, $lang); // applies POST frontmatter/content to $obj 1748: $filename = $this->determineFilenameIncludingLanguage($obj->name(), $lang); 1750: $path = $obj->path() . DS . $filename; // pageDir + "/" + <name>.<lang>.md 1751: $aFile = File::instance($path); $aFile->save(); // writes to LITERAL path (no ../ normalization) 1765: $aPage->init(new SplFileInfo($path), $lang.'.md'); ... $aPage->save(); // writes attacker content // determineFilenameIncludingLanguage($current_filename, $language) ~ :2975 $ext='.md'; $filename = substr($current_filename,0,-3); // "default.md" -> "default" $parts=explode('.',trim($filename,'.')); $lang=array_pop($parts); if ($lang === $language) return $filename.$ext; if (in_array($lang,$languages_enabled,true)) $filename=implode('.',$parts); // only strips a real-lang suffix return $filename . '.' . $language . $ext; // raw $language -> "default./../../../../tmp/pwn.md" // preparePage() ~ :41 — attacker controls the ENTIRE page content if (isset($input['frontmatter'],$input['content'])) $page->raw("---\n".(string)$input['frontmatter']."\n---\n".(string)$input['content']); Root cause: lang is used as a filesystem path component with no Utils::checkFilename/allowlist rejection; the only in_array check governs suffix-stripping, not validity. The File/Page save writes to the literal (traversed) path
- Proof of Concept (Embedded Replication Code)
6a. Reproduction — numbered steps Authenticate to /admin as a user with admin.pages.create; obtain a valid admin-nonce. Send the request in §6c against an existing page route. Observed: <pages>/<page>/default./../../../../../../tmp/pwn.md resolves to /tmp/pwn.md, written with the attacker frontmatter+content.