uploads: fix it being possible to upload .mkv files as .webm.

Fix it being possible to upload arbitrary .mkv files and have them
be treated as .webm. This was possible because WebM uses the Matroska
container format, and we only checked for the Matroska header, not that
the file was actually a WebM.

There were only 6 such files in production:

* https://danbooru.donmai.us/posts?tags=exif:Matroska:DocType=matroska
* https://danbooru.donmai.us/posts/5522036
* https://danbooru.donmai.us/posts/4743498
* https://danbooru.donmai.us/posts/3925427
* https://danbooru.donmai.us/posts/3147897
* https://danbooru.donmai.us/posts/2965862
* https://danbooru.donmai.us/posts/2430436

These videos are playable in Chrome, but not in Firefox, since Firefox
doesn't support .mkv files (it supports some, depending on which codecs
are used, but not .mkv files in general).
This commit is contained in:
evazion
2022-10-25 19:21:32 -05:00
parent 6413b9abcd
commit df0e9bc4a7
8 changed files with 38 additions and 9 deletions

View File

@@ -20,7 +20,7 @@ class PostPreviewComponentTest < ViewComponent::TestCase
context "for a video post" do
should "render" do
@post = create(:post_with_file, filename: "test-512x512.webm").reload
@post = create(:post_with_file, filename: "webm/test-512x512.webm").reload
node = render_preview(@post, current_user: User.anonymous)
assert_equal(post_path(@post), node.css("article a").attr("href").value)

Binary file not shown.

View File

@@ -224,6 +224,13 @@ class UploadsControllerTest < ActionDispatch::IntegrationTest
end
end
context "for an unsupported video file" do
should "fail for a .mkv file" do
create_upload!("test/files/webm/test-512x512.mkv", user: @user)
assert_match("File type is not supported", Upload.last.error)
end
end
context "for a video longer than the video length limit" do
should "fail for a regular user" do
create_upload!("https://cdn.donmai.us/original/63/cb/63cb09f2526ef3ac14f11c011516ad9b.webm", user: @user)
@@ -293,7 +300,7 @@ class UploadsControllerTest < ActionDispatch::IntegrationTest
should_upload_successfully("test/files/test-static-32x32.gif")
should_upload_successfully("test/files/test-animated-86x52.gif")
should_upload_successfully("test/files/test-300x300.mp4")
should_upload_successfully("test/files/test-512x512.webm")
should_upload_successfully("test/files/webm/test-512x512.webm")
should_upload_successfully("test/files/test-audio.m4v")
# should_upload_successfully("test/files/compressed.swf")

View File

@@ -27,7 +27,7 @@ class MediaFileTest < ActiveSupport::TestCase
should "determine the correct dimensions for a webm file" do
skip unless MediaFile.videos_enabled?
assert_equal([512, 512], MediaFile.open("test/files/test-512x512.webm").dimensions)
assert_equal([512, 512], MediaFile.open("test/files/webm/test-512x512.webm").dimensions)
end
should "determine the correct dimensions for a mp4 file" do
@@ -58,7 +58,7 @@ class MediaFileTest < ActiveSupport::TestCase
should "work for a video if called twice" do
skip unless MediaFile.videos_enabled?
mf = MediaFile.open("test/files/test-512x512.webm")
mf = MediaFile.open("test/files/webm/test-512x512.webm")
assert_equal([512, 512], mf.dimensions)
assert_equal([512, 512], mf.dimensions)
@@ -96,7 +96,7 @@ class MediaFileTest < ActiveSupport::TestCase
end
should "determine the correct extension for a webm file" do
assert_equal(:webm, MediaFile.open("test/files/test-512x512.webm").file_ext)
assert_equal(:webm, MediaFile.open("test/files/webm/test-512x512.webm").file_ext)
end
should "determine the correct extension for a mp4 file" do
@@ -150,7 +150,7 @@ class MediaFileTest < ActiveSupport::TestCase
should "generate a preview image for a video" do
skip unless MediaFile.videos_enabled?
assert_equal([150, 150], MediaFile.open("test/files/test-512x512.webm").preview(150, 150).dimensions)
assert_equal([150, 150], MediaFile.open("test/files/webm/test-512x512.webm").preview(150, 150).dimensions)
assert_equal([150, 150], MediaFile.open("test/files/test-300x300.mp4").preview(150, 150).dimensions)
end
@@ -204,11 +204,17 @@ class MediaFileTest < ActiveSupport::TestCase
context "for a webm file" do
should "determine the duration of the video" do
file = MediaFile.open("test/files/test-512x512.webm")
file = MediaFile.open("test/files/webm/test-512x512.webm")
assert_equal(0.48, file.duration)
assert_equal(10/0.48, file.frame_rate)
assert_equal(10, file.frame_count)
end
should "not detect .mkv files as .webm" do
file = MediaFile.open("test/files/webm/test-512x512.mkv")
assert_equal(false, file.is_supported?)
end
end
context "a compressed SWF file" do

View File

@@ -775,7 +775,7 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
end
should "return posts for the duration:<x> metatag" do
post = create(:post, media_asset: create(:media_asset, file: "test/files/test-512x512.webm"))
post = create(:post, media_asset: create(:media_asset, file: "test/files/webm/test-512x512.webm"))
assert_tag_match([post], "duration:0.48")
assert_tag_match([post], "duration:>0.4")