Skip to content

fix(server): limit the recipe file endpoints to .cook and .menu files - #550

Merged
LeMyst merged 1 commit into
cooklang:mainfrom
LeMyst:fix/recipe-api-file-types
Sep 26, 2026
Merged

LeMyst merged 1 commit into
cooklang:mainfrom
LeMyst:fix/recipe-api-file-types

Conversation

@LeMyst

@LeMyst LeMyst commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator

Closes #545.

What changes

GET /api/recipes/raw/{path}, PUT /api/recipes/{path} and DELETE /api/recipes/{path} now only ever touch a .cook or .menu file. Before, each of them took the joined path as it was whenever something existed there, so they read, overwrote and deleted config/aisle.conf, .shopping-list, a checkout's .git/config or anything else under the served directory.

  • One resolver for all three. recipe_file in handlers/common.rs runs check_path, then resolves the request path to a recipe or menu file. Pasta.cook and Plan.menu name those files. Pasta is looked up as Pasta.cook, then Pasta.menu. A path naming anything else never resolves to it: config/aisle.conf becomes config/aisle.conf.cook, so raw and delete answer 404 and a save writes a new recipe beside it instead of overwriting it. The three handlers lost their copies of the old lookup, so they cannot drift apart again.
  • No hidden paths. check_path now also refuses any component starting with ., as the issue suggested. That covers every /api route using it, not just these three. It matters beyond raw/save/delete: cooklang-find opens any existing file whose name has an extension, so GET /api/recipes/.env.local (or .config/gh/hosts.yml) used to parse the file and return its lines as recipe steps.
  • The editor page follows the same rules. /edit/{path} now opens a file through the same resolver. It used to open any file cooklang-find would, which showed config/aisle.conf in an editor whose Save could no longer write it back.

Behaviour changes worth a look

  • A new file saved as Week.menu is now created as Week.menu, not Week.menu.cook. The same goes for New.cook, which used to become New.cook.cook. Without an extension it is still created as .cook.
  • A path that names a directory (say Breakfast) now resolves to Breakfast.cook. Before, the raw endpoint tried to read the directory and answered 500.
  • Recipes inside a hidden folder can no longer be read, saved or deleted through the API. cooklang-find's index still lists them, since its glob matches hidden folders, so they show up in the listing but the editor refuses to open them. A .trash/ folder is the realistic case. I think refusing them is the right trade, but it is a visible change.

Not in this PR

These read paths stay open and are better handled separately:

  • /api/static/{path} is a ServeDir over the whole recipe directory, so /api/static/config/aisle.conf and /api/static/.git/config are still served as-is. Limiting it to media types is its own change.
  • Server-rendered /recipe/{path} and the parsed GET /api/recipes/{path} still go through cooklang_find::get_recipe, which accepts any existing file with an extension. The API side now refuses hidden paths, but it still renders a visible file like config/aisle.conf as steps. The recipe page builder was left alone because static-site mode feeds it index paths, which may sit in hidden folders. The clean fix is for get_recipe to accept only .cook/.menu.

Tests

  • tests/recipe_file_api_test.rs boots cook server on a folder holding a recipe and a menu next to config/aisle.conf, config/pantry.conf, .shopping-list, .git/config, .env.local and notes.txt. It checks that raw, PUT, DELETE and the editor page still work on the recipe and the menu and leave every other file unread, unchanged and in place. It also checks that the parsed endpoint refuses .env.local. The requests carry no Origin, like curl from another machine, so the cross-site write guard does not interfere. The server runs through common::with_isolated_config.
  • Unit tests in handlers/common.rs cover the resolver and the hidden-component rule.
  • Against unfixed main (e65180f), all five new integration tests fail. Raw, DELETE and the parsed endpoint answer 200 on the other files, and the editor opens config/aisle.conf. The save test fails first on its new-file check, since Stew.cook is created as Stew.cook.cook, before it reaches the overwrite check.
  • cargo fmt, cargo clippy --all-targets and cargo test pass. docs/api.md is regenerated from the updated API reference.

🤖 Generated with Claude Code

GET /api/recipes/raw, PUT and DELETE /api/recipes took the joined path
as-is whenever something existed there, so they read, overwrote and
deleted any file under the recipe directory: config/aisle.conf,
.shopping-list, a checkout's .git/config. The pattern dates from
423c0c6 (cooklang#246).

All three now resolve the path through one helper, recipe_file, which
only ever returns a .cook or .menu file: `Pasta` is looked up as
Pasta.cook then Pasta.menu, and `config/aisle.conf` resolves to
config/aisle.conf.cook. A new file keeps the extension it is given
(`Week.menu` is no longer created as Week.menu.cook). The editor page
opens files through the same helper.

check_path also refuses any path component starting with a dot, for
every /api route using it. cooklang-find opens any existing file whose
name has an extension, so the parsed recipe endpoint returned
.env.local as recipe steps.

Closes cooklang#545

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@LeMyst
LeMyst marked this pull request as ready for review September 26, 2026 12:08
@LeMyst
LeMyst merged commit 6ff6f9c into cooklang:main Sep 26, 2026
6 checks passed
LeMyst added a commit to LeMyst/cookcli that referenced this pull request Sep 27, 2026
Resolve the edit_page conflict with cooklang#550: keep its recipe_file() lookup,
which already rules out a missing file path, and pass the viewer through
to the error page. Add the sign-in strings to the Italian locale that
arrived with cooklang#543.

Co-Authored-By: Claude Opus 5.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.

Recipe API reads, overwrites and deletes any file under the recipe directory

1 participant