Repository navigation
fix(web): stop step picture paths escaping cooking mode's img src - #551
Merged
Merged
Conversation
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
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #548.
What changes
Cooking mode wrote the step picture's path into
src="…"inside aninnerHTMLstring. The escaper went throughtextContent→innerHTML, which leaves"alone. The path is made of folder and file names, which the server never encoded. So a folder namedx" 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 oncook serverand on acook build website alike.The fix works on both ends, and either one alone would stop the reproduction from the issue:
static/js/cooking-mode.jsnow creates the<img>withcreateElementandsetAttribute, so the path is only ever an attribute value.escapeHTMLnow also encodes"and'. The remaining calls put text in element content, but a future attribute use will be safe too.get_image_pathinsrc/web/builders.rsencodes 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.GET /api/recipes/{path}each carried their own copy of the unencoded formatting. They all callget_image_pathnow, so the list, the recipe page, cooking mode and the API report the same URL.Behaviour changes worth a look
/api/static/Breakfast/Easy Pancakes.jpgbecomes/api/static/Breakfast/Easy%20Pancakes.jpg./api/staticdecodes 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-prefixin itsimageURL, as the recipe page and/api/recipe_imagealready did. Behind a prefix, the old URL pointed outside the app.Tests
src/web/builders.rs: the folder name from the issue,#/?/%/', relative paths, andhttp(s)addresses passed through unchanged.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 carriesx" onerror="window.__cookingModeXss = true" y=". It checks that the<img>has onlyalt,classandsrc, thatsrcholds 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.tsand the editor title-picture test intests/e2e/editor.spec.tsnow 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.main'scooking-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-targetsandcargo testpass. The full Playwright suite passes in Chromium, with 153 passed and 5 already-skipped.docs/api.mdis regenerated.🤖 Generated with Claude Code