diff --git a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.cpp b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.cpp index 38bad0f4ca1..290c2a9ccdd 100644 --- a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.cpp +++ b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -43,11 +44,28 @@ WebIDL::ExceptionOr> TrustedTypePolicy::create_html(String // input // arguments // arguments - return create_a_trusted_type(TrustedTypeName::TrustedHTML, input, arguments); + auto const trusted_type = TRY(create_a_trusted_type(TrustedTypeName::TrustedHTML, input, arguments)); + return trusted_type.get>(); +} + +// https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicy-createscript +WebIDL::ExceptionOr> TrustedTypePolicy::create_script(String const& input, GC::RootVector const& arguments) +{ + // 1. Returns the result of executing the Create a Trusted Type algorithm, with the following arguments: + // policy + // this value + // trustedTypeName + // "TrustedScript" + // value + // input + // arguments + // arguments + auto const trusted_type = TRY(create_a_trusted_type(TrustedTypeName::TrustedScript, input, arguments)); + return trusted_type.get>(); } // https://w3c.github.io/trusted-types/dist/spec/#create-a-trusted-type-algorithm -WebIDL::ExceptionOr> TrustedTypePolicy::create_a_trusted_type(TrustedTypeName trusted_type_name, String const& value, GC::RootVector const& arguments) +TrustedTypesVariants TrustedTypePolicy::create_a_trusted_type(TrustedTypeName trusted_type_name, String const& value, GC::RootVector const& arguments) { auto& vm = this->vm(); auto& realm = this->realm(); @@ -76,7 +94,14 @@ WebIDL::ExceptionOr> TrustedTypePolicy::create_a_trusted_t data_string = ""_utf16; // 5. Return a new instance of an interface with a type name trustedTypeName, with its associated data value set to dataString. - return realm.create(realm, move(data_string)); + switch (trusted_type_name) { + case TrustedTypeName::TrustedHTML: + return realm.create(realm, move(data_string)); + case TrustedTypeName::TrustedScript: + return realm.create(realm, move(data_string)); + default: + VERIFY_NOT_REACHED(); + } } // https://w3c.github.io/trusted-types/dist/spec/#abstract-opdef-get-trusted-type-policy-value diff --git a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.h b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.h index 95aa5bc3171..016548dd653 100644 --- a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.h +++ b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.h @@ -12,6 +12,10 @@ namespace Web::TrustedTypes { +using TrustedTypesVariants = WebIDL::ExceptionOr, + GC::Root>>; + enum class TrustedTypeName { TrustedHTML, TrustedScript, @@ -39,12 +43,13 @@ public: String const& name() const { return m_name; } WebIDL::ExceptionOr> create_html(String const&, GC::RootVector const&); + WebIDL::ExceptionOr> create_script(String const&, GC::RootVector const&); private: explicit TrustedTypePolicy(JS::Realm&, String const&, TrustedTypePolicyOptions const&); virtual void initialize(JS::Realm&) override; - WebIDL::ExceptionOr> create_a_trusted_type(TrustedTypeName, String const&, GC::RootVector const& values); + TrustedTypesVariants create_a_trusted_type(TrustedTypeName, String const&, GC::RootVector const& values); WebIDL::ExceptionOr get_trusted_type_policy_value(TrustedTypeName, String const& value, GC::RootVector const& values, ThrowIfCallbackMissing throw_if_missing); diff --git a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.idl b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.idl index 7a7b36c921f..3c600836da3 100644 --- a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.idl +++ b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.idl @@ -1,11 +1,12 @@ #import +#import // https://w3c.github.io/trusted-types/dist/spec/#trusted-type-policy [Exposed=(Window,Worker)] interface TrustedTypePolicy { readonly attribute DOMString name; TrustedHTML createHTML(DOMString input, any... arguments); - [FIXME] TrustedScript createScript(DOMString input, any... arguments); + TrustedScript createScript(DOMString input, any... arguments); [FIXME] TrustedScriptURL createScriptURL(DOMString input, any... arguments); }; diff --git a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.cpp b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.cpp index c4c325b66c4..5bb38b1ae63 100644 --- a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.cpp +++ b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -164,6 +165,13 @@ bool TrustedTypePolicyFactory::is_html(JS::Value value) return value.is_object() && is(value.as_object()); } +// https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-isscript +bool TrustedTypePolicyFactory::is_script(JS::Value value) +{ + // 1. Returns true if value is an instance of TrustedScript and has an associated data value set, false otherwise. + return value.is_object() && is(value.as_object()); +} + // https://w3c.github.io/trusted-types/dist/spec/#create-trusted-type-policy-algorithm WebIDL::ExceptionOr> TrustedTypePolicyFactory::create_a_trusted_type_policy(String const& policy_name, TrustedTypePolicyOptions const& options, JS::Object& global) { diff --git a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.h b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.h index 618cb9964bb..1319cd48499 100644 --- a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.h +++ b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.h @@ -26,6 +26,7 @@ public: WebIDL::ExceptionOr> create_policy(String const&, TrustedTypePolicyOptions const&); bool is_html(JS::Value); + bool is_script(JS::Value); Optional get_attribute_type(String const& tag_name, String& attribute, Optional element_ns, Optional attr_ns); Optional get_property_type(String const& tag_name, String const& property, Optional element_ns); diff --git a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.idl b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.idl index 74a93d2ab40..614898f6ab1 100644 --- a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.idl +++ b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicyFactory.idl @@ -6,7 +6,7 @@ interface TrustedTypePolicyFactory { TrustedTypePolicy createPolicy( DOMString policyName, optional TrustedTypePolicyOptions policyOptions = {}); boolean isHTML(any value); - [FIXME] boolean isScript(any value); + boolean isScript(any value); [FIXME] boolean isScriptURL(any value); [FIXME] readonly attribute TrustedHTML emptyHTML; [FIXME] readonly attribute TrustedScript emptyScript;