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
5 changes: 4 additions & 1 deletion MODULE.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ is optional:
`settle` (`prompt`, the default, or `steady`: how long a page is let
settle after an action), and `prelaunch` (a boolean, `true` by default:
open a browser-only task's browser while it is planned, at the one web
address the task's text names, if it names one).
address the task's text names, if it names one). Its `profile` (the folder
kept between sessions, the only one a launch may name under egress) applies
only when a session or task names it as its profile (`user_data_dir` or
`browser_profile`); one that names none starts fresh.

Configuration is delivered as sensitive host-control traffic and is never
shown to monitors.
Expand Down
8 changes: 4 additions & 4 deletions crates/tinycomputer-browser/src/linked/linked_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ async fn a_closed_or_lost_browser_cannot_implicitly_launch_without_the_proxy() {
proxy: None,
proxy_session_id: "private-session-id".into(),
proxy_password: "private-proxy-password".into(),
host_executable: None,
host: super::HostBrowser::default(),
enforced_launch: true,
};
let reply = engine
Expand All @@ -124,7 +124,7 @@ async fn an_exited_enforced_browser_can_still_be_closed() {
proxy: None,
proxy_session_id: "private-session-id".into(),
proxy_password: "private-proxy-password".into(),
host_executable: None,
host: super::HostBrowser::default(),
enforced_launch: true,
};

Expand All @@ -143,7 +143,7 @@ async fn enforced_browser_refuses_process_side_reads_with_a_url() {
proxy: None,
proxy_session_id: "private-session-id".into(),
proxy_password: "private-proxy-password".into(),
host_executable: None,
host: super::HostBrowser::default(),
enforced_launch: true,
};
let reply = engine
Expand All @@ -170,7 +170,7 @@ async fn enforced_browser_rechecks_liveness_after_waiting_for_engine_lock() {
proxy: None,
proxy_session_id: "private-session-id".into(),
proxy_password: "private-proxy-password".into(),
host_executable: None,
host: super::HostBrowser::default(),
enforced_launch: true,
};
let serialized = ENGINE.lock().await;
Expand Down
102 changes: 92 additions & 10 deletions crates/tinycomputer-browser/src/linked/linked_tests/launch_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Tests for browser launch command rewriting and policy refusal.
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

use super::super::{AMBIENT_BROWSER_ENV, enforce_launch_with_config};
use super::super::{AMBIENT_BROWSER_ENV, HostBrowser, enforce_launch_with_config};
use serde_json::json;

#[test]
Expand All @@ -11,7 +11,7 @@ fn enforced_launch_uses_only_the_host_proxy_and_network_containment_flags() {
"http://127.0.0.1:39123",
"session-secret",
false,
None,
&HostBrowser::default(),
)
.expect("enforced launch");
assert_eq!(launch["proxy"]["server"], "http://127.0.0.1:39123");
Expand All @@ -36,31 +36,31 @@ fn enforced_launch_refuses_external_cdp_and_caller_flags() {
"http://127.0.0.1:39123",
"session-secret",
false,
None,
&HostBrowser::default(),
);
assert!(cdp.is_err());
let args = enforce_launch_with_config(
json!({"action":"launch","args":["--proxy-server=http://attacker.test"]}),
"http://127.0.0.1:39123",
"session-secret",
false,
None,
&HostBrowser::default(),
);
assert!(args.is_err());
let executable = enforce_launch_with_config(
json!({"action":"launch","executablePath":"/tmp/attacker-browser"}),
"http://127.0.0.1:39123",
"session-secret",
false,
None,
&HostBrowser::default(),
);
assert!(executable.is_err());
let ambient = enforce_launch_with_config(
json!({"action":"launch"}),
"http://127.0.0.1:39123",
"session-secret",
true,
None,
&HostBrowser::default(),
);
assert!(ambient.is_err());
}
Expand Down Expand Up @@ -92,7 +92,7 @@ fn enforced_launch_refuses_caller_plugins_extensions_and_init_scripts() {
"http://127.0.0.1:39123",
"session-secret",
false,
None,
&HostBrowser::default(),
)
.is_err(),
"{field} was accepted"
Expand All @@ -111,7 +111,7 @@ fn a_host_executable_launches_under_the_enforced_proxy() {
"http://127.0.0.1:39123",
"session-secret",
false,
Some("/opt/host/chrome"),
&HostBrowser::executable("/opt/host/chrome"),
)
.expect("host executable launches");
assert_eq!(launch["executablePath"], "/opt/host/chrome");
Expand All @@ -138,7 +138,10 @@ fn a_request_cannot_name_an_executable_the_host_did_not_configure() {
"http://127.0.0.1:39123",
"session-secret",
false,
host,
&HostBrowser {
executable: host.map(str::to_owned),
profile: None,
},
)
.is_err(),
"{requested} with host {host:?}"
Expand All @@ -151,8 +154,87 @@ fn a_request_cannot_name_an_executable_the_host_did_not_configure() {
"http://127.0.0.1:39123",
"session-secret",
true,
Some("/opt/host/chrome"),
&HostBrowser::executable("/opt/host/chrome"),
)
.is_err()
);
}

