Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(svg): validate filter property receivers and reflect windowless values

ldm01f03f98f5268
diff --git a/moli-renderer-v8/src/context_bootstrap/svg_runtime/bindings.rs b/moli-renderer-v8/src/context_bootstrap/svg_runtime/bindings.rs
index 2f2dd7857..0e3ef8a5b 100644
--- a/moli-renderer-v8/src/context_bootstrap/svg_runtime/bindings.rs
+++ b/moli-renderer-v8/src/context_bootstrap/svg_runtime/bindings.rs
@@ -1792,7 +1792,7 @@ struct SvgFilterPrimitiveStandardAttributesPrototypeAccessorsDeclaration {
}
#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::SVGComponentTransferFunctionElement, enumerable)]
+#[webapi(interface = web_api_interfaces::SVGComponentTransferFunctionElement, enumerable, receiver)]
struct SvgComponentTransferFunctionElementPrototypeAccessorsDeclaration {
#[webapi(
accessor_property = "type",
@@ -1803,7 +1803,7 @@ struct SvgComponentTransferFunctionElementPrototypeAccessorsDeclaration {
}
#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::SVGFEBlendElement, enumerable)]
+#[webapi(interface = web_api_interfaces::SVGFEBlendElement, enumerable, receiver)]
struct SvgFeBlendElementPrototypeAccessorsDeclaration {
#[webapi(
accessor_property = "mode",
@@ -1814,7 +1814,7 @@ struct SvgFeBlendElementPrototypeAccessorsDeclaration {
}
#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::SVGFEColorMatrixElement, enumerable)]
+#[webapi(interface = web_api_interfaces::SVGFEColorMatrixElement, enumerable, receiver)]
struct SvgFeColorMatrixElementPrototypeAccessorsDeclaration {
#[webapi(
accessor_property = "type",
@@ -1825,7 +1825,7 @@ struct SvgFeColorMatrixElementPrototypeAccessorsDeclaration {
}
#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::SVGFECompositeElement, enumerable)]
+#[webapi(interface = web_api_interfaces::SVGFECompositeElement, enumerable, receiver)]
struct SvgFeCompositeElementPrototypeAccessorsDeclaration {
#[webapi(
accessor_property = "operator",
@@ -1836,7 +1836,7 @@ struct SvgFeCompositeElementPrototypeAccessorsDeclaration {
}
#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::SVGFEConvolveMatrixElement, enumerable)]
+#[webapi(interface = web_api_interfaces::SVGFEConvolveMatrixElement, enumerable, receiver)]
struct SvgFeConvolveMatrixElementPrototypeAccessorsDeclaration {
#[webapi(
accessor_property = "orderX",
@@ -1881,7 +1881,7 @@ struct SvgFeConvolveMatrixElementPrototypeAccessorsDeclaration {
}
#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::SVGFEDisplacementMapElement, enumerable)]
+#[webapi(interface = web_api_interfaces::SVGFEDisplacementMapElement, enumerable, receiver)]
struct SvgFeDisplacementMapElementPrototypeAccessorsDeclaration {
#[webapi(
accessor_property = "xChannelSelector",
@@ -1899,7 +1899,7 @@ struct SvgFeDisplacementMapElementPrototypeAccessorsDeclaration {
}
#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::SVGFEMorphologyElement, enumerable)]
+#[webapi(interface = web_api_interfaces::SVGFEMorphologyElement, enumerable, receiver)]
struct SvgFeMorphologyElementPrototypeAccessorsDeclaration {
#[webapi(
accessor_property = "operator",
@@ -1910,7 +1910,7 @@ struct SvgFeMorphologyElementPrototypeAccessorsDeclaration {
}
#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::SVGFETurbulenceElement, enumerable)]
+#[webapi(interface = web_api_interfaces::SVGFETurbulenceElement, enumerable, receiver)]
struct SvgFeTurbulenceElementPrototypeAccessorsDeclaration {
#[webapi(
accessor_property = "numOctaves",
diff --git a/moli-renderer-v8/src/context_bootstrap/svg_runtime/callbacks.rs b/moli-renderer-v8/src/context_bootstrap/svg_runtime/callbacks.rs
index f48ab6108..f7e3f0597 100644
--- a/moli-renderer-v8/src/context_bootstrap/svg_runtime/callbacks.rs
+++ b/moli-renderer-v8/src/context_bootstrap/svg_runtime/callbacks.rs
@@ -344,27 +344,12 @@ pub(super) fn svg_fe_convolve_matrix_preserve_alpha_getter<'s>(
args: v8::FunctionCallbackArguments<'s>,
rv: v8::ReturnValue<'_, v8::Value>,
) {
- let owner = args.this();
- let Ok((runtime_ptr, handle)) =
- crate::native_bridge::node_runtime_and_handle_from_object_or_detached(scope, owner)
- else {
- webidl::throw_type_error(
- scope,
- "SVGFEConvolveMatrixElement.preserveAlpha called on incompatible receiver.",
- );
+ let owner = moli_webapi_declare::web_api_object_target(scope, args.this())
+ .expect("convolve matrix receiver validated by WebIDL");
+ let Some(context) = owner.get_creation_context(scope) else {
return;
};
- let runtime = unsafe { &*runtime_ptr };
- if !runtime.dom_host().node(handle).is_some_and(|node| {
- node.namespace() == Some(crate::native_bridge::document::SVG_NS)
- && node.local_name() == Some("feConvolveMatrix")
- }) {
- webidl::throw_type_error(
- scope,
- "SVGFEConvolveMatrixElement.preserveAlpha called on incompatible receiver.",
- );
- return;
- }
+ let scope = &mut v8::ContextScope::new(scope, context);
svg_animated_boolean_attribute_getter(
scope,
owner,
@@ -1029,34 +1014,12 @@ pub(super) fn svg_element_animated_integer_getter<'s>(
rv.set_undefined();
return;
};
- let holder = args.this();
- let Ok((runtime_ptr, handle)) =
- crate::native_bridge::node_runtime_and_handle_from_object_or_detached(scope, holder)
- else {
- webidl::throw_type_error(
- scope,
- &format!(
- "{}.{} called on incompatible receiver.",
- property.interface, property.name
- ),
- );
+ let holder = moli_webapi_declare::web_api_object_target(scope, args.this())
+ .expect("integer receiver validated by WebIDL");
+ let Some(context) = holder.get_creation_context(scope) else {
return;
};
- let is_expected_element = unsafe { &*runtime_ptr }
- .dom_host()
- .node(handle)
- .and_then(|node| node.as_element())
- .is_some_and(|element| element.is_svg_element(property.local_name));
- if !is_expected_element {
- webidl::throw_type_error(
- scope,
- &format!(
- "{}.{} called on incompatible receiver.",
- property.interface, property.name
- ),
- );
- return;
- }
+ let scope = &mut v8::ContextScope::new(scope, context);
if let Some(value) = get_private_value(scope, holder, property.cache_slot) {
if let Ok(animated) = v8::Local::<v8::Object>::try_from(value) {
sync_svg_animated_integer_from_owner_attribute(scope, animated);
diff --git a/moli-renderer-v8/src/context_bootstrap/svg_runtime/mod.rs b/moli-renderer-v8/src/context_bootstrap/svg_runtime/mod.rs
index 94347b707..9a00d2bfd 100644
--- a/moli-renderer-v8/src/context_bootstrap/svg_runtime/mod.rs
+++ b/moli-renderer-v8/src/context_bootstrap/svg_runtime/mod.rs
@@ -206,9 +206,6 @@ enum SvgAnimatedIntegerComponent {
#[derive(Clone, Copy)]
struct SvgAnimatedIntegerProperty {
index: usize,
- interface: &'static str,
- local_name: &'static str,
- name: &'static str,
attribute: &'static str,
cache_slot: &'static str,
initial_value: i32,
@@ -1232,9 +1229,6 @@ const SVG_ANIMATED_NUMBER_PROPERTIES: &[SvgAnimatedNumberProperty] = &[
const SVG_ANIMATED_INTEGER_PROPERTIES: &[SvgAnimatedIntegerProperty] = &[
SvgAnimatedIntegerProperty {
index: 0,
- interface: "SVGFEConvolveMatrixElement",
- local_name: "feConvolveMatrix",
- name: "orderX",
attribute: "order",
cache_slot: SVG_FE_CONVOLVE_MATRIX_ORDER_X_SLOT,
initial_value: 3,
@@ -1242,9 +1236,6 @@ const SVG_ANIMATED_INTEGER_PROPERTIES: &[SvgAnimatedIntegerProperty] = &[
},
SvgAnimatedIntegerProperty {
index: 1,
- interface: "SVGFEConvolveMatrixElement",
- local_name: "feConvolveMatrix",
- name: "orderY",
attribute: "order",
cache_slot: SVG_FE_CONVOLVE_MATRIX_ORDER_Y_SLOT,
initial_value: 3,
@@ -1252,9 +1243,6 @@ const SVG_ANIMATED_INTEGER_PROPERTIES: &[SvgAnimatedIntegerProperty] = &[
},
SvgAnimatedIntegerProperty {
index: 2,
- interface: "SVGFEConvolveMatrixElement",
- local_name: "feConvolveMatrix",
- name: "targetX",
attribute: "targetX",
cache_slot: SVG_FE_CONVOLVE_MATRIX_TARGET_X_SLOT,
initial_value: 0,
@@ -1262,9 +1250,6 @@ const SVG_ANIMATED_INTEGER_PROPERTIES: &[SvgAnimatedIntegerProperty] = &[
},
SvgAnimatedIntegerProperty {
index: 3,
- interface: "SVGFEConvolveMatrixElement",
- local_name: "feConvolveMatrix",
- name: "targetY",
attribute: "targetY",
cache_slot: SVG_FE_CONVOLVE_MATRIX_TARGET_Y_SLOT,
initial_value: 0,
@@ -1272,9 +1257,6 @@ const SVG_ANIMATED_INTEGER_PROPERTIES: &[SvgAnimatedIntegerProperty] = &[
},
SvgAnimatedIntegerProperty {
index: 4,
- interface: "SVGFETurbulenceElement",
- local_name: "feTurbulence",
- name: "numOctaves",
attribute: "numOctaves",
cache_slot: SVG_FE_TURBULENCE_NUM_OCTAVES_SLOT,
initial_value: 1,
diff --git a/moli-renderer-v8/src/script_vm/tests/svg_filter_interfaces.rs b/moli-renderer-v8/src/script_vm/tests/svg_filter_interfaces.rs
index 5600a5a11..c94a3a059 100644
--- a/moli-renderer-v8/src/script_vm/tests/svg_filter_interfaces.rs
+++ b/moli-renderer-v8/src/script_vm/tests/svg_filter_interfaces.rs
@@ -128,3 +128,100 @@ fn svg_filter_and_marker_getters_accept_registered_native_proxies() {
"true"
);
}
+
+#[test]
+fn svg_filter_property_getters_and_windowless_writeback_use_native_receivers() {
+ let mut vm = new_storage_page_task_executor_test_vm("https://svg-filter-properties.test/");
+ vm.eval("document.body.innerHTML = '<iframe></iframe>'")
+ .unwrap();
+ vm.eval(include_str!("svg_filter_property_receivers.js"))
+ .unwrap();
+ assert_eq!(vm.eval("__uiEventResults.complete").unwrap(), "true");
+ assert_eq!(vm.eval("__uiEventResults.total").unwrap(), "3078");
+ assert_eq!(
+ vm.eval("JSON.stringify(__uiEventResults.checks.filter(row => !row.passed))")
+ .unwrap(),
+ "[]"
+ );
+}
+
+#[test]
+fn svg_filter_property_getters_accept_registered_native_proxies() {
+ let mut vm =
+ new_storage_page_task_executor_test_vm("https://svg-filter-property-proxies.test/");
+ vm.eval(
+ r#"
+ document.body.innerHTML = '<iframe></iframe>';
+ globalThis.childWindow = document.querySelector('iframe').contentWindow;
+ globalThis.definitions = [
+ ['feFuncR', 'SVGComponentTransferFunctionElement', ['type']],
+ ['feBlend', 'SVGFEBlendElement', ['mode']],
+ ['feColorMatrix', 'SVGFEColorMatrixElement', ['type']],
+ ['feComposite', 'SVGFECompositeElement', ['operator']],
+ ['feConvolveMatrix', 'SVGFEConvolveMatrixElement', ['orderX','orderY','targetX','targetY','edgeMode','preserveAlpha']],
+ ['feDisplacementMap', 'SVGFEDisplacementMapElement', ['xChannelSelector','yChannelSelector']],
+ ['feMorphology', 'SVGFEMorphologyElement', ['operator']],
+ ['feTurbulence', 'SVGFETurbulenceElement', ['numOctaves','stitchTiles','type']],
+ ];
+ for (const [tag] of definitions) {
+ globalThis[tag] = childWindow.document.createElementNS('http://www.w3.org/2000/svg', tag);
+ }
+ "#,
+ )
+ .unwrap();
+ vm.with_default_context_scope_and_checkpoint_for_test(|scope, _| {
+ let global = scope.get_current_context().global(scope);
+ for (tag, proxy_name) in [
+ ("feFuncR", "feFuncRProxy"),
+ ("feBlend", "feBlendProxy"),
+ ("feColorMatrix", "feColorMatrixProxy"),
+ ("feComposite", "feCompositeProxy"),
+ ("feConvolveMatrix", "feConvolveMatrixProxy"),
+ ("feDisplacementMap", "feDisplacementMapProxy"),
+ ("feMorphology", "feMorphologyProxy"),
+ ("feTurbulence", "feTurbulenceProxy"),
+ ] {
+ let key = crate::util::v8str(scope, tag);
+ let object = global.get(scope, key.into()).unwrap();
+ let object = v8::Local::<v8::Object>::try_from(object).unwrap();
+ let handler = crate::util::new_null_prototype_object(scope);
+ let proxy = v8::Proxy::new(scope, object, handler).unwrap();
+ moli_webapi_declare::register_web_api_proxy(scope, proxy).unwrap();
+ let key = crate::util::v8str(scope, proxy_name);
+ assert_eq!(
+ global.create_data_property(scope, key.into(), proxy.into()),
+ Some(true)
+ );
+ }
+ Ok(())
+ })
+ .unwrap();
+ assert_eq!(
+ vm.eval(
+ r#"(() => {
+ for (const [tag, name, properties] of definitions) {
+ const element = globalThis[tag], native = globalThis[tag + 'Proxy'];
+ let traps = 0;
+ const author = new Proxy(native, {get() {traps++;throw 42;}, getPrototypeOf() {traps++;throw 42;}});
+ const revoked = Proxy.revocable(native, {}); revoked.revoke();
+ for (const property of properties) {
+ const getter = Object.getOwnPropertyDescriptor(window[name].prototype, property).get;
+ const value = getter.call(native);
+ if (value !== element[property] || value !== getter.call(element) ||
+ Object.getPrototypeOf(value) !== childWindow[value.constructor.name].prototype) {
+ throw Error(name + '.' + property + ' native proxy state/realm');
+ }
+ for (const receiver of [author, revoked.proxy, Object.create(native)]) {
+ let error;
+ try {getter.call(receiver);} catch (caught) {error = caught;}
+ if (!(error instanceof TypeError) || traps !== 0) throw Error(property + ' author proxy accepted');
+ }
+ }
+ }
+ return true;
+ })()"#,
+ )
+ .unwrap(),
+ "true"
+ );
+}
diff --git a/moli-renderer-v8/src/script_vm/tests/svg_filter_property_receivers.js b/moli-renderer-v8/src/script_vm/tests/svg_filter_property_receivers.js
new file mode 100644
index 000000000..432c86329
--- /dev/null
+++ b/moli-renderer-v8/src/script_vm/tests/svg_filter_property_receivers.js
@@ -0,0 +1,122 @@
+(() => {
+ const checks = [];
+ const check = (name, callback) => {
+ try { checks.push({name, passed: callback() === true}); }
+ catch (error) { checks.push({name, passed: false, error: String(error)}); }
+ };
+ const invoke = (getter, receiver) => Reflect.apply(getter, receiver, []);
+ const typeError = (getter, receiver, realm) => {
+ try { invoke(getter, receiver); }
+ catch (error) { return error instanceof realm.TypeError; }
+ return false;
+ };
+ const ns = 'http://www.w3.org/2000/svg';
+ const child = document.querySelector('iframe').contentWindow;
+ const transfer = [['type', 'SVGAnimatedEnumeration', 'type', 'gamma', 5, 4, 'linear']];
+ const definitions = [
+ ...['feFuncA','feFuncB','feFuncG','feFuncR'].map(tag => [tag, 'SVGComponentTransferFunctionElement', transfer]),
+ ['feBlend', 'SVGFEBlendElement', [['mode', 'SVGAnimatedEnumeration', 'mode', 'screen', 3, 2, 'multiply']]],
+ ['feColorMatrix', 'SVGFEColorMatrixElement', [['type', 'SVGAnimatedEnumeration', 'type', 'hueRotate', 3, 2, 'saturate']]],
+ ['feComposite', 'SVGFECompositeElement', [['operator', 'SVGAnimatedEnumeration', 'operator', 'xor', 5, 2, 'in']]],
+ ['feConvolveMatrix', 'SVGFEConvolveMatrixElement', [
+ ['orderX', 'SVGAnimatedInteger', 'order', '5 6', 5, 7, '7 6'],
+ ['orderY', 'SVGAnimatedInteger', 'order', '5 6', 6, 7, '5 7'],
+ ['targetX', 'SVGAnimatedInteger', 'targetX', '2', 2, 3, '3'],
+ ['targetY', 'SVGAnimatedInteger', 'targetY', '2', 2, 3, '3'],
+ ['edgeMode', 'SVGAnimatedEnumeration', 'edgeMode', 'none', 3, 2, 'wrap'],
+ ['preserveAlpha', 'SVGAnimatedBoolean', 'preserveAlpha', 'true', true, false, 'false'],
+ ]],
+ ['feDisplacementMap', 'SVGFEDisplacementMapElement', [
+ ['xChannelSelector', 'SVGAnimatedEnumeration', 'xChannelSelector', 'B', 3, 1, 'R'],
+ ['yChannelSelector', 'SVGAnimatedEnumeration', 'yChannelSelector', 'G', 2, 1, 'R'],
+ ]],
+ ['feMorphology', 'SVGFEMorphologyElement', [['operator', 'SVGAnimatedEnumeration', 'operator', 'dilate', 2, 1, 'erode']]],
+ ['feTurbulence', 'SVGFETurbulenceElement', [
+ ['numOctaves', 'SVGAnimatedInteger', 'numOctaves', '2', 2, 3, '3'],
+ ['stitchTiles', 'SVGAnimatedEnumeration', 'stitchTiles', 'stitch', 1, 2, 'noStitch'],
+ ['type', 'SVGAnimatedEnumeration', 'type', 'fractalNoise', 1, 2, 'turbulence'],
+ ]],
+ ];
+ for (const [world, realm] of [['main', globalThis], ['child', child]]) {
+ for (const [documentKind, doc] of [
+ ['live', realm.document],
+ ['windowless-html', realm.document.implementation.createHTMLDocument('')],
+ ['windowless-xml', new realm.DOMParser().parseFromString('<root/>', 'application/xml')],
+ ]) {
+ for (const [tag, name, properties] of definitions) {
+ const proto = realm[name].prototype;
+ const make = () => doc.createElementNS(ns, tag);
+ for (const [property, type, attribute, raw, expected, written, serialized] of properties) {
+ const prefix = `${world}/${documentKind}/${tag}/${property}`;
+ const descriptor = Object.getOwnPropertyDescriptor(proto, property);
+ const getter = descriptor.get;
+ const real = make();
+ const value = real[property];
+ check(prefix + '/readonly-descriptor', () => typeof getter === 'function' &&
+ descriptor.set === undefined && descriptor.enumerable && descriptor.configurable);
+ check(prefix + '/function-realm', () => Object.getPrototypeOf(getter) === realm.Function.prototype);
+ check(prefix + '/valid-native-receiver', () => invoke(getter, real) === value && value instanceof realm[type]);
+ check(prefix + '/stable-tear-off', () => invoke(getter, real) === invoke(getter, real));
+ check(prefix + '/clone', () => invoke(getter, real.cloneNode(false)) instanceof realm[type]);
+ check(prefix + '/import', () => invoke(getter, doc.importNode(real, false)) instanceof realm[type]);
+ check(prefix + '/bypass-author-property', () => {
+ const element = make(), original = element[property]; let reads = 0;
+ Object.defineProperty(element, property, {get() { reads++; throw 42; }});
+ return invoke(getter, element) === original && reads === 0;
+ });
+ const other = realm === globalThis ? child : globalThis;
+ const foreign = Object.getOwnPropertyDescriptor(other[name].prototype, property).get;
+ check(prefix + '/borrowed-foreign-getter', () => invoke(foreign, real) === value);
+ check(prefix + '/foreign-error-realm', () => typeError(foreign, {}, other));
+ const revoked = Proxy.revocable(real, {}); revoked.revoke();
+ let traps = 0;
+ const author = new Proxy(real, {
+ get() { traps++; throw 42; },
+ getPrototypeOf() { traps++; throw 42; },
+ has() { traps++; throw 42; },
+ });
+ const wrong = doc.createElementNS(ns, tag === 'feBlend' ? 'feConvolveMatrix' : 'feBlend');
+ Object.setPrototypeOf(wrong, proto);
+ for (const [label, receiver] of [
+ ['undefined', undefined], ['null', null], ['boolean', true], ['number', 42],
+ ['symbol', Symbol('receiver')], ['plain', {}], ['interface-prototype', proto],
+ ['forged-prototype', Object.create(proto)], ['inherit-real', Object.create(real)],
+ ['author-proxy', author], ['revoked-proxy', revoked.proxy],
+ ['wrong-svg', doc.createElementNS(ns, tag === 'feBlend' ? 'feConvolveMatrix' : 'feBlend')],
+ ['html-namespace', doc.createElementNS('http://www.w3.org/1999/xhtml', tag)],
+ ['prototype-swapped-wrong-svg', wrong],
+ ]) {
+ check(prefix + '/receiver/' + label, () => typeError(getter, receiver, realm) && traps === 0);
+ }
+ check(prefix + '/retained-value-after-invalid-calls', () => real[property] === value);
+ check(prefix + '/attribute-forward-reflection', () => {
+ const element = make(), original = element[property];
+ element.setAttribute(attribute, raw);
+ return original === element[property] && original.baseVal === expected && original.animVal === expected;
+ });
+ check(prefix + '/baseVal-writeback', () => {
+ const element = make(); element.setAttribute(attribute, raw);
+ const original = element[property]; original.baseVal = written;
+ return original === element[property] && original.baseVal === written && original.animVal === written &&
+ element.getAttribute(attribute) === serialized;
+ });
+ check(prefix + '/single-native-attribute-mutation', () => {
+ const element = make(); element.setAttribute(attribute, raw);
+ const original = element[property];
+ const observer = new realm.MutationObserver(() => {});
+ observer.observe(element, {attributes: true, attributeOldValue: true});
+ try {
+ original.baseVal = written;
+ const records = observer.takeRecords();
+ return records.length === 1 && records[0].target === element && records[0].attributeName === attribute &&
+ records[0].attributeNamespace === null && records[0].oldValue === raw;
+ } finally { observer.disconnect(); }
+ });
+ }
+ }
+ }
+ }
+ globalThis.__uiEventResults = {complete: true, total: checks.length,
+ passed: checks.filter(row => row.passed).length, checks};
+ return true;
+})()