|Ruby on Rails Unsafe Query Generation Risk in Ruby on Rails (CVE-2012-2694)||Aaron Patterson||6/12/12 2:29 PM|
Unsafe Query Generation Risk in Ruby on Rails
There is a vulnerability when Active Record is used in conjunction with parameter parsing from Rack via Action Pack. This vulnerability has been assigned the CVE identifier CVE-2012-2694.
Versions Affected: ALL versions
Not affected: NONE
Fixed Versions: 3.2.6, 3.1.6, 3.0.14
Due to the way Active Record interprets parameters in combination with the way that Rack parses query parameters, it is possible for an attacker to issue unexpected database queries with "IS NULL" where clauses. This issue does *not* let an attacker insert arbitrary values into an SQL query, however they can cause the query to check for NULL where most users wouldn't expect it.
For example, a system has password reset with token functionality:
user = User.find_by_token(params[:token])
An attacker can craft a request such that `params[:token]` will return `['xyz', nil]`. The `['xyz', nil]` value will bypass the test for nil, but will still add an "IN ('xyz', NULL)" clause to the SQL query.
All users running an affected release should either upgrade or use one of the work arounds immediately. All users running an affected release should upgrade immediately. Please note, this vulnerability is a variant of CVE-2012-2660, even if you upgraded to address that issue, you must take action again.
The FIXED releases are available at the normal locations.
This problem can be mitigated by casting the parameter to a sting before passing it to Active Record. For example:
unless params[:token].nil? || params[:token].to_s.empty?
user = User.find_by_token(params[:token].to_s)
Note the parameter is still cast to a string before being send to Active Record.This is because an array with a nil value can still bypass the `to_s.empty?` test:
>> ['xyz', nil].to_s
>> ['xyz', nil].to_s.empty?
To aid users who aren't able to upgrade immediately we have provided patches for the two supported release series. They are in git-am format and consist of a single changeset.
* 3-0-null_array_param.patch - Patch for 3.0 series
* 3-1-null_array_param.patch - Patch for 3.1 series
* 3-2-null_array_param.patch - Patch for 3.2 series
Please note that only the 3.1.x and 3.2.x series are supported at present. Users of earlier unsupported releases are advised to upgrade as soon as possible as we cannot guarantee the continued availability of security fixes for unsupported releases.
Thanks to the following people for reporting this bug:
* Egor Homakov
* Paul Lynch