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.
This commit is contained in:
Kumi 2024-11-16 20:01:42 +01:00
parent 26664be144
commit c3794bd1b6
Signed by: kumi
GPG key ID: ECBCC9082395383F
3 changed files with 11 additions and 2 deletions

View file

@ -11,4 +11,5 @@ email:
password: your_password password: your_password
tls: true tls: true
admin: admin:
email: admin@your.server email: admin@your.server
trust_proxy: false

View file

@ -52,11 +52,17 @@ class EmailInputView(FormView):
return self.form_invalid(form) return self.form_invalid(form)
token = token_urlsafe(32) 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( UserRegistration.objects.create(
username=self.request.session["username"], username=self.request.session["username"],
email=email, email=email,
token=token, token=token,
ip_address=self.request.META.get("REMOTE_ADDR"), ip_address=ip_address,
) )
verification_link = self.request.build_absolute_uri( verification_link = self.request.build_absolute_uri(
reverse_lazy("verify_email", args=[token]) reverse_lazy("verify_email", args=[token])

View file

@ -47,6 +47,8 @@ DEBUG = config.get("debug", False)
ALLOWED_HOSTS = config.get("hosts") ALLOWED_HOSTS = config.get("hosts")
TRUST_PROXY = config.get("trust_proxy", False)
if not ALLOWED_HOSTS: if not ALLOWED_HOSTS:
raise KeyError("Please specify a list of allowed hosts in the configuration file.") raise KeyError("Please specify a list of allowed hosts in the configuration file.")