-
Notifications
You must be signed in to change notification settings - Fork 21.8k
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
Request origin does not match request base_url #22965
Comments
Fix by adding more headers in Nginx (X-Forwarded-Ssl on, X-Forwarded-Port 443 and X-Forwarded-Host hostname) |
I also ran into this issue. I'll describe it differently to help with future searches. Submission of any form to Nginx proxying to a Puma unix socket running a Rails 5.0.0.beta1 app responds with:
and throws:
even though the App's CSRF is set up correctly. The app works in development (puma or thin, without Nginx), and worked in Nginx/Puma/Rails-4.2. I don't know if this is specific to using Puma, but seems like the same problem could exist with other servers (unicorn, passenger, etc.) I don't know if this behavior change is an error or intended. I can confirm @tpbowden's solution worked for me as well. In my nginx configuration, I setup the app to proxy to puma listening on a unix socket. Here are the relevant parts of the configuration.
|
The issue isn't related to Puma and would happen to any web server running behind a reverse proxy which isn't passing the correct headers through. The way I managed to figure out how this issue was being caused was by following the error down the stack trace. The root of the issue is the code in You can follow the source for |
I've just done a bit more research and it is definitely the intended behaviour. It can be disabled in your app config using |
@tpbowden Did you find a solution that doesn't disable forgery protection in the end? I think I'm facing the same problem and really struggling to make 'clean' progress. |
Yes the problem was not passing enough headers through my reverse proxy (nginx) for Rails/Rack to determine whether the request was originally made using https or not (since it was already decrypted by the time it hit Rails) |
ahh, so how did you find out what was missing...and how did you add the missing headers? Thanks so much for the quick reply! |
If you look at @afair's comment above he was referenced a correct Nginx config, you just need to add the x-forwarded headers related to ssl. I figured out which ones were missing by digging though the source code of Rack's request object to find out how it determine's the protocol of the request. |
Alas @afair's config doesn't work for me. Moreover, my logs have been annoyingly sparse since adding my Puma config (but that's another story.) |
I narrowed my issue down to nginx not playing well with 2 virtual hosts running at the same time. I'm trying to run a staging VPS and a production VPS using jwilder's nginx-proxy. |
@afair You, Sir, have saved a lot of my time. Thank you. |
Thanks @tpbowden! You made my day! |
Thanks so much : |
@afair fixed my problem! thank you! |
My problem was similar, using heroku and cloudflare, solution was:
from: http://til.obiefernandez.com/posts/875a2a69af-cloudflare-flexible-ssl-mode-breaks-rails-5-csrf |
@Onumis Thank so much! I turn Cloudflare SSL to Full mode and fixed the problem. |
Add http header on nginx reverse proxy setting. See also rails/rails#22965
Add http header on nginx reverse proxy setting. See also rails/rails#22965
For those using Apache, this worked for me:
|
Thank you so much. Worked for me too. |
Also works without the socket thing:
|
Thank you so much buddy it's worked for me. |
Fix issue described in [1]. We were incorrectly overriding the X-Forwarded-Proto header from the ingress, which caused CSRF token validation in Rails to fail. 1: rails/rails#22965.
Thank you, I love you :) |
If you are using Dokku and facing this problem, check out this awesome guide https://okhlopkov.com/cloudflare-certificates-dokku/ |
I'm using Aliyun load balance which is called SLB for my two other application servers. My application servers started at port 3000 and the application server didn't set up ssl. When the user access my website by https, SLB redirect the request to application server with http. So now we have the issue: The solution for this is to make sure when SLB redirect request to application server we should provided the proxy header: X-Forwarded-Proto I think the header missing is the root cause for this case. |
Thank you. this solved the issue for me as well 🙏 |
usando o proxy reverso do apache é adicionar RequestHeader set X-Forwarded-Proto "https" |
I am running the Rails 5.0.0 beta1 and after deploying to production have noticed something odd. My application is running being an Nginx reverse proxy which decrypts SSL.
Every time I try to submit a form, a ActionController::InvalidAuthenticityToken is raised. I have managed to narrow this down to line 399 in ActionController. The problem is that the request origin has
https://
as the protocol and the request base_url hashttp://
as the protocol.I have tried setting the
X-Forwarded-Proto
header and usingconfig.force_ssl = true
but neither made any difference.I have not yet figured out a way to isolate a failing test case but am happy to look into it if anyone has any suggestions.
The text was updated successfully, but these errors were encountered: