Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[Rails 3] Routes redirect method bug with https

#5408

The redirect method doesn't work with https request scheme.

match 'account/logout' => redirect("/logout")

When going on https://www.example.com/account/logout, the redirect going on http://www.example.com:443/logout rather than https://www.example.com/logout. Quite annoying :(

I haven't found how to test https scheme on dispatch/routing_test.rb, sorry.

Reported by Thibaud Guillaume-Gentil · August 19th, 2010 @ 01:43 PM

State: resolved
Milestone: 3.0.2
Assigned to: Andrew White Andrew White
Importance: High

Activity

  1. Andrew White
    Andrew White
    • Milestone cleared.
    • State changed from new to open
    • Assigned user set to Andrew White
    • Importance changed from to High

    I've got a test but the scheme 'https' seems to be maintained, it's just adding an unnecessary 443 port number which I know where to fix. Can you confirm that this is the case for you?

    August 19th, 2010 @ 03:09 PM

  2. Thibaud Guillaume-Gentil
    Thibaud Guillaume-Gentil

    In my case https is not maintained and replaced by http. I can retry after you have fixed the useless 443 port addition, if you want. Thanks

    August 19th, 2010 @ 03:16 PM

  3. Andrew White
    Andrew White

    How are you testing it?

    August 19th, 2010 @ 03:27 PM

  4. Andrew White
    Andrew White

    The attached patch fixes the port number issue. I can't see how the scheme wouldn't be maintained since the redirect code reads it from the current request.

    August 19th, 2010 @ 03:44 PM

  5. Thibaud Guillaume-Gentil
    Thibaud Guillaume-Gentil

    I don't test it, it's how it "work" on my staging Heroku env. Maybe it's an issue related to Heroku nginx config with the 443 port. I'll try with your patch tomorrow.

    August 19th, 2010 @ 03:49 PM

  6. Thibaud Guillaume-Gentil
    Thibaud Guillaume-Gentil

    With your patch the 443 port is properly removed but https is still replaced by http.
    I have apply your patch on edge rails (http://github.com/thibaudgg/rails/tree/routes_patched) and make a little app (deployed on Heroku with SSL add-on (heroku addons:add ssl:piggyback)) to show you the https problem.

    The little app: http://github.com/thibaudgg/https_app
    Heroku deploy: https://https-app.heroku.com (redirected to http://https-app.heroku.com/hello)

    Hope it'll help!

    August 20th, 2010 @ 08:56 AM

  7. Andrew White
    Andrew White

    Looks as though the problem is in Rack rather than Rails. Looking at the source code for the various handlers not all of them set the rack.url_scheme environment variable correctly. There's already a workaround for request.ssl? which will work here as heroku sets HTTP_X_FORWARDED_PROTO to https if the incoming request is ssl.

    The attached combined patch should fix it.

    August 20th, 2010 @ 10:20 AM

  8. Thibaud Guillaume-Gentil
    Thibaud Guillaume-Gentil

    Great, our second patch fix the problem.

    https://https-app.heroku.com => https://https-app.heroku.com/hello

    Thanks a lot Andrew and have a nice week-end.

    August 20th, 2010 @ 02:29 PM

  9. Repository
    Repository
    • State changed from open to resolved

    (from [47280f083a06dfd034f2e1a2661adf02b0e3a064]) Don't add the standard https port when using redirect in routes.rb and ensure that request.scheme returns https when using a reverse proxy.

    [#5408 [Rails 3] Routes redirect method bug with https state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/47280f083a06dfd034f2e1a2661adf...

    August 20th, 2010 @ 02:41 PM

  10. Repository
    Repository

    (from [0d0fbf1e648606c9499e332bad412da005a4e37f]) Don't add the standard https port when using redirect in routes.rb and ensure that request.scheme returns https when using a reverse proxy.

    [#5408 [Rails 3] Routes redirect method bug with https state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/0d0fbf1e648606c9499e332bad412d...

    August 20th, 2010 @ 02:42 PM

  11. Jeremy Kemper
  12. blair.silverberg
    blair.silverberg

    I am still encountering this problem on heroku. When navigating to https://secure.ignitiontutoring.com user is redirected to http://secure.ignitiontutoring.com.

    January 26th, 2011 @ 04:15 PM

  13. Andrew White
    Andrew White

    Blair, you'll need to give me more to work with than that - for example a dump of the request environment that your app receives, some relevant routes and whether you're using any plugins, gems or middleware that performs ssl redirects like ssl_requirement or rack-ssl-enforcer.

    January 26th, 2011 @ 04:37 PM

  14. mdrozdziel
    mdrozdziel

    Looks like I am getting this error too. I am using rack-ssl-enforcer with :strict => true. Every https:// request (which should be enforced to http://), and is beeing redirected inside the controller ends up beeing redirected to http://https://

    February 3rd, 2011 @ 05:15 AM

  15. mdrozdziel
    mdrozdziel

    I am very sorry, but this is probably not related to the previous bug. I read Blair's report, and immediately though that this is the case. I even created a demo app, but it turned out it works in on a clean up. I found that url_for help was returning http://https://.. paths. This is probably more related to translate_routes gem, than rack-ssl-enforcer.

    In case someone runs into this issue and ends up here from Google: I solved this for just by adding :path_only => true to the url_for helpers. Now everything is ok.

    February 4th, 2011 @ 11:24 AM