Repository navigation
fix(server): keep requests inside the collection once links resolve - #751
Conversation
LeMyst
left a comment
There was a problem hiding this comment.
Thanks, this is careful work. The endpoints it touches hold up:
- raw/save/delete/edit/
plan_editthroughrecipe_file /api/recipes/{p},/api/menus/{p},add_menu,recipe_image- the
/recipepage /api/static
A symlinked recipe root is fine, because it is canonicalized at startup, and links that stay inside the collection still work. Some points remain, plus one decision for the maintainers.
1. Missed route, reachable by guests
POST /api/shopping_list (src/server/handlers/shopping_list.rs:60) only runs check_path, then extract_ingredients, and core's parse_source → find::get_recipe follows the link. The route is in OPEN_WRITES (src/server/auth/middleware.rs:25), so a signed-out guest can post [{"recipe":"Linked/Secret.cook"}] and get the outside recipe's ingredients back. Paths stored through /api/shopping_list/add (also check_path only) are followed later by the page and by compaction.
2. Listings and search still follow links
cooklang-find walks the tree with glob, which follows symlinked directories. So /api/recipes, /directory/Linked, /api/search, the feeds and /api/stats still list or read files outside the collection. Search hits reveal which outside files contain a word. It's also inconsistent for users: those recipes still appear in the index and in search, but their thumbnails now 404 and opening one fails.
3. Reads reveal whether a file exists
recipes.rs:136 (and the same pattern in menus.rs, recipe_image find_recipe and add_menu) answers 400 with a specific message. So GET /api/recipes/Linked/<guess>.cook returns 400 if the outside file exists and 404 if not. #698 asked for 403/404, and /api/static already answers a plain 404. Answering 404 "Recipe not found" for reads, and keeping 400 for writes as the rename does, would close that.
4. Two copies of the check
src/web/builders.rs:393 re-implements it with canonicalize and fails closed. inside_collection (common.rs:71) uses canonical_or_nearest and fails open when canonicalizing errors. One helper in util would give one rule. (inside_collection also re-canonicalizes base_path on every call although it is already canonical. That's cheap, just noting it.)
5. Decision for maintainers: no opt-out
Someone with Shared -> ~/Dropbox/recipes loses all of those recipes in the server, while cook build still follows the link. That probably needs a flag (e.g. --follow-symlinks) or at least a release note.
Tests
The tests are properly gated on unix and use with_isolated_config. There is no coverage yet for menus, add_menu, /edit, plan_edit or the shopping list.
This conflicts with #750 in recipe_image.rs find_recipe, since both rewrite it. The resolved order would be check_path → extension check (404) → get_recipe → inside_collection. It merges cleanly with #749.
🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.
3f2f10f to
3abdb85
Compare
|
Reworked along all of these lines:
On the opt-out question: I kept this strict with no |
|
Thanks for the rework. I checked it on 3abdb85 against a real collection containing:
I ran it as a guest with no users file, and again with a Most of it holds now:
The shopping list still reads outside the collection, in three ways: 1. A name without
|
3abdb85 to
3fd8807
Compare
|
On 3fd8807, rebased past merged #750 — all three holes are closed now:
On the wording: the refusal now surfaces as core's New tests in |
|
Thanks, the shopping list is closed now. On 3fd8807:
A few places still read outside the collection. All of them do the same on
The CLI still follows links ( fmt, clippy and 🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst. |
3fd8807 to
dd86455
Compare
|
All four on dd86455:
Three new server tests ( Also added the docs line: |
The path checks are lexical, so `Link/Secret.cook` through a `Link -> /outside` symlink passed them and the handlers that then opened the file — raw/save/delete, the editor, the menu and recipe pages, `/api/recipe_image`, and `ServeDir` under `/api/static` — read, wrote or removed whatever the link pointed to. The rename already refused this (#549 covered creation); the rest now check the canonical path too. `handlers::common::inside_collection` canonicalizes the nearest existing ancestor, so it covers files that do not exist yet; `recipe_file` refuses there for every caller, the `cooklang-find` entry lookups check the resolved path, and `/api/static`'s middleware joins the decoded request path and checks it before `ServeDir` follows any link. A link that stays inside the collection still resolves; `cook build` keeps following links out, since there the collection and the links are the user's own. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
dd86455 to
167f765
Compare
Closes #698
Summary
Every request-path check in the server is lexical (
check_path/is_request_path), soLink/Secret.cookthrough aLink -> /outsidesymlink passed them. Two places then reached the file the link pointed
to: the handlers that opened the resolved path, and
cooklang-find'sglob walk, which follows linked folders — so listings, search, feeds and
stats would also publish names (and contents) from outside the
collection.
Changes
util::inside_collection(base, path)canonicalizespath's nearestexisting ancestor (so it also works for files a write would create)
and compares it to the canonical base — one helper now, used by the
API handlers,
build_recipe_template,/api/staticand the existingPOST /new/ rename paths (canonical_or_nearestmoved toutilbeside it).
GET /api/recipes,/api/menus,/api/recipe_imageand the recipe page return the same404 "… not found: …: Invalid recipe path: …"bodyget_recipeproduces for a missing name, so an outside link reveals nothing.
GET /api/recipes/rawlikewise collapses "not there" and "not inside"into one 404 (the lexical
check_path400 for e.g. dotfiles is kept —it never depends on what exists).
delete, picture upload, plan edits and
/api/shopping_list/add.POST /api/shopping_listis guest-reachable, so each entry isrefused before it is read;
/api/shopping_list/addrefuses at thedoor rather than storing a path later reads would have to defend
against, and
aggregate_current_ingredient_namesskips stored pathsthat resolve outside (a
.shopping-listwritten before this fix canstill hold one).
build_recipes_templateprunes tree nodes that resolve outside when!static_mode(cook buildkeeps following links — there thecollection and the links are the user's own), and
all_recipes,list_menus,stats, the random-recipe pick, feeds and/api/searchapply the same pruning/filtering.
find_todays_menufilters its hitthe same way on the server.
Aliasdir or file link —still resolve: the check is on the canonical path, not on whether a
link is involved.
Test plan
tests/server_symlinks_test.rs(cfg(unix)):/api/recipes,/api/recipes/raw,/api/recipe_imageand/api/static; the recipe page shows the same "Recipe not found" pageas a missing file.
PUT/DELETEthrough a linked dir still answer 400 and changenothing outside.
GET /api/menus/…→ 404;POST /api/plans/…andPOST /api/shopping_list/add_menu→ 400.POST /api/shopping_listrefuses a linked recipe with the same statusas a missing one;
addrefuses 400./api/recipes,/api/menus,/api/search,/api/statsand the/directory/Linkedpage publish nothing that resolves outside.Full
cargo test --all-featuresgreen.Generated with Devin