Skip to content
SQL injection Disclosed report Intermediate CVE-2016-6317CVE-2012-2660CVE-2012-2694CVE-2013-0155

Unsafe Query Generation (CVE-2012-2660, CVE-2012-2694 and CVE-2013-0155) mitigation bypass

Active Record combined with JSON parameter parsing could interpret crafted parameters as an array containing nil or an empty hash, so a nil check passed while the generated query used an IS NULL or dropped the WHERE clause. An attacker could then trigger queries the developer did not intend, such as resetting a password by token without a valid token.

Joern Schneeweisz Follow

Originally disclosed on HackerOne

  • Triaged in 13 hours
  • Fixed in 3 months
  • Paid in 21 months
  • Disclosed after 21 months

Summary by the program

Unsafe Query Generation Risk in Active Record

There is a vulnerability when Active Record is used in conjunction with JSON parameter parsing. This vulnerability has been assigned the CVE identifier CVE-2016-6317. This vulnerability is similar to CVE-2012-2660, CVE-2012-2694 and CVE-2013-0155.

Versions Affected: >= 4.2.0 Not affected: < 4.2.0, >= 5.0.0 Fixed Versions: 4.2.7.1

Impact

Due to the way Active Record interprets parameters in combination with the way that JSON parameters are parsed, it is possible for an attacker to issue unexpected database queries with "IS NULL" or empty 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 or eliminate a WHERE clause when most users wouldn't expect it.

For example, a system has password reset with token functionality:

unless params[:token].nil? 
  user = User.find_by_token(params[:token]) 
  user.reset_password! 
end 

An attacker can craft a request such that params[:token] will return [nil]. The [nil] value will bypass the test for nil, but will still add an "IN ('xyz', NULL)" clause to the SQL query.

Similarly, an attacker can craft a request such that params[:token] will return an empty hash. An empty hash will eliminate the WHERE clause of the query, but can bypass the nil? check.

Note that this impacts not only dynamic finders (find_by_*) but also relations (User.where(:name => params[:name])).

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, CVE-2012-2694, and CVE-2013-0155. Even if you upgraded to address those issues, you must take action again.

If this chance in behavior impacts your application, you can manually decode the original values from the request like so:

ActiveSupport::JSON.decode(request.body) 

Releases

The FIXED releases are available at the normal locations.

Workarounds

This problem can be mitigated by casting the parameter to a string 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) 
  user.reset_password! 
end 

Patches

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.

  • 4-2-unsafe-query-generation.patch - Patch for 4.2 series

Please note that only the 5.0.x and 4.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.

Credits

Thanks to joernchen of Phenoelit for reporting this!

The original report as published on HackerOne, by joernchen. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the JSON parameter parser produced arrays or empty hashes that Active Record treated as query conditions, so a nil check on the parameter did not reflect what the database query would do.

Impact

An attacker could cause unexpected queries checking for NULL or with no WHERE clause, which in the password reset example could let them reset a user's password without a valid token; the text notes this does not allow inserting arbitrary SQL values.

What the attacker needed

Affected Rails 4.2.x with JSON params; attacker can send crafted request

The fix

It was fixed in version 4.2.7.1, and as a workaround parameters should be cast to a string before being passed to Active Record.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

When a web app receives data, it often turns it into a structured object before using it. Here, a specially shaped request made that object look non-empty to a simple check, while the database query built from it behaved as if no real value was given. That let an attacker make the app run a query the developer never meant to allow.

What is SQL injection?

Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.

More by Joern Schneeweisz

View all

2 more disclosed reports, best first.

More from Ruby on Rails

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

SQL injection

The lesson behind this report.

Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.