Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make sure range strings are quoted after we quote the range.
  • Loading branch information
rafaelfranca committed Jul 2, 2014
1 parent dfa7a76 commit 958be0e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
Expand Up @@ -24,7 +24,7 @@ def quote(value, column = nil) #:nodoc:
when Range
if /range$/ =~ sql_type
escaped = quote_string(PostgreSQLColumn.range_to_string(value))
"#{escaped}::#{sql_type}"
"'#{escaped}'::#{sql_type}"
else
super
end
Expand Down
Expand Up @@ -61,7 +61,7 @@ def test_quote_time_usec
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
Expand Down
18 changes: 18 additions & 0 deletions activerecord/test/cases/adapters/postgresql/range_test.rb
Expand Up @@ -216,6 +216,24 @@ def test_exclude_beginning_for_date_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)
Expand Down

0 comments on commit 958be0e

Please sign in to comment.