LibWeb: Implement methods dependant on TrustedScript

This commit is contained in:
Tete17 2025-08-07 21:30:47 +02:00 committed by Luke Wilde
commit 40691bf25d
Notes: github-actions[bot] 2025-08-11 11:23:06 +00:00
6 changed files with 46 additions and 6 deletions

View file

@ -10,6 +10,7 @@
#include <LibJS/Runtime/Realm.h> #include <LibJS/Runtime/Realm.h>
#include <LibWeb/Bindings/Intrinsics.h> #include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/TrustedTypes/TrustedHTML.h> #include <LibWeb/TrustedTypes/TrustedHTML.h>
#include <LibWeb/TrustedTypes/TrustedScript.h>
#include <LibWeb/WebIDL/AbstractOperations.h> #include <LibWeb/WebIDL/AbstractOperations.h>
#include <LibWeb/WebIDL/CallbackType.h> #include <LibWeb/WebIDL/CallbackType.h>
#include <LibWeb/WebIDL/ExceptionOr.h> #include <LibWeb/WebIDL/ExceptionOr.h>
@ -43,11 +44,28 @@ WebIDL::ExceptionOr<GC::Root<TrustedHTML>> TrustedTypePolicy::create_html(String
// input // input
// arguments // arguments
// 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<GC::Root<TrustedHTML>>();
}
// https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicy-createscript
WebIDL::ExceptionOr<GC::Root<TrustedScript>> TrustedTypePolicy::create_script(String const& input, GC::RootVector<JS::Value> 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<GC::Root<TrustedScript>>();
} }
// https://w3c.github.io/trusted-types/dist/spec/#create-a-trusted-type-algorithm // https://w3c.github.io/trusted-types/dist/spec/#create-a-trusted-type-algorithm
WebIDL::ExceptionOr<GC::Root<TrustedHTML>> TrustedTypePolicy::create_a_trusted_type(TrustedTypeName trusted_type_name, String const& value, GC::RootVector<JS::Value> const& arguments) TrustedTypesVariants TrustedTypePolicy::create_a_trusted_type(TrustedTypeName trusted_type_name, String const& value, GC::RootVector<JS::Value> const& arguments)
{ {
auto& vm = this->vm(); auto& vm = this->vm();
auto& realm = this->realm(); auto& realm = this->realm();
@ -76,7 +94,14 @@ WebIDL::ExceptionOr<GC::Root<TrustedHTML>> TrustedTypePolicy::create_a_trusted_t
data_string = ""_utf16; data_string = ""_utf16;
// 5. Return a new instance of an interface with a type name trustedTypeName, with its associated data value set to dataString. // 5. Return a new instance of an interface with a type name trustedTypeName, with its associated data value set to dataString.
return realm.create<TrustedHTML>(realm, move(data_string)); switch (trusted_type_name) {
case TrustedTypeName::TrustedHTML:
return realm.create<TrustedHTML>(realm, move(data_string));
case TrustedTypeName::TrustedScript:
return realm.create<TrustedScript>(realm, move(data_string));
default:
VERIFY_NOT_REACHED();
}
} }
// https://w3c.github.io/trusted-types/dist/spec/#abstract-opdef-get-trusted-type-policy-value // https://w3c.github.io/trusted-types/dist/spec/#abstract-opdef-get-trusted-type-policy-value

View file

