Skip to content

fix(server): keep request-supplied paths inside the recipe directory - #531

Merged
dubadub merged 2 commits into
cooklang:mainfrom
LeMyst:fix/shopping-list-path-traversal
Sep 24, 2026
Merged

dubadub merged 2 commits into
cooklang:mainfrom
LeMyst:fix/shopping-list-path-traversal

Conversation

@LeMyst

@LeMyst LeMyst commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

Closes #530.

The problem

Several cook server routes hand a client-supplied path to the filesystem without checking it, so .., an absolute path, a drive letter (C:) or a UNC share (\\host\share) reaches files outside the recipe directory. Two halves, one bug.

GET /recipe/{*path} and GET /directory/{*path} pass the URL path straight to the template builders, which join it to the recipe directory. axum percent-decodes it first, so ..%2F arrives as ../:

  • /recipe/..%2F..%2FWindows%2Fwin.ini renders win.ini as a recipe.
  • /directory/C:%2F walks the whole drive.
  • On Windows, \\host\share makes the server connect to that host, which is handed the user's NTLM hash before anything is read.

These are GETs, and the default CORS policy lets any origin read a GET response, so any page the user visits can ask for them. That is the serious half.

The shopping list endpoints (POST /api/shopping_list, /api/shopping_list/add, /api/shopping_list/add_menu) never ran check_path either. The first returns the ingredients of any .cook file on disk; /add stores the path in .shopping-list, where it is resolved later. cors::write_guard refuses cross-origin non-GETs, so a hostile page cannot reach these by default — but any client sending no Origin header can: a local process, a LAN client when the server runs with --host, or a cross-site page when it runs with --cors-origin or --no-csrf-check.

The fix

One lexical check, is_safe_relative_path, run wherever a request-supplied path is about to be joined to the recipe directory. Every component has to be a plain name: .. climbs out, and a root, a drive letter or a UNC prefix makes the join drop the directory altogether (base.join("C:\\x") is just C:\x). On Windows a : is refused in any component as well — no file name can hold one, so past a drive letter it could only name an NTFS alternate data stream. The check never touches the filesystem, on purpose: looking a UNC path up is itself the leak.

The three copies of that check that had grown up (handlers::common, handlers::recipes, edit_page) now call the single definition.

The ../ reference complication

Refusing every path that is not plainly relative breaks something. Cooklang allows @../sauces/tomato{}, and the parser keeps .. in the reference's components. Today every reference is resolved from the collection root, and the seed depends on that: Breakfast/Mexican Style Burrito.cook writes @./Shared/Red Beans and means Shared/Red Beans.cook at the root, not a file beside itself. The same root-relative rule applied to .. means such a reference always lands outside the collection, whoever wrote it — so today it either does not work or reads outside.

find::resolve_reference in cookcli-core settles it: a reference containing .. steps up from the directory of the file that writes it, and anything else is still read from the root, exactly as before. The result is normalised, and one that still climbs above the root — or that normalises to something the check refuses, since a recipe is free to write @./C:/Windows/win.ini{} — resolves to nothing and is skipped with a warning, the same way a reference nested too deep already is. Expansion::expand and doctor::broken_references use it too.

add_menu now stores the resolved, root-relative path (Shared/Sauce rather than ../Shared/Sauce), and entries already sitting in a .shopping-list written before this are skipped during aggregation, so an old list cannot still read outside the directory.

Nothing that works today changes: the seed's nested ./Shared/... references resolve as before, add_menu on Weekly Plan.menu stores the same paths, and cook doctor validate still reports every reference valid.

Tests

  • tests/path_traversal_test.rs and tests/shopping_list_path_traversal_test.rs boot cook server on <tmp>/recipes with a recipe sitting next to that directory rather than in it, and check each route both refuses to reach it and still serves what is inside. The shopping list tests send no Origin header, so the write guard lets them through; they fail against main, one of them with the leaked marker in a 200 response.
  • Unit tests cover the check — including the Windows spellings (C:, \\host\share, ..\x, x.cook::$DATA), which CI exercises on windows-latest — and resolve_reference.
  • cargo fmt, cargo clippy --all-targets and cargo test --workspace are clean.

