Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix SQL injection when querying against ranges and bitstrings
Fix CVE-2014-3483 and protect against CVE-2014-3482.
  • Loading branch information
rafaelfranca committed Jul 2, 2014
1 parent 276b72c commit c4598b9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
Expand Up @@ -23,7 +23,8 @@ def quote(value, column = nil) #:nodoc:
case value
when Range
if /range$/ =~ sql_type
"'#{PostgreSQLColumn.range_to_string(value)}'::#{sql_type}"
escaped = quote_string(PostgreSQLColumn.range_to_string(value))
"#{escaped}::#{sql_type}"
else
super
end
Expand Down Expand Up @@ -70,8 +71,8 @@ def quote(value, column = nil) #:nodoc:
when 'xml' then "xml '#{quote_string(value)}'"
when /^bit/
case value
when /^[01]*$/ then "B'#{value}'" # Bit-string notation
when /^[0-9A-F]*$/i then "X'#{value}'" # Hexadecimal notation
when /\A[01]*\Z/ then "B'#{value}'" # Bit-string notation
when /\A[0-9A-F]*\Z/i then "X'#{value}'" # Hexadecimal notation
end
else
super
Expand Down
Expand Up @@ -795,7 +795,7 @@ def initialize_type_map
FEATURE_NOT_SUPPORTED = "0A000" #:nodoc:

def exec_no_cache(sql, binds)
@connection.async_exec(sql)
@connection.async_exec(sql, [])
end

def exec_cache(sql, binds)
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/adapters/postgresql/quoting_test.rb
Expand Up @@ -52,6 +52,12 @@ def test_quote_cast_numeric
c = Column.new(nil, nil, 'text')
assert_equal "'666'", @conn.quote(fixnum, c)
end

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)
end
end
end
end
Expand Down

0 comments on commit c4598b9

Please sign in to comment.