diff --git a/crates/tinycomputer-browser/src/action_gate.rs b/crates/tinycomputer-browser/src/action_gate.rs index ad491b9c4..f2331c414 100644 --- a/crates/tinycomputer-browser/src/action_gate.rs +++ b/crates/tinycomputer-browser/src/action_gate.rs @@ -10,6 +10,8 @@ use tinycomputer_bus::browser::{Action, ElementRef, Target}; pub struct ActionContext { /// The task that is driving the page, when there is one. pub task_id: Option, + /// The retained browser session driving this request, when it is open. + pub session_id: Option, /// Whether payment handling is in play for the task. pub payment_in_play: bool, } diff --git a/crates/tinycomputer-browser/src/sessions/mod.rs b/crates/tinycomputer-browser/src/sessions/mod.rs index acde2480d..ec0709261 100644 --- a/crates/tinycomputer-browser/src/sessions/mod.rs +++ b/crates/tinycomputer-browser/src/sessions/mod.rs @@ -327,6 +327,8 @@ impl Browser { Reservation(&self.opening) }; let id = SessionId::new(format!("s-{}", self.next())); + let mut action_context = action_context; + action_context.session_id = Some(id.to_string()); let mut session = Session { engine: self.launcher.open(id.as_str()), info: SessionInfo { @@ -412,6 +414,8 @@ impl Browser { let session = self.session(id)?; let mut session = session.lock().await; session.check(false).await?; + let mut action_context = action_context; + action_context.session_id = Some(id.to_string()); session.action_context = action_context; Ok(()) } diff --git a/crates/tinycomputer-browser/src/sessions/sessions_tests/page_tests.rs b/crates/tinycomputer-browser/src/sessions/sessions_tests/page_tests.rs index 826bcb46c..fda811556 100644 --- a/crates/tinycomputer-browser/src/sessions/sessions_tests/page_tests.rs +++ b/crates/tinycomputer-browser/src/sessions/sessions_tests/page_tests.rs @@ -222,6 +222,7 @@ async fn gate_receives_task_page_destination_and_value_without_debugging_the_val SessionOptions::default(), ActionContext { task_id: Some("task-17".to_owned()), + session_id: Some("untrusted-session-id".to_owned()), payment_in_play: true, }, ) @@ -248,6 +249,8 @@ async fn gate_receives_task_page_destination_and_value_without_debugging_the_val .find(|request| request.contains("destination_url: Some(\"https://shop.test/checkout\")")) .unwrap(); assert!(navigation.contains("task_id: Some(\"task-17\")")); + assert!(navigation.contains(&format!("session_id: Some(\"{id}\")"))); + assert!(!navigation.contains("untrusted-session-id")); assert!(navigation.contains("payment_in_play: true")); let fill = requests .iter() @@ -259,6 +262,41 @@ async fn gate_receives_task_page_destination_and_value_without_debugging_the_val assert!(!fill.contains("private@example.test")); } +#[tokio::test] +async fn updating_action_context_cannot_replace_the_actual_session_id() { + let fake = Fake::scripted(|_| None); + let gate = Arc::new(CaptureActionRequest::default()); + let browser = Browser::with_scratch(Arc::new(fake), super::scratch("gate-context-update")) + .with_action_gate(gate.clone()); + let id = browser + .open_session(SessionOptions::default()) + .await + .unwrap() + .id; + browser + .update_action_context( + &id, + ActionContext { + task_id: Some("task-18".to_owned()), + session_id: Some("spoofed-session-id".to_owned()), + payment_in_play: true, + }, + ) + .await + .unwrap(); + browser + .navigate(&id, NavigateRequest::new("https://shop.test/checkout")) + .await + .unwrap(); + let requests = gate.0.lock().unwrap(); + let navigation = requests + .iter() + .find(|request| request.contains("destination_url: Some(\"https://shop.test/checkout\")")) + .unwrap(); + assert!(navigation.contains(&format!("session_id: Some(\"{id}\")"))); + assert!(!navigation.contains("spoofed-session-id")); +} + #[tokio::test] async fn an_unknown_current_url_is_not_replaced_with_the_last_known_page() { let url_unavailable = Arc::new(AtomicBool::new(false)); @@ -306,6 +344,7 @@ async fn refused_order_click_never_reaches_the_page_and_reports_the_reason() { SessionOptions::default(), ActionContext { task_id: Some("task-17".to_owned()), + session_id: None, payment_in_play: true, }, ) diff --git a/crates/tinycomputer-browser/src/surface/mod.rs b/crates/tinycomputer-browser/src/surface/mod.rs index 156e31df0..eff0bf146 100644 --- a/crates/tinycomputer-browser/src/surface/mod.rs +++ b/crates/tinycomputer-browser/src/surface/mod.rs @@ -209,9 +209,10 @@ impl BrowserSurface { /// /// Returns an error when the current session is unavailable or either /// the surface or session state is poisoned. - pub async fn update_action_context(&self, context: ActionContext) -> Result<()> { + pub async fn update_action_context(&self, mut context: ActionContext) -> Result<()> { let id = self.session(); if let Some(id) = id { + context.session_id = Some(id.to_string()); self.browser .update_action_context(&id, context.clone()) .await?; diff --git a/crates/tinycomputer-browser/src/surface/surface_tests/operations_tests.rs b/crates/tinycomputer-browser/src/surface/surface_tests/operations_tests.rs index 30363ce10..babcf5a4d 100644 --- a/crates/tinycomputer-browser/src/surface/surface_tests/operations_tests.rs +++ b/crates/tinycomputer-browser/src/surface/surface_tests/operations_tests.rs @@ -89,6 +89,7 @@ fn secret_type_preflight_uses_gate_metadata_without_sending_text_to_the_page() { ) .with_action_context(ActionContext { task_id: Some("task-secret".to_owned()), + session_id: None, payment_in_play: true, }); let screen = surface.observe("browser", None, Depth::Full).unwrap(); diff --git a/crates/tinycomputer-engine/src/task/browser.rs b/crates/tinycomputer-engine/src/task/browser.rs index 569934c80..54049fd8d 100644 --- a/crates/tinycomputer-engine/src/task/browser.rs +++ b/crates/tinycomputer-engine/src/task/browser.rs @@ -101,6 +101,7 @@ impl BrowserRunner { ) .with_action_context(ActionContext { task_id: Some(task.0.clone()), + session_id: None, payment_in_play: constraints.payment == PaymentMode::FillThenApprove, }) }; @@ -274,6 +275,7 @@ impl BrowserRunner { let surface = self.surface(task, constraints); let context = ActionContext { task_id: Some(task.0.clone()), + session_id: None, payment_in_play: constraints.payment == PaymentMode::FillThenApprove, }; let mut request = request; diff --git a/crates/tinycomputer-engine/src/task/browser_tests.rs b/crates/tinycomputer-engine/src/task/browser_tests.rs index 340e285bf..31b5d52de 100644 --- a/crates/tinycomputer-engine/src/task/browser_tests.rs +++ b/crates/tinycomputer-engine/src/task/browser_tests.rs @@ -310,10 +310,14 @@ async fn retained_browser_updates_payment_gate_context_without_reopening() .lock() .map_err(|error| std::io::Error::other(error.to_string()))?; assert!(contexts.iter().any(|context| { - context.task_id.as_deref() == Some(task.0.as_str()) && !context.payment_in_play + context.task_id.as_deref() == Some(task.0.as_str()) + && context.session_id.as_deref() == Some(session.as_str()) + && !context.payment_in_play })); assert!(contexts.iter().any(|context| { - context.task_id.as_deref() == Some(task.0.as_str()) && context.payment_in_play + context.task_id.as_deref() == Some(task.0.as_str()) + && context.session_id.as_deref() == Some(session.as_str()) + && context.payment_in_play })); runner.release(&task); Ok(()) diff --git a/crates/tinycomputer/src/tinybus_module/runner.rs b/crates/tinycomputer/src/tinybus_module/runner.rs index 2b69c93da..5fa1495ad 100644 --- a/crates/tinycomputer/src/tinybus_module/runner.rs +++ b/crates/tinycomputer/src/tinybus_module/runner.rs @@ -75,6 +75,7 @@ impl WorkspaceRunner { .with_cursor(self.cursor.clone()) .with_action_context(ActionContext { task_id: Some(task.0.clone()), + session_id: None, payment_in_play: constraints.payment == PaymentMode::FillThenApprove, }) .with_perception(self.defaults.perception)