Skip to content

Commit

Permalink
Check for invalid hex escapes in URI#query=
Browse files Browse the repository at this point in the history
Fixes [Bug #11275]
  • Loading branch information
jeremyevans committed Oct 8, 2019
1 parent 8feb8c9 commit 7909f06
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/uri/generic.rb
Expand Up @@ -836,6 +836,7 @@ def query=(v)
v.encode!(Encoding::UTF_8) rescue nil
v.delete!("\t\r\n")
v.force_encoding(Encoding::ASCII_8BIT)
raise InvalidURIError, "invalid percent escape: #{$1}" if /(%\H\H)/n.match(v)
v.gsub!(/(?!%\h\h|[!$-&(-;=?-_a-~])./n.freeze){'%%%02X' % $&.ord}
v.force_encoding(Encoding::US_ASCII)
@query = v
Expand Down
5 changes: 5 additions & 0 deletions test/uri/test_parser.rb
Expand Up @@ -40,6 +40,11 @@ def test_parse
uri_to_ary(u1))
end

def test_parse_query_pct_encoded
assert_equal('q=%32!$&-/?.09;=:@AZ_az~', URI.parse('https://www.example.com/search?q=%32!$&-/?.09;=:@AZ_az~').query)
assert_raise(URI::InvalidURIError) { URI.parse('https://www.example.com/search?q=%XX') }
end

def test_raise_bad_uri_for_integer
assert_raise(URI::InvalidURIError) do
URI.parse(1)
Expand Down

0 comments on commit 7909f06

Please sign in to comment.