From c3794bd1b6949a665e9cb5090fb182c64586fa6b Mon Sep 17 00:00:00 2001 From: Kumi Date: Sat, 16 Nov 2024 20:01:42 +0100 Subject: [PATCH] feat: Add trust proxy configuration support Introduces a 'trust_proxy' setting in the configuration file to handle client IP addresses correctly. Updates logic in the EmailInputView to extract the client IP based on trust proxy settings, improving flexibility for deployments behind proxies. Relates to handling requests within various network environments. --- config.dist.yaml | 3 ++- src/synapse_registration/registration/views.py | 8 +++++++- src/synapse_registration/synapse_registration/settings.py | 2 ++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/config.dist.yaml b/config.dist.yaml index 943208c..28079d3 100644 --- a/config.dist.yaml +++ b/config.dist.yaml @@ -11,4 +11,5 @@ email: password: your_password tls: true admin: - email: admin@your.server \ No newline at end of file + email: admin@your.server +trust_proxy: false \ No newline at end of file diff --git a/src/synapse_registration/registration/views.py b/src/synapse_registration/registration/views.py index ee30b59..bd6d3e5 100644 --- a/src/synapse_registration/registration/views.py +++ b/src/synapse_registration/registration/views.py @@ -52,11 +52,17 @@ class EmailInputView(FormView): return self.form_invalid(form) token = token_urlsafe(32) + + if not settings.TRUST_PROXY: + ip_address = self.request.META.get("REMOTE_ADDR") + else: + ip_address = self.request.META.get("HTTP_X_FORWARDED_FOR") + UserRegistration.objects.create( username=self.request.session["username"], email=email, token=token, - ip_address=self.request.META.get("REMOTE_ADDR"), + ip_address=ip_address, ) verification_link = self.request.build_absolute_uri( reverse_lazy("verify_email", args=[token]) diff --git a/src/synapse_registration/synapse_registration/settings.py b/src/synapse_registration/synapse_registration/settings.py index 469f13b..856f71a 100644 --- a/src/synapse_registration/synapse_registration/settings.py +++ b/src/synapse_registration/synapse_registration/settings.py @@ -47,6 +47,8 @@ DEBUG = config.get("debug", False) ALLOWED_HOSTS = config.get("hosts") +TRUST_PROXY = config.get("trust_proxy", False) + if not ALLOWED_HOSTS: raise KeyError("Please specify a list of allowed hosts in the configuration file.")