goauthentik / authentik

The authentication glue you need.
https://goauthentik.io
Other
13.1k stars 872 forks source link

Parsing error while sending notification mails when display name contains square brackets #10197

Closed cypr0 closed 1 month ago

cypr0 commented 3 months ago

Describe the bug If the display name contains a string with square brackets, mails cannot be sent to the mail address associated with the account.

To Reproduce Steps to reproduce the behavior:

  1. Open the admin interface.
  2. Navigate to the "Directory" menu, then to "Users," and modify the display name of any user so that it includes a string enclosed in square brackets. The selected user should be your current admin user, have admin rights or at least the permission to send test emails.
  3. Go to "Events," then to "Notifications Transports," and click on the "Test action" for the "default.email-transport."
  4. A error message should appear indicating that the email delivery failed.

Expected behavior The test email was sent successfully.

Logs

Traceback (most recent call last):
  File "/ak-root/venv/lib/python3.12/site-packages/asgiref/sync.py", line 518, in thread_handler
    raise exc_info[1]
  File "/ak-root/venv/lib/python3.12/site-packages/django/core/handlers/base.py", line 253, in _get_response_async
    response = await wrapped_callback(
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/ak-root/venv/lib/python3.12/site-packages/asgiref/sync.py", line 468, in __call__
    ret = await asyncio.shield(exec_coro)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/ak-root/venv/lib/python3.12/site-packages/asgiref/current_thread_executor.py", line 40, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/ak-root/venv/lib/python3.12/site-packages/asgiref/sync.py", line 522, in thread_handler
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/ak-root/venv/lib/python3.12/site-packages/django/views/decorators/csrf.py", line 65, in _view_wrapper
    return view_func(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/ak-root/venv/lib/python3.12/site-packages/rest_framework/viewsets.py", line 125, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/ak-root/venv/lib/python3.12/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/ak-root/venv/lib/python3.12/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/ak-root/venv/lib/python3.12/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/ak-root/venv/lib/python3.12/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/authentik/rbac/decorators.py", line 39, in wrapper
    return func(self, request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/authentik/events/api/notification_transports.py", line 101, in test
    data={"messages": transport.send(notification)}
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/authentik/events/models.py", line 357, in send
    return self.send_email(notification)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/authentik/events/models.py", line 510, in send_email
    return send_mail(mail.__dict__)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/ak-root/venv/lib/python3.12/site-packages/celery/local.py", line 182, in __call__
    return self._get_current_object()(*a, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/ak-root/venv/lib/python3.12/site-packages/celery/app/task.py", line 411, in __call__
    return self.run(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/ak-root/venv/lib/python3.12/site-packages/celery/app/autoretry.py", line 38, in run
    return task._orig_run(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/authentik/stages/email/tasks.py", line 89, in send_mail
    backend.send_messages([message_object])
  File "/ak-root/venv/lib/python3.12/site-packages/django/core/mail/backends/smtp.py", line 136, in send_messages
    sent = self._send(message)
           ^^^^^^^^^^^^^^^^^^^
  File "/ak-root/venv/lib/python3.12/site-packages/django/core/mail/backends/smtp.py", line 151, in _send
    sanitize_address(addr, encoding) for addr in email_message.recipients()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/ak-root/venv/lib/python3.12/site-packages/django/core/mail/message.py", line 92, in sanitize_address
    raise ValueError(
builtins.ValueError: Invalid address; only <name> <surname>  could be parsed from "<name> <surname> [Admin] <mail@domain.tld>"

Version and Deployment (please complete the following information):

Additional context The email is sent successfully once the square brackets are removed from the display name.

BeryJu commented 3 months ago

This should be fixed in 2024.6 with https://github.com/goauthentik/authentik/pull/9999

authentik-automation[bot] commented 1 month ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.