Skip to content

create_recipe deletes a pre-existing directory when the parent resolves outside the recipe folder through a symlink #549

Closed

Description

@LeMyst

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 server

In 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_all leaves nothing harmful behind.
  • Drop the dead /../ check.
  • Regression test with a symlinked sub-folder fixture asserting the target survives.

Related

Activity

  1. added a commit that references this issue on Sep 30, 2026
    2b2b075
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions