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

CSRF verification fails when using https before reverse proxy, but not after #186

Open
DrMaxNix opened this issue Sep 3, 2024 · 2 comments

Comments

@DrMaxNix
Copy link

DrMaxNix commented Sep 3, 2024

When using a reverse proxy, which forwards https requests via http, I get CSRF verification failed. Request aborted.
I have allowed hosts set to wildcard (allowed_host1 = *). When I set it explicitly to the domain I'm using, it works.

For some reason, adding SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") to settings.py also fixed the issue.
I guess this is because from django's perspective, the request is made via http, but the Origin/Referer headers use a https:// scheme, so they are not the same.

As adding the SECURE_PROXY_SSL_HEADER option by default adds insecurities for people without a reverse proxy, I suggest adding an etebase-server.ini option for it.
Also weirdly enough, I didn't have this problem before, with the same reverse proxy setup, but when running without docker. Feel free to suggest what I could possibly have done wrong..

@tasn
Copy link
Member

tasn commented Sep 3, 2024

This is exactly it, http and https are not the same address, and so it doesn't treat it the same. You need both/either.

You can also override the host header in the revere proxy to strip the https if you don't want it.

@DrMaxNix
Copy link
Author

DrMaxNix commented Sep 3, 2024

But what I don't understand is why it worked in my old setup, and also the settings.py should add both http and https as trusted origins:

CSRF_TRUSTED_ORIGINS = ["https://" + y for x, y in config.items("allowed_hosts")] + \
["http://" + y for x, y in config.items("allowed_hosts")]

This part works for specific domains, but not for wildcards..?
For wildcards it should generate this list:

["https://*", "http://*"]

According to django docs, this should be fine..?

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

No branches or pull requests

2 participants