Skip to content

fix(web): stop step picture paths escaping cooking mode's img src - #551

Merged
LeMyst merged 2 commits into
cooklang:mainfrom
LeMyst:fix/cooking-mode-xss
Sep 26, 2026
Merged

LeMyst merged 2 commits into
cooklang:mainfrom
LeMyst:fix/cooking-mode-xss

Conversation

@LeMyst

@LeMyst LeMyst commented Sep 26, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #548.

What changes

Cooking mode wrote the step picture's path into src="…" inside an innerHTML string. The escaper went through textContent → innerHTML, which leaves " alone. The path is made of folder and file names, which the server never encoded. So a folder named x" onerror="…" y=" in a shared, synced or cloned collection ran script on the server's origin as soon as someone opened cooking mode. That happened on cook server and on a cook build web site alike.

The fix works on both ends, and either one alone would stop the reproduction from the issue:

  • Cooking mode builds the picture as an element. static/js/cooking-mode.js now creates the <img> with createElement and setAttribute, so the path is only ever an attribute value. escapeHTML now also encodes " and '. The remaining calls put text in element content, but a future attribute use will be safe too.
  • Picture URLs are percent-encoded, one path segment at a time. get_image_path in src/web/builders.rs encodes each segment and joins them with /. Folder and file names with #, ? or % used to cut the URL short or be decoded by the server, so those pictures now load too. A Windows path now yields / separators.
  • One function for every picture URL. The recipe list, the menu page and GET /api/recipes/{path} each carried their own copy of the unencoded formatting. They all call get_image_path now, so the list, the recipe page, cooking mode and the API report the same URL.

Behaviour changes worth a look

  • Picture URLs are now encoded everywhere. /api/static/Breakfast/Easy Pancakes.jpg becomes /api/static/Breakfast/Easy%20Pancakes.jpg. /api/static decodes it back, so the same file is served. A client that compared URL strings would see the difference.
  • GET /api/recipes/{path} now includes --url-prefix in its image URL, as the recipe page and /api/recipe_image already did. Behind a prefix, the old URL pointed outside the app.

Tests

  • Unit tests in src/web/builders.rs: the folder name from the issue, #/?/%/', relative paths, and http(s) addresses passed through unchanged.
  • Playwright in tests/e2e/cooking-mode.spec.ts: one test checks that cooking mode shows the encoded picture URL. Another rewrites the page's cooking-mode data so the step picture path carries x" onerror="window.__cookingModeXss = true" y=". It checks that the <img> has only alt, class and src, that src holds the whole string, and that no handler ran once the picture failed to load. The rewrite happens through a route intercept, so no folder with " in its name is needed. Windows cannot create one.
  • tests/e2e/recipe-display.spec.ts and the editor title-picture test in tests/e2e/editor.spec.ts now expect the encoded URL. The editor test used to accept a \ separator for Windows; the URL now always uses /, so it checks the exact string.
  • Against main's cooking-mode.js, the new Playwright test fails the way the issue describes. The picture renders as <img src="/api/static/x" y="/Pancakes.3.jpg" onerror="window.__cookingModeXss = true">.
  • cargo fmt, cargo clippy --all-targets and cargo test pass. The full Playwright suite passes in Chromium, with 153 passed and 5 already-skipped. docs/api.md is regenerated.

🤖 Generated with Claude Code

Cooking mode wrote the step picture path into `src="…"` inside an
innerHTML string, through an escaper (textContent -> innerHTML) that
keeps `"`. The path is made of folder and file names and was never
URL-encoded, so a folder named `x" onerror="…" y="` in a shared or
synced collection ran script on the server's origin once cooking mode
opened. Introduced by a6b0d21 (cooklang#263).

- cooking-mode.js builds the <img> with createElement/setAttribute, and
  escapeHTML now encodes `"` and `'` as well.
- get_image_path percent-encodes each path segment, which also makes
  pictures under names with `#`, `?` or `%` load. The recipe list, menu
  page and GET /api/recipes/{path} had their own unencoded copies; they
  call it now, so the API's `image` also gains --url-prefix.

Closes cooklang#548

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@LeMyst
LeMyst marked this pull request as ready for review September 26, 2026 12:24
The title picture test matched the unencoded URL, and allowed a `\` for
the separator Windows used to produce. get_image_path now percent-encodes
every segment and joins them with `/` on every platform, so it looks for
that exact URL.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@LeMyst
LeMyst merged commit f4cf875 into cooklang:main Sep 26, 2026
6 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.

Stored XSS in cooking mode: step-image path is inserted into innerHTML with an escaper that keeps double quotes

1 participant