The headless browser for AI agents and web scraping
fix(js): read callFunctionOn objectId before value
yvv4git48953ef1b434
diff --git a/crates/obscura-cdp/tests/call_function_on_object_id_arg.rs b/crates/obscura-cdp/tests/call_function_on_object_id_arg.rsnew file mode 100644index 00000000..f906a893--- /dev/null+++ b/crates/obscura-cdp/tests/call_function_on_object_id_arg.rs@@ -0,0 +1,118 @@+use obscura_cdp::dispatch::{dispatch, CdpContext};+use obscura_cdp::types::CdpRequest;+use serde_json::{json, Value};++async fn call_with_arguments(session_id: &str, arguments: Value, declaration: &str) -> Value {+ let mut ctx = CdpContext::new();+ let page_id = ctx.create_page();+ ctx.sessions.insert(session_id.to_string(), page_id);++ let evaluated = dispatch(+ &CdpRequest {+ id: 1,+ method: "Runtime.evaluate".to_string(),+ params: json!({"expression": "window"}),+ session_id: Some(session_id.to_string()),+ },+ &mut ctx,+ )+ .await;+ let window_id = evaluated.result.unwrap()["result"]["objectId"]+ .as_str()+ .expect("window must be returned by reference")+ .to_string();++ let response = dispatch(+ &CdpRequest {+ id: 2,+ method: "Runtime.callFunctionOn".to_string(),+ params: json!({+ "functionDeclaration": declaration,+ "arguments": arguments,+ "objectId": window_id,+ }),+ session_id: Some(session_id.to_string()),+ },+ &mut ctx,+ )+ .await;++ assert!(response.error.is_none(), "{:?}", response.error);+ let reply = response.result.unwrap();+ assert!(reply.get("exceptionDetails").is_none(), "{reply}");+ reply["result"].clone()+}++// go-rod marshals CallArgument from a Go struct, so `omitempty` never drops the+// unset `Value` field (gson.JSON is a struct) and every object handle reaches+// the wire as `{"value":null,"objectId":"..."}`. value must not win over+// objectId, or rod's JS helpers get `null` instead of the `functions` object:+// "TypeError: Cannot set properties of null (setting 'selectable')".+#[tokio::test(flavor = "current_thread")]+async fn object_id_argument_wins_over_a_null_value() {+ let result = call_with_arguments(+ "rod-null-shadow",+ json!([{"value": null, "objectId": "__never_resolves__"}]),+ "function(o) { return o === undefined; }",+ )+ .await;+ assert_eq!(result["type"], "boolean", "{result}");+ assert_eq!(result["value"], json!(true), "{result}");+}++#[tokio::test(flavor = "current_thread")]+async fn object_id_argument_passes_the_real_object() {+ let mut ctx = CdpContext::new();+ let page_id = ctx.create_page();+ let session_id = "rod-arg".to_string();+ ctx.sessions.insert(session_id.clone(), page_id);++ let evaluated = dispatch(+ &CdpRequest {+ id: 1,+ method: "Runtime.evaluate".to_string(),+ params: json!({"expression": "({answer: 42})"}),+ session_id: Some(session_id.clone()),+ },+ &mut ctx,+ )+ .await;+ let object_id = evaluated.result.unwrap()["result"]["objectId"]+ .as_str()+ .expect("object must be returned by reference")+ .to_string();++ // The exact shape go-rod sends (page_eval.go ensureJSHelper).+ let response = dispatch(+ &CdpRequest {+ id: 2,+ method: "Runtime.callFunctionOn".to_string(),+ params: json!({+ "functionDeclaration": "function(o) { return o.answer; }",+ "arguments": [{"value": null, "objectId": object_id}],+ }),+ session_id: Some(session_id),+ },+ &mut ctx,+ )+ .await;++ assert!(response.error.is_none(), "{:?}", response.error);+ let reply = response.result.unwrap();+ assert!(reply.get("exceptionDetails").is_none(), "{reply}");+ assert_eq!(reply["result"]["type"], "number", "{reply}");+ assert_eq!(reply["result"]["value"], json!(42.0), "{reply}");+}++// Without an objectId a null `value` is a genuine null argument and must stay one.+#[tokio::test(flavor = "current_thread")]+async fn null_value_without_object_id_stays_null() {+ let result = call_with_arguments(+ "plain-null",+ json!([{"value": null}]),+ "function(o) { return o === null; }",+ )+ .await;+ assert_eq!(result["type"], "boolean", "{result}");+ assert_eq!(result["value"], json!(true), "{result}");+}diff --git a/crates/obscura-cdp/tests/integration.rs b/crates/obscura-cdp/tests/integration.rsindex e92712d5..cb6acb54 100644--- a/crates/obscura-cdp/tests/integration.rs+++ b/crates/obscura-cdp/tests/integration.rs@@ -25,6 +25,7 @@ integration_tests!(accessibility_names,backspace_surrogate,binding_called_session,+ call_function_on_object_id_arg,cdp_click_submit_parity,child_frame_tree,concurrent_connections_heavy_page,diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex 462153a3..7a34232b 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -4171,16 +4171,23 @@ impl ObscuraJsRuntime {for (i, arg) in arguments.iter().enumerate() {let arg_name = format!("__arg{}", i);- if let Some(value) = arg.get("value") {- let json_str =- serde_json::to_string(value).unwrap_or_else(|_| "undefined".to_string());- setup_lines.push(format!("var {} = {};", arg_name, json_str));- } else if let Some(oid) = arg.get("objectId").and_then(|v| v.as_str()) {+ // objectId wins over value. go-rod marshals CallArgument through a+ // struct, and Go's `omitempty` is a no-op on structs, so every+ // object handle it sends arrives as `{"value":null,"objectId":...}`.+ // Chromium reads objectId first and ignores the null; reading value+ // first passed `null` as the argument and broke rod's JS helpers+ // (`functions.selectable = ...` → "Cannot set properties of null").+ if let Some(oid) = arg.get("objectId").and_then(|v| v.as_str()) {if let Some(retrieval) = self.resolve_remote_object(oid) {setup_lines.push(format!("var {} = {};", arg_name, retrieval));} else {setup_lines.push(format!("var {} = undefined;", arg_name));}+ } else if let Some(value) = arg.get("value") {+ // No handle: a plain JSON argument, explicit null included.+ let json_str =+ serde_json::to_string(value).unwrap_or_else(|_| "undefined".to_string());+ setup_lines.push(format!("var {} = {};", arg_name, json_str));} else if let Some(unser) = arg.get("unserializableValue").and_then(|v| v.as_str()) {// CDP defines UnserializableValue as a closed set. Anything// else is arbitrary source text arriving in a data field;