Rails: Send links in emails with the right protocol

Posted Almost 11 years ago. Visible to the public.

ActionMailer per default uses http as protocol, which enables SSL-stripping. When a logged-in user follows an http link to your application, it sends the cookies along with it. Although the application redirects the user to https and from that point has a secure connection to the user, an attacker may overhear that first unsafe request and hijack your session.

Teach ActionMailer to use the right protocol

If your application is behind SSL, turn on using https application-wide. In your environment file (either global or per environment, e.g. production only):

config.action_mailer.default_url_options = { :protocol => 'https', :host => 'your-application-host.com' }

If you want to send emails from public parts of your application with HTTP links and emails from SSL-protected parts with HTTPS, build a before_filter.

If you need certain links with HTTP, independent from the request's protocol, set the protocol per link:

= link_to 'My App', root_url(:protocol => 'http') # per link

Also see Make ActionMailer use the current request host and protocol for URL generation

Dominik Schöler
Last edit
Over 1 year ago
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2013-06-14 08:29)