Skip to content

Commit

Permalink
Added SQL escaping for :limit and :offset in MySQL [Jonathan Wiess]
Browse files Browse the repository at this point in the history
  • Loading branch information
dhh committed Jun 1, 2008
1 parent 71528b1 commit 3282bf3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG
@@ -1,3 +1,8 @@
*Edge*

* Added SQL escaping for :limit and :offset in MySQL [Jonathan Wiess]


*2.1.0 (May 31st, 2008)* *2.1.0 (May 31st, 2008)*


* Add ActiveRecord::Base.sti_name that checks ActiveRecord::Base#store_full_sti_class? and returns either the full or demodulized name. [rick] * Add ActiveRecord::Base.sti_name that checks ActiveRecord::Base#store_full_sti_class? and returns either the full or demodulized name. [rick]
Expand Down
Expand Up @@ -336,10 +336,11 @@ def rollback_db_transaction #:nodoc:


def add_limit_offset!(sql, options) #:nodoc: def add_limit_offset!(sql, options) #:nodoc:
if limit = options[:limit] if limit = options[:limit]
limit = sanitize_limit(limit)
unless offset = options[:offset] unless offset = options[:offset]
sql << " LIMIT #{limit}" sql << " LIMIT #{limit}"
else else
sql << " LIMIT #{offset}, #{limit}" sql << " LIMIT #{offset.to_i}, #{limit}"
end end
end end
end end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/adapter_test.rb
Expand Up @@ -118,7 +118,7 @@ def test_add_limit_offset_should_sanitize_sql_injection_for_limit_with_comas
sql_inject = "1, 7 procedure help()" sql_inject = "1, 7 procedure help()"
if current_adapter?(:MysqlAdapter) if current_adapter?(:MysqlAdapter)
assert_equal " LIMIT 1,7", @connection.add_limit_offset!("", :limit=>sql_inject) assert_equal " LIMIT 1,7", @connection.add_limit_offset!("", :limit=>sql_inject)
assert_equal " LIMIT 7, 1", @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7) assert_equal " LIMIT 7, 1", @connection.add_limit_offset!("", :limit=> '1 ; DROP TABLE USERS', :offset=>7)
else else
assert_equal " LIMIT 1,7", @connection.add_limit_offset!("", :limit=>sql_inject) assert_equal " LIMIT 1,7", @connection.add_limit_offset!("", :limit=>sql_inject)
assert_equal " LIMIT 1,7 OFFSET 7", @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7) assert_equal " LIMIT 1,7 OFFSET 7", @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7)
Expand Down

0 comments on commit 3282bf3

Please sign in to comment.