Known gap, deliberately left out

The recipe and menu pages still build reference links from the reference as written. Browsers normalise ./ away, so those links work; for a ../ reference the browser resolves the link above /recipe/, so it is broken. No path from those links reaches the filesystem unchecked. Fixing it means four near-identical spots covered by snapshot tests, which do not belong in a security change — follow-up.

🤖 Generated with Claude Code

LeMyst and others added 2 commits September 23, 2026 20:23
`recipe_page` and `recipes_directory` handed the URL path straight to the
template builders, which join it to the recipe directory. Neither ran the
`check_path` the API routes run, so `..`, an absolute path, a drive letter
or a UNC share reached anything on the disk:
`/recipe/..%2F..%2FWindows%2Fwin.ini` rendered win.ini as a recipe,
`/directory/C:%2F` walked the whole disk, and on Windows `\host\share`
handed that host the user's NTLM hash. A `GET` is readable from any origin
under the default CORS policy, so any web page could ask for one.

The check now has a single definition, `util::is_safe_relative_path`, and
runs in both builders, where the path is actually joined. It is purely
lexical on purpose: looking a UNC path up on disk is itself the leak. On
Windows it also refuses `:` in a component — no file name can hold one, so
past a drive letter it could only name an NTFS alternate data stream. The
three copies that had grown (`handlers::common`, `handlers::recipes`,
`edit_page`) now call it.

Co-Authored-By: Claude Opus 5 <[email protected]>
`POST /api/shopping_list`, `/api/shopping_list/add` and
`/api/shopping_list/add_menu` each took a recipe path from the request
body and handed it to `cooklang-find`, which joins it to the directory
the server was started on and asks the filesystem. None of them ran the
`check_path` the URL routes run, so `..`, an absolute path, a drive
letter or a UNC share reached anything on the disk: a request naming
`../secret` came back 200 with that recipe's ingredients, and on Windows
`\host\share` handed the host the user's NTLM hash before anything was
read. `add` stored what it was given, so an escape could also be parked
in `.shopping-list` and resolved later, by the page posting it back or
by the compact walking it.

`cors::write_guard` refuses a cross-origin write, so a page on another
site could not reach these — but a client sending no `Origin` at all
can, and that is every local process, every LAN client under `--host`,
and a cross-site page under `--cors-origin` or `--no-csrf-check`.

The check cannot be applied to a recipe *reference*, though, because
cooklang lets one step up: `@../Shared/Sauce{}`. So resolution is fixed
first, in `find::resolve_reference`. A reference that steps up does so
from the directory of the file writing it; one that does not is read
from the recipe directory. The second half is not new — `./` has always
meant the root here, and the seed relies on it, `Breakfast/Mexican Style
Burrito.cook` writing `@./Shared/Red Beans` and meaning the one at the
root. The first half had no working meaning at all before: `..` was
resolved from the root like everything else, so `../Shared/Sauce` landed
*beside* the collection rather than in it. Now it resolves, and what
still climbs out names no recipe in the collection and is skipped with a
warning, the way a reference nested too deep or leading back into its
own chain already was.

`add_menu` stores the resolved path rather than the authored spelling,
so `.shopping-list` holds paths the rest of the server can use without
resolving a reference a second time. `included_references` is matched
resolved on both sides, which is what the old `./`-stripping did and
goes on holding for a `../` reference stored resolved.

`is_safe_relative_path` moves to `cookcli-core` beside the resolver,
which needs the same rule — a reference is free to spell
`@./C:/Windows/win.ini{}` — and is re-exported from `crate::util` so the
CLI call sites are unchanged. `doctor::broken_references` resolves the
same way, so a `../` reference that lands inside the collection is no
longer reported broken.

Known gap, unchanged by this and pre-existing: the recipe and menu pages
still spell a reference link as authored, so a `../` reference renders a
link the browser resolves above `/recipe/`. That is display only — no
path from it reaches the filesystem unchecked.

Co-Authored-By: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Path traversal: shopping list API and /recipe, /directory routes read files outside the recipe directory

2 participants