Repository: Mailu/Mailu

Issue:
Sender access fails with void addresses
Some MTA send bounces and or notifications using an empty address `MAIL FROM: <>`; this causes an error in the route `/internal/postfix/sender/access`:

```
[2019-01-18 21:23:32,211] ERROR in app: Exception on /internal/postfix/sender/access/<> [GET]
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/idna/core.py", line 271, in alabel
    ulabel(label)
  File "/usr/lib/python3.6/site-packages/idna/core.py", line 307, in ulabel
    check_label(label)
  File "/usr/lib/python3.6/site-packages/idna/core.py", line 261, in check_label
    raise InvalidCodepoint('Codepoint {0} at position {1} of {2} not allowed'.format(_unot(cp_value), pos+1, repr(label)))
idna.core.InvalidCodepoint: Codepoint U+003C at position 1 of '<>' not allowed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1127, in _execute_context
    context = constructor(dialect, self, conn, *args)
  File "/usr/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 669, in _init_compiled
    param.append(processors[key](compiled_params[key]))
  File "/usr/lib/python3.6/site-packages/sqlalchemy/sql/type_api.py", line 1182, in process
    return process_param(value, dialect)
  File "/app/mailu/models.py", line 30, in process_bind_param
    return idna.encode(value).decode("ascii").lower()
  File "/usr/lib/python3.6/site-packages/idna/core.py", line 361, in encode
    s = alabel(label)
  File "/usr/lib/python3.6/site-packages/idna/core.py", line 273, in alabel
    raise IDNAError('The label {0} is not a valid A-label'.format(label))
idna.core.IDNAError: The label b'<>' is not a valid A-label

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/usr/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/app/mailu/internal/views/postfix.py", line 52, in postfix_sender_access
    localpart, domain_name = models.Email.resolve_domain(sender)
  File "/app/mailu/models.py", line 263, in resolve_domain
    alternative = Alternative.query.get(domain_name)
  File "/usr/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 924, in get
    ident, loading.load_on_pk_identity)
  File "/usr/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 1007, in _get_impl
    return db_load_fn(self, primary_key_identity)
  File "/usr/lib/python3.6/site-packages/sqlalchemy/orm/loading.py", line 250, in load_on_pk_identity
    return q.one()
  File "/usr/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 2954, in one
    ret = self.one_or_none()
  File "/usr/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 2924, in one_or_none
    ret = list(self)
  File "/usr/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 2995, in __iter__
    return self._execute_and_instances(context)
  File "/usr/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3018, in _execute_and_instances
    result = conn.execute(querycontext.statement, self._params)
  File "/usr/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 948, in execute
    return meth(self, multiparams, params)
  File "/usr/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 269, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/usr/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1060, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/usr/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1132, in _execute_context
    None, None)
  File "/usr/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1413, in _handle_dbapi_exception
    exc_info
  File "/usr/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 265, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/usr/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 248, in reraise
    raise value.with_traceback(tb)
  File "/usr/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1127, in _execute_context
    context = constructor(dialect, self, conn, *args)
  File "/usr/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 669, in _init_compiled
    param.append(processors[key](compiled_params[key]))
  File "/usr/lib/python3.6/site-packages/sqlalchemy/sql/type_api.py", line 1182, in process
    return process_param(value, dialect)
  File "/app/mailu/models.py", line 30, in process_bind_param
    return idna.encode(value).decode("ascii").lower()
  File "/usr/lib/python3.6/site-packages/idna/core.py", line 361, in encode
    s = alabel(label)
  File "/usr/lib/python3.6/site-packages/idna/core.py", line 273, in alabel
    raise IDNAError('The label {0} is not a valid A-label'.format(label))
sqlalchemy.exc.StatementError: (idna.core.IDNAError) The label b'<>' is not a valid A-label ...
172.17.0.35 - - [18/Jan/2019:21:23:32 +0000] "GET /internal/postfix/sender/access/%3C%3E HTTP/1.1" 500 291 "-" "Python/3.6 aiohttp/3.5.4"
```


Discussion:

Resolving patch:
diff --git a/core/admin/mailu/internal/views/postfix.py b/core/admin/mailu/internal/views/postfix.py
--- a/core/admin/mailu/internal/views/postfix.py
+++ b/core/admin/mailu/internal/views/postfix.py
@@ -49,5 +49,18 @@ def postfix_sender_login(sender):
 def postfix_sender_access(sender):
     """ Simply reject any sender that pretends to be from a local domain
     """
-    localpart, domain_name = models.Email.resolve_domain(sender)
-    return flask.jsonify("REJECT") if models.Domain.query.get(domain_name) else flask.abort(404)
+    if not is_void_address(sender):
+        localpart, domain_name = models.Email.resolve_domain(sender)
+        return flask.jsonify("REJECT") if models.Domain.query.get(domain_name) else flask.abort(404)
+    else:
+        return flask.abort(404)
+
+
+def is_void_address(email):
+    '''True if the email is void (null) email address.
+    '''
+    if email.startswith('<') and email.endswith('>'):
+        email = email[1:-1]
+    # Some MTAs use things like '<MAILER-DAEMON>' instead of '<>'; so let's
+    # consider void any such thing.
+    return '@' not in email


Test patch: