Agile Web Development

Build it. Launch it. Love it.

Safe ERB

Overview

Safe ERB lets you make sure that the string written by "<%= %>" in your rhtml template is escaped correctly. If you try to show the attributes in the ActiveRecord instance read from the database or the parameters received from the request without escaping them using "h" method, an exception will be raised. This will significantly reduce the possibility of putting cross-site scripting vulnerability into your web application.

The check is done using "tainted?" method in Object class which is a standard feature provided by Ruby - the string is "tainted" when it is read from IO. When ERB::Util#h method is called, this plugin "untaints" the string, and when "<%= %>" is called in your rhtml template, it raises an exception if the string you are trying to show is tainted.

Installation

Just put this plugin into vendor/plugins directory in your Rails application. No configuration is needed.

Details

The string becomes tainted when it is read from IO, such as the data read from the DB or HTTP request. However, the request parameters are not tainted in functional and integration tests, and also if your server is Mongrel. Hence this plugin installs before_filter into ActionController::Base that always taints request parameters and cookies.

The taint check is done when the ERB template is complied from following methods in ActionController::Base:

  • render_template
  • render_file

The check is limited to these methods so that it won’t affect other parts of Rails using ERB, such as generators and the views for ActionMailer. To skip checking for specific controllers or actions, you can set @skip_checking_tainted variable to true in your filter or action.

The returned values from the following methods become untainted:

  • ERB::Util#h
  • ActionView::Helpers::TextHelper#strip_tags

Also, you can always untaint any string manually by calling "untaint" method (standard Ruby feature).

Vitals

Home http://wiki.rubyonrails.com/rails/pages/Safe+ERB
License Rails' (MIT)
Rating (9 votes)
Owner Shinya Kasatani
Created 17 July 2006

Comments

  • Avatar
    Shinya Kasatani
    23 September 2006

    I wrote this plugin for Ruby 1.8.4, but it doesn't work for Ruby 1.8.5. I'll post a fixed version later.

  • Avatar
    Shinya Kasatani
    24 September 2006

    A new version (0.2) is now available that fixes the problem in Ruby 1.8.5.

  • mnb
    16 November 2007

    good job shinya.

  • OA
    10 January 2008

    Looking forward to that fixed version!!

  • Avatar
    Basti
    3 July 2008

    http://www.rorsecurity.info/2008/01/06/safeerb-for-rails-2/

Add a comment