Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Django not detect that our site is HTTPS #282

Open
wants to merge 1 commit into
base: development
Choose a base branch
from

Conversation

hongquan
Copy link
Member

@hongquan hongquan commented Feb 26, 2025

Fixes #267

Add SECURE_PROXY_SSL_HEADER settings. This guides Django to look into Nginx header to detect that our website is in HTTPS. We need this settings because the TLS termination is done on Nginx, not on Django, so Django originally cannot know if our site is HTTP or HTTPS.

How has this been tested?

This is the page where user see the link:

image

Before this PR, user will see this after clicking the link:

image

User no longer sees this page after this PR.

Checklist

  • I have added tests to cover my changes.

Summary by Sourcery

Bug Fixes:

  • Fixes an issue where Django was unable to detect that the site was being served over HTTPS due to TLS termination occurring at the Nginx proxy level.

Copy link

sourcery-ai bot commented Feb 26, 2025

Reviewer's Guide by Sourcery

This pull request fixes an issue where Django was not correctly detecting HTTPS connections due to TLS termination at the Nginx proxy. It introduces the SECURE_PROXY_SSL_HEADER setting to address this. Additionally, type hints were added to the redirect view.

Sequence diagram for HTTPS redirection with Nginx proxy

sequenceDiagram
    actor User
    participant Browser
    participant Nginx
    participant Django

    User->>Browser: Clicks on a link
    Browser->>Nginx: HTTPS Request to the site
    Nginx->>Django: Forwards request with X-Forwarded-Proto header
    Django->>Django: Reads SECURE_PROXY_SSL_HEADER setting
    Django->>Django: Detects HTTPS based on header
    Django-->>Nginx: HTTPS Response
    Nginx-->>Browser: HTTPS Response
    Browser-->>User: Renders page securely
Loading

File-Level Changes

Change Details Files
Configured Django to recognize HTTPS connections when TLS termination occurs at the Nginx proxy level.
  • Added SECURE_PROXY_SSL_HEADER to inform Django to trust the HTTP_X_FORWARDED_PROTO header set by Nginx.
  • Set the value of SECURE_PROXY_SSL_HEADER to ('HTTP_X_FORWARDED_PROTO', 'https').
src/pretalx/settings.py
Added type hints to the redirect view.
  • Added type hints to the request parameter of the _is_samesite_referer function.
  • Added type hints to the return value of the _is_samesite_referer function.
  • Added type hints to the request parameter of the redirect_view function.
  • Added type hints to the return value of the redirect_view function.
  • Added type hints to the url parameter of the safelink function.
  • Added type hints to the return value of the safelink function.
src/pretalx/common/views/redirect.py

Assessment against linked issues

Issue Objective Addressed Explanation
#267 Remove the outbound link warning/redirect page.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @hongquan - I've reviewed your changes - here's some feedback:

Overall Comments:

  • It might be worth adding a comment explaining why SECURE_PROXY_SSL_HEADER is needed.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@hongquan
Copy link
Member Author

@sourcery-api The explanation comment is right on top of SECURE_PROXY_SSL_HEADER.

Copy link
Member

@norbusan norbusan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look ok, but please do not mix two different things.
This PR now contains:

  • the described fix to add SECURE_PROXY_SSL_HEADER
  • unrelated typing changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Delete URL warning to links for outbound link signing and redirects
2 participants