Skip to content

stages/email: fix sanitization of email addresses #9999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion authentik/stages/email/tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"""Test addresses are correctly parsed"""
message = TemplateEmailMessage(to=[("[email protected]", "[email protected]")])
[sanitize_address(addr, "utf-8") for addr in message.recipients()]
self.assertEqual(message.recipients(), ["[email protected]"])
self.assertEqual(message.recipients(), ['"[email protected]" <[email protected]>'])

Check warning on line 99 in authentik/stages/email/tests/test_templates.py

View check run for this annotation

Codecov / codecov/patch

authentik/stages/email/tests/test_templates.py#L99

Added line #L99 was not covered by tests
message = TemplateEmailMessage(to=[("some-name", "[email protected]")])
[sanitize_address(addr, "utf-8") for addr in message.recipients()]
self.assertEqual(message.recipients(), ["some-name <[email protected]>"])
6 changes: 2 additions & 4 deletions authentik/stages/email/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pathlib import Path

from django.core.mail import EmailMultiAlternatives
from django.core.mail.message import sanitize_address
from django.template.exceptions import TemplateDoesNotExist
from django.template.loader import render_to_string
from django.utils import translation
Expand All @@ -31,10 +32,7 @@ def __init__(
sanitized_to = []
# Ensure that all recipients are valid
for recipient_name, recipient_email in to:
if recipient_name == recipient_email:
sanitized_to.append(recipient_email)
else:
sanitized_to.append(f"{recipient_name} <{recipient_email}>")
sanitized_to.append(sanitize_address((recipient_name, recipient_email), "utf-8"))
super().__init__(to=sanitized_to, **kwargs)
if not template_name:
return
Expand Down
Loading