From 2997cf72f0aa05a300c9fa9befb727a256ae4e40 Mon Sep 17 00:00:00 2001 From: Herculino Trotta Date: Fri, 20 Nov 2026 20:13:58 -0300 Subject: [PATCH] fix(charts): embed chart data with json_script instead of |safe Currency, account and category names were rendered into inline or a quote ran as script for every user whose net worth or insights page included it. Fixes GHSA-fh52-f8r4-hv6g --- app/apps/net_worth/tests.py | 39 ++++++++++++++++++- app/apps/net_worth/views.py | 16 ++------ .../category_explorer/charts/account.html | 3 +- .../category_explorer/charts/currency.html | 3 +- app/templates/insights/fragments/sankey.html | 3 +- app/templates/net_worth/net_worth.html | 15 ++++--- 6 files changed, 56 insertions(+), 23 deletions(-) diff --git a/app/apps/net_worth/tests.py b/app/apps/net_worth/tests.py index a733c68b..e35042be 100644 --- a/app/apps/net_worth/tests.py +++ b/app/apps/net_worth/tests.py @@ -1,4 +1,3 @@ -import json import tempfile from datetime import date from decimal import Decimal @@ -58,7 +57,7 @@ class NetWorthCurrencyChartTests(TestCase): response = self.client.get(reverse("net_worth")) self.assertEqual(response.status_code, 200) - chart_data = json.loads(response.context["chart_data_currency_json"]) + chart_data = response.context["chart_data_currency"] datasets = {dataset["label"]: dataset for dataset in chart_data["datasets"]} self.assertIn("US Dollar Consolidated", datasets) regular = datasets["US Dollar"] @@ -121,3 +120,39 @@ class NetWorthCurrencyChartTests(TestCase): ) self.assertContains(response, "75.0%") self.assertContains(response, "25.0%") + + +@override_settings( + STATIC_ROOT=tempfile.gettempdir(), + STORAGES={ + "default": {"BACKEND": "django.core.files.storage.FileSystemStorage"}, + "staticfiles": { + "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage" + }, + }, +) +class NetWorthChartEscapingTests(TestCase): + def test_currency_and_account_names_cannot_break_out_of_the_chart_scripts(self): + user = get_user_model().objects.create_user( + email="xss@example.com", password="password" + ) + payload = "'" + currency = Currency.objects.create(code="XSS", name=payload, prefix="$ ") + account = Account.all_objects.create( + name=payload, currency=currency, owner=user + ) + Transaction.userless_all_objects.create( + account=account, + owner=user, + type=Transaction.Type.INCOME, + amount=Decimal("10"), + date=date(2026, 1, 15), + reference_date=date(2026, 1, 1), + is_paid=True, + ) + + self.client.force_login(user) + response = self.client.get(reverse("net_worth")) + + self.assertEqual(response.status_code, 200) + self.assertNotContains(response, " + {{ chart_data_accounts|json_script:"accountChartData" }} + {{ accounts|json_script:"accountChartAccounts" }} + {{ chart_data_monthly_difference|json_script:"monthlyDifferenceChartData" }}