diff --git a/Tests/LibWeb/TestMimeSniff.cpp b/Tests/LibWeb/TestMimeSniff.cpp index 540cbacc654..160254fbe05 100644 --- a/Tests/LibWeb/TestMimeSniff.cpp +++ b/Tests/LibWeb/TestMimeSniff.cpp @@ -320,3 +320,13 @@ TEST_CASE(determine_computed_mime_type_in_a_font_context) EXPECT_EQ(mime_type, computed_mime_type.essence()); } + +TEST_CASE(determine_computed_mime_type_given_text_or_binary_context) +{ + auto supplied_type = MUST(Web::MimeSniff::MimeType::create("text"_string, "plain"_string)); + auto computed_mime_type = MUST(Web::MimeSniff::Resource::sniff("\x00"sv.bytes(), Web::MimeSniff::SniffingConfiguration { + .sniffing_context = Web::MimeSniff::SniffingContext::TextOrBinary, + .supplied_type = supplied_type, + })); + EXPECT_EQ("application/octet-stream"sv, MUST(computed_mime_type.serialized())); +} diff --git a/Userland/Libraries/LibWeb/MimeSniff/Resource.cpp b/Userland/Libraries/LibWeb/MimeSniff/Resource.cpp index 81bd906c0f4..82211e46e8b 100644 --- a/Userland/Libraries/LibWeb/MimeSniff/Resource.cpp +++ b/Userland/Libraries/LibWeb/MimeSniff/Resource.cpp @@ -680,6 +680,8 @@ ErrorOr Resource::context_specific_sniffing_algorithm(SniffingContext snif return rules_for_sniffing_audio_or_video_specifically(); if (sniffing_context == SniffingContext::Font) return rules_for_sniffing_fonts_specifically(); + if (sniffing_context == SniffingContext::TextOrBinary) + return rules_for_distinguishing_if_a_resource_is_text_or_binary(); return {}; } diff --git a/Userland/Libraries/LibWeb/MimeSniff/Resource.h b/Userland/Libraries/LibWeb/MimeSniff/Resource.h index c8a605ac354..2c0704d03b1 100644 --- a/Userland/Libraries/LibWeb/MimeSniff/Resource.h +++ b/Userland/Libraries/LibWeb/MimeSniff/Resource.h @@ -16,6 +16,10 @@ enum class SniffingContext { Image, AudioOrVideo, Font, + + // Non-standard but used in cases where the spec expects us to only run + // https://mimesniff.spec.whatwg.org/#sniffing-a-mislabeled-binary-resource + TextOrBinary, }; struct SniffingConfiguration {