Skip to content

Truly independent web browser

Meta: Support [SecureContext] attributes in the Rust bindings

Andreas Kling9a1d6214e26e
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs
index 0efbd3d6edf..0484c07adb2 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs
@@ -31,9 +31,9 @@ use libjs_rust::api::{
use crate::bindings::CallContext;
use crate::bindings::realm_data::wrapper_world_of;
use crate::bindings::services::{
- web_bindings_ensure_web_constructor, web_bindings_ensure_web_prototype, web_bindings_realm_is_window,
- web_bindings_rejected_promise, web_bindings_rust_wrapper_for, web_bindings_set_rust_wrapper,
- web_bindings_with_temporary_execution_context,
+ web_bindings_ensure_web_constructor, web_bindings_ensure_web_prototype, web_bindings_realm_is_secure_context,
+ web_bindings_realm_is_window, web_bindings_rejected_promise, web_bindings_rust_wrapper_for,
+ web_bindings_set_rust_wrapper, web_bindings_with_temporary_execution_context,
};
/// What an implementation can tell the bindings about its objects, as a C++ implementation object does by overriding
@@ -376,6 +376,12 @@ pub(crate) fn realm_is_window(realm: Gc<Realm>) -> bool {
unsafe { web_bindings_realm_is_window(realm.as_ptr().cast()) }
}
+/// Whether the realm is a secure context, which the [SecureContext] members are defined for.
+pub(crate) fn realm_is_secure_context(realm: Gc<Realm>) -> bool {
+ // SAFETY: The realm is live.
+ unsafe { web_bindings_realm_is_secure_context(realm.as_ptr().cast()) }
+}
+
/// Runs the steps while an execution context of the realm is on the stack, for steps that can run script but that do not
/// run on behalf of script, as an HTML::TemporaryExecutionContext does for C++.
pub(crate) fn with_temporary_execution_context<F: FnOnce() -> R, R>(realm: Gc<Realm>, steps: F) -> R {
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs
index 8633f55fc71..a6d83ed8a31 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs
@@ -34,6 +34,7 @@ unsafe extern "C" {
/// Whether the global object of the realm is a Window.
pub(crate) fn web_bindings_realm_is_window(realm: *mut c_void) -> bool;
+ pub(crate) fn web_bindings_realm_is_secure_context(realm: *mut c_void) -> bool;
/// Runs the steps with the context while an execution context of the realm is on the stack, as an
/// HTML::TemporaryExecutionContext puts it there, with callbacks enabled if `callbacks_enabled`.
diff --git a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
index 8acc190529e..5b145a8b507 100644
--- a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
+++ b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
@@ -11,6 +11,7 @@
#include <LibJS/Runtime/Realm.h>
#include <LibURL/RustIntegration.h>
#include <LibWeb/Bindings/DOMException.h>
+#include <LibWeb/Bindings/PrincipalHostDefined.h>
#include <LibWeb/Bindings/RustInterface.h>
#include <LibWeb/Bindings/RustSerialization.h>
#include <LibWeb/Bindings/Wrappable.h>
@@ -107,6 +108,7 @@ void* web_bindings_create_callback_function(JSObject* callable, bool returns_pro
JSCompletion web_bindings_invoke_callback_function(void* callback, bool has_this_value, JSValue this_value, ReadonlySpan<JSValue> arguments);
void* web_bindings_invoke_promise_callback_function(void* callback, bool has_this_value, JSValue this_value, ReadonlySpan<JSValue> arguments);
bool web_bindings_realm_is_window(void* realm);
+bool web_bindings_realm_is_secure_context(void* realm);
HTML::Window* web_bindings_window_from_global_object(JSObject* global_object);
DOM::Document* web_bindings_window_associated_document(HTML::Window*);
bool web_bindings_document_is_fully_active(DOM::Document*);
@@ -172,6 +174,11 @@ void* web_css_realm_document(void* realm)
return static_cast<GC::Cell*>(const_cast<DOM::Document*>(HTML::relevant_window(static_cast<JS::Realm*>(realm)->global_object()).document().ptr()));
}
+bool web_bindings_realm_is_secure_context(void* realm)
+{
+ return HTML::is_secure_context(principal_host_defined_environment_settings_object(*static_cast<JS::Realm*>(realm)));
+}
+
bool web_bindings_realm_is_window(void* realm)
{
return HTML::window_from_global_object(static_cast<JS::Realm*>(realm)->global_object());
diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.py
index 3fa0e0aeca7..f733281dbe0 100644
--- a/Meta/Generators/libweb_bindings/rust_bindings.py
+++ b/Meta/Generators/libweb_bindings/rust_bindings.py
@@ -874,7 +874,7 @@ RUST_BINDINGS_IMPORTS = """ #![allow(unused_imports)]
define_attribute, define_cached_attribute, define_constants, define_indexed_properties_iterator,
define_interface_object_properties, define_operation, define_to_string_tag, define_unimplemented_property,
define_value_iterator_properties, entry_point_arguments, implementation_if, implementation_of,
- prototype_from_new_target, realm_is_window, rejected_promise, set_dictionary_member,
+ prototype_from_new_target, realm_is_secure_context, realm_is_window, rejected_promise, set_dictionary_member,
set_interface_prototype_object_prototype, this_object, to_interface,
};
use crate::bindings::async_iterable::{
@@ -1086,13 +1086,16 @@ def write_rust_interface(
continue
name = attribute.attribute.name
getter = f"raw_native!({attribute.getter_function})"
+ is_secure_context_only = "SecureContext" in attribute.attribute.extended_attributes
if attribute.is_cached:
- out.write(f' define_cached_attribute(vm, realm, prototype, "{name}", {getter});\n')
+ definition = f'define_cached_attribute(vm, realm, prototype, "{name}", {getter});'
+ out.write(secure_context_only_definition(definition, is_secure_context_only))
defined_property_names.append(name)
continue
has_setter = not attribute.attribute.readonly or attribute.put_forwards is not None
setter = f"raw_native!({attribute.setter_function})" if has_setter else "None"
- out.write(f' define_attribute(vm, realm, prototype, "{name}", {getter}, {setter});\n')
+ definition = f'define_attribute(vm, realm, prototype, "{name}", {getter}, {setter});'
+ out.write(secure_context_only_definition(definition, is_secure_context_only))
defined_property_names.append(name)
for operation_set in members.operation_sets:
name = operation_set.name
@@ -1322,6 +1325,17 @@ def write_rust_namespace(out: TextIO, context: GenerationContext, interface: Int
out.write(" }\n}\n")
+def secure_context_only_definition(definition: str, is_secure_context_only: bool) -> str:
+ """A statement of the steps that initialize an interface prototype object, which defines a [SecureContext] member
+ only if the realm is a secure context."""
+ if not is_secure_context_only:
+ return f" {definition}\n"
+ return f""" if realm_is_secure_context(realm) {{
+ {definition}
+ }}
+"""
+
+
def window_only_definition(definition: str, is_window_only: bool) -> str:
"""A statement of the steps that initialize an interface object or an interface prototype object, which defines a
member that only a Window has only if the global object of the realm is a Window."""
diff --git a/Meta/Generators/libweb_bindings/rust_members.py b/Meta/Generators/libweb_bindings/rust_members.py
index 5f5d99fc98a..3d56d938c78 100644
--- a/Meta/Generators/libweb_bindings/rust_members.py
+++ b/Meta/Generators/libweb_bindings/rust_members.py
@@ -44,6 +44,7 @@ SUPPORTED_ATTRIBUTE_EXTENDED_ATTRIBUTES = {
"NewObject",
"PutForwards",
"SameObject",
+ "SecureContext",
}
SUPPORTED_OPERATION_EXTENDED_ATTRIBUTES = {
"Default",
@@ -329,7 +330,7 @@ def rust_members(interface: Interface) -> RustMembers:
event handler."""
if attribute.readonly or not attribute.name.startswith("on"):
fail(f"its event handler IDL attribute {attribute.name} is not a plain attribute named on<event type>")
- check_extended_attributes(attribute.name, attribute.extended_attributes, {"ImplementedAs"})
+ check_extended_attributes(attribute.name, attribute.extended_attributes, {"ImplementedAs", "SecureContext"})
setter_method = None
if "ImplementedAs" in attribute.extended_attributes:
setter_method = method_of(attribute.name, attribute.extended_attributes)