Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/tinycomputer-browser/src/action_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// The retained browser session driving this request, when it is open.
pub session_id: Option<String>,
/// Whether payment handling is in play for the task.
pub payment_in_play: bool,
}
Expand Down
4 changes: 4 additions & 0 deletions crates/tinycomputer-browser/src/sessions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
)
Expand All @@ -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()
Expand All @@ -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));
Expand Down Expand Up @@ -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,
},
)
Expand Down
3 changes: 2 additions & 1 deletion crates/tinycomputer-browser/src/surface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions crates/tinycomputer-engine/src/task/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
};
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions crates/tinycomputer-engine/src/task/browser_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
1 change: 1 addition & 0 deletions crates/tinycomputer/src/tinybus_module/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading