Skip to content

security: enable Jinja2 autoescape to prevent XSS in HTML reprs - #12423

Merged
crusaderky merged 4 commits into
dask:mainfrom
dfgvaetyj3456356-hash:security/fix-jinja2-xss
Jun 6, 2026
Merged

crusaderky merged 4 commits into
dask:mainfrom
dfgvaetyj3456356-hash:security/fix-jinja2-xss

Conversation

@dfgvaetyj3456356-hash

Copy link
Copy Markdown
Contributor

This PR fixes a cross-site scripting (XSS) vulnerability in dask HTML widgets.

CWE: CWE-79 (Cross-site Scripting)
File: dask/widgets/widgets.py

The Jinja2 Environment used for rendering HTML representations (repr_html) did not have �utoescape enabled. User-influenceable strings such as dask graph layer names, task keys, and dependency names were rendered as raw HTML. A malicious actor could craft a dask graph with keys containing HTML/JS payloads (e.g., <img src=x onerror=alert(1)>), and when another user views the object in Jupyter, the payload executes.

Fix:

  • Enabled �utoescape=True on the Jinja2 Environment
  • Added | safe filter to intentionally-HTML template variables (SVG reprs, pandas HTML tables, nested repr_html output)
  • Added tests verifying that <script> payloads are properly escaped

Templates updated:

  • �rray.html.j2: {{ grid | safe }}
  • dataframe.html.j2: {{ data | safe }}
  • highlevelgraph.html.j2: {{ repr_html() | safe }}
  • highlevelgraph_layer.html.j2: {{ svg_repr | safe }}

@github-actions

github-actions Bot commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

Unit Test Results

See test report for an extended history of previous test failures. This is useful for diagnosing flaky tests.

     25 files  ± 0       25 suites  ±0   6h 58m 40s ⏱️ - 6m 22s
 18 638 tests + 3   17 448 ✅ + 2   1 190 💤 + 1  0 ❌ ±0 
378 965 runs  +44  331 717 ✅ +31  47 248 💤 +13  0 ❌ ±0 

Results for commit 487e742. ± Comparison against base commit a0856a0.

♻️ This comment has been updated with latest results.

Signed-off-by: Security Researcher <[email protected]>
@dfgvaetyj3456356-hash

Copy link
Copy Markdown
Contributor Author

Hi maintainers, I checked the remaining failing check. The PR-specific jobs are green (pre-commit, build, docs, codecov, and the main test matrix). The only red job is the nightly upstream lane, and its failures are five dataframe Series.map dtype assertion mismatches under Python 3.14 nightly, e.g. int64 vs int64[pyarrow] and float64 vs double[pyarrow] in dask/dataframe/tests/test_dataframe.py / dask/dataframe/dask_expr/tests/test_collection.py.

That looks unrelated to this Jinja2 autoescape/XSS change, which only touches HTML rendering/templates. Happy to adjust anything in this PR if reviewers spot a direct issue, but I don't think the nightly failure is caused by this patch.

@crusaderky crusaderky mentioned this pull request Jun 5, 2026
1 task
@crusaderky

crusaderky commented Jun 6, 2026 •

Copy link
Copy Markdown
Collaborator

I successfully reproduced the exploit for HighLevelGraph:

Screenshot from 2026-06-06 17-10-54

I cannot see anything that an attacker could use to inject a script in the dask.array HTML repr or in the Layer HTML repr.

I failed to reproduce this for dataframe columns. This doesn't trigger code execution:

image

Impact assessment

LOW.

An arbitrary <script> in a jupyter notebook allows for arbitrary code execution as the user running the notebook.

However, AFAIK you can only visualize HLGs in the jupyter notebook, not in any other UIs (e.g. the dashboard).
It would be very hard for an untrusted attacker (read: one that is not trusted to send pickles or python modules to the victim) to craft a custom layer name. At least I cannot think of any way. However, this is true strictly for Dask proper; third party extensions could offer the ability to name layers for whatever reason.

I've tested all the impacted jinja templates in the PR and it does fix the issue, without introducing regressions:

image

@crusaderky
crusaderky merged commit 9124454 into dask:main Jun 6, 2026
32 of 33 checks passed
@crusaderky

Copy link
Copy Markdown
Collaborator

Thank you!

@crusaderky

crusaderky commented Jun 9, 2026 •

Copy link
Copy Markdown
Collaborator

The PR broke distributed widgets; everything that was consciously building nested structures by injecting HTML from a widget into another widget started outputting text &gt; marks.

More importantly, it broke all third-party widgets that install themselves in dask.widgets.TEMPLATE_PATHS.
Worse, the regression is very hard to spot without a specific test that probes the exact issue where you want HTML from a subwidget. I spotted it in distributed out of sheer luck.

I cannot see a soft way to do a transition.
Reverted in #12451

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.

2 participants