Skip to content

Commit

Permalink
fix: enhance middleware to set HTTP_X_FORWARDED_PROTO and secure prox…
Browse files Browse the repository at this point in the history
…y SSL header
  • Loading branch information
seanmorley15 committed Jan 14, 2025
1 parent 62d2fd7 commit 4c84ac0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 9 additions & 5 deletions backend/server/adventures/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ class OverrideHostMiddleware:
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request: HttpRequest):
# Override the host with the PUBLIC_URL environment variable
def __call__(self, request):
public_url = os.getenv('PUBLIC_URL', None)
if public_url:
# Split the public URL to extract the host and port (if available)
host = public_url.split("//")[-1].split("/")[0]
request.META['HTTP_HOST'] = host # Override the HTTP_HOST header
# Extract host and scheme
scheme, host = public_url.split("://")
request.META['HTTP_HOST'] = host
request.META['wsgi.url_scheme'] = scheme

# Set X-Forwarded-Proto for Django
request.META['HTTP_X_FORWARDED_PROTO'] = scheme

response = self.get_response(request)
return response
2 changes: 2 additions & 0 deletions backend/server/main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')


BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_ROOT = BASE_DIR / "staticfiles"
Expand Down

0 comments on commit 4c84ac0

Please sign in to comment.