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 0690f6f commit 27a0c13
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

This comment has been minimized.

Copy link
@homakov

homakov Jun 4, 2015

Contributor

maybe \z? @rafaelfranca

This comment has been minimized.

Copy link
@rafaelfranca

rafaelfranca Jun 4, 2015

Author Member

I tried \z and it breaks the behavior.

when /\A[0-9A-F]*\Z/i then "X'#{value}'" # Hexadecimal notation
end
else
super
Expand Down
Expand Up @@ -819,7 +819,7 @@ def initialize_type_map(type_map)
FEATURE_NOT_SUPPORTED = "0A000" #:nodoc:

def exec_no_cache(sql, name, binds)
log(sql, name, binds) { @connection.async_exec(sql) }
log(sql, name, binds) { @connection.async_exec(sql, []) }
end

def exec_cache(sql, name, binds)
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/adapters/postgresql/quoting_test.rb
Expand Up @@ -57,6 +57,12 @@ def test_quote_time_usec
assert_equal "'1970-01-01 00:00:00.000000'", @conn.quote(Time.at(0))
assert_equal "'1970-01-01 00:00:00.000000'", @conn.quote(Time.at(0).to_datetime)
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 27a0c13

Please sign in to comment.