@ -12,6 +12,10 @@
namespace Web::TrustedTypes { namespace Web::TrustedTypes {
using TrustedTypesVariants = WebIDL::ExceptionOr<Variant<
GC::Root<TrustedHTML>,
GC::Root<TrustedScript>>>;
enum class TrustedTypeName { enum class TrustedTypeName {
TrustedHTML, TrustedHTML,
TrustedScript, TrustedScript,
@ -39,12 +43,13 @@ public:
String const& name() const { return m_name; } String const& name() const { return m_name; }
WebIDL::ExceptionOr<GC::Root<TrustedHTML>> create_html(String const&, GC::RootVector<JS::Value> const&); WebIDL::ExceptionOr<GC::Root<TrustedHTML>> create_html(String const&, GC::RootVector<JS::Value> const&);
WebIDL::ExceptionOr<GC::Root<TrustedScript>> create_script(String const&, GC::RootVector<JS::Value> const&);
private: private:
explicit TrustedTypePolicy(JS::Realm&, String const&, TrustedTypePolicyOptions const&); explicit TrustedTypePolicy(JS::Realm&, String const&, TrustedTypePolicyOptions const&);
virtual void initialize(JS::Realm&) override; virtual void initialize(JS::Realm&) override;
WebIDL::ExceptionOr<GC::Root<TrustedHTML>> create_a_trusted_type(TrustedTypeName, String const&, GC::RootVector<JS::Value> const& values); TrustedTypesVariants create_a_trusted_type(TrustedTypeName, String const&, GC::RootVector<JS::Value> const& values);
WebIDL::ExceptionOr<JS::Value> get_trusted_type_policy_value(TrustedTypeName, String const& value, GC::RootVector<JS::Value> const& values, ThrowIfCallbackMissing throw_if_missing); WebIDL::ExceptionOr<JS::Value> get_trusted_type_policy_value(TrustedTypeName, String const& value, GC::RootVector<JS::Value> const& values, ThrowIfCallbackMissing throw_if_missing);

View file

@ -1,11 +1,12 @@
#import <TrustedTypes/TrustedHTML.idl> #import <TrustedTypes/TrustedHTML.idl>
#import <TrustedTypes/TrustedScript.idl>
// https://w3c.github.io/trusted-types/dist/spec/#trusted-type-policy // https://w3c.github.io/trusted-types/dist/spec/#trusted-type-policy
[Exposed=(Window,Worker)] [Exposed=(Window,Worker)]
interface TrustedTypePolicy { interface TrustedTypePolicy {
readonly attribute DOMString name; readonly attribute DOMString name;
TrustedHTML createHTML(DOMString input, any... arguments); 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); [FIXME] TrustedScriptURL createScriptURL(DOMString input, any... arguments);
}; };

View file

@ -21,6 +21,7 @@
#include <LibWeb/Namespace.h> #include <LibWeb/Namespace.h>
#include <LibWeb/SVG/TagNames.h> #include <LibWeb/SVG/TagNames.h>
#include <LibWeb/TrustedTypes/TrustedHTML.h> #include <LibWeb/TrustedTypes/TrustedHTML.h>
#include <LibWeb/TrustedTypes/TrustedScript.h>
#include <LibWeb/TrustedTypes/TrustedTypePolicy.h> #include <LibWeb/TrustedTypes/TrustedTypePolicy.h>
#include <LibWeb/WebIDL/ExceptionOr.h> #include <LibWeb/WebIDL/ExceptionOr.h>
@ -164,6 +165,13 @@ bool TrustedTypePolicyFactory::is_html(JS::Value value)
return value.is_object() && is<TrustedHTML>(value.as_object()); return value.is_object() && is<TrustedHTML>(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<TrustedScript>(value.as_object());
}
// https://w3c.github.io/trusted-types/dist/spec/#create-trusted-type-policy-algorithm // https://w3c.github.io/trusted-types/dist/spec/#create-trusted-type-policy-algorithm
WebIDL::ExceptionOr<GC::Ref<TrustedTypePolicy>> TrustedTypePolicyFactory::create_a_trusted_type_policy(String const& policy_name, TrustedTypePolicyOptions const& options, JS::Object& global) WebIDL::ExceptionOr<GC::Ref<TrustedTypePolicy>> TrustedTypePolicyFactory::create_a_trusted_type_policy(String const& policy_name, TrustedTypePolicyOptions const& options, JS::Object& global)
{ {

View file

@ -26,6 +26,7 @@ public:
WebIDL::ExceptionOr<GC::Ref<TrustedTypePolicy>> create_policy(String const&, TrustedTypePolicyOptions const&); WebIDL::ExceptionOr<GC::Ref<TrustedTypePolicy>> create_policy(String const&, TrustedTypePolicyOptions const&);
bool is_html(JS::Value); bool is_html(JS::Value);
bool is_script(JS::Value);
Optional<String> get_attribute_type(String const& tag_name, String& attribute, Optional<String> element_ns, Optional<String> attr_ns); Optional<String> get_attribute_type(String const& tag_name, String& attribute, Optional<String> element_ns, Optional<String> attr_ns);
Optional<String> get_property_type(String const& tag_name, String const& property, Optional<String> element_ns); Optional<String> get_property_type(String const& tag_name, String const& property, Optional<String> element_ns);

View file

@ -6,7 +6,7 @@ interface TrustedTypePolicyFactory {
TrustedTypePolicy createPolicy( TrustedTypePolicy createPolicy(
DOMString policyName, optional TrustedTypePolicyOptions policyOptions = {}); DOMString policyName, optional TrustedTypePolicyOptions policyOptions = {});
boolean isHTML(any value); boolean isHTML(any value);
[FIXME] boolean isScript(any value); boolean isScript(any value);
[FIXME] boolean isScriptURL(any value); [FIXME] boolean isScriptURL(any value);
[FIXME] readonly attribute TrustedHTML emptyHTML; [FIXME] readonly attribute TrustedHTML emptyHTML;
[FIXME] readonly attribute TrustedScript emptyScript; [FIXME] readonly attribute TrustedScript emptyScript;