Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip sanitizing the request body if the charset is non-utf-8 #84

Merged
merged 2 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 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 @@ -115,17 +116,13 @@ def build_strategy(options)
end

def sanitize_rack_input(env)
# https://github.com/rack/rack/blob/master/lib/rack/request.rb#L42
# Logic borrowed from Rack::Request#media_type,#media_type_params,#content_charset
# Ignoring charset in content type.
if content_type = env['CONTENT_TYPE']
content_type = content_type.split(/[;,]/, 2).first
if content_type
content_type.strip!
content_type.downcase!
end
end
request = Rack::Request.new(env)
content_type = request.media_type
return unless @sanitizable_content_types.any? {|type| content_type == type }

charset = request.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