Skip to content

fix(server): keep pantry edits inside /api/pantry - #666

Merged
LeMyst merged 2 commits into
cooklang:mainfrom
LeMyst:fix/pantry-section-urls
Oct 10, 2026
Merged

LeMyst merged 2 commits into
cooklang:mainfrom
LeMyst:fix/pantry-section-urls

Conversation

@LeMyst

@LeMyst LeMyst commented Oct 10, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #659

Problem

The pantry page built its edit and remove requests as /api/pantry/${section}/${encodeURIComponent(name)}, with section unencoded. A section named ../recipes/Soup.cook# sent an editor's Save to PUT /api/recipes/Soup.cook and Remove to DELETE /api/recipes/…, with the editor's rights. A shopper can create such a section through POST /api/pantry/add, and since #629 through POST /api/pantry/rename. A pantry.conf that arrives through sync or version control can contain one too.

#653 (merged) rewrote the page and now encodes both parts. Encoding does not cover . and .., though: encodeURIComponent leaves dots alone, and the URL parser resolves %2E%2E as .. too. So a section named .. still moves the request out of /api/pantry/, and the server still accepts such sections.

Changes

templates/pantry.html (on top of #653's page)

  • New pantryItemUrl(section, name), used by Save and Remove, encodes both parts.
  • send() refuses a URL with a . or .. segment. The page shows the error banner instead of sending the request.

src/server/handlers/pantry.rs: returns 400 when

  • POST /api/pantry/add gets a section that is . or .., or contains /, \, #, ? or a control character. A section is a TOML table name, so nothing legitimate needs these.
  • POST /api/pantry/add gets an item name that is . or .., or contains a control character. The page encodes names, so salt/pepper or Milk #2 stay allowed.
  • POST /api/pantry/rename gets a new name that breaks the section rule. Both endpoints share is_section_segment.

PUT and DELETE /api/pantry/{section}/{name} stay as they are: they only match items that already exist, and they have to keep working on items in a hand-edited or synced file.

src/web/api_docs.rs, docs/api.md: the new 400s on the add and rename endpoints.

Testing

  • cargo fmt, cargo clippy --all-targets (no warnings), cargo test (1201 passed) on the branch before the rebase onto feat(pantry): list items as rows, like the aisle page #653. New unit tests cover the section and name rules, including renames. CI is green on the rebased head.

  • curl against a debug build: POST /api/pantry/rename with new_name .. or ../recipes/x# → 400, pantry.conf untouched. POST /api/pantry/add with the section from the issue → 400.

  • Manual check in headless Chromium (Playwright 1.63), done on the pantry page before feat(pantry): list items as rows, like the aisle page #653, with a pantry.conf containing the sections from the issue, a .. section, and a legitimate dry goods/spices section:

    • Save → PUT /api/pantry/..%2Frecipes%2FSoup.cook%23/Oregano. Remove → DELETE /api/pantry/..%2Frecipes%2FStew.cook%23/Basil. Both change only the pantry; Soup.cook and Stew.cook are untouched.
    • dry goods/spices still saves, through PUT /api/pantry/dry%20goods%2Fspices/Salt.
    • The .. section sends no request and shows an error.

    Not yet repeated on feat(pantry): list items as rows, like the aisle page #653's page.

🤖 Generated with Claude Code

@LeMyst
LeMyst marked this pull request as ready for review October 10, 2026 15:18
@LeMyst
LeMyst force-pushed the fix/pantry-section-urls branch from 5834d39 to 39066e0 Compare October 10, 2026 16:46
myst and others added 2 commits October 10, 2026 19:06
The pantry page put the section name into its edit and remove URLs
unencoded, so a section such as `../recipes/Soup.cook#` made an
editor's Save overwrite `Soup.cook` and Remove delete a recipe, with
the editor's rights. A shopper could add such a section.

The page now builds those URLs in one place, encoding both parts, and
refuses to send a request for a section or item named `.` or `..`, which
the browser resolves as path steps even when encoded. The server refuses
new sections that are `.` or `..` or contain `/`, `\`, `#`, `?` or
control characters, and item names that are `.` or `..` or contain
control characters.

Closes cooklang#659

Co-Authored-By: Claude Opus 5.5 <[email protected]>
cooklang#629 added POST /api/pantry/rename after this branch was opened, and it
took any new name, so a section could still be renamed to `..` or
`../recipes/Soup.cook#`. The rename now applies the rule a new section
gets when an item is added: no `.` or `..`, no `/`, `\`, `#`, `?` or
control characters, answered with `400`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@LeMyst
LeMyst force-pushed the fix/pantry-section-urls branch from 39066e0 to d554614 Compare October 10, 2026 17:25
@LeMyst
LeMyst merged commit ad7c9a9 into cooklang:main Oct 10, 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.

Pantry section names go into request URLs unencoded: a shopper can make an editor overwrite or delete a recipe

1 participant