LibWeb: Hook TrustedTypes to the ServiceWorkers api

This commit is contained in:
Tete17 2025-10-06 18:21:57 +02:00 committed by Luke Wilde
parent 74aa7e8a82
commit 18199f8f2a
Notes: github-actions[bot] 2025-10-13 12:23:16 +00:00
4 changed files with 34 additions and 22 deletions

View file

@ -18,6 +18,9 @@
#include <LibWeb/ServiceWorker/ServiceWorkerContainer.h>
#include <LibWeb/ServiceWorker/ServiceWorkerRegistration.h>
#include <LibWeb/StorageAPI/StorageKey.h>
#include <LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h>
#include <LibWeb/TrustedTypes/TrustedScriptURL.h>
#include <LibWeb/TrustedTypes/TrustedTypePolicy.h>
namespace Web::ServiceWorker {
@ -49,7 +52,7 @@ GC::Ref<ServiceWorkerContainer> ServiceWorkerContainer::create(JS::Realm& realm)
}
// https://w3c.github.io/ServiceWorker/#navigator-service-worker-register
GC::Ref<WebIDL::Promise> ServiceWorkerContainer::register_(String script_url, RegistrationOptions const& options)
GC::Ref<WebIDL::Promise> ServiceWorkerContainer::register_(TrustedTypes::TrustedScriptURLOrString script_url, RegistrationOptions const& options)
{
auto& realm = this->realm();
// Note: The register(scriptURL, options) method creates or updates a service worker registration for the given scope url.
@ -59,15 +62,21 @@ GC::Ref<WebIDL::Promise> ServiceWorkerContainer::register_(String script_url, Re
// 1. Let p be a promise.
auto p = WebIDL::create_promise(realm);
// FIXME: 2. Set scriptURL to the result of invoking Get Trusted Type compliant string with TrustedScriptURL,
// 2. Set scriptURL to the result of invoking Get Trusted Type compliant string with TrustedScriptURL,
// this's relevant global object, scriptURL, "ServiceWorkerContainer register", and "script".
auto const compliant_script_url = MUST(TrustedTypes::get_trusted_type_compliant_string(
TrustedTypes::TrustedTypeName::TrustedScriptURL,
HTML::relevant_global_object(*this),
script_url,
TrustedTypes::InjectionSink::ServiceWorkerContainerregister,
TrustedTypes::Script.to_string()));
// 3 Let client be this's service worker client.
auto client = m_service_worker_client;
// 4. Let scriptURL be the result of parsing scriptURL with this's relevant settings objects API base URL.
auto base_url = HTML::relevant_settings_object(*this).api_base_url();
auto parsed_script_url = DOMURL::parse(script_url, base_url);
auto parsed_script_url = DOMURL::parse(compliant_script_url.to_utf8_but_should_be_ported_to_utf16(), base_url);
// 5. Let scopeURL be null.
Optional<URL::URL> scope_url;

View file

@ -10,6 +10,8 @@
#include <LibWeb/Bindings/ServiceWorkerRegistrationPrototype.h>
#include <LibWeb/Bindings/WorkerPrototype.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/TrustedTypes/TrustedScript.h>
#include <LibWeb/TrustedTypes/TrustedScriptURL.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
#include <LibWeb/WebIDL/Promise.h>
@ -34,7 +36,7 @@ public:
[[nodiscard]] static GC::Ref<ServiceWorkerContainer> create(JS::Realm& realm);
virtual ~ServiceWorkerContainer() override;
GC::Ref<WebIDL::Promise> register_(String script_url, RegistrationOptions const& options);
GC::Ref<WebIDL::Promise> register_(TrustedTypes::TrustedScriptURLOrString script_url, RegistrationOptions const& options);
GC::Ref<WebIDL::Promise> get_registration(String const& client_url);

View file

@ -2,6 +2,7 @@
#import <DOM/EventHandler.idl>
#import <HTML/Worker.idl>
#import <ServiceWorker/ServiceWorkerRegistration.idl>
#import <TrustedTypes/TrustedScriptURL.idl>
// https://w3c.github.io/ServiceWorker/#serviceworkercontainer-interface
[SecureContext, Exposed=(Window,Worker)]
@ -9,8 +10,7 @@ interface ServiceWorkerContainer : EventTarget {
[FIXME] readonly attribute ServiceWorker? controller;
[FIXME] readonly attribute Promise<ServiceWorkerRegistration> ready;
// FIXME: [NewObject] Promise<ServiceWorkerRegistration> register((TrustedScriptURL or USVString) scriptURL, optional RegistrationOptions options = {});
[NewObject, ImplementedAs=register_] Promise<ServiceWorkerRegistration> register(USVString scriptURL, optional RegistrationOptions options = {});
[NewObject, ImplementedAs=register_] Promise<ServiceWorkerRegistration> register((TrustedScriptURL or Utf16USVString) scriptURL, optional RegistrationOptions options = {});
[NewObject] Promise<(ServiceWorkerRegistration or undefined)> getRegistration(optional USVString clientURL = "");
[FIXME, NewObject] Promise<FrozenArray<ServiceWorkerRegistration>> getRegistrations();

View file

@ -16,22 +16,23 @@ namespace Web::TrustedTypes {
__ENUMERATE_INJECTION_SINKS(Element##attribute_name, "Element " #attribute_name)
// https://w3c.github.io/trusted-types/dist/spec/#injection-sink
#define ENUMERATE_INJECTION_SINKS \
__ENUMERATE_INJECTION_SINKS(Documentwrite, "Document write") \
__ENUMERATE_INJECTION_SINKS(Documentwriteln, "Document writeln") \
__ENUMERATE_INJECTION_SINKS(DocumentexecCommand, "Document execCommand") \
__ENUMERATE_INJECTION_SINKS(Function, "Function") \
__ENUMERATE_INJECTION_SINKS(HTMLIFrameElementsrcdoc, "HTMLIFrameElement srcdoc") \
__ENUMERATE_INJECTION_SINKS(HTMLScriptElementinnerText, "HTMLScriptElement innerText") \
__ENUMERATE_INJECTION_SINKS(HTMLScriptElementsrc, "HTMLScriptElement src") \
__ENUMERATE_INJECTION_SINKS(HTMLScriptElementtext, "HTMLScriptElement text") \
__ENUMERATE_INJECTION_SINKS(HTMLScriptElementtextContent, "HTMLScriptElement textContent") \
__ENUMERATE_INJECTION_SINKS(Locationhref, "Location href") \
__ENUMERATE_INJECTION_SINKS(RangecreateContextualFragment, "Range createContextualFragment") \
__ENUMERATE_INJECTION_SINKS(SharedWorkerconstructor, "SharedWorker constructor") \
__ENUMERATE_INJECTION_SINKS(SVGScriptElementhref, "SVGScriptElement href") \
__ENUMERATE_INJECTION_SINKS(Workerconstructor, "Worker constructor") \
ENUMERATE_GLOBAL_EVENT_HANDLERS(EVENT_HANDLERS_INJECTION_SINKS) \
#define ENUMERATE_INJECTION_SINKS \
__ENUMERATE_INJECTION_SINKS(Documentwrite, "Document write") \
__ENUMERATE_INJECTION_SINKS(Documentwriteln, "Document writeln") \
__ENUMERATE_INJECTION_SINKS(DocumentexecCommand, "Document execCommand") \
__ENUMERATE_INJECTION_SINKS(Function, "Function") \
__ENUMERATE_INJECTION_SINKS(HTMLIFrameElementsrcdoc, "HTMLIFrameElement srcdoc") \
__ENUMERATE_INJECTION_SINKS(HTMLScriptElementinnerText, "HTMLScriptElement innerText") \
__ENUMERATE_INJECTION_SINKS(HTMLScriptElementsrc, "HTMLScriptElement src") \
__ENUMERATE_INJECTION_SINKS(HTMLScriptElementtext, "HTMLScriptElement text") \
__ENUMERATE_INJECTION_SINKS(HTMLScriptElementtextContent, "HTMLScriptElement textContent") \
__ENUMERATE_INJECTION_SINKS(Locationhref, "Location href") \
__ENUMERATE_INJECTION_SINKS(RangecreateContextualFragment, "Range createContextualFragment") \
__ENUMERATE_INJECTION_SINKS(ServiceWorkerContainerregister, "ServiceWorkerContainer register") \
__ENUMERATE_INJECTION_SINKS(SharedWorkerconstructor, "SharedWorker constructor") \
__ENUMERATE_INJECTION_SINKS(SVGScriptElementhref, "SVGScriptElement href") \
__ENUMERATE_INJECTION_SINKS(Workerconstructor, "Worker constructor") \
ENUMERATE_GLOBAL_EVENT_HANDLERS(EVENT_HANDLERS_INJECTION_SINKS) \
ENUMERATE_WINDOW_EVENT_HANDLERS(EVENT_HANDLERS_INJECTION_SINKS)
enum class InjectionSink {