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
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ async fn live_chromium_worker_fetch_never_reaches_a_denied_destination() {
let denied_targets = Arc::new(Mutex::new(Vec::new()));
let fixture = TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = fixture.local_addr().unwrap();
let html = format!("<script>new Worker('/worker.js')</script><p>worker fixture</p>");
let html = "<script>new Worker('/worker.js')</script><p>worker fixture</p>".to_string();
let worker = format!(
"fetch('http://127.0.0.1:{}/private').catch(() => {{}});",
denied_address.port()
Expand Down Expand Up @@ -367,7 +367,7 @@ async fn live_chromium_dns_rebinding_never_reaches_the_rebound_private_socket()
.execute(json!({"id":"close","action":"close"}))
.await;
assert_eq!(navigation["success"], true, "{navigation}");
let observed_addresses = observed_addresses.lock().unwrap();
let observed_addresses = observed_addresses.lock().unwrap().clone();
assert_eq!(
observed_addresses.first(),
Some(&allowed_address),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ async fn live_chromium_denies_stale_egress_generation_before_dialing() -> tinybu
Some(EgressHost {
allowed: Arc::new(AtomicBool::new(true)),
allowed_addresses: vec![allowed_address],
resolved_address: None,
grant_generation: Some(40),
authorize_delay: Duration::ZERO,
observations: observations.clone(),
Expand Down Expand Up @@ -168,6 +169,7 @@ async fn live_chromium_denies_when_host_authorization_times_out() -> tinybus::Re
Some(EgressHost {
allowed: Arc::new(AtomicBool::new(true)),
allowed_addresses: vec![denied_address.clone()],
resolved_address: None,
grant_generation: None,
authorize_delay: Duration::from_secs(6),
observations: observations.clone(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//! Live Chromium coverage for a fixture host rebound between authorizations.

use super::*;

#[tokio::test]
#[allow(clippy::too_many_lines)]
async fn live_chromium_module_never_dials_a_rebound_private_socket() -> tinybus::Result<()> {
// Keep both endpoints on assigned loopback addresses and the same port;
// the resolver fixture changes only the address returned for fixture.test.
let allowed_listener = tokio::net::TcpListener::bind("[::1]:0").await?;
let port = allowed_listener.local_addr()?.port();
let rebound_listener = tokio::net::TcpListener::bind(("127.0.0.1", port)).await?;
let allowed_address = allowed_listener.local_addr()?.to_string();
let rebound_address = rebound_listener.local_addr()?.to_string();
let extra_connections = Arc::new(AtomicUsize::new(0));
let (served_tx, served_rx) = tokio::sync::oneshot::channel();
let server = tokio::spawn(serve_fixture(
allowed_listener,
b"<!doctype html><title>allowed fixture</title>".to_vec(),
extra_connections.clone(),
served_tx,
None,
));
let resolved_address = Arc::new(Mutex::new(allowed_address.clone()));
let browser = start_browser_with_fixture_hosts_and_resolution(
vec![allowed_address.clone()],
port,
"/before-rebind",
served_rx,
Some(resolved_address.clone()),
)
.await?;

let calls_before_rebind = browser.observations.lock().unwrap().clone();
let before_rebind_authorizations = calls_before_rebind
.iter()
.filter(|call| call.starts_with("authorize:fixture-policy:41:fixture.test:"))
.count();
assert!(
calls_before_rebind
.iter()
.any(|call| call == &format!("consume:{allowed_address}")),
"the initial allowed socket was not consumed: {calls_before_rebind:?}"
);
let connections_before_rebind = extra_connections.load(Ordering::SeqCst);
*resolved_address.lock().unwrap() = rebound_address.clone();
let denied: DesktopResponse = browser
.proxy
.call(
browser_names::methods::NAVIGATE,
(serde_json::json!({
"session": browser.session,
"url": format!("http://fixture.test:{port}/after-rebind")
}),),
)
.await?;

assert!(!denied.ok, "navigation to the rebound address was allowed");
assert!(
tokio::time::timeout(Duration::from_millis(500), rebound_listener.accept())
.await
.is_err(),
"the rebound private socket received a connection"
);
assert!(
tokio::time::timeout(Duration::from_millis(500), async {
while extra_connections.load(Ordering::SeqCst) == connections_before_rebind {
tokio::task::yield_now().await;
}
})
.await
.is_err(),
"the request reached the previously allowed socket after rebinding"
);

let calls = browser.observations.lock().unwrap().clone();
assert!(
calls
.iter()
.filter(|call| call.starts_with("authorize:fixture-policy:41:fixture.test:"))
.count()
> before_rebind_authorizations,
"the rebound request did not reach host authorization: {calls:?}"
);
assert!(
calls
.iter()
.any(|call| call == &format!("resolve:fixture.test:{port}:{rebound_address}")),
"the controlled resolver did not return the rebound address: {calls:?}"
);
assert!(
!calls
.iter()
.any(|call| call == &format!("consume:{rebound_address}")),
"the rebound socket was consumed: {calls:?}"
);
let closed: DesktopResponse = browser
.proxy
.call(
browser_names::methods::CLOSE_SESSION,
(serde_json::json!({"session": browser.session}),),
)
.await?;
assert!(
closed.ok,
"browser session did not close: {:?}",
closed.error
);
browser.broker_task.abort();
let _ = browser.broker_task.await;
server.abort();
let _ = server.await;
Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![allow(clippy::expect_used, clippy::panic, clippy::unwrap_used)]

mod authority_denial_tests;
mod dns_rebinding_tests;
#[cfg(feature = "test-connector")]
mod public_connector_tests;
mod transport_classes_tests;
Expand All @@ -28,6 +29,7 @@ use tinycomputer_bus::{
struct EgressHost {
allowed: Arc<AtomicBool>,
allowed_addresses: Vec<String>,
resolved_address: Option<Arc<Mutex<String>>>,
grant_generation: Option<u64>,
authorize_delay: Duration,
observations: Arc<Mutex<Vec<String>>>,
Expand All @@ -42,16 +44,29 @@ impl EgressHost {
));
tokio::time::sleep(self.authorize_delay).await;
tokio::task::yield_now().await;
let resolved_address = self
.resolved_address
.as_ref()
.map(|resolved| resolved.lock().unwrap().clone());
let allowed_address = self.allowed_addresses.iter().find(|address| {
address
.rsplit(':')
.next()
.and_then(|port| port.parse::<u16>().ok())
== Some(request.port)
resolved_address
.as_ref()
.is_none_or(|resolved| *address == resolved)
&& address
.rsplit(':')
.next()
.and_then(|port| port.parse::<u16>().ok())
== Some(request.port)
});
let allow = self.allowed.load(Ordering::SeqCst)
&& request.hostname == "fixture.test"
&& allowed_address.is_some();
if let Some(resolved_address) = resolved_address {
self.observations.lock().unwrap().push(format!(
"resolve:{}:{}:{}",
request.hostname, request.port, resolved_address
));
}
Ok(BrowserEgressGrant {
generation: self.grant_generation.unwrap_or(request.expected_generation),
authorization: if allow {
Expand Down Expand Up @@ -195,6 +210,7 @@ async fn start_browser_with_fixture_hosts(
.await
}

#[allow(clippy::too_many_arguments)]
async fn start_browser_with_fixture_hosts_for_policy(
allowed_addresses: Vec<String>,
page_port: u16,
Expand All @@ -204,6 +220,53 @@ async fn start_browser_with_fixture_hosts_for_policy(
generation: u64,
shared_observations: Option<Arc<Mutex<Vec<String>>>>,
test_connector_map: Option<(String, String)>,
) -> tinybus::Result<LiveBrowser> {
start_browser_with_fixture_hosts_for_policy_and_resolution(
allowed_addresses,
page_port,
page_path,
served,
policy_id,
generation,
shared_observations,
test_connector_map,
None,
)
.await
}

async fn start_browser_with_fixture_hosts_and_resolution(
allowed_addresses: Vec<String>,
page_port: u16,
page_path: &str,
served: tokio::sync::oneshot::Receiver<()>,
resolved_address: Option<Arc<Mutex<String>>>,
) -> tinybus::Result<LiveBrowser> {
start_browser_with_fixture_hosts_for_policy_and_resolution(
allowed_addresses,
page_port,
page_path,
served,
"fixture-policy",
41,
None,
None,
resolved_address,
)
.await
}

#[allow(clippy::too_many_arguments)]
async fn start_browser_with_fixture_hosts_for_policy_and_resolution(
allowed_addresses: Vec<String>,
page_port: u16,
page_path: &str,
served: tokio::sync::oneshot::Receiver<()>,
policy_id: &str,
generation: u64,
shared_observations: Option<Arc<Mutex<Vec<String>>>>,
test_connector_map: Option<(String, String)>,
resolved_address: Option<Arc<Mutex<String>>>,
) -> tinybus::Result<LiveBrowser> {
let bus = MemoryBus::new();
let broker = Broker::new();
Expand All @@ -217,6 +280,7 @@ async fn start_browser_with_fixture_hosts_for_policy(
EgressHost {
allowed: allowed.clone(),
allowed_addresses,
resolved_address,
grant_generation: None,
authorize_delay: Duration::ZERO,
observations: observations.clone(),
Expand Down
Loading