Skip to content

Commit

Permalink
handle invalid encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
ahorek authored and jeremyevans committed Feb 25, 2021
1 parent 8f0b257 commit 2b1c2e2
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/cgi/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ def escapeHTML(string)
table = Hash[TABLE_FOR_ESCAPE_HTML__.map {|pair|pair.map {|s|s.encode(enc)}}]
string = string.gsub(/#{"['&\"<>]".encode(enc)}/, table)
string.encode!(origenc) if origenc
return string
string
else
string = string.b
string.gsub!(/['&\"<>]/, TABLE_FOR_ESCAPE_HTML__)
string.force_encoding(enc)
end
string.gsub(/['&\"<>]/, TABLE_FOR_ESCAPE_HTML__)
end

begin
Expand Down Expand Up @@ -90,7 +93,8 @@ def unescapeHTML(string)
when Encoding::ISO_8859_1; 256
else 128
end
string.gsub(/&(apos|amp|quot|gt|lt|\#[0-9]+|\#[xX][0-9A-Fa-f]+);/) do
string = string.b
string.gsub!(/&(apos|amp|quot|gt|lt|\#[0-9]+|\#[xX][0-9A-Fa-f]+);/) do
match = $1.dup
case match
when 'apos' then "'"
Expand All @@ -116,6 +120,7 @@ def unescapeHTML(string)
"&#{match};"
end
end
string.force_encoding enc
end

# Synonym for CGI.escapeHTML(str)
Expand Down

0 comments on commit 2b1c2e2

Please sign in to comment.