Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Enforce SSL based on your Rails routes.
Ruby
tree: 6d82759aa2

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
README.rdoc
Rakefile
ssl_routes.gemspec

README.rdoc

SSL Routes

This gem was created to enforce SSL in a Rails app based on the defined routes. Unlike ssl_requirement, controllers will use the routes to determine enforcement. In order to prevent redirects between protocols where possible, the routes will also be used to generate URLs – such as those created by url_for and Paperclip. This means that there will be little need to redirect the user, which is a security issue when sensitive data has already been sent. Finally, in response to session hijacking tools such as Firesheep, you can place all logged-in traffic under SSL. This is a work in progress, so please feel free to contribute improvements and bug fixes.

Installation

For Rails 2.3, add the following to your environment.rb:

config.gem 'ssl_routes'

For Rails 3 (or if using Bundler), add the following to your Gemfile:

gem 'ssl_routes'

Configuration

Place enforce_protocols with the configuration block in ApplicationController and define the configuration options.

parameter (default: :protocol)

This is the parameter in your routes file that pertains to the protocol. The default is to use the standard :protocol, but you can use a custom one if you want.

secure_session (default: false)

This is to defend against session hijacking tools such as Firesheep. When a user is logged-in (requires current_user) all traffic will go under SSL. In order for this to work, routes must exist for both protocols. This means using a custom parameter (such as :ssl => true/false) that can drive your protocol preference but leave both routes intact.

enable_ssl (default: false)

This is to enable or disable SSL. Useful for turning it on or off based on the environment, a session variable or a user setting.

Example

Controller

class ApplicationController < ActionController::Base
  enforce_protocols do |config|
    config.parameter = :ssl
    config.secure_session = true
    config.enable_ssl = true
  end
end

Route

ActionController::Routing::Routes.draw do |map|
  map.signup '/signup', :controller => 'users', :action => 'new', :ssl => true
end

View

Any helper that uses url_for will use the current protocol to determine if a full URL is needed:

signup_path
=> https://hostname/signup

If the current protocol is correct, then nothing changes:

signup_path
=> /signup

Note

If using Rails 3.1 or the Rack::SSL gem, use the :exclude option to allow both http and https traffic:

config.middleware.use Rack::SSL, :exclude => proc { |env| env['HTTPS'] != 'on' }

License

(The MIT License)

Copyright © 2011 Cedric Howe

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.