Merge pull request #834 from Django-CRM/dev

feat(pdf): escape field values in custom invoice templates to prevent…
This commit is contained in:
Ashwin Kumar 2026-10-15 12:43:37 +05:30 committed by GitHub
commit ed7df58d29
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 98 additions and 3 deletions

View file

@ -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

View file

@ -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 = '<b class="x">A&B</b>'
ESCAPED = "&lt;b class=&quot;x&quot;&gt;A&amp;B&lt;/b&gt;"
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 = '<h1 style="color:red">{{invoice.client_name}}</h1>{{invoice.due_date}}'
assert _render(html, invoice) == '<h1 style="color:red">Acme Ltd</h1>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="<p>{{invoice.notes}}</p>",
org=org_a,
)
invoice.notes = '<div style="position:absolute;top:0">PAY TO 9999</div>'
with patch("invoices.pdf.HTML") as html:
generate_invoice_pdf(invoice)
rendered = html.call_args.kwargs["string"]
assert "<div" not in rendered
assert "&lt;div style=&quot;position:absolute;top:0&quot;&gt;" in rendered

View file

@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "django-crm"
version = "1.14.7"
version = "1.14.8"
# The one-line summary PyPI shows under the project title and in search results,
# so it carries the terms people actually search for ("open source CRM",
# "self-hosted CRM", "Django CRM") and names the modules rather than the

View file

@ -681,7 +681,7 @@ wheels = [
[[package]]
name = "django-crm"
version = "1.14.7"
version = "1.14.8"
source = { editable = "." }
dependencies = [
{ name = "cairocffi" },