Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

sandbox: Replace allow_network bool with a NetworkAccess enum (#59218)

Richard Feldmanbf746a7a26b7
diff --git a/crates/acp_thread/src/terminal.rs b/crates/acp_thread/src/terminal.rs
index ffe3108a7f5..9b0b08b99d8 100644
--- a/crates/acp_thread/src/terminal.rs
+++ b/crates/acp_thread/src/terminal.rs
@@ -73,14 +73,21 @@ pub(crate) fn apply_sandbox_wrap(
#[cfg(target_os = "macos")]
{
+ use sandbox::macos_seatbelt::NetworkAccess;
+
let writable: Vec<&std::path::Path> = sandbox_wrap
.writable_paths
.iter()
.chain(sandbox_wrap.extra_write_paths.iter())
.map(|p| p.as_path())
.collect();
+ let network = if sandbox_wrap.allow_network {
+ NetworkAccess::All
+ } else {
+ NetworkAccess::None
+ };
let permissions = sandbox::macos_seatbelt::SandboxPermissions {
- allow_network: sandbox_wrap.allow_network,
+ network,
allow_fs_write: sandbox_wrap.allow_fs_write,
};
let (new_program, new_args, config_file) =
diff --git a/crates/sandbox/src/macos_seatbelt.rs b/crates/sandbox/src/macos_seatbelt.rs
index 0357ecb361b..52561db8e04 100644
--- a/crates/sandbox/src/macos_seatbelt.rs
+++ b/crates/sandbox/src/macos_seatbelt.rs
@@ -37,12 +37,31 @@ use tempfile::NamedTempFile;
/// module deliberately doesn't expose).
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct SandboxPermissions {
- /// Allow network access for the command.
- pub allow_network: bool,
+ /// Network access policy for the command.
+ pub network: NetworkAccess,
/// Allow unrestricted filesystem writes.
pub allow_fs_write: bool,
}
+/// Network-access setting for a sandboxed command.
+///
+/// The default ([`NetworkAccess::None`]) blocks all outbound network at the
+/// Seatbelt layer. [`NetworkAccess::LocalhostPort`] confines the command to a
+/// single loopback port — used to force all egress through the in-process
+/// HTTP/HTTPS proxy that enforces a hostname allowlist (see the `http_proxy`
+/// crate). [`NetworkAccess::All`] lifts the restriction entirely.
+#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
+pub enum NetworkAccess {
+ /// All outbound network blocked.
+ #[default]
+ None,
+ /// Outbound TCP allowed only to `localhost:<port>`. Used to confine
+ /// sandboxed commands to the in-process HTTP/HTTPS proxy.
+ LocalhostPort(u16),
+ /// All outbound network allowed.
+ All,
+}
+
/// A Seatbelt config file written to a temporary path on disk, suitable
/// for `sandbox-exec -f <path>`. The file is deleted when this is dropped.
///
@@ -216,13 +235,31 @@ fn generate_seatbelt_config(
);
}
- if permissions.allow_network {
- config.push_str(
- r#"
+ match permissions.network {
+ NetworkAccess::None => {}
+ NetworkAccess::All => {
+ config.push_str(
+ r#"
; Allow network access
(allow network*)
"#,
- );
+ );
+ }
+ NetworkAccess::LocalhostPort(port) => {
+ // Seatbelt rejects IP literals in `(remote tcp ...)` rules — it
+ // only accepts `*` or `localhost` as the host part. The runtime
+ // resolves correctly when the sandboxed process connects to
+ // `127.0.0.1:<port>`, so this is a syntactic constraint, not a
+ // routing one.
+ config.push_str(&format!(
+ r#"
+; Allow outbound network only to the in-process proxy on localhost
+(allow network-outbound (remote tcp "localhost:{port}"))
+; Network binds (sandboxed process picking its own ephemeral source port)
+(allow network-bind (local ip "localhost:*"))
+"#,
+ ));
+ }
}
Ok(config)
@@ -267,7 +304,7 @@ mod tests {
let config = generate_seatbelt_config(
&[dir.as_path()],
SandboxPermissions {
- allow_network: false,
+ network: NetworkAccess::None,
allow_fs_write: true,
},
)
@@ -286,7 +323,7 @@ mod tests {
let config = generate_seatbelt_config(
&[dir.as_path()],
SandboxPermissions {
- allow_network: true,
+ network: NetworkAccess::All,
allow_fs_write: false,
},
)
@@ -298,6 +335,24 @@ mod tests {
assert!(!config.contains("; Allow unrestricted filesystem writes"));
}
+ #[test]
+ fn test_generate_seatbelt_config_localhost_port_narrows_network() {
+ let dir = PathBuf::from("/Users/test/projects/myproject");
+ let config = generate_seatbelt_config(
+ &[dir.as_path()],
+ SandboxPermissions {
+ network: NetworkAccess::LocalhostPort(54321),
+ allow_fs_write: false,
+ },
+ )
+ .unwrap();
+
+ // Only the loopback proxy port is reachable; no blanket network rule.
+ assert!(config.contains("(allow network-outbound (remote tcp \"localhost:54321\"))"));
+ assert!(config.contains("(allow network-bind (local ip \"localhost:*\"))"));
+ assert!(!config.contains("(allow network*)"));
+ }
+
#[test]
fn test_generate_seatbelt_config_emits_one_subpath_per_writable_directory() {
let project_dir = PathBuf::from("/Users/test/projects/myproject");
@@ -411,7 +466,7 @@ mod tests {
&["-c".to_string(), "echo test 2>/dev/null".to_string()],
&[temp_dir.path()],
SandboxPermissions {
- allow_network: false,
+ network: NetworkAccess::None,
allow_fs_write: true,
},
)
@@ -444,7 +499,7 @@ mod tests {
],
&[temp_dir.path()],
SandboxPermissions {
- allow_network: false,
+ network: NetworkAccess::None,
allow_fs_write: true,
},
)
@@ -542,7 +597,7 @@ mod tests {
],
&[project_dir.path()],
SandboxPermissions {
- allow_network: false,
+ network: NetworkAccess::None,
allow_fs_write: true,
},
)