diff options
author | Jeremy Evans <code@jeremyevans.net> | 2022-05-10 10:53:08 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2022-05-25 08:33:11 -0700 |
commit | e1e85e80ee85942e0a47d06fc394d843fe883f25 (patch) | |
tree | 72123f194da22ddbafb204bfc3a5a157d44279a3 | |
parent | 86e8f8d1f065ce3a3b571e6944224e8ea962e8be (diff) | |
download | rack-e1e85e80ee85942e0a47d06fc394d843fe883f25.tar.gz |
Use binary coding for invalid charsets
Previously, this would raise an error for invalid charsets, but that is unlikely to be desired behavior. While here, inline the CHARSET constant, since it is only used in a single case, and we are using frozen string literals.
-rw-r--r-- | lib/rack/multipart/parser.rb | 9 | ||||
-rw-r--r-- | test/spec_multipart.rb | 12 |
2 files changed, 20 insertions, 1 deletions
diff --git a/lib/rack/multipart/parser.rb b/lib/rack/multipart/parser.rb index ce82cb1b..8e770ecf 100644 --- a/lib/rack/multipart/parser.rb +++ b/lib/rack/multipart/parser.rb @@ -355,6 +355,7 @@ module Rack end CHARSET = "charset" + deprecate_constant :CHARSET def tag_multipart_encoding(filename, content_type, name, body) name = name.to_s @@ -375,7 +376,13 @@ module Rack k.strip! v.strip! v = v[1..-2] if v.start_with?('"') && v.end_with?('"') - encoding = Encoding.find v if k == CHARSET + if k == "charset" + encoding = begin + Encoding.find v + rescue ArgumentError + Encoding::BINARY + end + end end end end diff --git a/test/spec_multipart.rb b/test/spec_multipart.rb index 1510b2a2..8e4a0c4c 100644 --- a/test/spec_multipart.rb +++ b/test/spec_multipart.rb @@ -76,6 +76,18 @@ describe Rack::Multipart do end end + it "sets BINARY encoding for invalid charsets" do + env = Rack::MockRequest.env_for("/", multipart_fixture(:content_type_and_unknown_charset)) + params = Rack::Multipart.parse_multipart(env) + params["text"].encoding.must_equal Encoding::BINARY + + # I'm not 100% sure if making the param name encoding match the + # content-type charset is the right thing to do. We should revisit this. + params.keys.each do |key| + key.encoding.must_equal Encoding::BINARY + end + end + it "set BINARY encoding on things without content type" do env = Rack::MockRequest.env_for("/", multipart_fixture(:none)) params = Rack::Multipart.parse_multipart(env) |