diff options
author | networkException <git@nwex.de> | 2022-09-17 17:57:30 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-10-06 16:41:36 +0200 |
commit | 297e293a3f08f07bc1a458ea4f9fefb9a34926b3 (patch) | |
tree | 96d429a896e1d8bff134bd34783e725623d39035 /Userland/Libraries/LibWeb/MimeSniff | |
parent | 93464d4e41e1e0554767d795740b452e29b092c2 (diff) | |
download | serenity-297e293a3f08f07bc1a458ea4f9fefb9a34926b3.zip |
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.
Diffstat (limited to 'Userland/Libraries/LibWeb/MimeSniff')
-rw-r--r-- | Userland/Libraries/LibWeb/MimeSniff/MimeType.cpp | 33 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/MimeSniff/MimeType.h | 4 |
2 files changed, 37 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/MimeSniff/MimeType.cpp b/Userland/Libraries/LibWeb/MimeSniff/MimeType.cpp index a5c0817c52..95c2a05a85 100644 --- a/Userland/Libraries/LibWeb/MimeSniff/MimeType.cpp +++ b/Userland/Libraries/LibWeb/MimeSniff/MimeType.cpp @@ -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); +} + } diff --git a/Userland/Libraries/LibWeb/MimeSniff/MimeType.h b/Userland/Libraries/LibWeb/MimeSniff/MimeType.h index 19f9b7e1d1..be48ccfea9 100644 --- a/Userland/Libraries/LibWeb/MimeSniff/MimeType.h +++ b/Userland/Libraries/LibWeb/MimeSniff/MimeType.h @@ -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; |