0.01
No commit activity in last 3 years
No release in over 3 years
Preventing open redirects in Rails apps
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 0
 Project Readme

Safe Redirect

A little gem to keep our Rails app safe from open redirection vulnerabilities.

Installation

Add this line to Gemfile.

gem 'safe_redirect'

Configuration

Create a config/initializers/safe_redirect.rb file.

SafeRedirect.configure do |config|
  config.default_path = 'https://www.yahoo.com' # default value: '/'
  config.domain_whitelists = ['www.google.com'] # default value: []
  config.log = Rails.env.development?           # default value: false
end

You can also use wildcard subdomain on domain whitelists (thanks to Mike Campbell).

SafeRedirect.configure do |config|
  config.domain_whitelists = ['*.foo.org'] # whitelisting foo.org, m.foo.org, www.foo.org, ...
end

To log with Rails.logger, set config.log = true. To use a custom logger, set config.log to an object that responds to #warn.

Usage

Add this line to the controllers you wish to secure from open redirection.

include SafeRedirect

The redirect_to method provided by Rails will be overridden by safe_redirect's redirect_to method.

redirect_to 'https://www.google.com' # => redirects to https://www.google.com
redirect_to 'https://www.golgege.com' # => redirects to '/'
redirect_to 'https://www.golgege.com', safe: true # => redirects to 'https://www.golgege.com'
redirect_to 'https://www.golgege.com/hahaha' # => redirects to '/hahaha'
redirect_to 1234 # => redirects to https://www.yahoo.com as default path

Contributing

  • Fork the repository
  • Create a branch for a new feature or bug fix, build it
  • Create a pull request

License

MIT License

Author