Repository navigation
fix(server): keep recipe references inside the collection - #749
Conversation
A menu's references only had `../`-prefixed paths resolved, so
`@./Sub/../../../outside/x{}` and absolute spellings were joined onto
the recipe directory verbatim and the server read files outside it —
disclosing servings/yield through resolved scale factors. Recipe pages
joined the raw reference path too, both for the link and for the
metadata lookup behind it.
`parse_menu` now resolves every reference through `resolve_reference`
(also used by the shopping list and `doctor`), which additionally
refuses absolute paths. A reference that cannot be kept inside keeps
its authored spelling — so `is_outside_reference`, now matching any
`..` component or absolute path, still flags it — and loses its unit so
`resolve_scales` never joins it. Recipe pages resolve their links the
same writer-relative way and skip the metadata lookup for outside
references, as does the menu page.
Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
/is/etc/passwd is absolute on Unix but only root-relative on Windows, where resolve_reference lands it inside the collection as etc/passwd — safe, just not None. Drive-letter paths are the absolute form there.
LeMyst
left a comment
There was a problem hiding this comment.
Thanks for this. Sending every menu reference through resolve_reference closes both bypasses I knew of (@././../x{} reaching us as ./../x, and @./a/../../x{} as a/../../x). The resolve_recipe_info backstop is a good addition. Two things before merging, plus one optional point.
1. Sub-recipes added to the shopping list lose their scale when the reference uses ../
The recipe page now stores the resolved path in data-ref-path (templates/recipe.html:284, :324). For @../Sauces/Pesto{2%servings} written in Mains/Stew.cook, that value is Sauces/Pesto.
reference_scales (src/server/handlers/shopping_list.rs:371-385) still matches against the reference as written: plain() only strips ./ and .cook. So Sauces/Pesto no longer equals ../Sauces/Pesto, the lookup returns None, and the Pesto link on the shopping list opens unscaled. Ingredients are not affected, because core expand resolves both sides.
Comparing resolve_reference(dir, stored) with resolve_reference(dir, written) there, as core does, would fix it. The comment at :371 ("The recipe page stores ./Sauces/Pesto, as the reference is written") would need updating too.
2. is_outside_reference is a second lexical test (Windows gap)
is_outside_reference (src/util/menu.rs:212) checks "absolute, or contains ..". That is not the same rule as resolve_reference returning None. On Windows, a drive-relative reference such as @./C:x.menu{} is refused by is_safe_relative_path, because a component contains :. So parse_menu keeps the authored C:x.menu. But that path is neither absolute nor has .., so GET /api/menus (src/server/handlers/menus.rs:213-215) calls get_recipe(base_path, "C:x.menu"). join with a prefixed path replaces the base, so the lookup happens outside the collection. The menu page and add_menu are covered by the new backstop; this JSON path is not. I found this by reading the code, not by running it on Windows.
Since every path that parse_menu keeps as "inside" came out of is_safe_relative_path, is_outside_reference(p) can just be !cookcli_core::is_safe_relative_path(p). That makes the two rules one, and the cfg(windows) test branches go away.
3. Optional: the absolute-path refusal in resolve_reference
The new early return in crates/core/src/find.rs:64 isn't needed for safety. Empty components were already dropped, so /etc/passwd resolved to etc/passwd inside the collection. It also makes the platforms behave differently: .//Sub/X gives None on Unix but Sub/X on Windows. That's why some tests need cfg gates. It is a behaviour change in a public cookcli-core function, so I'd lean towards dropping it, but it isn't blocking.
Note: #750 and #751 both rewrite find_recipe in recipe_image.rs, so whichever of those lands second needs a rebase. This PR merges cleanly with both.
🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.
The recipe page now stores a reference resolved (`Sauces/Pesto`), so
`reference_scales` no longer matched one written with a step up
(`@../Sauces/Pesto{}`) and the shopping list opened it unscaled.
Resolve both sides against the recipe's directory before comparing, as
core does when it expands the list. Lists stored before still match.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
I pushed 266fd79 to this branch to fix point 1 of my review (sub-recipes losing their scale on the shopping list).
The new test Points 2 ( 🤖 Analysis and changes made by Claude (Anthropic's AI assistant), committed and posted on behalf of @LeMyst. |
…o itself
`is_outside_reference` tested for `..` or an absolute path, a second rule
next to `resolve_reference`. On Windows a drive-relative `@./C:x.menu{}`
is refused by `resolve_reference`, so the menu keeps it as written, but it
is neither absolute nor has `..`: `GET /api/menus` then looked it up, and
joining it onto the recipe directory replaces the directory.
A reference kept inside is what `resolve_reference` gave, and resolving it
again changes nothing; an escape never resolves to itself. Ask that.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
I pushed 0f065d1 for point 2 of my review ( A path now counts as outside unless resolving it from the root gives back the same path: In
The full suite passes locally on Linux; the Windows assertions will run in CI. That leaves point 3 (the absolute-path early return in 🤖 Analysis and changes made by Claude (Anthropic's AI assistant), committed and posted on behalf of @LeMyst. |
…form `resolve_reference` refused a path absolute of its own. That added no safety, since empty components were already dropped and `/etc/passwd` became `etc/passwd` inside the collection, but it split the platforms: a leading `/` is absolute on Unix and not on Windows, so `.//Sub/X` in a menu resolved on one and not the other, and the tests needed cfg gates. Drop the early return. A leading `/` is read from the root everywhere, and a drive prefix on Windows is still refused by `is_safe_relative_path`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
I pushed ca6597e for point 3, the last one from my review. The early return for absolute paths in
The full suite passes locally. 🤖 Analysis and changes made by Claude (Anthropic's AI assistant), committed and posted on behalf of @LeMyst. |
Closes #696
Summary
The server read files outside the recipe directory via references in menu/recipe content. Two holes let the raw authored path reach
base_dir.join(path):parse_menuonly ranresolve_referenceon paths starting../, so@./Sub/../../../outside/Outside{4%servings}was passed through untouched —resolve_scalesthen joined it onto the base dir and read/outside/Outside.cook'sservings, disclosing it as"scale": 0.5714…inGET /api/menus/....reference_pathstraight fromr.componentsand used it both as the link target and as the lookup for servings/yield (ref_info_or_default), so@../Outsideread the wrong file entirely — and/recipe/../xlinks pointed outside the page tree.Changes
parse_menuresolves every reference throughcookcli_core::resolve_reference— the same rule the shopping list anddoctor validatealready apply (..steps up from the writer's directory, everything else reads from the collection root).resolve_referencenow also refuses absolute references, which a menu (not parsed by the recipe parser) can spell.resolve_scalesuses the target as a raw multiplier and never joins the path.is_outside_referencenow flags any..component or absolute path, covering every escape spelling — the API,add_menuand the menu page all reuse it and skip outside lookups.reference_paththe same writer-relative way viareference_link_path:@../OutsideinMains/Stew.cooklinks/recipe/Outside(the in-collection file), and an escaping reference gets no link and no lookup.resolve_recipe_inforefuses non-safe-relative paths as a backstop.Test plan
GET /api/menus/Menus/Evil.menuwith@./Sub/../../../outside/Outside{4%servings}now reports"scale": 4.0(raw multiplier) instead of0.5714from the outside file'sservings: 7;POST /api/shopping_list/add_menuskips the entry, nothing outside is persisted.Mains/Stew.cookwith@../Outside{6%kg}links/recipe/Outside(the collection's own file).find::tests::an_absolute_reference_resolves_to_nothing,util::menu::tests::references_are_confined_to_the_recipe_directory; whole suite green (cargo test --features server).Generated with Devin