|
| 1 | +--- |
| 2 | +gem: activerecord |
| 3 | +framework: rails |
| 4 | +cve: 2016-6317 |
| 5 | +date: 2016-08-11 |
| 6 | +url: https://groups.google.com/forum/#!topic/rubyonrails-security/rgO20zYW33s |
| 7 | + |
| 8 | +title: Unsafe Query Generation Risk in Active Record |
| 9 | + |
| 10 | +description: | |
| 11 | + There is a vulnerability when Active Record is used in conjunction with JSON |
| 12 | + parameter parsing. This vulnerability is similar to CVE-2012-2660, |
| 13 | + CVE-2012-2694 and CVE-2013-0155. |
| 14 | +
|
| 15 | + Impact |
| 16 | + ------ |
| 17 | +
|
| 18 | + Due to the way Active Record interprets parameters in combination with the way |
| 19 | + that JSON parameters are parsed, it is possible for an attacker to issue |
| 20 | + unexpected database queries with "IS NULL" or empty where clauses. This issue |
| 21 | + does *not* let an attacker insert arbitrary values into an SQL query, however |
| 22 | + they can cause the query to check for NULL or eliminate a WHERE clause when |
| 23 | + most users wouldn't expect it. |
| 24 | +
|
| 25 | + For example, a system has password reset with token functionality: |
| 26 | +
|
| 27 | + ```ruby |
| 28 | + unless params[:token].nil? |
| 29 | + user = User.find_by_token(params[:token]) |
| 30 | + user.reset_password! |
| 31 | + end |
| 32 | + ``` |
| 33 | +
|
| 34 | + An attacker can craft a request such that `params[:token]` will return |
| 35 | + `[nil]`. The `[nil]` value will bypass the test for nil, but will still add |
| 36 | + an "IN ('xyz', NULL)" clause to the SQL query. |
| 37 | +
|
| 38 | + Similarly, an attacker can craft a request such that `params[:token]` will |
| 39 | + return an empty hash. An empty hash will eliminate the WHERE clause of the |
| 40 | + query, but can bypass the `nil?` check. |
| 41 | +
|
| 42 | + Note that this impacts not only dynamic finders (`find_by_*`) but also |
| 43 | + relations (`User.where(:name => params[:name])`). |
| 44 | +
|
| 45 | + All users running an affected release should either upgrade or use one of the |
| 46 | + work arounds immediately. All users running an affected release should upgrade |
| 47 | + immediately. Please note, this vulnerability is a variant of CVE-2012-2660, |
| 48 | + CVE-2012-2694, and CVE-2013-0155. Even if you upgraded to address those |
| 49 | + issues, you must take action again. |
| 50 | +
|
| 51 | + If this chance in behavior impacts your application, you can manually decode |
| 52 | + the original values from the request like so: |
| 53 | +
|
| 54 | + `ActiveSupport::JSON.decode(request.body)` |
| 55 | +
|
| 56 | + Workarounds |
| 57 | + ----------- |
| 58 | + This problem can be mitigated by casting the parameter to a string before |
| 59 | + passing it to Active Record. For example: |
| 60 | +
|
| 61 | + ```ruby |
| 62 | + unless params[:token].nil? || params[:token].to_s.empty? |
| 63 | + user = User.find_by_token(params[:token].to_s) |
| 64 | + user.reset_password! |
| 65 | + end |
| 66 | + ``` |
| 67 | +
|
| 68 | +unaffected_versions: |
| 69 | + - "< 4.2.0" |
| 70 | + - ">= 5.0.0" |
| 71 | + |
| 72 | +patched_versions: |
| 73 | + - ~> 4.2.7.1 |
0 commit comments