1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:17:35 +00:00

LibWeb/MimeSniff: Add sniffing in an audio or video context

This commit is contained in:
Kemal Zebari 2023-11-09 15:32:34 -08:00 committed by Andrew Kaster
parent 02ea85da2c
commit 3a820ddbdf
3 changed files with 86 additions and 7 deletions

View file

@ -50,6 +50,16 @@ static void set_image_type_mappings(HashMap<StringView, Vector<StringView>>& mim
mime_type_to_headers_map.set("image/jpeg"sv, { "\xFF\xD8\xFF"sv });
}
static void set_audio_or_video_type_mappings(HashMap<StringView, Vector<StringView>>& mime_type_to_headers_map)
{
mime_type_to_headers_map.set("audio/aiff"sv, { "FORM\x00\x00\x00\x00\x41IFF"sv });
mime_type_to_headers_map.set("audio/mpeg"sv, { "ID3"sv });
mime_type_to_headers_map.set("application/ogg"sv, { "OggS\x00"sv });
mime_type_to_headers_map.set("audio/midi"sv, { "MThd\x00\x00\x00\x06"sv });
mime_type_to_headers_map.set("video/avi"sv, { "RIFF\x00\x00\x00\x00\x41\x56\x49\x20"sv });
mime_type_to_headers_map.set("audio/wave"sv, { "RIFF\x00\x00\x00\x00WAVE"sv });
}
TEST_CASE(determine_computed_mime_type_in_both_none_and_browsing_sniffing_context)
{
HashMap<StringView, Vector<StringView>> mime_type_to_headers_map;
@ -85,13 +95,8 @@ TEST_CASE(determine_computed_mime_type_in_both_none_and_browsing_sniffing_contex
});
set_image_type_mappings(mime_type_to_headers_map);
set_audio_or_video_type_mappings(mime_type_to_headers_map);
mime_type_to_headers_map.set("audio/aiff"sv, { "FORM\x00\x00\x00\x00\x41IFF"sv });
mime_type_to_headers_map.set("audio/mpeg"sv, { "ID3"sv });
mime_type_to_headers_map.set("application/ogg"sv, { "OggS\x00"sv });
mime_type_to_headers_map.set("audio/midi"sv, { "MThd\x00\x00\x00\x06"sv });
mime_type_to_headers_map.set("video/avi"sv, { "RIFF\x00\x00\x00\x00\x41\x56\x49\x20"sv });
mime_type_to_headers_map.set("audio/wave"sv, { "RIFF\x00\x00\x00\x00WAVE"sv });
mime_type_to_headers_map.set("application/x-gzip"sv, { "\x1F\x8B\x08"sv });
mime_type_to_headers_map.set("application/zip"sv, { "PK\x03\x04"sv });
mime_type_to_headers_map.set("application/x-rar-compressed"sv, { "Rar\x20\x1A\x07\x00"sv });
@ -159,3 +164,41 @@ TEST_CASE(determine_computed_mime_type_in_image_sniffing_context)
EXPECT_EQ(mime_type, computed_mime_type.essence());
}
TEST_CASE(determine_computed_mime_type_in_audio_or_video_sniffing_context)
{
// Cover case where supplied type is an XML MIME type.
auto mime_type = "application/rss+xml"sv;
auto supplied_type = MUST(Web::MimeSniff::MimeType::parse(mime_type)).release_value();
auto computed_mime_type = MUST(Web::MimeSniff::Resource::sniff(""sv.bytes(), Web::MimeSniff::SniffingConfiguration {
.sniffing_context = Web::MimeSniff::SniffingContext::AudioOrVideo,
.supplied_type = supplied_type,
}));
EXPECT_EQ(mime_type, MUST(computed_mime_type.serialized()));
HashMap<StringView, Vector<StringView>> mime_type_to_headers_map;
set_audio_or_video_type_mappings(mime_type_to_headers_map);
// Also consider a resource that is not an audio or video.
mime_type_to_headers_map.set("application/octet-stream"sv, { "\x00"sv });
for (auto const& mime_type_to_headers : mime_type_to_headers_map) {
auto mime_type = mime_type_to_headers.key;
for (auto const& header : mime_type_to_headers.value) {
auto computed_mime_type = MUST(Web::MimeSniff::Resource::sniff(header.bytes(), Web::MimeSniff::SniffingConfiguration { .sniffing_context = Web::MimeSniff::SniffingContext::AudioOrVideo }));
EXPECT_EQ(mime_type, computed_mime_type.essence());
}
}
// Cover case where we aren't dealing with an audio or video MIME type.
mime_type = "text/html"sv;
supplied_type = MUST(Web::MimeSniff::MimeType::parse("text/html"sv)).release_value();
computed_mime_type = MUST(Web::MimeSniff::Resource::sniff(""sv.bytes(), Web::MimeSniff::SniffingConfiguration {
.sniffing_context = Web::MimeSniff::SniffingContext::AudioOrVideo,
.supplied_type = supplied_type,
}));
EXPECT_EQ(mime_type, computed_mime_type.essence());
}