diff --git a/backend/invoices/pdf.py b/backend/invoices/pdf.py index ff9f74fe..57257281 100644 --- a/backend/invoices/pdf.py +++ b/backend/invoices/pdf.py @@ -12,6 +12,7 @@ from urllib.parse import unquote, urlparse from django.conf import settings from django.template.loader import render_to_string +from django.utils.html import escape try: from weasyprint import CSS, HTML @@ -662,6 +663,10 @@ def render_invoice_template(template_html, context): Render a custom invoice template with context variables. Supports basic variable substitution like {{invoice.invoice_number}}. + Every value is HTML-escaped, as the default template's autoescape does: + the template is admin-authored, but the values are written by any member + who may create an invoice, and raw markup in them could restyle or cover + the admin's layout in the PDF. """ invoice = context.get("invoice") org = context.get("org") @@ -707,7 +712,7 @@ def render_invoice_template(template_html, context): replacements[f"{{{{invoice.{field}}}}}"] = getattr(invoice, field, "") or "" for placeholder, value in replacements.items(): - template_html = template_html.replace(placeholder, str(value)) + template_html = template_html.replace(placeholder, escape(value)) return template_html diff --git a/backend/invoices/tests/test_custom_template_escaping.py b/backend/invoices/tests/test_custom_template_escaping.py new file mode 100644 index 00000000..ddb95a5b --- /dev/null +++ b/backend/invoices/tests/test_custom_template_escaping.py @@ -0,0 +1,90 @@ +"""Field values are escaped when they fill a custom invoice template. + +Only admins may write `template_html`, but any member who may create an invoice +writes the values that fill it. `render_invoice_template` pasted those values +in raw, so a member could put markup in `notes` that restyled or covered the +admin's layout in the PDF a client receives, such as the bank details or the +totals (issue #825). The default template already escaped the same fields +through Django's autoescape; custom templates now match it. +""" + +from datetime import date +from unittest.mock import patch + +import pytest + +from accounts.models import Account +from invoices.models import Invoice, InvoiceTemplate +from invoices.pdf import generate_invoice_pdf, render_invoice_template + +MARKUP = 'A&B' +ESCAPED = "<b class="x">A&B</b>" + +MEMBER_FIELDS = [ + "invoice_number", + "invoice_title", + "client_name", + "client_email", + "client_phone", + "notes", + "terms", + "billing_address_line", + "billing_city", + "billing_state", + "billing_postcode", + "billing_country", + "client_address_line", + "client_city", + "client_state", + "client_postcode", + "client_country", +] + + +@pytest.fixture +def invoice(org_a): + account = Account.objects.create(name="Acme", org=org_a) + return Invoice.objects.create( + invoice_title="March work", + client_name="Acme Ltd", + due_date=date(2026, 11, 14), + account=account, + currency="USD", + org=org_a, + ) + + +def _render(template_html, invoice): + return render_invoice_template( + template_html, {"invoice": invoice, "org": invoice.org} + ) + + +@pytest.mark.django_db +class TestCustomTemplateEscaping: + @pytest.mark.parametrize("field", MEMBER_FIELDS) + def test_member_written_field_is_escaped(self, invoice, field): + setattr(invoice, field, MARKUP) + assert _render(f"[{{{{invoice.{field}}}}}]", invoice) == f"[{ESCAPED}]" + + def test_org_name_is_escaped(self, invoice, org_a): + org_a.name = MARKUP + assert _render("[{{org.name}}]", invoice) == f"[{ESCAPED}]" + + def test_the_templates_own_markup_and_plain_values_are_unchanged(self, invoice): + html = '

{{invoice.client_name}}

{{invoice.due_date}}' + assert _render(html, invoice) == '

Acme Ltd

2026-11-14' + + def test_the_pdf_renderer_receives_the_escaped_value(self, invoice, org_a): + """The report's payload, end to end through `generate_invoice_pdf`.""" + invoice.template = InvoiceTemplate.objects.create( + name="Custom", + template_html="

{{invoice.notes}}

", + org=org_a, + ) + invoice.notes = '
PAY TO 9999
' + with patch("invoices.pdf.HTML") as html: + generate_invoice_pdf(invoice) + rendered = html.call_args.kwargs["string"] + assert "