Skip to content

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

Closed

Description

@LeMyst

Summary

Cooking mode builds each step card with innerHTML, and puts the step image path into src="…" after running it through an escaper that only encodes &, < and > — it uses textContent → innerHTML, which leaves " alone. The path comes from the recipe's folder and file names, which the server never URL-encodes. A folder name containing a double quote therefore breaks out of the attribute and runs script on the server's own origin.

The server-side template renders the same value correctly; only the JavaScript re-render is affected.

Affected code (at e65180f)

function escapeHTML(str) { const div = document.createElement('div'); div.textContent = str; return div.innerHTML; }
…
? '<img class="cooking-step-image" src="' + escapeHTML(card.image) + '" alt="Step ' + card.number + '" />'

Introduced by

a6b0d21 (feat(cooking-mode): add cooking mode JS with carousel, swipe, and wake lock, merged in #263).

Reproduction

In the served folder:

mkdir 'x" onerror="alert(document.domain)" y="'
cd    'x" onerror="alert(document.domain)" y="'
printf 'Mix @flour{1%%cup}.\n\nBake for ~{20%%minutes}.\n' > Pancakes.cook
cp ../Breakfast/*.jpg Pancakes.1.jpg    # any image, named for step 1

Open the recipe in the browser and press c (or click Cook). The card is rendered as

<img class="cooking-step-image" src="/api/static/x" onerror="alert(document.domain)" y="/Pancakes.1.jpg" />

and the alert fires.

A recipe collection that is shared, synced from cook.md, or cloned from a repository is enough to deliver this; the user only has to open cooking mode. cook build web emits the same markup, so the static site is affected too.

Impact

Script runs on the server's origin, so it passes the CSRF write guard and can PUT/DELETE any recipe through the API (and, with #545, any file in the folder), read everything the user can, and keep running in the tab.

Suggested fix

  • Build the <img> with document.createElement + setAttribute (or escape " and ' as well; textContent/innerHTML is not an attribute escaper).
  • Percent-encode each path segment in get_image_path, which also fixes folder names containing #, ? or % not loading their images at all.
  • Regression test: a fixture folder with a " in its name, asserting the rendered src.

Related

Activity

  1. added a commit that references this issue on Oct 3, 2026
    73ac842
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions