Skip to content

Commit 22f241a

Browse files
committed
Rename X-Forwarded- settings and add type annotation for env var support
1 parent 7ac2a43 commit 22f241a

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

plain/plain/http/request.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def host(self) -> str:
157157
property can safely return the host without any validation.
158158
"""
159159
# We try three options, in order of decreasing preference.
160-
if settings.USE_X_FORWARDED_HOST and ("HTTP_X_FORWARDED_HOST" in self.meta):
160+
if settings.HTTP_X_FORWARDED_HOST and ("HTTP_X_FORWARDED_HOST" in self.meta):
161161
host = self.meta["HTTP_X_FORWARDED_HOST"]
162162
elif "HTTP_HOST" in self.meta:
163163
host = self.meta["HTTP_HOST"]
@@ -172,7 +172,7 @@ def host(self) -> str:
172172
@cached_property
173173
def port(self) -> str:
174174
"""Return the port number for the request as a string."""
175-
if settings.USE_X_FORWARDED_PORT and "HTTP_X_FORWARDED_PORT" in self.meta:
175+
if settings.HTTP_X_FORWARDED_PORT and "HTTP_X_FORWARDED_PORT" in self.meta:
176176
port = self.meta["HTTP_X_FORWARDED_PORT"]
177177
else:
178178
port = self.meta["SERVER_PORT"]
@@ -182,13 +182,13 @@ def port(self) -> str:
182182
def client_ip(self) -> str:
183183
"""Return the client's IP address.
184184
185-
If USE_X_FORWARDED_FOR is True, checks the X-Forwarded-For header first
185+
If HTTP_X_FORWARDED_FOR is True, checks the X-Forwarded-For header first
186186
(using the first/leftmost IP). Otherwise returns REMOTE_ADDR directly.
187187
188-
Only enable USE_X_FORWARDED_FOR when behind a trusted proxy that
188+
Only enable HTTP_X_FORWARDED_FOR when behind a trusted proxy that
189189
overwrites the X-Forwarded-For header.
190190
"""
191-
if settings.USE_X_FORWARDED_FOR:
191+
if settings.HTTP_X_FORWARDED_FOR:
192192
if xff := self.headers.get("X-Forwarded-For"):
193193
return xff.split(",")[0].strip()
194194
return self.meta["REMOTE_ADDR"]

plain/plain/runtime/global_settings.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@
5959
# Whether to use the X-Forwarded-Host, X-Forwarded-Port, and X-Forwarded-For
6060
# headers when determining the host, port, and client IP for the request.
6161
# Only enable these when behind a trusted proxy that overwrites these headers.
62-
USE_X_FORWARDED_HOST = False
63-
USE_X_FORWARDED_PORT = False
64-
USE_X_FORWARDED_FOR = False
62+
HTTP_X_FORWARDED_HOST: bool = False
63+
HTTP_X_FORWARDED_PORT: bool = False
64+
HTTP_X_FORWARDED_FOR: bool = False
6565

6666
# A secret key for this particular Plain installation. Used in secret-key
6767
# hashing algorithms. Set this in your settings, or Plain will complain

0 commit comments

Comments
 (0)