mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-11-22 00:21:50 +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,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@@ -78,10 +78,12 @@
|
||||
<div class="fieldset">
|
||||
<span class="fieldset-legend">{% translate 'Difference' %}</span>
|
||||
<div class="text-base"
|
||||
data-prefix="{{ form.currency_prefix }}"
|
||||
data-suffix="{{ form.currency_suffix }}"
|
||||
_="on input from #id_form-{{ forloop.counter0 }}-new_balance
|
||||
set original_amount to parseFloat('{{ form.current_balance|floatformat:"-40u" }}')
|
||||
then set prefix to '{{ form.currency_prefix }}'
|
||||
then set suffix to '{{ form.currency_suffix }}'
|
||||
then set prefix to @data-prefix
|
||||
then set suffix to @data-suffix
|
||||
then set decimal_places to {{ form.currency_decimal_places }}
|
||||
then call parseLocaleNumber(#id_form-{{ forloop.counter0 }}-new_balance.value)
|
||||
then set new_amount to result
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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') {
|
||||
|
||||
@@ -56,7 +56,7 @@
|
||||
<li>
|
||||
{% if currency.consolidated and currency.consolidated.total_final != currency.total_final %}
|
||||
<a class="cursor-pointer select-auto flex justify-between items-center w-full"
|
||||
_="on click showOnlyCurrencyDataset('{{ currency.currency.name }}', '{{ currency.currency.name }}')">
|
||||
data-dataset="{{ currency.currency.name }}" data-difference="{{ currency.currency.name }}" _="on click showOnlyCurrencyDataset(@data-dataset, @data-difference)">
|
||||
<span
|
||||
class="currency-name text-start font-mono shrink text-ellipsis">{{ currency.currency.name }}</span>
|
||||
<span class="text-end shrink-0">
|
||||
@@ -86,7 +86,7 @@
|
||||
else call localStorage.setItem('net-worth-consolidated-{{ currency.currency.id }}', 'closed') end
|
||||
catch e log e end">
|
||||
<summary class="cursor-pointer text-base-content/60 select-auto flex"
|
||||
_="on click showOnlyCurrencyDataset('{{ currency.currency.name }} {% trans "Consolidated" %}', '{{ currency.currency.name }}')">
|
||||
data-dataset="{{ currency.currency.name }} {% trans "Consolidated" %}" data-difference="{{ currency.currency.name }}" _="on click showOnlyCurrencyDataset(@data-dataset, @data-difference)">
|
||||
<span class="flex justify-between items-center w-full">
|
||||
<span class="text-start shrink">{% trans "Consolidated" %}</span>
|
||||
<span class="text-end shrink-0">
|
||||
@@ -104,7 +104,7 @@
|
||||
{% for component in currency.consolidated.components %}
|
||||
<li class="col-span-3 grid grid-cols-subgrid">
|
||||
<a class="col-span-3 grid grid-cols-subgrid items-center cursor-pointer text-base-content/60 select-auto text-sm"
|
||||
_="on click showOnlyCurrencyDataset('{{ component.currency.name }}', '{{ component.currency.name }}')">
|
||||
data-dataset="{{ component.currency.name }}" data-difference="{{ component.currency.name }}" _="on click showOnlyCurrencyDataset(@data-dataset, @data-difference)">
|
||||
<span class="currency-name text-start font-mono truncate">{{ component.currency.name }}</span>
|
||||
<span class="justify-self-start">
|
||||
{% if component.share is not None %}
|
||||
@@ -128,7 +128,7 @@
|
||||
</ul>
|
||||
{% else %}
|
||||
<a class="cursor-pointer select-auto flex justify-between items-center w-full"
|
||||
_="on click showOnlyCurrencyDataset('{{ currency.currency.name }}', '{{ currency.currency.name }}')">
|
||||
data-dataset="{{ currency.currency.name }}" data-difference="{{ currency.currency.name }}" _="on click showOnlyCurrencyDataset(@data-dataset, @data-difference)">
|
||||
<span class="currency-name text-start font-mono shrink">{{ currency.currency.name }}</span>
|
||||
<span class="text-end shrink-0">
|
||||
<div>
|
||||
@@ -207,7 +207,7 @@
|
||||
{% for account in data.list %}
|
||||
<li>
|
||||
<a class="cursor-pointer select-auto flex justify-between items-center w-full"
|
||||
_="on click showOnlyAccountDataset('{{ account.account.name }}')">
|
||||
data-dataset="{{ account.account.name }}" _="on click showOnlyAccountDataset(@data-dataset)">
|
||||
<span
|
||||
class="account-name text-start font-mono shrink text-ellipsis">{{ account.account.name }}</span>
|
||||
<span class="text-end shrink-0">
|
||||
@@ -237,7 +237,7 @@
|
||||
{% for account in data.list %}
|
||||
<li>
|
||||
<a class="cursor-pointer flex select-auto justify-between items-center w-full"
|
||||
_="on click showOnlyAccountDataset('{{ account.account.name }}')">
|
||||
data-dataset="{{ account.account.name }}" _="on click showOnlyAccountDataset(@data-dataset)">
|
||||
<span class="account-name text-start font-mono shrink">{{ account.account.name }}</span>
|
||||
<span class="text-end shrink-0">
|
||||
<div>
|
||||
@@ -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, {
|
||||
|
||||
Reference in new issue
Block a user