Truly independent web browser
LibWeb: Implement SecurityPolicyViolationEvent in Rust
Shannon Boothdca61e64c32c
diff --git a/Libraries/LibWeb/Bindings/Rust/src/content_security_policy/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/content_security_policy/mod.rsnew file mode 100644index 00000000000..b4abf0d7051--- /dev/null+++ b/Libraries/LibWeb/Bindings/Rust/src/content_security_policy/mod.rs@@ -0,0 +1,9 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++//! Content Security Policy, https://w3c.github.io/webappsec-csp/++pub(crate) mod security_policy_violation_event;diff --git a/Libraries/LibWeb/Bindings/Rust/src/content_security_policy/security_policy_violation_event.rs b/Libraries/LibWeb/Bindings/Rust/src/content_security_policy/security_policy_violation_event.rsnew file mode 100644index 00000000000..b219f51ceb4--- /dev/null+++ b/Libraries/LibWeb/Bindings/Rust/src/content_security_policy/security_policy_violation_event.rs@@ -0,0 +1,234 @@+/*+ * Copyright (c) 2025, Luke Wilde <[email protected]>+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++//! SecurityPolicyViolationEvent, https://w3c.github.io/webappsec-csp/#securitypolicyviolationevent++use core::ffi::c_void;+use core::ptr::NonNull;++use ak::{Utf16FlyString, Utf16String};+use libjs_rust::api::{Gc, Realm, Trace, Vm};++use crate::bindings::conversions::UsvString;+use crate::bindings::event_target::CppEvent;+use crate::bindings::interface::{Ref, Wrappable, allocate, realm_arguments};+use crate::bindings::{CallContext, WebIdlResult};+use crate::dom::event::EventInit;++// https://w3c.github.io/webappsec-csp/#enumdef-securitypolicyviolationeventdisposition+#[derive(Clone, Copy, PartialEq, Eq)]+#[repr(u8)]+pub(crate) enum SecurityPolicyViolationEventDisposition {+ Enforce,+ Report,+}++/// The dictionary SecurityPolicyViolationEventInit of ContentSecurityPolicy/SecurityPolicyViolationEvent.idl.+pub(crate) struct SecurityPolicyViolationEventInit {+ pub(crate) parent: EventInit,+ pub(crate) document_uri: UsvString,+ pub(crate) referrer: UsvString,+ pub(crate) blocked_uri: UsvString,+ pub(crate) violated_directive: Utf16String,+ pub(crate) effective_directive: Utf16String,+ pub(crate) original_policy: Utf16String,+ pub(crate) source_file: UsvString,+ pub(crate) sample: Utf16String,+ pub(crate) disposition: SecurityPolicyViolationEventDisposition,+ pub(crate) status_code: u16,+ pub(crate) line_number: u32,+ pub(crate) column_number: u32,+}++/// The members of SecurityPolicyViolationEventInit that C++ lends to create an event.+#[repr(C)]+pub struct FfiSecurityPolicyViolationEventInit {+ bubbles: bool,+ cancelable: bool,+ composed: bool,+ document_uri: usize,+ referrer: usize,+ blocked_uri: usize,+ violated_directive: usize,+ effective_directive: usize,+ original_policy: usize,+ source_file: usize,+ sample: usize,+ disposition: SecurityPolicyViolationEventDisposition,+ status_code: u16,+ line_number: u32,+ column_number: u32,+}++struct SecurityPolicyViolationEventAttributes {+ document_uri: UsvString,+ referrer: UsvString,+ blocked_uri: UsvString,+ violated_directive: Utf16String,+ effective_directive: Utf16String,+ original_policy: Utf16String,+ source_file: UsvString,+ sample: Utf16String,+ disposition: SecurityPolicyViolationEventDisposition,+ status_code: u16,+ line_number: u32,+ column_number: u32,+}++// https://w3c.github.io/webappsec-csp/#securitypolicyviolationevent+#[derive(Trace)]+#[gc(crate = libjs_rust)]+pub(crate) struct SecurityPolicyViolationEvent {+ event: CppEvent,+ #[gc(untraced)]+ attributes: SecurityPolicyViolationEventAttributes,+}++impl Wrappable for SecurityPolicyViolationEvent {+ fn cpp_parent(&self) -> Option<NonNull<c_void>> {+ self.event.as_cpp_parent()+ }+}++impl SecurityPolicyViolationEvent {+ pub(crate) fn create(+ vm: &Vm,+ realm: Gc<Realm>,+ event_name: &Utf16String,+ event_init: SecurityPolicyViolationEventInit,+ ) -> Ref<SecurityPolicyViolationEvent> {+ let event = CppEvent::create_for_owner(+ realm,+ event_name,+ event_init.parent.bubbles,+ event_init.parent.cancelable,+ event_init.parent.composed,+ );+ let object = allocate(vm, |_| SecurityPolicyViolationEvent {+ event,+ attributes: SecurityPolicyViolationEventAttributes {+ document_uri: event_init.document_uri,+ referrer: event_init.referrer,+ blocked_uri: event_init.blocked_uri,+ violated_directive: event_init.violated_directive,+ effective_directive: event_init.effective_directive,+ original_policy: event_init.original_policy,+ source_file: event_init.source_file,+ sample: event_init.sample,+ disposition: event_init.disposition,+ status_code: event_init.status_code,+ line_number: event_init.line_number,+ column_number: event_init.column_number,+ },+ });+ object.event.set_owner(object);+ object+ }++ pub(crate) fn construct(+ cx: &mut CallContext,+ r#type: Utf16String,+ event_init_dict: SecurityPolicyViolationEventInit,+ ) -> WebIdlResult<Ref<SecurityPolicyViolationEvent>> {+ Ok(Self::create(cx.vm(), cx.realm(), &r#type, event_init_dict))+ }++ pub(crate) fn event(&self) -> &CppEvent {+ &self.event+ }++ pub(crate) fn document_uri(&self, _: &mut CallContext) -> WebIdlResult<UsvString> {+ Ok(self.attributes.document_uri.clone())+ }++ pub(crate) fn referrer(&self, _: &mut CallContext) -> WebIdlResult<UsvString> {+ Ok(self.attributes.referrer.clone())+ }++ pub(crate) fn blocked_uri(&self, _: &mut CallContext) -> WebIdlResult<UsvString> {+ Ok(self.attributes.blocked_uri.clone())+ }++ pub(crate) fn effective_directive(&self, _: &mut CallContext) -> WebIdlResult<Utf16String> {+ Ok(self.attributes.effective_directive.clone())+ }++ pub(crate) fn violated_directive(&self, _: &mut CallContext) -> WebIdlResult<Utf16String> {+ Ok(self.attributes.violated_directive.clone())+ }++ pub(crate) fn original_policy(&self, _: &mut CallContext) -> WebIdlResult<Utf16String> {+ Ok(self.attributes.original_policy.clone())+ }++ pub(crate) fn source_file(&self, _: &mut CallContext) -> WebIdlResult<UsvString> {+ Ok(self.attributes.source_file.clone())+ }++ pub(crate) fn sample(&self, _: &mut CallContext) -> WebIdlResult<Utf16String> {+ Ok(self.attributes.sample.clone())+ }++ pub(crate) fn disposition(&self, _: &mut CallContext) -> WebIdlResult<SecurityPolicyViolationEventDisposition> {+ Ok(self.attributes.disposition)+ }++ pub(crate) fn status_code(&self, _: &mut CallContext) -> WebIdlResult<u16> {+ Ok(self.attributes.status_code)+ }++ pub(crate) fn line_number(&self, _: &mut CallContext) -> WebIdlResult<u32> {+ Ok(self.attributes.line_number)+ }++ pub(crate) fn column_number(&self, _: &mut CallContext) -> WebIdlResult<u32> {+ Ok(self.attributes.column_number)+ }+}++/// The Event of a new SecurityPolicyViolationEvent in the realm, for the implementation to fire.+///+/// # Safety+///+/// The VM and the realm must be live. The type and the strings of the dictionary are lent for the call.+#[unsafe(no_mangle)]+pub unsafe extern "C" fn web_rust_security_policy_violation_event_create(+ vm: *mut c_void,+ realm: *mut c_void,+ r#type: usize,+ event_init: &FfiSecurityPolicyViolationEventInit,+) -> *mut c_void {+ // SAFETY: The caller passes the live VM and a live realm.+ let (vm, realm) = unsafe { realm_arguments(vm, realm) };+ // SAFETY: The caller lends each string for the call.+ let string = |raw| unsafe { Utf16String::from_raw_borrowed(raw) };+ let usv_string = |raw| UsvString::from_utf16_string(&string(raw));+ let event_init = SecurityPolicyViolationEventInit {+ parent: EventInit {+ bubbles: event_init.bubbles,+ cancelable: event_init.cancelable,+ composed: event_init.composed,+ },+ document_uri: usv_string(event_init.document_uri),+ referrer: usv_string(event_init.referrer),+ blocked_uri: usv_string(event_init.blocked_uri),+ violated_directive: string(event_init.violated_directive),+ effective_directive: string(event_init.effective_directive),+ original_policy: string(event_init.original_policy),+ source_file: usv_string(event_init.source_file),+ sample: string(event_init.sample),+ disposition: event_init.disposition,+ status_code: event_init.status_code,+ line_number: event_init.line_number,+ column_number: event_init.column_number,+ };+ // SAFETY: The caller lends the type for the call.+ let r#type: Utf16String = unsafe { Utf16FlyString::from_raw_borrowed(r#type) }.into();+ SecurityPolicyViolationEvent::create(vm, realm, &r#type, event_init)+ .event()+ .as_ptr()+}diff --git a/Libraries/LibWeb/Bindings/Rust/src/lib.rs b/Libraries/LibWeb/Bindings/Rust/src/lib.rsindex 75c5fdb14e4..6aad6587c1d 100644--- a/Libraries/LibWeb/Bindings/Rust/src/lib.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/lib.rs@@ -86,6 +86,16 @@ pub(crate) mod compression;clippy::unnecessary_wraps,reason = "the bindings call every attribute and operation as one that can throw")]+pub(crate) mod content_security_policy;+#[cfg(not(test))]+#[cfg_attr(+ not(web_idl_bindings),+ allow(dead_code, reason = "only the generated bindings use the interfaces")+)]+#[allow(+ clippy::unnecessary_wraps,+ reason = "the bindings call every attribute and operation as one that can throw"+)]pub(crate) mod cookie_store;#[cfg(not(test))]#[cfg_attr(diff --git a/Libraries/LibWeb/ContentSecurityPolicy/SecurityPolicyViolationEvent.cpp b/Libraries/LibWeb/ContentSecurityPolicy/SecurityPolicyViolationEvent.cppindex 0f52e172a98..03a624ec832 100644--- a/Libraries/LibWeb/ContentSecurityPolicy/SecurityPolicyViolationEvent.cpp+++ b/Libraries/LibWeb/ContentSecurityPolicy/SecurityPolicyViolationEvent.cpp@@ -1,38 +1,60 @@/** Copyright (c) 2025, Luke Wilde <[email protected]>+ * Copyright (c) 2026-present, the Ladybird developers.** SPDX-License-Identifier: BSD-2-Clause*/-#include <LibGC/Heap.h>+#include <LibJS/HostClassBuilder.h>+#include <LibJS/Runtime/Realm.h>#include <LibWeb/ContentSecurityPolicy/SecurityPolicyViolationEvent.h>namespace Web::ContentSecurityPolicy {-GC_DEFINE_ALLOCATOR(SecurityPolicyViolationEvent);+// Matches the Rust FfiSecurityPolicyViolationEventInit.+struct FfiSecurityPolicyViolationEventInit {+ bool bubbles;+ bool cancelable;+ bool composed;+ FlatPtr document_uri;+ FlatPtr referrer;+ FlatPtr blocked_uri;+ FlatPtr violated_directive;+ FlatPtr effective_directive;+ FlatPtr original_policy;+ FlatPtr source_file;+ FlatPtr sample;+ Bindings::SecurityPolicyViolationEventDisposition disposition;+ u16 status_code;+ u32 line_number;+ u32 column_number;+};-GC::Ref<SecurityPolicyViolationEvent> SecurityPolicyViolationEvent::create(Utf16FlyString const& event_name, SecurityPolicyViolationEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp time_stamp)-{- return GC::Heap::the().allocate<SecurityPolicyViolationEvent>(event_name, event_init, time_stamp);+extern "C" {+// Defined in Bindings/Rust/src/content_security_policy/security_policy_violation_event.rs.+DOM::Event* web_rust_security_policy_violation_event_create(JSVM*, void* realm, FlatPtr event_name, FfiSecurityPolicyViolationEventInit const*);}-SecurityPolicyViolationEvent::SecurityPolicyViolationEvent(Utf16FlyString const& event_name, SecurityPolicyViolationEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp time_stamp)- : Event(FlyString { event_name.view().to_utf8_but_should_be_ported_to_utf16() }, event_init, time_stamp)- , m_document_uri(event_init.document_uri)- , m_referrer(event_init.referrer)- , m_blocked_uri(event_init.blocked_uri)- , m_violated_directive(event_init.violated_directive)- , m_effective_directive(event_init.effective_directive)- , m_original_policy(event_init.original_policy)- , m_source_file(event_init.source_file)- , m_sample(event_init.sample)- , m_disposition(event_init.disposition)- , m_status_code(event_init.status_code)- , m_line_number(event_init.line_number)- , m_column_number(event_init.column_number)+GC::Ref<DOM::Event> create_security_policy_violation_event(JS::Realm& realm, Utf16FlyString const& event_name, SecurityPolicyViolationEventInit const& event_init){+ FfiSecurityPolicyViolationEventInit ffi_event_init {+ .bubbles = event_init.bubbles,+ .cancelable = event_init.cancelable,+ .composed = event_init.composed,+ .document_uri = event_init.document_uri.raw_identity(),+ .referrer = event_init.referrer.raw_identity(),+ .blocked_uri = event_init.blocked_uri.raw_identity(),+ .violated_directive = event_init.violated_directive.raw_identity(),+ .effective_directive = event_init.effective_directive.raw_identity(),+ .original_policy = event_init.original_policy.raw_identity(),+ .source_file = event_init.source_file.raw_identity(),+ .sample = event_init.sample.raw_identity(),+ .disposition = event_init.disposition,+ .status_code = event_init.status_code,+ .line_number = event_init.line_number,+ .column_number = event_init.column_number,+ };+ return *web_rust_security_policy_violation_event_create(JS::HostABI::vm_to_abi(realm.vm()), &realm, event_name.raw_identity(), &ffi_event_init);}-SecurityPolicyViolationEvent::~SecurityPolicyViolationEvent() = default;-}diff --git a/Libraries/LibWeb/ContentSecurityPolicy/SecurityPolicyViolationEvent.h b/Libraries/LibWeb/ContentSecurityPolicy/SecurityPolicyViolationEvent.hindex c1061793b63..1b032c6bdd9 100644--- a/Libraries/LibWeb/ContentSecurityPolicy/SecurityPolicyViolationEvent.h+++ b/Libraries/LibWeb/ContentSecurityPolicy/SecurityPolicyViolationEvent.h@@ -1,5 +1,6 @@/** Copyright (c) 2025, Luke Wilde <[email protected]>+ * Copyright (c) 2026-present, the Ladybird developers.** SPDX-License-Identifier: BSD-2-Clause*/@@ -8,53 +9,29 @@#include <AK/Utf16FlyString.h>#include <AK/Utf16String.h>+#include <LibGC/Ptr.h>#include <LibJS/Forward.h>-#include <LibWeb/Bindings/SecurityPolicyViolationEvent.h>-#include <LibWeb/ContentSecurityPolicy/Policy.h>#include <LibWeb/DOM/Event.h>-#include <LibWebCommon/HighResolutionTime/DOMHighResTimeStamp.h>+#include <LibWeb/Forward.h>+#include <LibWebCommon/Bindings/SecurityPolicyViolationEvent.h>namespace Web::ContentSecurityPolicy {-using SecurityPolicyViolationEventInit = Bindings::SecurityPolicyViolationEventInit;--class SecurityPolicyViolationEvent final : public DOM::Event {- WEB_WRAPPABLE(SecurityPolicyViolationEvent, DOM::Event);- GC_DECLARE_ALLOCATOR(SecurityPolicyViolationEvent);--public:- [[nodiscard]] static GC::Ref<SecurityPolicyViolationEvent> create(Utf16FlyString const& event_name, SecurityPolicyViolationEventInit const& = {}, HighResolutionTime::DOMHighResTimeStamp = 0);-- virtual ~SecurityPolicyViolationEvent() override;-- Utf16String const& document_uri() const { return m_document_uri; }- Utf16String const& referrer() const { return m_referrer; }- Utf16String const& blocked_uri() const { return m_blocked_uri; }- Utf16String const& violated_directive() const { return m_violated_directive; }- Utf16String const& effective_directive() const { return m_effective_directive; }- Utf16String const& original_policy() const { return m_original_policy; }- Utf16String const& source_file() const { return m_source_file; }- Utf16String const& sample() const { return m_sample; }- Bindings::SecurityPolicyViolationEventDisposition disposition() const { return m_disposition; }- u16 status_code() const { return m_status_code; }- u32 line_number() const { return m_line_number; }- u32 column_number() const { return m_column_number; }--private:- SecurityPolicyViolationEvent(Utf16FlyString const& event_name, SecurityPolicyViolationEventInit const&, HighResolutionTime::DOMHighResTimeStamp);-- Utf16String m_document_uri;- Utf16String m_referrer;- Utf16String m_blocked_uri;- Utf16String m_violated_directive;- Utf16String m_effective_directive;- Utf16String m_original_policy;- Utf16String m_source_file;- Utf16String m_sample;- Bindings::SecurityPolicyViolationEventDisposition m_disposition { Bindings::SecurityPolicyViolationEventDisposition::Enforce };- u16 m_status_code { 0 };- u32 m_line_number { 0 };- u32 m_column_number { 0 };+struct SecurityPolicyViolationEventInit : public DOM::EventInit {+ Utf16String document_uri;+ Utf16String referrer;+ Utf16String blocked_uri;+ Utf16String violated_directive;+ Utf16String effective_directive;+ Utf16String original_policy;+ Utf16String source_file;+ Utf16String sample;+ Bindings::SecurityPolicyViolationEventDisposition disposition { Bindings::SecurityPolicyViolationEventDisposition::Enforce };+ u16 status_code { 0 };+ u32 line_number { 0 };+ u32 column_number { 0 };};+GC::Ref<DOM::Event> create_security_policy_violation_event(JS::Realm&, Utf16FlyString const& event_name, SecurityPolicyViolationEventInit const&);+}diff --git a/Libraries/LibWeb/ContentSecurityPolicy/SecurityPolicyViolationEvent.idl b/Libraries/LibWeb/ContentSecurityPolicy/SecurityPolicyViolationEvent.idlindex b36d533341d..3fc447da71b 100644--- a/Libraries/LibWeb/ContentSecurityPolicy/SecurityPolicyViolationEvent.idl+++ b/Libraries/LibWeb/ContentSecurityPolicy/SecurityPolicyViolationEvent.idl@@ -5,7 +5,7 @@ enum SecurityPolicyViolationEventDisposition {};// https://w3c.github.io/webappsec-csp/#securitypolicyviolationevent-[Exposed=(Window,Worker)]+[Exposed=(Window,Worker), ImplementedIn=Rust]interface SecurityPolicyViolationEvent : Event {constructor(DOMString type, optional SecurityPolicyViolationEventInit eventInitDict = {});readonly attribute USVString documentURI;diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Violation.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Violation.cppindex 860fe0c2d62..2c47f7e6e0d 100644--- a/Libraries/LibWeb/ContentSecurityPolicy/Violation.cpp+++ b/Libraries/LibWeb/ContentSecurityPolicy/Violation.cpp@@ -22,7 +22,6 @@#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>#include <LibWeb/HTML/Window.h>#include <LibWeb/HTML/WorkerGlobalScope.h>-#include <LibWeb/HighResolutionTime/TimeOrigin.h>#include <LibWeb/Infra/JSON.h>#include <LibWebCommon/Fetch/Infrastructure/URL.h>@@ -390,7 +389,7 @@ void Violation::report_a_violation(JS::Realm& realm)// violation's sampleevent_init.sample = self.m_sample;- auto event = SecurityPolicyViolationEvent::create(HTML::EventNames::securitypolicyviolation, event_init, HighResolutionTime::current_high_resolution_time(realm.global_object()));+ auto event = create_security_policy_violation_event(realm, HTML::EventNames::securitypolicyviolation, event_init);event->set_is_trusted(true);event_target->dispatch_event(event);}diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.hindex 827d116ae44..61fa73b47d2 100644--- a/Libraries/LibWeb/Forward.h+++ b/Libraries/LibWeb/Forward.h@@ -219,7 +219,6 @@ namespace Web::ContentSecurityPolicy {class Policy;class PolicyList;-class SecurityPolicyViolationEvent;class Violation;}diff --git a/Meta/Linters/check_libweb_realm_mentions.py b/Meta/Linters/check_libweb_realm_mentions.pyindex ba75e73a018..5081c128523 100755--- a/Meta/Linters/check_libweb_realm_mentions.py+++ b/Meta/Linters/check_libweb_realm_mentions.py@@ -47,7 +47,12 @@ ALLOWED_REALM_MENTIONS = {"CSS Typed OM, parser-facing reification, style-value JS materialization, setlike/maplike glue, and wrappers of declaration blocks owned by Rust objects",),"Clipboard": (4, 8, "the C++ facades create the Rust objects and wrap DataTransfers in a given realm"),- "ContentSecurityPolicy": (4, 10, "CSP violation/report objects and callbacks still materialize JS-facing values"),+ "ContentSecurityPolicy": (+ 6,+ 13,+ "CSP violation/report objects and callbacks still materialize JS-facing values; "+ "the C++ glue of the Rust SecurityPolicyViolationEvent creates it in a given realm",+ ),"CookieStore": (2, 3, "the C++ facade creates the Rust CookieStore in a given realm"),"DOM": (11,