Make sure range strings are quoted after we quote the range.

This commit is contained in:
Rafael Mendonça França 2014-07-02 15:24:11 -03:00
parent dfa7a76de8
commit 958be0e7cc
3 changed files with 20 additions and 2 deletions

View File

@ -24,7 +24,7 @@ module ActiveRecord
when Range
if /range$/ =~ sql_type
escaped = quote_string(PostgreSQLColumn.range_to_string(value))
"#{escaped}::#{sql_type}"
"'#{escaped}'::#{sql_type}"
else
super
end

View File

@ -61,7 +61,7 @@ module ActiveRecord
def test_quote_range
range = "1,2]'; SELECT * FROM users; --".."a"
c = PostgreSQLColumn.new(nil, nil, OID::Range.new(:integer), 'int8range')
assert_equal "[1,2]''; SELECT * FROM users; --,a]::int8range", @conn.quote(range, c)
assert_equal "'[1,2]''; SELECT * FROM users; --,a]'::int8range", @conn.quote(range, c)
end
end
end

View File

@ -216,6 +216,24 @@ if ActiveRecord::Base.connection.supports_ranges?
assert_equal Date.new(2012, 1, 3)..Date.new(2012, 1, 4), range.date_range
end
def test_update_all_with_ranges
PostgresqlRange.create!
PostgresqlRange.update_all(int8_range: 1..100)
assert_equal 1...101, PostgresqlRange.first.int8_range
end
def test_ranges_correctly_escape_input
e = assert_raises(ActiveRecord::StatementInvalid) do
range = "1,2]'; SELECT * FROM users; --".."a"
PostgresqlRange.update_all(int8_range: range)
end
assert e.message.starts_with?("PG::InvalidTextRepresentation")
ActiveRecord::Base.connection.rollback_transaction
end
private
def assert_equal_round_trip(range, attribute, value)
round_trip(range, attribute, value)