Skip to content

Commit

Permalink
Skip if the charset is non-utf-8
Browse files Browse the repository at this point in the history
  • Loading branch information
hibariya authored and whitequark committed Dec 26, 2023
1 parent d8eae48 commit 1393acf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/rack/utf8_sanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'uri'
require 'stringio'
require 'rack/request'

module Rack
class UTF8Sanitizer
Expand Down Expand Up @@ -126,6 +127,10 @@ def sanitize_rack_input(env)
end
end
return unless @sanitizable_content_types.any? {|type| content_type == type }

charset = Rack::Request.new(env).content_charset
return if charset && charset.downcase != 'utf-8'

uri_encoded = URI_ENCODED_CONTENT_TYPES.any? {|type| content_type == type}

if env['rack.input']
Expand Down
24 changes: 24 additions & 0 deletions test/test_utf8_sanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,18 @@ def read
end
end

it "sanitizes the rack body if the charset is present and utf-8" do
input = "name=#{CGI.escape("まつもと")}"
@rack_input = StringIO.new input

env = request_env.update('CONTENT_TYPE' => "application/x-www-form-urlencoded; charset=utf-8")
sanitize_form_data(env) do |sanitized_input|
sanitized_input.encoding.should == Encoding::UTF_8
sanitized_input.should.be.valid_encoding
sanitized_input.should == input
end
end

it "strip UTF-8 BOM from StringIO rack.input" do
input = %(\xef\xbb\xbf{"Hello": "World"})
@rack_input = StringIO.new input
Expand Down Expand Up @@ -327,6 +339,18 @@ def read
end
end

it "does not sanitize the rack body if the charset is present and not utf-8" do
input = "name=".encode("Shift_JIS") + CGI.escape("まつもと".encode("Shift_JIS", "UTF-8"))
@rack_input = StringIO.new input

env = request_env.update('CONTENT_TYPE' => "application/x-www-form-urlencoded; charset=Shift_JIS")
sanitize_form_data(env) do |sanitized_input|
sanitized_input.encoding.should == Encoding::SHIFT_JIS
sanitized_input.should.be.valid_encoding
sanitized_input.should == input
end
end

it "adjusts content-length when replacing input" do
input = "foo=bla&quux=bar\xED"
@rack_input = StringIO.new input
Expand Down

0 comments on commit 1393acf

Please sign in to comment.