summary refs log tree commit
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2022-05-10 10:53:08 -0700
committerJeremy Evans <code@jeremyevans.net>2022-05-25 08:33:11 -0700
commite1e85e80ee85942e0a47d06fc394d843fe883f25 (patch)
tree72123f194da22ddbafb204bfc3a5a157d44279a3
parent86e8f8d1f065ce3a3b571e6944224e8ea962e8be (diff)
downloadrack-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.rb9
-rw-r--r--test/spec_multipart.rb12
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)