Fix CSRF verification failures by correcting nginx Host header configuration#699
Merged
Merged
Conversation
Contributor
|
Important Review skippedBot user detected. To trigger a single review, invoke the You can disable this status message by setting the Comment |
Co-authored-by: A1L13N <193832434+A1L13N@users.noreply.github.com>
Co-authored-by: A1L13N <193832434+A1L13N@users.noreply.github.com>
Copilot
AI
changed the title
Please fix Forbidden (403)
CSRF verification failed. Request aborted.
You are seeing this message because this site requires a CSRF cookie when submitting forms. This cookie is required for security reasons, to ensure that your browser is not being hij...
Fix CSRF verification failures by correcting nginx Host header configuration
Oct 5, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Users were encountering CSRF verification failures when submitting forms on the website:
This issue was particularly affecting users accessing the site via
www.alphaonelabs.comand the pythonanywhere domain.Root Cause
The nginx configuration was hardcoding the
Hostheader to a fixed domain:This caused a mismatch between the request's
Originheader and theHostheader that Django received. For example:https://www.alphaonelabs.comOrigin: https://www.alphaonelabs.comHost: alphaonelabs.comEven though
https://www.alphaonelabs.comwas inCSRF_TRUSTED_ORIGINS, the Host header mismatch caused Django's CSRF middleware to reject the request.Solution
1. Fixed nginx Host header configuration
Changed the nginx configuration to preserve the actual client's Host header:
This ensures Django receives the same Host header that the client sent, allowing Origin and Host to match correctly.
2. Added pythonanywhere domain to CSRF_TRUSTED_ORIGINS
The pythonanywhere domain was in
ALLOWED_HOSTSbut missing fromCSRF_TRUSTED_ORIGINS. Added it to ensure explicit CSRF validation support:This is necessary because the
HostnameRewriteMiddlewarerewrites the pythonanywhere hostname toalphaonelabs.combut doesn't rewrite the Origin header.3. Added comprehensive CSRF tests
Created
web/tests/test_csrf.pywith tests verifying CSRF tokens work correctly across all allowed domains:test_csrf_with_alphaonelabs_domain()test_csrf_with_www_subdomain()test_csrf_with_pythonanywhere_domain()test_csrf_with_localhost()Impact
✅ Forms can now be submitted from
www.alphaonelabs.comwithout CSRF errors✅ Forms can be submitted from the pythonanywhere domain without CSRF errors
✅ All legitimate domains in
ALLOWED_HOSTSare now properly supported✅ No breaking changes - backward compatible
✅ No performance impact
✅ Security maintained - no weakening of CSRF protection
Deployment Notes
After merging, the nginx configuration needs to be updated on the server:
ansible-playbook ansible/deploy.yml # OR sudo systemctl reload nginxFiles Changed
ansible/nginx.conf.j2- Fixed Host header passing (2 lines)web/settings.py- Added pythonanywhere to CSRF_TRUSTED_ORIGINS (1 line)web/tests/test_csrf.py- Added comprehensive CSRF tests (148 lines, new file)Total: 3 files changed, 151 insertions(+), 2 deletions(-)
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.