Skip to content
This repository has been archived by the owner on Sep 24, 2019. It is now read-only.

Commit

Permalink
Make sure range strings are quoted after we quote the range.
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Jul 2, 2014
1 parent f17b04a commit c1156bf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_quote_cast_numeric
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
26 changes: 26 additions & 0 deletions activerecord/test/cases/adapters/postgresql/range_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "cases/helper"

if ActiveRecord::Base.connection.supports_ranges?
class PostgresqlRange < ActiveRecord::Base
self.table_name = "postgresql_ranges"
end

class PostgresqlRangeTest < ActiveRecord::TestCase
test "update_all with ranges" do
PostgresqlRange.create!

PostgresqlRange.update_all(int8_range: 1..100)

assert_equal 1...101, PostgresqlRange.first.int8_range
end

test "ranges correctly escape input" do
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")
end
end
end

0 comments on commit c1156bf

Please sign in to comment.