Skip to content

Truly independent web browser

LibWeb: Create parser options in the policy's relevant realm

Shannon Boothe101c3a1b3fb
diff --git a/Libraries/LibWeb/Bindings/Rust/src/trusted_types/trusted_type_policy.rs b/Libraries/LibWeb/Bindings/Rust/src/trusted_types/trusted_type_policy.rs
index de02a1a9012..0438514a452 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/trusted_types/trusted_type_policy.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/trusted_types/trusted_type_policy.rs
@@ -12,7 +12,8 @@ use core::ptr::NonNull;
use ak::Utf16String;
use libjs_rust::api::{
- Gc, MarkedVec, Object, PrimitiveString, RawNativeFunctionResult, Trace, Utf16View, Value, Vm, utf16_formatted,
+ Gc, MarkedVec, Object, PrimitiveString, RawNativeFunctionResult, Realm, Trace, Utf16View, Value, Vm,
+ utf16_formatted,
};
use crate::bindings::callback::CallbackFunction;
@@ -132,6 +133,7 @@ fn stringify(vm: &Vm, trusted_type_name: TrustedTypeName, policy_value: Value) -
#[derive(Trace)]
#[gc(crate = libjs_rust)]
pub(crate) struct TrustedTypePolicy {
+ realm: Gc<Realm>,
name: Utf16String,
create_html: Option<CallbackFunction>,
create_script: Option<CallbackFunction>,
@@ -139,11 +141,16 @@ pub(crate) struct TrustedTypePolicy {
create_parser_options: Option<CallbackFunction>,
}
-impl Wrappable for TrustedTypePolicy {}
+impl Wrappable for TrustedTypePolicy {
+ fn relevant_realm(&self) -> Option<Gc<Realm>> {
+ Some(self.realm)
+ }
+}
impl TrustedTypePolicy {
- pub(crate) fn new(name: Utf16String, options: TrustedTypePolicyOptions) -> Self {
+ pub(crate) fn new(realm: Gc<Realm>, name: Utf16String, options: TrustedTypePolicyOptions) -> Self {
Self {
+ realm,
name,
create_html: options.create_html,
create_script: options.create_script,
@@ -230,11 +237,11 @@ impl TrustedTypePolicy {
// https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicy-createparseroptions
pub(crate) fn create_parser_options(
&self,
- cx: &mut CallContext,
+ _: &mut CallContext,
options: CppDictionary,
arguments: MarkedVec<'_, Value>,
) -> WebIdlResult<CppInterface> {
- let global = cx.realm().global_object();
+ let global = self.realm.global_object();
let callback = self
.create_parser_options
.as_ref()
diff --git a/Libraries/LibWeb/Bindings/Rust/src/trusted_types/trusted_type_policy_factory.rs b/Libraries/LibWeb/Bindings/Rust/src/trusted_types/trusted_type_policy_factory.rs
index 2c53ca9c16d..a50ead73295 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/trusted_types/trusted_type_policy_factory.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/trusted_types/trusted_type_policy_factory.rs
@@ -265,7 +265,9 @@ impl TrustedTypePolicyFactory {
// 4. Let policy be a new TrustedTypePolicy object.
// 5. Set policy’s name property value to policyName.
// 6. Set policy’s options value to «[ "createHTML" -> options["createHTML", "createScript" -> options["createScript", "createScriptURL" -> options["createScriptURL" ]».
- let policy = allocate(cx.vm(), |_| TrustedTypePolicy::new(policy_name.clone(), options));
+ let policy = allocate(cx.vm(), |_| {
+ TrustedTypePolicy::new(self.realm, policy_name.clone(), options)
+ });
// 7. If the policyName is default, set the factory’s default policy value to policy.
if is_default {
diff --git a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.idl b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.idl
index 403c1f32c57..541a699f835 100644
--- a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.idl
+++ b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.idl
@@ -8,7 +8,7 @@ interface TrustedTypePolicy {
[NeedsCallerRealm] TrustedHTML createHTML(DOMString input, any... arguments);
[NeedsCallerRealm] TrustedScript createScript(DOMString input, any... arguments);
[NeedsCallerRealm] TrustedScriptURL createScriptURL(DOMString input, any... arguments);
- [Exposed=Window, NeedsCallerRealm] TrustedHTMLParserOptions createParserOptions(optional SetHTMLUnsafeOptions options = {}, any... arguments);
+ [Exposed=Window] TrustedHTMLParserOptions createParserOptions(optional SetHTMLUnsafeOptions options = {}, any... arguments);
};
// https://w3c.github.io/trusted-types/dist/spec/#trusted-type-policy-options
diff --git a/Tests/LibWeb/Text/expected/TrustedTypes/createParserOptions-uses-policy-realm.txt b/Tests/LibWeb/Text/expected/TrustedTypes/createParserOptions-uses-policy-realm.txt
new file mode 100644
index 00000000000..cf75a3f6729
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/TrustedTypes/createParserOptions-uses-policy-realm.txt
@@ -0,0 +1,2 @@
+argument in policy realm: true
+options in policy realm: true
diff --git a/Tests/LibWeb/Text/input/TrustedTypes/createParserOptions-uses-policy-realm.html b/Tests/LibWeb/Text/input/TrustedTypes/createParserOptions-uses-policy-realm.html
new file mode 100644
index 00000000000..2b971140e44
--- /dev/null
+++ b/Tests/LibWeb/Text/input/TrustedTypes/createParserOptions-uses-policy-realm.html
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<iframe></iframe>
+<script>
+ test(() => {
+ const frame = frames[0];
+ let argument;
+ const policy = frame.trustedTypes.createPolicy("policy", {
+ createParserOptions: options => {
+ argument = options;
+ return options;
+ },
+ });
+ const options = TrustedTypePolicy.prototype.createParserOptions.call(policy, {});
+ println(`argument in policy realm: ${Object.getPrototypeOf(argument) === frame.Object.prototype}`);
+ println(`options in policy realm: ${Object.getPrototypeOf(options) === frame.TrustedHTMLParserOptions.prototype}`);
+ });
+</script>