fn host_profile() -> HostBrowser {
HostBrowser {
executable: Some("/opt/host/chrome".into()),
profile: Some("/srv/state/browser/profile".into()),
}
}

/// The host's own profile folder keeps cookies and sign-ins between
/// sessions; it launches under the proxy with extensions off, so nothing
/// stored in the profile can install a proxy of its own.
#[test]
fn a_host_profile_launches_under_the_enforced_proxy_without_extensions() {
for command in [
json!({"action":"launch"}),
json!({"action":"launch","profile":"/srv/state/browser/profile"}),
] {
let launch = enforce_launch_with_config(
command.clone(),
"http://127.0.0.1:39123",
"session-secret",
false,
&host_profile(),
)
.expect("host profile launches");
let args = launch["args"].as_array().expect("args");
assert_eq!(
args.contains(&json!("--disable-extensions")),
command.get("profile").is_some(),
"{command}"
);
assert_eq!(launch["proxy"]["server"], "http://127.0.0.1:39123");
// The profile is used only when the session asked for it: a session
// that named none still starts fresh.
assert_eq!(launch.get("profile"), command.get("profile"));
}
}

#[test]
fn a_request_cannot_name_a_profile_the_host_did_not_configure() {
for (requested, host) in [
(json!("/tmp/attacker-profile"), host_profile()),
(json!(7), host_profile()),
(json!("/srv/state/browser/profile"), HostBrowser::default()),
(
json!("/srv/state/browser/profile"),
HostBrowser::executable("/opt/host/chrome"),
),
] {
assert!(
enforce_launch_with_config(
json!({"action":"launch","profile":requested}),
"http://127.0.0.1:39123",
"session-secret",
false,
&host,
)
.is_err(),
"{requested} with host {:?}",
host.profile
);
}
// The host profile does not lift the other refusals.
for field in ["storageState", "restore", "cdpUrl"] {
let mut command = json!({"action":"launch","profile":"/srv/state/browser/profile"});
command[field] = json!("x");
assert!(
enforce_launch_with_config(
command,
"http://127.0.0.1:39123",
"session-secret",
false,
&host_profile(),
)
.is_err(),
"{field}"
);
}
}
72 changes: 57 additions & 15 deletions crates/tinycomputer-browser/src/linked/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,25 @@ pub struct AgentBrowser;
#[derive(Clone)]
pub struct EnforcedAgentBrowser {
authorizer: Arc<dyn EgressAuthorizer>,
executable: Option<String>,
host: HostBrowser,
}

/// What the host, never a browser call, chooses for an enforced launch: the
/// Chrome binary and the profile folder kept between sessions.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct HostBrowser {
pub(crate) executable: Option<String>,
pub(crate) profile: Option<String>,
}

impl HostBrowser {
#[cfg(test)]
pub(crate) fn executable(executable: &str) -> Self {
Self {
executable: Some(executable.to_owned()),
profile: None,
}
}
}

