From 168311d031ca8c673142913297887fe842fdb95c Mon Sep 17 00:00:00 2001 From: Austin Ziegler Date: Mon, 21 Aug 2023 22:56:07 -0400 Subject: [PATCH 1/2] 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 From ce91b32d1b8e4669fe1c44106c17e09deb9ff2b6 Mon Sep 17 00:00:00 2001 From: Austin Ziegler Date: Mon, 21 Aug 2023 23:02:30 -0400 Subject: [PATCH 2/2] Move truffleruby{,+graalvm}-head to ubuntu-22.04 Allow the builds to fail, because the head build is unstable. --- .github/workflows/ci.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 71746d5..e15d7af 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,9 +24,7 @@ jobs: - jruby - jruby-head - truffleruby - - truffleruby-head - truffleruby+graalvm - - truffleruby+graalvm-head include: - ruby: head continue-on-error: true @@ -45,6 +43,13 @@ jobs: ruby: '3.1' - os: ubuntu-22.04 ruby: '3.2' + - os: ubuntu-22.04 + ruby: truffleruby+graalvm-head + continue-on-error: true + - os: ubuntu-22.04 + ruby: truffleruby-head + continue-on-error: true + runs-on: ${{ matrix.os }} continue-on-error: ${{ matrix.continue-on-error || false }} steps: