From 4f9eb78b9debe16c5df9978c1610a627f82442e0 Mon Sep 17 00:00:00 2001 From: Tete17 Date: Sat, 9 Aug 2025 03:46:29 +0200 Subject: [PATCH] LibWeb: Refactor magic string in favour of TrustedTypeName --- .../LibWeb/TrustedTypes/TrustedTypePolicy.cpp | 13 ++++++ .../LibWeb/TrustedTypes/TrustedTypePolicy.h | 2 + .../TrustedTypes/TrustedTypePolicyFactory.cpp | 44 +++++++++++-------- .../TrustedTypes/TrustedTypePolicyFactory.h | 2 +- 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.cpp b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.cpp index 3db940861ce..27302ca515d 100644 --- a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.cpp +++ b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.cpp @@ -33,6 +33,19 @@ void TrustedTypePolicy::initialize(JS::Realm& realm) Base::initialize(realm); } +Utf16String to_string(TrustedTypeName trusted_type_name) +{ + switch (trusted_type_name) { + case TrustedTypeName::TrustedHTML: + return "TrustedHTML"_utf16; + case TrustedTypeName::TrustedScript: + return "TrustedScript"_utf16; + case TrustedTypeName::TrustedScriptURL: + return "TrustedScriptURL"_utf16; + default: + VERIFY_NOT_REACHED(); + } +} // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicy-createhtml WebIDL::ExceptionOr> TrustedTypePolicy::create_html(Utf16String const& input, GC::RootVector const& arguments) { diff --git a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.h b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.h index 3640be81177..cbe48cb46c1 100644 --- a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.h +++ b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.h @@ -23,6 +23,8 @@ enum class TrustedTypeName { TrustedScriptURL, }; +Utf16String to_string(TrustedTypeName); + enum class ThrowIfCallbackMissing { Yes, No diff --git a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.cpp b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.cpp index f5e7be352f3..4b03c9b22ca 100644 --- a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.cpp +++ b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.cpp @@ -76,7 +76,7 @@ Optional TrustedTypePolicyFactory::get_attribute_type(Utf16String c // 8. If attributeData is not null, then set expectedType to the interface’s name of the value of the fourth member of attributeData. if (attribute_data.has_value()) { - expected_type = attribute_data.value().trusted_type; + expected_type = to_string(attribute_data.value().trusted_type); } // 9. Return expectedType. @@ -108,24 +108,30 @@ Optional TrustedTypePolicyFactory::get_property_type(Utf16String co // 4. Let expectedType be null. Optional expected_type; - static Vector> const table { - { "HTMLIFrameElement"_utf16, "srcdoc"_utf16, "TrustedHTML"_utf16 }, - { "HTMLScriptElement"_utf16, "innerText"_utf16, "TrustedScript"_utf16 }, - { "HTMLScriptElement"_utf16, "src"_utf16, "TrustedScriptURL"_utf16 }, - { "HTMLScriptElement"_utf16, "text"_utf16, "TrustedScript"_utf16 }, - { "HTMLScriptElement"_utf16, "textContent"_utf16, "TrustedScript"_utf16 }, - { "*"_utf16, "innerHTML"_utf16, "TrustedHTML"_utf16 }, - { "*"_utf16, "outerHTML"_utf16, "TrustedHTML"_utf16 }, + struct TrustedTypesPropertyTypeData { + Utf16String interface; + Utf16String property; + TrustedTypeName trusted_type; + }; + + static Vector const table { + { "HTMLIFrameElement"_utf16, "srcdoc"_utf16, TrustedTypeName::TrustedHTML }, + { "HTMLScriptElement"_utf16, "innerText"_utf16, TrustedTypeName::TrustedScript }, + { "HTMLScriptElement"_utf16, "src"_utf16, TrustedTypeName::TrustedScriptURL }, + { "HTMLScriptElement"_utf16, "text"_utf16, TrustedTypeName::TrustedScript }, + { "HTMLScriptElement"_utf16, "textContent"_utf16, TrustedTypeName::TrustedScript }, + { "*"_utf16, "innerHTML"_utf16, TrustedTypeName::TrustedHTML }, + { "*"_utf16, "outerHTML"_utf16, TrustedTypeName::TrustedHTML }, }; // 5. Find the row in the following table, where the first column is "*" or interface’s name, and property is in the second column. // If a matching row is found, set expectedType to the interface’s name of the value of the third column. auto const matching_row = table.first_matching([&interface, &property](auto const& row) { - return (row[0] == interface || row[0] == "*"sv) && row[1] == property; + return (row.interface == interface || row.interface == "*"sv) && row.property == property; }); if (matching_row.has_value()) { - expected_type = matching_row.value()[2]; + expected_type = to_string(matching_row.value().trusted_type); } // 6. Return expectedType. @@ -293,10 +299,10 @@ Optional get_trusted_type_data_for_attribute(Utf16String const& // 2. If attributeNs is null, and attribute is the name of an event handler content attribute, then: if (!attribute_ns.has_value()) { #undef __ENUMERATE -#define __ENUMERATE(attribute_name, event_name) \ - if (attribute == HTML::AttributeNames::attribute_name) { \ - /* 1. Return (Element, null, attribute, TrustedScript, "Element " + attribute). */ \ - return TrustedTypeData { "Element"_utf16, {}, attribute, "TrustedScript"_utf16, "Element " #attribute_name ""_utf16 }; \ +#define __ENUMERATE(attribute_name, event_name) \ + if (attribute == HTML::AttributeNames::attribute_name) { \ + /* 1. Return (Element, null, attribute, TrustedScript, "Element " + attribute). */ \ + return TrustedTypeData { "Element"_utf16, {}, attribute, TrustedTypeName::TrustedScript, "Element " #attribute_name ""_utf16 }; \ } ENUMERATE_GLOBAL_EVENT_HANDLERS(__ENUMERATE) ENUMERATE_WINDOW_EVENT_HANDLERS(__ENUMERATE) @@ -304,10 +310,10 @@ Optional get_trusted_type_data_for_attribute(Utf16String const& } static Vector const table { - { "HTMLIFrameElement"_utf16, {}, "srcdoc"_utf16, "TrustedHTML"_utf16, "HTMLIFrameElement srcdoc"_utf16 }, - { "HTMLScriptElement"_utf16, {}, "src"_utf16, "TrustedScriptURL"_utf16, "HTMLScriptElement src"_utf16 }, - { "SVGScriptElement"_utf16, {}, "href"_utf16, "TrustedScriptURL"_utf16, "SVGScriptElement href"_utf16 }, - { "SVGScriptElement"_utf16, Utf16String::from_utf8(Namespace::XLink), "href"_utf16, "TrustedScriptURL"_utf16, "SVGScriptElement href"_utf16 }, + { "HTMLIFrameElement"_utf16, {}, "srcdoc"_utf16, TrustedTypeName::TrustedHTML, "HTMLIFrameElement srcdoc"_utf16 }, + { "HTMLScriptElement"_utf16, {}, "src"_utf16, TrustedTypeName::TrustedScriptURL, "HTMLScriptElement src"_utf16 }, + { "SVGScriptElement"_utf16, {}, "href"_utf16, TrustedTypeName::TrustedScriptURL, "SVGScriptElement href"_utf16 }, + { "SVGScriptElement"_utf16, Utf16String::from_utf8(Namespace::XLink), "href"_utf16, TrustedTypeName::TrustedScriptURL, "SVGScriptElement href"_utf16 }, }; // 3. Find the row in the following table, where element is in the first column, attributeNs is in the second column, diff --git a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.h b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.h index 9fc72a56741..e8bcb0edd59 100644 --- a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.h +++ b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.h @@ -57,7 +57,7 @@ struct TrustedTypeData { Utf16String element; Optional attribute_ns; Utf16String attribute_local_name; - Utf16String trusted_type; + TrustedTypeName trusted_type; Utf16String sink; };