Repository: Mailu/Mailu

Issue:
fix keyError WEBMAIL_ADDRESS
## What type of PR?
bugfix WEBMAIL_ADDRESS not initialized in admin/mailu/configuration.py, leading to lot of errors in log.

## What does this PR do?
Initialize 'WEBMAIL_ADDRESS' to None in the admin configuration

### Related issue(s)
- closes #2125

## Prerequisites
None


Discussion:
Thanks for submitting this pull request.
Bors-ng will now build test images. When it succeeds, we will continue to review and test your PR.

bors try

Note: if this build fails, [read this](http://mailu.io/master/contributors/environment.html#when-bors-try-fails).
## try

Build succeeded!

*And happy new year! 🎉*


  * [CI-Done](https://github.com/Mailu/Mailu/runs/4684075607?check_suite_focus=true)
Thank you for your contribution but I do think that this is the wrong approach.

Let's remove WEBMAIL_ADDRESS from the limiter instead... and make the caller decide whether it's worth calling rate_limit_ip() or not.

https://github.com/Mailu/Mailu/blob/3453d12ccbc2624b45178ddc1332c4e650b2c9ce/core/admin/mailu/internal/views/auth.py#L50 should be replaced to ensure that we only call it if the port isn't one of the webmail's (see https://github.com/Mailu/Mailu/blob/3453d12ccbc2624b45178ddc1332c4e650b2c9ce/core/admin/mailu/internal/nginx.py#L35 on how-to)
Well, that's totally your call @nextgens ! I confess I didn't try to find the best solution but the easiest and fastest one...

Resolving patch:
diff --git a/core/admin/mailu/internal/views/auth.py b/core/admin/mailu/internal/views/auth.py
--- a/core/admin/mailu/internal/views/auth.py
+++ b/core/admin/mailu/internal/views/auth.py
@@ -31,6 +31,7 @@ def nginx_authentication():
     for key, value in headers.items():
         response.headers[key] = str(value)
     is_valid_user = False
+    is_from_webmail = headers['Auth-Port'] in ['10143', '10025']
     if response.headers.get("Auth-User-Exists"):
         username = response.headers["Auth-User"]
         if utils.limiter.should_rate_limit_user(username, client_ip):
@@ -47,7 +48,7 @@ def nginx_authentication():
         utils.limiter.exempt_ip_from_ratelimits(client_ip)
     elif is_valid_user:
         utils.limiter.rate_limit_user(username, client_ip)
-    else:
+    elif not is_from_webmail:
         utils.limiter.rate_limit_ip(client_ip)
     return response
 
diff --git a/core/admin/mailu/limiter.py b/core/admin/mailu/limiter.py
--- a/core/admin/mailu/limiter.py
+++ b/core/admin/mailu/limiter.py
@@ -53,11 +53,10 @@ def should_rate_limit_ip(self, ip):
         return is_rate_limited
 
     def rate_limit_ip(self, ip):
-        if ip != app.config['WEBMAIL_ADDRESS']:
-            limiter = self.get_limiter(app.config["AUTH_RATELIMIT_IP"], 'auth-ip')
-            client_network = utils.extract_network_from_ip(ip)
-            if self.is_subject_to_rate_limits(ip):
-                limiter.hit(client_network)
+        limiter = self.get_limiter(app.config["AUTH_RATELIMIT_IP"], 'auth-ip')
+        client_network = utils.extract_network_from_ip(ip)
+        if self.is_subject_to_rate_limits(ip):
+            limiter.hit(client_network)
 
     def should_rate_limit_user(self, username, ip, device_cookie=None, device_cookie_name=None):
         limiter = self.get_limiter(app.config["AUTH_RATELIMIT_USER"], 'auth-user')


Test patch: