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).
This commit is contained in:
Kemal Zebari 2023-11-13 21:42:12 -08:00 committed by Andreas Kling
parent 2248d85894
commit 6b37095ffd

View file

@ -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 types 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> MimeType::create(String type, String value)
{
auto mime_type = MimeType { move(type), move(value) };