aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorAaron Patterson <tenderlove@github.com>2019-03-19 09:13:49 -0700
committerGitHub <noreply@github.com>2019-03-19 09:13:49 -0700
commit12a1a6658c0eff71d528cdda25f29ef1c950d53a (patch)
tree95c14c34a52594ed5b92f83436a39d29667576fe /actionpack
parent04ae0b0b5e594e0bb99c5cd608921745977bcdcd (diff)
parentab38aa45497a38bc4a97f5eca430d43989f0b124 (diff)
downloadrails-12a1a6658c0eff71d528cdda25f29ef1c950d53a.tar.gz
rails-12a1a6658c0eff71d528cdda25f29ef1c950d53a.tar.bz2
rails-12a1a6658c0eff71d528cdda25f29ef1c950d53a.zip
Merge pull request #35669 from cpruitt/update-mime-type-regexp
Update regular expression for checking valid MIME type
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_dispatch/http/mime_type.rb5
-rw-r--r--actionpack/test/dispatch/mime_type_test.rb15
2 files changed, 19 insertions, 1 deletions
diff --git a/actionpack/lib/action_dispatch/http/mime_type.rb b/actionpack/lib/action_dispatch/http/mime_type.rb
index 296a36ad28..962d10d81b 100644
--- a/actionpack/lib/action_dispatch/http/mime_type.rb
+++ b/actionpack/lib/action_dispatch/http/mime_type.rb
@@ -223,7 +223,10 @@ module Mime
attr_reader :hash
MIME_NAME = "[a-zA-Z0-9][a-zA-Z0-9#{Regexp.escape('!#$&-^_.+')}]{0,126}"
- MIME_REGEXP = /\A(?:\*\/\*|#{MIME_NAME}\/(?:\*|#{MIME_NAME}))\z/
+ MIME_PARAMETER_KEY = "[a-zA-Z0-9][a-zA-Z0-9#{Regexp.escape('!#$&-^_.+')}]{0,126}"
+ MIME_PARAMETER_VALUE = "#{Regexp.escape('"')}?[a-zA-Z0-9][a-zA-Z0-9#{Regexp.escape('!#$&-^_.+')}]{0,126}#{Regexp.escape('"')}?"
+ MIME_PARAMETER = "\s*\;\s+#{MIME_PARAMETER_KEY}(?:\=#{MIME_PARAMETER_VALUE})?"
+ MIME_REGEXP = /\A(?:\*\/\*|#{MIME_NAME}\/(?:\*|#{MIME_NAME})(?:\s*#{MIME_PARAMETER}\s*)*)\z/
class InvalidMimeType < StandardError; end
diff --git a/actionpack/test/dispatch/mime_type_test.rb b/actionpack/test/dispatch/mime_type_test.rb
index bb3d888e30..50f6c06fee 100644
--- a/actionpack/test/dispatch/mime_type_test.rb
+++ b/actionpack/test/dispatch/mime_type_test.rb
@@ -181,6 +181,13 @@ class MimeTypeTest < ActiveSupport::TestCase
assert_equal "video/*", Mime::Type.new("video/*").to_s
end
+ test "can be initialized with parameters" do
+ assert_equal "text/html; parameter", Mime::Type.new("text/html; parameter").to_s
+ assert_equal "text/html; parameter=abc", Mime::Type.new("text/html; parameter=abc").to_s
+ assert_equal 'text/html; parameter="abc"', Mime::Type.new('text/html; parameter="abc"').to_s
+ assert_equal 'text/html; parameter=abc; parameter2="xyz"', Mime::Type.new('text/html; parameter=abc; parameter2="xyz"').to_s
+ end
+
test "invalid mime types raise error" do
assert_raises Mime::Type::InvalidMimeType do
Mime::Type.new("too/many/slash")
@@ -191,6 +198,14 @@ class MimeTypeTest < ActiveSupport::TestCase
end
assert_raises Mime::Type::InvalidMimeType do
+ Mime::Type.new("improper/semicolon;")
+ end
+
+ assert_raises Mime::Type::InvalidMimeType do
+ Mime::Type.new('improper/semicolon; parameter=abc; parameter2="xyz";')
+ end
+
+ assert_raises Mime::Type::InvalidMimeType do
Mime::Type.new("text/html, text/plain")
end