mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-11-27 10:52:58 +01:00
Merge pull request #700 from eitchtee/GHSA-fh52-f8r4-hv6g
fix(charts): embed chart data with json_script instead of |safe
This commit is contained in:
7 files changed
+73
-32
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import json
|
||||
import re
|
||||
import tempfile
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
@@ -58,7 +58,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"]
|
||||
@@ -72,7 +72,7 @@ class NetWorthCurrencyChartTests(TestCase):
|
||||
self.assertEqual(consolidated["pointHitRadius"], 8)
|
||||
self.assertContains(
|
||||
response,
|
||||
"showOnlyCurrencyDataset('US Dollar Consolidated', 'US Dollar')",
|
||||
'data-dataset="US Dollar Consolidated" data-difference="US Dollar"',
|
||||
html=False,
|
||||
)
|
||||
self.assertContains(
|
||||
@@ -121,3 +121,44 @@ 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 = "</script><script>alert(1)</script>'"
|
||||
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, "<script>alert(1)")
|
||||
# Entities in an attribute are decoded before hyperscript parses it, so
|
||||
# names must never be interpolated into ``_="..."`` code.
|
||||
hyperscript = re.findall(r'\s_="([^"]*)"', response.content.decode())
|
||||
self.assertTrue(hyperscript)
|
||||
self.assertFalse([code for code in hyperscript if "alert(1)" in code])
|
||||
@@ -1,7 +1,4 @@
|
||||
import json
|
||||
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.serializers.json import DjangoJSONEncoder
|
||||
from django.shortcuts import render, redirect
|
||||
from django.utils.translation import gettext
|
||||
from django.views.decorators.http import require_http_methods
|
||||
@@ -148,8 +145,6 @@ def net_worth(request):
|
||||
|
||||
chart_data_currency = {"labels": labels, "datasets": datasets}
|
||||
|
||||
chart_data_currency_json = json.dumps(chart_data_currency, cls=DjangoJSONEncoder)
|
||||
|
||||
monthly_difference_data = calculate_monthly_net_worth_difference(
|
||||
historical_net_worth=historical_currency_net_worth
|
||||
)
|
||||
@@ -178,9 +173,6 @@ def net_worth(request):
|
||||
)
|
||||
|
||||
chart_data_monthly_difference = {"labels": diff_labels, "datasets": diff_datasets}
|
||||
chart_data_monthly_difference_json = json.dumps(
|
||||
chart_data_monthly_difference, cls=DjangoJSONEncoder
|
||||
)
|
||||
|
||||
historical_account_balance = calculate_historical_account_balance(
|
||||
queryset=transactions_account_queryset
|
||||
@@ -213,20 +205,18 @@ def net_worth(request):
|
||||
|
||||
chart_data_accounts = {"labels": labels, "datasets": datasets}
|
||||
|
||||
chart_data_accounts_json = json.dumps(chart_data_accounts, cls=DjangoJSONEncoder)
|
||||
|
||||
return render(
|
||||
request,
|
||||
"net_worth/net_worth.html",
|
||||
{
|
||||
"currency_net_worth": currency_net_worth,
|
||||
"account_net_worth": account_net_worth,
|
||||
"chart_data_currency_json": chart_data_currency_json,
|
||||
"chart_data_currency": chart_data_currency,
|
||||
"currencies": currencies,
|
||||
"chart_data_accounts_json": chart_data_accounts_json,
|
||||
"chart_data_accounts": chart_data_accounts,
|
||||
"accounts": accounts,
|
||||
"type": view_type,
|
||||
"chart_data_monthly_difference_json": chart_data_monthly_difference_json,
|
||||
"chart_data_monthly_difference": chart_data_monthly_difference,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user