Repository navigation
Description
Summary
create_recipe (POST /new) creates the parent directories of the new recipe before checking that they resolve inside the recipe folder, and when the check then fails it runs remove_dir_all(parent) — whether or not that directory existed before the request. If a sub-folder of the collection is a symlink to somewhere else (a NAS share, another repository, Dropbox), creating a recipe inside it deletes the target directory and everything in it.
Affected code (at e65180f)
create_recipe, src/server/ui.rs:
if !parent.exists() {
tokio::fs::create_dir_all(parent).await … // 1. create first
}
match … parent_owned.canonicalize_utf8() … {
Ok(Ok(parent_canonical)) => {
if !parent_canonical.starts_with(&base_canonical) { // 2. check after
// Clean up the created directory if it's outside base_path
let _ = tokio::fs::remove_dir_all(parent).await; // 3. deletes pre-existing content too
return new_page_error(…);The /../ check just above it (L346-L350) can never trigger, because . is already filtered out of the name earlier in the handler.
Introduced by
ad14fa8 (fix: address TOCTOU race condition in create_recipe, merged in #246) — the clean-up branch was added as part of that fix.
Reproduction
mkdir -p /tmp/nas/recipes/Desserts && touch /tmp/nas/recipes/Desserts/Tiramisu.cook
cd ~/recipes && ln -s /tmp/nas/recipes shared
cook serverIn the UI, New Recipe with the name shared/Desserts/Cheesecake, or:
curl -X POST http://127.0.0.1:9080/new -H 'Origin: http://127.0.0.1:9080' \
--data-urlencode 'filename=shared/Desserts/Cheesecake'The page reports "Invalid recipe path" — and /tmp/nas/recipes/Desserts is gone, Tiramisu.cook with it. parent.exists() was true, so nothing was created, but the canonical path /tmp/nas/recipes/Desserts is not under ~/recipes, so the "clean-up" runs.
Impact
Unrecoverable data loss from an ordinary, well-formed request. The CSRF guard keeps it same-origin, so it is not directly cross-site, but the symlinked-sub-folder layout is common (recipes on a NAS, a shared family folder), and any user creating a recipe there hits it.
Suggested fix
- Decide before creating anything: walk to the deepest existing ancestor of the target, canonicalize that, and refuse if it is not under the base.
- Only ever remove what this request created (the first missing component and below), never a directory that already existed — or simply do not clean up on failure, as
create_dir_allleaves nothing harmful behind. - Drop the dead
/../check. - Regression test with a symlinked sub-folder fixture asserting the target survives.
Related
- Path traversal: shopping list API and /recipe, /directory routes read files outside the recipe directory #530 / fix(server): keep request-supplied paths inside the recipe directory #531 — request paths kept inside the recipe directory.
Activity
fix(server): never delete a folder when refusing a new recipe path (c…
fix(server): keep requests inside the collection once links resolve (#…