impl std::fmt::Debug for EnforcedAgentBrowser {
Expand All @@ -45,15 +63,26 @@ impl EnforcedAgentBrowser {
pub fn new(authorizer: Arc<dyn EgressAuthorizer>) -> Self {
Self {
authorizer,
executable: None,
host: HostBrowser::default(),
}
}

/// Launch the host-configured Chrome or Chromium binary. It is host
/// control, like the proxy: a launch may repeat it but never name another.
#[must_use]
pub fn with_executable(mut self, executable: Option<String>) -> Self {
self.executable = executable;
self.host.executable = executable;
self
}

/// Let a launch use the host-configured profile folder, so cookies and
/// sign-ins survive between sessions. It is host control, like the
/// executable: a launch may name this folder but never another, and it
/// runs with extensions off, since an extension stored in the profile
/// could set a proxy that outranks the enforced one.
#[must_use]
pub fn with_profile(mut self, profile: Option<String>) -> Self {
self.host.profile = profile;
self
}
}
Expand All @@ -66,7 +95,7 @@ impl Launcher for AgentBrowser {
proxy: None,
proxy_session_id: String::new(),
proxy_password: String::new(),
host_executable: None,
host: HostBrowser::default(),
enforced_launch: false,
})
}
Expand All @@ -75,7 +104,7 @@ impl Launcher for AgentBrowser {
impl Launcher for EnforcedAgentBrowser {
fn open(&self, session: &str) -> Box<dyn Engine> {
let mut engine = enforced(self.authorizer.clone(), session);
engine.host_executable.clone_from(&self.executable);
engine.host.clone_from(&self.host);
Box::new(engine)
}
}
Expand All @@ -90,7 +119,7 @@ fn enforced(authorizer: Arc<dyn EgressAuthorizer>, session: &str) -> Linked {
proxy: None,
proxy_session_id: private_session_id(),
proxy_password: private_session_id(),
host_executable: None,
host: HostBrowser::default(),
enforced_launch: false,
}
}
Expand All @@ -101,7 +130,7 @@ struct Linked {
proxy: Option<EgressProxy>,
proxy_session_id: String,
proxy_password: String,
host_executable: Option<String>,
host: HostBrowser,
enforced_launch: bool,
}

Expand Down Expand Up @@ -144,7 +173,7 @@ impl Engine for Linked {
command.clone(),
&proxy.url(),
&self.proxy_password,
self.host_executable.as_deref(),
&self.host,
) {
Ok(command) => command,
Err(reason) => return failure_reply(&command, reason),
Expand Down Expand Up @@ -232,7 +261,7 @@ fn enforce_launch(
command: Value,
proxy: &str,
proxy_password: &str,
host_executable: Option<&str>,
host: &HostBrowser,
) -> Result<Value, &'static str> {
let has_ambient_browser_config = command.get("action").and_then(Value::as_str)
== Some("launch")
Expand All @@ -242,7 +271,7 @@ fn enforce_launch(
proxy,
proxy_password,
has_ambient_browser_config,
host_executable,
host,
)
}

Expand All @@ -251,8 +280,9 @@ fn enforce_launch_with_config(
proxy: &str,
proxy_password: &str,
has_ambient_browser_config: bool,
host_executable: Option<&str>,
host: &HostBrowser,
) -> Result<Value, &'static str> {
let host_executable = host.executable.as_deref();
if command.get("action").and_then(Value::as_str) != Some("launch") {
return Ok(command);
}
Expand All @@ -261,7 +291,6 @@ fn enforce_launch_with_config(
"cdpPort",
"autoConnect",
"provider",
"profile",
"storageState",
"restore",
]
Expand All @@ -270,6 +299,13 @@ fn enforce_launch_with_config(
{
return Err("network policy refuses attached or externally managed browsers");
}
// Only the host chooses the profile folder, as it does the binary: a
// launch may name the host's own folder, or none for a fresh profile.
if let Some(requested) = command.get("profile")
&& (host.profile.is_none() || requested.as_str() != host.profile.as_deref())
{
return Err("network policy refuses a browser profile the host did not configure");
}
// Only the host chooses the binary. A launch may repeat the host's own
// executable (hosts send it with each session) but not name another.
if let Some(requested) = command.get("executablePath")
Expand Down Expand Up @@ -309,12 +345,18 @@ fn enforce_launch_with_config(
if let Some(executable) = host_executable {
command["executablePath"] = json!(executable);
}
command["args"] = json!([
let mut args = vec![
"--disable-quic",
"--disable-http2",
"--host-resolver-rules=MAP * ~NOTFOUND, EXCLUDE 127.0.0.1",
"--force-webrtc-ip-handling-policy=disable_non_proxied_udp"
]);
"--force-webrtc-ip-handling-policy=disable_non_proxied_udp",
];
// Chrome ranks an extension's proxy above the command line's, and a kept
// profile can carry installed extensions.
if command.get("profile").is_some() {
args.push("--disable-extensions");
}
command["args"] = json!(args);
Ok(command)
}

Expand Down
Loading
Loading