From cc7e4e0f7c9b2dc2c7d2dd85579cb78c107224af Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Wed, 11 Nov 2026 18:28:25 +0000 Subject: [PATCH] fix: block stored HTML injection and lint against raw HTML sinks (#9129) --- .github/actions/setup-tools/action.yml | 10 ++ .github/workflows/test-backend.yml | 2 +- .semgrep/xss.py | 54 ++++++++ .semgrep/xss.yml | 39 ++++++ .../Domain/Recipe/RecipeDialogShare.vue | 15 +-- .../components/Domain/Recipe/RecipeList.vue | 13 +- .../RecipePageParts/RecipePageFooter.vue | 3 +- .../Domain/Recipe/RecipePrintView.vue | 31 ++--- .../Domain/Recipe/RecipeScaleEditButton.vue | 6 +- .../components/Domain/Recipe/RecipeYield.vue | 13 +- frontend/app/components/global/DropZone.vue | 11 +- .../app/components/global/SafeInlineHtml.vue | 15 +++ .../app/components/global/SafeMarkdown.vue | 9 +- .../__tests__/use-recipe-ingredients.test.ts | 14 +- .../recipes/use-recipe-ingredients.ts | 21 +-- frontend/app/lib/sanitize/index.ts | 4 + frontend/app/lib/sanitize/inline.test.ts | 41 ++++++ frontend/app/lib/sanitize/inline.ts | 23 ++++ frontend/app/lib/sanitize/markdown.test.ts | 24 ++++ frontend/app/lib/sanitize/markdown.ts | 78 ++++++----- frontend/app/lib/validators/index.ts | 1 + frontend/app/lib/validators/inputs.ts | 13 +- frontend/app/lib/validators/url.test.ts | 24 ++++ frontend/app/lib/validators/url.ts | 17 +++ frontend/app/types/components.d.ts | 2 + .../eslint-rules/check-security-rules.mjs | 18 +++ frontend/eslint-rules/security-rules.test.ts | 100 ++++++++++++++ frontend/eslint-rules/security.mjs | 23 ++++ frontend/eslint.config.mjs | 123 ++++++++++++++---- frontend/package.json | 5 +- frontend/pnpm-lock.yaml | 18 +-- mealie/routes/spa/__init__.py | 51 ++++---- mealie/services/email/email_service.py | 2 +- mise.toml | 3 +- tests/e2e/package.json | 2 +- tests/e2e/pnpm-lock.yaml | 42 +++--- tests/integration_tests/test_spa.py | 34 +++-- .../services_tests/test_email_template.py | 19 +++ 38 files changed, 676 insertions(+), 247 deletions(-) create mode 100644 .semgrep/xss.py create mode 100644 .semgrep/xss.yml create mode 100644 frontend/app/components/global/SafeInlineHtml.vue create mode 100644 frontend/app/lib/sanitize/index.ts create mode 100644 frontend/app/lib/sanitize/inline.test.ts create mode 100644 frontend/app/lib/sanitize/inline.ts create mode 100644 frontend/app/lib/validators/url.test.ts create mode 100644 frontend/app/lib/validators/url.ts create mode 100644 frontend/eslint-rules/check-security-rules.mjs create mode 100644 frontend/eslint-rules/security-rules.test.ts create mode 100644 frontend/eslint-rules/security.mjs create mode 100644 tests/unit_tests/services_tests/test_email_template.py diff --git a/.github/actions/setup-tools/action.yml b/.github/actions/setup-tools/action.yml index 745754fcc..39c388576 100644 --- a/.github/actions/setup-tools/action.yml +++ b/.github/actions/setup-tools/action.yml @@ -12,9 +12,19 @@ inputs: runs: using: composite steps: + # pipx tools install through `uv tool install`, and their venvs link to the Python that uv + # picked. When the runner has no matching system Python (ubuntu-26.04 has none for semgrep), + # uv downloads one to ~/.local/share/uv, which the mise cache does not hold. A later cache hit + # then restores a venv whose interpreter is gone. Keep uv's Pythons inside the cached mise dir. + - name: Keep uv-managed Pythons in the mise cache + shell: bash + run: echo "UV_PYTHON_INSTALL_DIR=${XDG_DATA_HOME:-$HOME/.local/share}/mise/uv-python" >> "$GITHUB_ENV" + - name: Install tools with mise uses: jdx/mise-action@v5 with: + # v1 caches can hold pipx venvs that link to an uncached uv Python (see the step above) + cache_key_prefix: mise-v2 # renovate: datasource=github-releases depName=jdx/mise extractVersion=^v(?.+)$ version: 2026.11.3 install_args: ${{ inputs.tools }} diff --git a/.github/workflows/test-backend.yml b/.github/workflows/test-backend.yml index 123a8af27..bf4019346 100644 --- a/.github/workflows/test-backend.yml +++ b/.github/workflows/test-backend.yml @@ -105,7 +105,7 @@ jobs: run: | mise run py:mypy - - name: Semgrep (authz rules) + - name: Semgrep (authz and XSS rules) if: matrix.Database == 'sqlite' run: | mise run py:semgrep diff --git a/.semgrep/xss.py b/.semgrep/xss.py new file mode 100644 index 000000000..142697800 --- /dev/null +++ b/.semgrep/xss.py @@ -0,0 +1,54 @@ +# Test cases for xss.yml. Run with `mise run py:semgrep`. +import json + +import jinja2 +from fastapi import Response +from fastapi.responses import HTMLResponse +from jinja2 import Environment, Template +from markupsafe import Markup + +from mealie.routes.spa import script_safe_json + + +def templates(source: str): + # ruleid: mealie-jinja-autoescape + Template(source) + + # ruleid: mealie-jinja-autoescape + jinja2.Environment(loader=None) + + # ruleid: mealie-jinja-autoescape + Template(source, autoescape=False) + + # ok: mealie-jinja-autoescape + Template(source, autoescape=True) + + # ok: mealie-jinja-autoescape + Environment(loader=None, autoescape=True) + + +def script_tags(schema: dict, name: str): + # ruleid: mealie-script-tag-json + a = f"""""" + + # ruleid: mealie-script-tag-json + b = f"" + + # ok: mealie-script-tag-json + c = f"""""" + + return a, b, c + + +def responses(name: str): + # ruleid: mealie-html-response-fstring + HTMLResponse(f"

{name}

") + + # ruleid: mealie-html-response-fstring + Response(f"

{name}

", media_type="text/html") + + # ruleid: mealie-html-response-fstring + Markup(f"{name}") + + # ok: mealie-html-response-fstring + Response(f'{{"name": "{name}"}}', media_type="application/json") diff --git a/.semgrep/xss.yml b/.semgrep/xss.yml new file mode 100644 index 000000000..fcc318bbb --- /dev/null +++ b/.semgrep/xss.yml @@ -0,0 +1,39 @@ +rules: + - id: mealie-jinja-autoescape + languages: [python] + severity: ERROR + message: >- + Pass `autoescape=True` to jinja2. Without it every `{{ value }}` is written into the HTML as-is, + so any user text that reaches the template becomes markup. + patterns: + - pattern-either: + - pattern: jinja2.Template(...) + - pattern: jinja2.Environment(...) + - pattern-not: jinja2.Template(..., autoescape=True, ...) + - pattern-not: jinja2.Environment(..., autoescape=True, ...) + + - id: mealie-script-tag-json + languages: [python] + severity: ERROR + message: >- + Serialize data placed inside a ` and ` -
+

{{ $t("recipe-share.scan-qr-code") }}

@@ -243,12 +242,14 @@ async function shareRecipe(token: string) { // ============================================================ // QR Code -const qrCodeSvg = computed(() => qrCodeToken.value ? renderSVG(getTokenLink(qrCodeToken.value)) : ""); +const qrCodeDataUrl = computed(() => qrCodeToken.value + ? `data:image/svg+xml;charset=utf-8,${encodeURIComponent(renderSVG(getTokenLink(qrCodeToken.value)))}` + : ""); async function downloadQrCode() { // Rasterize the SVG so the download opens in any photo viewer const image = new Image(); - image.src = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(qrCodeSvg.value)}`; + image.src = qrCodeDataUrl.value; await image.decode(); const size = 1024; @@ -271,13 +272,9 @@ async function downloadQrCode() { diff --git a/frontend/app/components/Domain/Recipe/RecipeList.vue b/frontend/app/components/Domain/Recipe/RecipeList.vue index 19199e04e..9410c0acb 100644 --- a/frontend/app/components/Domain/Recipe/RecipeList.vue +++ b/frontend/app/components/Domain/Recipe/RecipeList.vue @@ -20,8 +20,7 @@ {{ recipe.description }}
diff --git a/frontend/app/components/global/DropZone.vue b/frontend/app/components/global/DropZone.vue index dfeecac28..807116d59 100644 --- a/frontend/app/components/global/DropZone.vue +++ b/frontend/app/components/global/DropZone.vue @@ -21,6 +21,7 @@ diff --git a/frontend/app/components/global/SafeMarkdown.vue b/frontend/app/components/global/SafeMarkdown.vue index 77f08e26c..5e015a1d7 100644 --- a/frontend/app/components/global/SafeMarkdown.vue +++ b/frontend/app/components/global/SafeMarkdown.vue @@ -1,12 +1,10 @@