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): reject invalid length mutations before changing state

ldm07d7189c8977f
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 9223a069d..eebb206eb 100644
--- a/moli-renderer-v8/src/context_bootstrap/svg_runtime/bindings.rs
+++ b/moli-renderer-v8/src/context_bootstrap/svg_runtime/bindings.rs
@@ -5,7 +5,7 @@ use crate::web_api_interfaces;
use moli_webapi_declare::WebApiFunctionTemplate;
#[derive(WebApiFunctionTemplate)]
-#[webapi(interface = web_api_interfaces::SVGLength, enumerable)]
+#[webapi(interface = web_api_interfaces::SVGLength, enumerable, receiver)]
struct SvgLengthTemplateMethodsDeclaration {
#[webapi(constant = "SVG_LENGTHTYPE_UNKNOWN", value = SVG_LENGTH_TYPE_UNKNOWN)]
length_type_unknown: (),
diff --git a/moli-renderer-v8/src/script_vm/tests/mod.rs b/moli-renderer-v8/src/script_vm/tests/mod.rs
index 40c70d47f..e940556e8 100644
--- a/moli-renderer-v8/src/script_vm/tests/mod.rs
+++ b/moli-renderer-v8/src/script_vm/tests/mod.rs
@@ -2186,6 +2186,7 @@ mod storage_dense_name_arrays;
mod streams;
mod svg_filter_interfaces;
mod svg_geometry_methods;
+mod svg_length_validation;
mod svg_number_validation;
mod svg_root_factory_receivers;
mod svg_switch_mpath_interfaces;
diff --git a/moli-renderer-v8/src/script_vm/tests/svg_length_validation.rs b/moli-renderer-v8/src/script_vm/tests/svg_length_validation.rs
new file mode 100644
index 000000000..a0c1137d1
--- /dev/null
+++ b/moli-renderer-v8/src/script_vm/tests/svg_length_validation.rs
@@ -0,0 +1,48 @@
+use super::*;
+
+#[test]
+fn svg_lengths_reject_invalid_mutations_without_changing_the_owner() {
+ let mut vm = new_parsed_test_vm(
+ "https://svg-length-validation.test/",
+ "<!doctype html><html><body><iframe></iframe></body></html>",
+ );
+ assert_eq!(vm.eval(r#"(() => {
+ const ns = 'http://www.w3.org/2000/svg';
+ const child = document.querySelector('iframe').contentWindow;
+ const check = (ok, label) => { if (!ok) throw Error(label); };
+ const error = f => { try { f(); } catch (e) { return e; } };
+ for (const realm of [window, child]) {
+ const rect = realm.document.createElementNS(ns, 'rect');
+ rect.setAttribute('x', '8px');
+ const value = rect.x.baseVal;
+ for (const invalid of ['', 'garbage', '1 px', '1px garbage']) {
+ const e = error(() => { value.valueAsString = invalid; });
+ check(e instanceof realm.DOMException && e.name === 'SyntaxError', 'invalid length text');
+ check(value.value === 8 && rect.getAttribute('x') === '8px', 'atomic text mutation');
+ }
+ for (const invalid of [NaN, Infinity, -Infinity]) {
+ for (const field of ['value', 'valueInSpecifiedUnits']) {
+ check(error(() => { value[field] = invalid; }) instanceof realm.TypeError, 'nonfinite length');
+ check(value.value === 8 && rect.getAttribute('x') === '8px', 'atomic numeric mutation');
+ }
+ }
+ for (const method of ['newValueSpecifiedUnits', 'convertToSpecifiedUnits']) {
+ const fn = realm.SVGLength.prototype[method];
+ for (const unit of [0, 11, 65535]) {
+ const e = error(() => fn.call(value, unit, 5));
+ check(e instanceof realm.DOMException && e.name === 'NotSupportedError', 'invalid unit type');
+ check(value.value === 8 && rect.getAttribute('x') === '8px', 'atomic unit mutation');
+ }
+ let conversions = 0;
+ const argument = {valueOf() { conversions++; return 5; }};
+ for (const receiver of [{}, Object.create(value), new Proxy(value, {})]) {
+ check(error(() => fn.call(receiver, argument, 4)) instanceof realm.TypeError, 'invalid receiver');
+ }
+ check(conversions === 0, 'receiver check precedes conversion');
+ }
+ value.newValueSpecifiedUnits(realm.SVGLength.SVG_LENGTHTYPE_PX, 12);
+ check(value.value === 12 && rect.getAttribute('x') === '12px', 'valid mutation reflects');
+ }
+ return true;
+ })()"#).unwrap(), "true");
+}