The headless browser for AI agents and web scraping
fix(js): reject unknown createEvent interfaces
xrip7e6f40356ccf
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.jsindex e8495742..177298e3 100644--- a/crates/obscura-js/js/bootstrap.js+++ b/crates/obscura-js/js/bootstrap.js@@ -4890,19 +4890,18 @@ class Document extends Node {// returned a generic Event for every type, which broke libraries that call// createEvent('CustomEvent').initCustomEvent(...) — see issue #41.createEvent(type) {- const normalized = String(type || '').toLowerCase();- if (normalized === 'promiserejectionevent') {- throw new DOMException(- "The provided event type ('PromiseRejectionEvent') is invalid",- 'NotSupportedError'- );- }+ const eventType = String(type || '');+ const normalized = eventType.toLowerCase();const map = {+ 'event': Event, 'events': Event,+ 'htmlevents': Event, 'svgevents': Event,'customevent': CustomEvent, 'customevents': CustomEvent,'mouseevent': MouseEvent, 'mouseevents': MouseEvent,'keyboardevent': KeyboardEvent, 'keyboardevents': KeyboardEvent,'focusevent': FocusEvent,+ 'hashchangeevent': HashChangeEvent,'inputevent': InputEvent,+ 'messageevent': MessageEvent,'uievent': UIEvent, 'uievents': UIEvent,'compositionevent': CompositionEvent,'wheelevent': WheelEvent,@@ -4913,7 +4912,13 @@ class Document extends Node {'transitionevent': TransitionEvent,'storageevent': StorageEvent,};- const Cls = map[normalized] || Event;+ const Cls = map[normalized];+ if (!Cls) {+ throw new DOMException(+ `The provided event type ('${eventType}') is invalid`,+ 'NotSupportedError'+ );+ }return new Cls('');}createRange() { return new Range(); }diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex 5e593cf9..d5ea07e7 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -13900,6 +13900,14 @@ mod tests {.evaluate("document.createEvent('KeyboardEvent') instanceof KeyboardEvent").unwrap();assert_eq!(kb, serde_json::json!(true));+ let hash_change = rt+ .evaluate("document.createEvent('HashChangeEvent') instanceof HashChangeEvent")+ .unwrap();+ assert_eq!(hash_change, serde_json::json!(true));+ let message = rt+ .evaluate("document.createEvent('MessageEvent') instanceof MessageEvent")+ .unwrap();+ assert_eq!(message, serde_json::json!(true));}#[test]@@ -13917,12 +13925,21 @@ mod tests {}#[test]- fn test_create_event_unknown_type_returns_event() {+ fn test_create_event_rejects_unknown_interface() {let mut rt = setup_runtime("<html><body></body></html>");- let kind = rt- .evaluate("document.createEvent('NotARealType') instanceof Event")+ let result = rt+ .evaluate(+ r#"(() => {+ try {+ document.createEvent('NotAnEventInterface');+ return null;+ } catch (error) {+ return [error.name, error instanceof DOMException];+ }+ })()"#,+ ).unwrap();- assert_eq!(kind, serde_json::json!(true));+ assert_eq!(result, serde_json::json!(["NotSupportedError", true]));}#[test]@@ -14000,6 +14017,28 @@ mod tests {assert_eq!(result, serde_json::json!(["NotSupportedError", true]));}+ #[test]+ fn test_create_event_supports_legacy_event_aliases() {+ let mut rt = setup_runtime("<html><body></body></html>");+ let result = rt+ .evaluate(+ r#"['Event', 'Events', 'HTMLEvents', 'SVGEvents'].map(name => {+ const event = document.createEvent(name);+ return [event instanceof Event, event.constructor === Event, event.type];+ })"#,+ )+ .unwrap();+ assert_eq!(+ result,+ serde_json::json!([+ [true, true, ""],+ [true, true, ""],+ [true, true, ""],+ [true, true, ""]+ ])+ );+ }+#[test]fn test_storage_event_constructor_and_legacy_factory() {let mut rt = setup_runtime("<html><body></body></html>");