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

LibWeb: Implement more close to spec javascript mime type checking

Previously we would simply check the an input string against a list of
mime type essences, ignoring that the input might not be a valid mime
type or contain parameters.

This patch moves the helpers into the MimeSniff namespace and properly
parses an input string before comparing the essence.
This commit is contained in:
networkException 2022-09-17 17:57:30 +02:00 committed by Andreas Kling
parent 93464d4e41
commit 297e293a3f
3 changed files with 39 additions and 8 deletions

View file

@ -1,6 +1,7 @@
/*
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, networkException <networkexception@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -13,6 +14,16 @@
namespace Web::MimeSniff {
// https://mimesniff.spec.whatwg.org/#javascript-mime-type-essence-match
bool is_javascript_mime_type_essence_match(String const& string)
{
// NOTE: The mime type parser automatically lowercases the essence.
auto type = MimeType::from_string(string);
if (!type.has_value())
return false;
return type->is_javascript();
}
static bool contains_only_http_quoted_string_token_code_points(StringView string)
{
// https://mimesniff.spec.whatwg.org/#http-quoted-string-token-code-point
@ -221,4 +232,26 @@ void MimeType::set_parameter(String const& name, String const& value)
m_parameters.set(name, value);
}
// https://mimesniff.spec.whatwg.org/#javascript-mime-type
bool MimeType::is_javascript() const
{
return essence().is_one_of(
"application/ecmascript"sv,
"application/javascript"sv,
"application/x-ecmascript"sv,
"application/x-javascript"sv,
"text/ecmascript"sv,
"text/javascript"sv,
"text/javascript1.0"sv,
"text/javascript1.1"sv,
"text/javascript1.2"sv,
"text/javascript1.3"sv,
"text/javascript1.4"sv,
"text/javascript1.5"sv,
"text/jscript"sv,
"text/livescript"sv,
"text/x-ecmascript"sv,
"text/x-javascript"sv);
}
}

View file

@ -12,6 +12,8 @@
namespace Web::MimeSniff {
bool is_javascript_mime_type_essence_match(String const&);
// https://mimesniff.spec.whatwg.org/#mime-type
class MimeType {
public:
@ -24,6 +26,8 @@ public:
String const& subtype() const { return m_subtype; }
OrderedHashMap<String, String> const& parameters() const { return m_parameters; }
bool is_javascript() const;
void set_parameter(String const& name, String const& value);
String essence() const;