Repository navigation
fix(server): keep request-supplied paths inside the recipe directory - #531
Merged
Merged
Conversation
`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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #530.
The problem
Several
cook serverroutes 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}andGET /directory/{*path}pass the URL path straight to the template builders, which join it to the recipe directory. axum percent-decodes it first, so..%2Farrives as../:/recipe/..%2F..%2FWindows%2Fwin.inirenderswin.inias a recipe./directory/C:%2Fwalks the whole drive.\\host\sharemakes 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 aGETresponse, 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 rancheck_patheither. The first returns the ingredients of any.cookfile on disk;/addstores the path in.shopping-list, where it is resolved later.cors::write_guardrefuses cross-origin non-GETs, so a hostile page cannot reach these by default — but any client sending noOriginheader can: a local process, a LAN client when the server runs with--host, or a cross-site page when it runs with--cors-originor--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 justC:\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 complicationRefusing 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.cookwrites@./Shared/Red Beansand meansShared/Red Beans.cookat 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_referenceincookcli-coresettles 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::expandanddoctor::broken_referencesuse it too.add_menunow stores the resolved, root-relative path (Shared/Saucerather than../Shared/Sauce), and entries already sitting in a.shopping-listwritten 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_menuonWeekly Plan.menustores the same paths, andcook doctor validatestill reports every reference valid.Tests
tests/path_traversal_test.rsandtests/shopping_list_path_traversal_test.rsbootcook serveron<tmp>/recipeswith 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 noOriginheader, so the write guard lets them through; they fail againstmain, one of them with the leaked marker in a200response.C:,\\host\share,..\x,x.cook::$DATA), which CI exercises onwindows-latest— andresolve_reference.cargo fmt,cargo clippy --all-targetsandcargo test --workspaceare 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