Skip to content

fix(server): keep recipe references inside the collection - #749

Merged
LeMyst merged 5 commits into
cooklang:mainfrom
drawmeanelephant:fix/696-server-reference-traversal
Oct 17, 2026
Merged

LeMyst merged 5 commits into
cooklang:mainfrom
drawmeanelephant:fix/696-server-reference-traversal

Conversation

@drawmeanelephant

Copy link
Copy Markdown
Contributor

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_menu only ran resolve_reference on paths starting ../, so @./Sub/../../../outside/Outside{4%servings} was passed through untouched — resolve_scales then joined it onto the base dir and read /outside/Outside.cook's servings, disclosing it as "scale": 0.5714… in GET /api/menus/....
  • The recipe page built reference_path straight from r.components and used it both as the link target and as the lookup for servings/yield (ref_info_or_default), so @../Outside read the wrong file entirely — and /recipe/../x links pointed outside the page tree.

Changes

  • parse_menu resolves every reference through cookcli_core::resolve_reference — the same rule the shopping list and doctor validate already apply (.. steps up from the writer's directory, everything else reads from the collection root). resolve_reference now also refuses absolute references, which a menu (not parsed by the recipe parser) can spell.
  • An unresolvable reference keeps its authored path and drops its unit, so resolve_scales uses the target as a raw multiplier and never joins the path. is_outside_reference now flags any .. component or absolute path, covering every escape spelling — the API, add_menu and the menu page all reuse it and skip outside lookups.
  • Recipe pages resolve reference_path the same writer-relative way via reference_link_path: @../Outside in Mains/Stew.cook links /recipe/Outside (the in-collection file), and an escaping reference gets no link and no lookup.
  • resolve_recipe_info refuses non-safe-relative paths as a backstop.

Test plan

  • Repro: GET /api/menus/Menus/Evil.menu with @./Sub/../../../outside/Outside{4%servings} now reports "scale": 4.0 (raw multiplier) instead of 0.5714 from the outside file's servings: 7; POST /api/shopping_list/add_menu skips the entry, nothing outside is persisted.
  • Mains/Stew.cook with @../Outside{6%kg} links /recipe/Outside (the collection's own file).
  • New tests: 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

drawmeanelephant and others added 2 commits October 13, 2026 09:48
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 LeMyst left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
@LeMyst

LeMyst commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

I pushed 266fd79 to this branch to fix point 1 of my review (sub-recipes losing their scale on the shopping list).

reference_scales (src/server/handlers/shopping_list.rs) now resolves both sides with resolve_reference(dir, …) before comparing, as core does when it expands the list, and still ignores a trailing .cook. So both of these match the reference written in the recipe:

  • the resolved Sauces/Pesto that the recipe page stores with this PR;
  • the ../Sauces/Pesto or ./Sauces/Pesto that lists saved earlier still hold.

The new test a_sub_recipe_written_with_a_step_up_keeps_its_size (tests/shopping_list_titles_test.rs) adds @../Shared/jam{2%servings} and checks both stored forms. It fails without the change ([null] instead of [0.5]). With it, the full suite passes locally, and fmt and clippy are clean.

Points 2 (is_outside_reference) and 3 are still open.


🤖 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]>
@LeMyst

LeMyst commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

I pushed 0f065d1 for point 2 of my review (is_outside_reference).

A path now counts as outside unless resolving it from the root gives back the same path: resolve_reference("", path) != Some(path). Every reference parse_menu keeps inside was rewritten to what resolve_reference returned, and resolving that again changes nothing. An escape keeps its authored path, which never resolves to itself. So the check follows resolve_reference exactly instead of being a second lexical rule. The Windows drive-relative @./C:x.menu{} is now treated as outside, and GET /api/menus no longer looks it up.

In references_are_confined_to_the_recipe_directory:

  • ..\Outside, /abs/Outside and the empty path are now checked on every platform, so the cfg(unix) branch is gone;
  • the drive forms (C:/abs/Outside, C:\abs\Outside and the new C:x.menu) stay Windows-only.

The full suite passes locally on Linux; the Windows assertions will run in CI.

That leaves point 3 (the absolute-path early return in resolve_reference), which is optional.


🤖 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]>
@LeMyst

LeMyst commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

I pushed ca6597e for point 3, the last one from my review.

The early return for absolute paths in resolve_reference is gone. A leading / is now read from the collection root on every platform, as before this PR: /etc/passwd resolves to etc/passwd inside the collection. A Windows drive prefix (C:/…, C:x) is still refused by is_safe_relative_path, and the result is still always a safe relative path.

  • Tests: an_absolute_reference_resolves_to_nothing is replaced by a_leading_slash_is_read_from_the_root. It now runs on all platforms, also covers \\attacker\share\x.cook → attacker/share/x.cook, and keeps the Windows-only drive checks.
  • Comments: the ones in parse_menu and core's shopping_list/mod.rs (which already claimed an absolute path resolves to nothing) now say "names a drive on Windows" instead.

The full suite passes locally. is_outside_reference from 0f065d1 is unaffected: a raw /x still doesn't resolve to itself, so it still counts as outside.


🤖 Analysis and changes made by Claude (Anthropic's AI assistant), committed and posted on behalf of @LeMyst.

@LeMyst
LeMyst merged commit 42bcbe2 into cooklang:main Oct 17, 2026
5 checks passed
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.

Server reads files outside the recipe directory via references in menu/recipe content

2 participants