From 27940f28195b4899cc2e4b5ffdbee2519617d1f5 Mon Sep 17 00:00:00 2001 From: Austin Ziegler Date: Mon, 21 Aug 2023 22:56:07 -0400 Subject: [PATCH] Handle `\n` in MIME::Types.type_for Resolves #177. Better handle possible line-termination strings (legal in Unix filenames) such as `\n` in `MIME::Types.type_for`. Reported by ooooooo-q. --- History.md | 10 ++++++++++ lib/mime/type.rb | 2 +- lib/mime/types.rb | 2 +- test/test_mime_types.rb | 4 ++++ test/test_mime_types_class.rb | 5 +++++ 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index a41fd62..789a901 100644 --- a/History.md +++ b/History.md @@ -1,5 +1,13 @@ # Changelog +## 3.5.1 / 2023-08-21 + +- 1 bug fix: + + - Better handle possible line-termination strings (legal in Unix filenames) + such as `\n` in `MIME::Types.type_for`. Reported by ooooooo-q in [#177][], + resolved in [#178][]. + ## 3.5.0 / 2023-08-07 - 1 minor enhancement: @@ -304,6 +312,8 @@ [#166]: https://github.com/mime-types/ruby-mime-types/issues/166 [#167]: https://github.com/mime-types/ruby-mime-types/pull/167 [#170]: https://github.com/mime-types/ruby-mime-types/pull/170 +[#177]: https://github.com/mime-types/ruby-mime-types/issues/177 +[#178]: https://github.com/mime-types/ruby-mime-types/pull/178 [code-of-conduct.md]: Code-of-Conduct_md.html [contributor covenant]: http://contributor-covenant.org [mime-types-data]: https://github.com/mime-types/mime-types-data diff --git a/lib/mime/type.rb b/lib/mime/type.rb index feae131..d19a6a9 100644 --- a/lib/mime/type.rb +++ b/lib/mime/type.rb @@ -93,7 +93,7 @@ def to_s end # The released version of the mime-types library. - VERSION = "3.5.0" + VERSION = "3.5.1" include Comparable diff --git a/lib/mime/types.rb b/lib/mime/types.rb index a6053bc..bb0a3d0 100644 --- a/lib/mime/types.rb +++ b/lib/mime/types.rb @@ -152,7 +152,7 @@ def [](type_id, complete: false, registered: false) # => [application/xml, image/gif, text/xml] def type_for(filename) Array(filename).flat_map { |fn| - @extension_index[fn.chomp.downcase[/\.?([^.]*?)$/, 1]] + @extension_index[fn.chomp.downcase[/\.?([^.]*?)\z/m, 1]] }.compact.inject(Set.new, :+).sort { |a, b| a.priority_compare(b) } diff --git a/test/test_mime_types.rb b/test/test_mime_types.rb index b01b702..a0812a8 100644 --- a/test/test_mime_types.rb +++ b/test/test_mime_types.rb @@ -159,6 +159,10 @@ def mime_types plain_text.add_extensions("xtxt") assert_includes mime_types.type_for("xtxt"), "text/plain" end + + it "handles newline characters correctly" do + assert_includes mime_types.type_for("test.pdf\n.txt"), "text/plain" + end end describe "#count" do diff --git a/test/test_mime_types_class.rb b/test/test_mime_types_class.rb index b47bca2..08e9556 100644 --- a/test/test_mime_types_class.rb +++ b/test/test_mime_types_class.rb @@ -100,6 +100,11 @@ def setup plain_text.add_extensions("xtxt") assert_includes MIME::Types.type_for("xtxt"), "text/plain" end + + it "handles newline characters correctly" do + assert_includes MIME::Types.type_for("test.pdf\n.txt"), "text/plain" + assert_includes MIME::Types.type_for("test.txt\n.pdf"), "application/pdf" + end end describe ".count" do