fix(charts): embed chart data with json_script instead of |safe

Currency, account and category names were rendered into inline <script>
blocks with |safe, so a name containing </script> or a quote ran as script
for every user whose net worth or insights page included it.

Fixes GHSA-fh52-f8r4-hv6g
This commit is contained in:
Herculino Trotta committed 2026-11-20 20:13:58 -03:00
1 parent f558d52ac6
commit 2997cf72f0
6 files changed
+56 -23

No files matched your search

+37 -2
View File
@@ -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 = "</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)")
+3 -13
View File
@@ -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,
},
)
@@ -5,9 +5,10 @@
<canvas id="accountChart"></canvas>
</div>
{{ account_data|json_script:"categoryAccountData" }}
<script>
// Get the data from your Django view (passed as JSON)
var accountData = {{ account_data|safe }};
var accountData = JSON.parse(document.getElementById('categoryAccountData').textContent);
function setupAccountChart() {
var chartOptions = {
@@ -5,9 +5,10 @@
<canvas id="currencyChart"></canvas>
</div>
{{ currency_data|json_script:"categoryCurrencyData" }}
<script>
// Get the data from your Django view (passed as JSON)
var currencyData = {{ currency_data|safe }};
var currencyData = JSON.parse(document.getElementById('categoryCurrencyData').textContent);
function setupCurrencyChart() {
var chartOptions = {
+2 -1
View File
@@ -14,6 +14,7 @@
</div>
</div>
{{ sankey_data|json_script:"sankeyData" }}
<script>
var style = getComputedStyle(document.body);
var incomeColor = style.getPropertyValue('--color-success');
@@ -21,7 +22,7 @@
var primaryColor = style.getPropertyValue('--color-primary');
var contentColor = style.getPropertyValue('--color-base-content');
console.log('Sankey colors:', incomeColor, expenseColor, primaryColor, contentColor);
var data = {{ sankey_data|safe }};
var data = JSON.parse(document.getElementById('sankeyData').textContent);
console.log(convertColorToRgba(incomeColor))
function setupSankeyChart(chartId = 'sankeyChart') {
+10 -5
View File
@@ -274,6 +274,8 @@
</div>
</div>
</div>
{{ chart_data_currency|json_script:"currencyChartData" }}
{{ currencies|json_script:"currencyChartCurrencies" }}
<script>
var currencyChart;
@@ -283,8 +285,8 @@
currencyChart.destroy();
}
var chartData = JSON.parse('{{ chart_data_currency_json|safe }}');
var currencies = {{ currencies|safe }};
var chartData = JSON.parse(document.getElementById('currencyChartData').textContent);
var currencies = JSON.parse(document.getElementById('currencyChartCurrencies').textContent);
var ctx = document.getElementById('currencyBalanceChart').getContext('2d');
currencyChart = new Chart(ctx, {
@@ -347,6 +349,8 @@
currencyChart.update('none');
}
</script>
{{ chart_data_accounts|json_script:"accountChartData" }}
{{ accounts|json_script:"accountChartAccounts" }}
<script id="accountBalanceChartScript">
var accountChart;
@@ -356,8 +360,8 @@
accountChart.destroy();
}
var chartData = JSON.parse('{{ chart_data_accounts_json|safe }}');
var accounts = {{ accounts|safe }};
var chartData = JSON.parse(document.getElementById('accountChartData').textContent);
var accounts = JSON.parse(document.getElementById('accountChartAccounts').textContent);
var ctx = document.getElementById('accountBalanceChart').getContext('2d');
accountChart = new Chart(ctx, {
@@ -412,6 +416,7 @@
});
}
</script>
{{ chart_data_monthly_difference|json_script:"monthlyDifferenceChartData" }}
<script id="monthlyDifferenceChartScript">
var monthlyDifferenceChart;
@@ -420,7 +425,7 @@
monthlyDifferenceChart.destroy();
}
var chartData = JSON.parse('{{ chart_data_monthly_difference_json|safe }}');
var chartData = JSON.parse(document.getElementById('monthlyDifferenceChartData').textContent);
var ctx = document.getElementById('monthlyDifferenceChart').getContext('2d');
monthlyDifferenceChart = new Chart(ctx, {