Skip to content

🛡️ Warracker is an open source, self-hostable warranty tracker to monitor expirations, store receipts, files. You own the data, your rules!

Security: escape names in the notification email HTML part (EMAIL-01)

Omega (QA)d4e6b24a3cb6
diff --git a/backend/notifications.py b/backend/notifications.py
index ec15768..c43795f 100644
--- a/backend/notifications.py
+++ b/backend/notifications.py
@@ -7,6 +7,7 @@ including email notifications, scheduling, and Apprise integration.
"""
import os
+import html
import threading
import time
import atexit
@@ -181,16 +182,26 @@ def format_expiration_email(user, warranties, get_db_connection, release_db_conn
# Ensure base URL doesn't end with a slash
email_base_url = email_base_url.rstrip('/')
-
+
+ # EMAIL-01: the recipient's own first name and product names are
+ # user-controlled strings. Escape them in the HTML part so markup
+ # typed into a name or product renders as text in the mailbox. The
+ # plain text part keeps the raw values. The base URL is escaped
+ # for its href attributes as well; it comes from an admin-written
+ # setting, and escaping keeps a stray quote or ampersand from
+ # breaking out of the attribute.
+ first_name_html = html.escape(str(user['first_name']))
+ email_base_url_html = html.escape(email_base_url, quote=True)
+
# Create both plain text and HTML versions of the email body
text_body = f"Hello {user['first_name']},\\n\\n"
text_body += "The following warranties are expiring soon:\\n\\n"
-
+
html_body = f"""\
<html>
<head></head>
<body>
- <p>Hello {user['first_name']},</p>
+ <p>Hello {first_name_html},</p>
<p>The following warranties are expiring soon:</p>
<table border="1" style="border-collapse: collapse;">
<thead>
@@ -204,9 +215,10 @@ def format_expiration_email(user, warranties, get_db_connection, release_db_conn
for warranty in warranties:
text_body += f"- {warranty['product_name']} (expires on {warranty['expiration_date']})\\n"
+ product_name_html = html.escape(str(warranty['product_name']))
html_body += f"""\
<tr>
- <td style="padding: 8px;">{warranty['product_name']}</td>
+ <td style="padding: 8px;">{product_name_html}</td>
<td style="padding: 8px;">{warranty['expiration_date']}</td>
</tr>
"""
@@ -219,8 +231,8 @@ def format_expiration_email(user, warranties, get_db_connection, release_db_conn
html_body += f"""\
</tbody>
</table>
- <p>Log in to <a href="{email_base_url}">Warracker</a> to view details.</p>
- <p>Manage your notification settings <a href="{email_base_url}/settings-new.html">here</a>.</p>
+ <p>Log in to <a href="{email_base_url_html}">Warracker</a> to view details.</p>
+ <p>Manage your notification settings <a href="{email_base_url_html}/settings-new.html">here</a>.</p>
</body>
</html>
"""
diff --git a/tests/test_email_escaping.py b/tests/test_email_escaping.py
new file mode 100644
index 0000000..4380f60
--- /dev/null
+++ b/tests/test_email_escaping.py
@@ -0,0 +1,53 @@
+"""EMAIL-01: notification emails must escape the recipient's first
+name and product names in the HTML part.
+
+The audit verified live markup in a real captured email produced by
+the app's own sending path. The plain text part keeps the raw
+values; only the HTML part is escaped.
+"""
+from backend.notifications import format_expiration_email
+
+
+def _build_email(monkeypatch, first_name, product_name):
+ monkeypatch.setenv('APP_BASE_URL', 'http://warracker.example')
+ user = {
+ 'first_name': first_name,
+ 'email': '[email protected]',
+ }
+ warranties = [{
+ 'product_name': product_name,
+ 'expiration_date': '2026-12-01',
+ }]
+ msg = format_expiration_email(user, warranties, None, None)
+ parts = {part.get_content_type(): part.get_payload(decode=True).decode()
+ for part in msg.get_payload()}
+ return parts['text/plain'], parts['text/html']
+
+
+def test_first_name_and_product_name_escaped_in_html_part(monkeypatch):
+ _, html_body = _build_email(
+ monkeypatch,
+ '<b>Mal</b>',
+ '<img src=x onerror=alert(1)>',
+ )
+ assert '<b>Mal</b>' not in html_body
+ assert '&lt;b&gt;Mal&lt;/b&gt;' in html_body
+ assert '<img src=x onerror=alert(1)>' not in html_body
+ assert '&lt;img src=x onerror=alert(1)&gt;' in html_body
+
+
+def test_plain_text_part_keeps_raw_values(monkeypatch):
+ text_body, _ = _build_email(
+ monkeypatch, '<b>Mal</b>', 'Acme <Widget>')
+ assert 'Hello <b>Mal</b>,' in text_body
+ assert '- Acme <Widget> (expires on 2026-12-01)' in text_body
+
+
+def test_quotes_escaped_in_greeting_and_product(monkeypatch):
+ _, html_body = _build_email(
+ monkeypatch,
+ 'Ann "Annie"',
+ "Mom's Laptop",
+ )
+ assert 'Ann &quot;Annie&quot;' in html_body
+ assert 'Mom&#x27;s Laptop' in html_body