From 6b37095ffd3d7d9629b87c3503acc51c3d67858e Mon Sep 17 00:00:00 2001 From: Kemal Zebari Date: Mon, 13 Nov 2023 21:42:12 -0800 Subject: [PATCH] LibWeb: Validate `MimeType(String, String)` arguments correctly This commit correctly validates the `type` and `subtype` arguments, instead of checking for http quoted code points, by following how the spec's MIME type parsing algorithm would validate a MIME type's type and subtype. It also uses the move-assigned member variables instead of the arguments within the constructor body (as using the arguments at this point will lead to undesired behavior). --- .../Libraries/LibWeb/MimeSniff/MimeType.cpp | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/Userland/Libraries/LibWeb/MimeSniff/MimeType.cpp b/Userland/Libraries/LibWeb/MimeSniff/MimeType.cpp index fa150252e3..0c5f9ddef8 100644 --- a/Userland/Libraries/LibWeb/MimeSniff/MimeType.cpp +++ b/Userland/Libraries/LibWeb/MimeSniff/MimeType.cpp @@ -39,18 +39,6 @@ static bool contains_only_http_quoted_string_token_code_points(StringView string return true; } -MimeType::MimeType(String type, String subtype) - : m_type(move(type)) - , m_subtype(move(subtype)) -{ - // https://mimesniff.spec.whatwg.org/#parameters - // A MIME type’s parameters is an ordered map whose keys are ASCII strings and values are strings limited to HTTP quoted-string token code points. - VERIFY(contains_only_http_quoted_string_token_code_points(type)); - VERIFY(contains_only_http_quoted_string_token_code_points(subtype)); -} - -MimeType::~MimeType() = default; - static bool contains_only_http_token_code_points(StringView string) { // https://mimesniff.spec.whatwg.org/#http-token-code-point @@ -64,6 +52,19 @@ static bool contains_only_http_token_code_points(StringView string) return true; } +MimeType::MimeType(String type, String subtype) + : m_type(move(type)) + , m_subtype(move(subtype)) +{ + // NOTE: type and subtype are expected to be non-empty and contain only + // http token code points in the MIME type parsing algorithm. That's + // why we are performing the same checks here. + VERIFY(!m_type.is_empty() && contains_only_http_token_code_points(m_type)); + VERIFY(!m_subtype.is_empty() && contains_only_http_token_code_points(m_subtype)); +} + +MimeType::~MimeType() = default; + ErrorOr MimeType::create(String type, String value) { auto mime_type = MimeType { move(type), move(value) };