Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: activating SSL support gets Chrome confused

There is a nice option to config for the Rails app:

config.force_ssl = true

However it seems that just putting that to true doesn't get the HTTPS connections working. Even more - after trying (and failing) to connect to https://localhost:3000 with Chrome, I've set this option to false, and Chrome still tries to open https, even if I write http.

So, couple of questions:

--How to force Chrome not to try https anymore? --What is the proper way of enabling SSL on my Rails app?

Update: The app is run on Heroku, and it seems that https is supported there automagically. Can I test SSL also locally? Like when running rails server?

like image 523
Alexander Savin Avatar asked May 04 '12 07:05

Alexander Savin


2 Answers

First, I should say that I haven't tried this, but there are mainly two possibly reasons for Chrome still using HTTPS:

  • Using HTTP Strict Transport Security headers: if the server sets them, the client (supporting HSTS, like Chrome) is meant to stick to HTTPS for all subsequent requests to that host.

  • Permanent redirects. If the initial redirect you got was using "301 Moved Permanently" (and not 302 for example) to make the redirection,(*) the browser is meant to remember it ("The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs").

A likely solution to this would be to clear the cache in your browser.

(*) This question seems to indicate this is the case for Ruby on Rails with this config).

like image 129
Bruno Avatar answered Sep 28 '22 01:09

Bruno


I had the same issue. What I did is using an ssl enforcer gem which adds a middleware that handles ssl and redirects. It has a strict option which enforces the configured protocols.

in your Gemfile add:

gem 'rack-ssl-enforcer'

in production.rb add:

config.middleware.use Rack::SslEnforcer, only: %r{your_regex_condition}, strict: true

This will force the requested pages to be secured and the rest to be non secured. It disables the HSTS header which is problematic in chrome (redirect caching issue).

You can also expire the cache for all cleints (if it already exist) to make sure you'll not get infinite redirect:

config.middleware.use Rack::SslEnforcer, only: %r{your_regex_condition}, :hsts => { :expires => 1, :subdomains => false }

also remove the ssl enforcement in production.rb (otherwise it might conflict with this middleware):

config.force_ssl = false
like image 37
ramigg Avatar answered Sep 28 '22 01:09

ramigg