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 @@