Skip to content

Commit

Permalink
Handle \n in MIME::Types.type_for
Browse files Browse the repository at this point in the history
Resolves #177.

Better handle possible line-termination strings (legal in Unix
filenames) such as `\n` in `MIME::Types.type_for`. Reported by
ooooooo-q.
  • Loading branch information
halostatue committed Aug 22, 2023
1 parent 9d58c6e commit 27940f2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
10 changes: 10 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/mime/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/mime/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions test/test_mime_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/test_mime_types_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 27940f2

Please sign in to comment.