#![cfg(target_os = "linux")]
use std::fs;
use std::io::{Read, Write};
use std::net::TcpListener;
use std::path::PathBuf;
use std::process::{Command, Output, Stdio};
use std::sync::{PoisonError, RwLock, mpsc};
use std::thread;
use std::time::Duration;
use nix::sys::signal::Signal;
const CRABPOT: &str = env!("CARGO_BIN_EXE_crabpot");
fn stdout(output: &Output) -> String {
String::from_utf8_lossy(&output.stdout).into_owned()
}
#[test]
fn allow_tcp_exchanges_bytes_with_a_host_address() {
tcp_round_trip("host", host_address(), None);
}
#[test]
fn allow_tcp_exchanges_bytes_with_an_address_resolved_at_startup() {
tcp_round_trip("resolved", std::net::Ipv4Addr::LOCALHOST, Some("localhost"));
}
#[test]
fn allow_tcp_on_a_proxy_port_of_another_address_still_starts_the_sandbox() {
tcp_round_trip_on("proxy-port", host_address(), 3128, None);
}
#[test]
fn allow_tcp_refuses_a_scoped_ipv6_address_naming_it() {
let settings = settings_allowing_tcp("scoped", &["[fe80::1%2]:22".to_owned()]);
let output = run_script(&settings, "print('ran')");
assert_eq!(output.status.code(), Some(125), "{output:?}");
assert!(output.stdout.is_empty(), "{output:?}");
let error = String::from_utf8_lossy(&output.stderr);
assert!(error.contains("fe80::1%2"), "{error}");
}
fn tcp_round_trip(case: &str, address: std::net::Ipv4Addr, host: Option<&str>) {
tcp_round_trip_on(case, address, 0, host);
}
fn tcp_round_trip_on(case: &str, address: std::net::Ipv4Addr, port: u16, host: Option<&str>) {
let server = TcpListener::bind((address, port)).unwrap();
let destination = server.local_addr().unwrap();
let (done, answered) = mpsc::channel();
thread::spawn(move || {
let (mut client, _) = server.accept().unwrap();
client
.set_read_timeout(Some(Duration::from_secs(10)))
.unwrap();
client
.set_write_timeout(Some(Duration::from_secs(10)))
.unwrap();
let mut request = Vec::new();
client.read_to_end(&mut request).unwrap();
assert_eq!(request, b"ping");
client.write_all(b"pong").unwrap();
done.send(()).unwrap();
});
let entry = match host {
Some(host) => format!("{host}:{}", destination.port()),
None => destination.to_string(),
};
let settings = settings_allowing_tcp(case, &[entry]);
let output = run_script(
&settings,
&format!(
r#"
import socket
with socket.create_connection(('{address}', {port}), timeout=5) as client:
client.sendall(b'ping')
client.shutdown(socket.SHUT_WR)
with client.makefile('rb') as response:
assert response.read() == b'pong'
print('exchanged')
"#,
port = destination.port()
),
);
assert!(output.status.success(), "{output:?}");
assert_eq!(stdout(&output), "exchanged\n");
answered.recv_timeout(Duration::from_secs(10)).unwrap();
}
fn host_address() -> std::net::Ipv4Addr {
nix::ifaddrs::getifaddrs()
.unwrap()
.find_map(|interface| {
let address = interface.address?;
let ip = address.as_sockaddr_in()?.ip();
(!ip.is_loopback() && !ip.is_unspecified()).then_some(ip)
})
.expect("the host has a non-loopback IPv4 address")
}
fn settings_allowing_tcp(case: &str, destinations: &[String]) -> PathBuf {
let path = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(format!("allow-tcp-{case}.json"));
fs::write(
&path,
serde_json::json!({
"network": {"allowedDomains": [], "deniedDomains": [], "allowTcp": destinations},
"filesystem": {"denyRead": [], "allowRead": [], "allowWrite": [], "denyWrite": []}
})
.to_string(),
)
.unwrap();
path
}
static LAUNCH: RwLock<()> = RwLock::new(());
fn run_script(settings: &std::path::Path, script: &str) -> Output {
let _launching = LAUNCH.read().unwrap_or_else(PoisonError::into_inner);
run_script_unlocked(settings, script)
}
fn run_script_alone(settings: &std::path::Path, script: &str) -> Output {
let _launching = LAUNCH.write().unwrap_or_else(PoisonError::into_inner);
run_script_unlocked(settings, script)
}
fn run_script_unlocked(settings: &std::path::Path, script: &str) -> Output {
let child = Command::new(CRABPOT)
.env("HOME", env!("CARGO_TARGET_TMPDIR"))
.args([
"-s",
settings.to_str().unwrap(),
"--",
"python3",
"-c",
script,
])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap();
let pid = child.id();
let (done, completed) = mpsc::channel();
thread::spawn(move || {
let _ = done.send(child.wait_with_output());
});
match completed.recv_timeout(Duration::from_secs(20)) {
Ok(output) => output.unwrap(),
Err(error) => {
let _ = nix::sys::signal::kill(nix::unistd::Pid::from_raw(pid as i32), Signal::SIGKILL);
panic!("allowTcp command did not end: {error}");
}
}
}
#[test]
fn allow_tcp_refuses_a_host_server_on_an_unlisted_port() {
let address = host_address();
let server = TcpListener::bind((address, 0)).unwrap();
let destination = server.local_addr().unwrap();
let listed = TcpListener::bind((address, 0)).unwrap();
let settings =
settings_allowing_tcp("unlisted-port", &[listed.local_addr().unwrap().to_string()]);
let output = run_script(
&settings,
&format!(
r#"
import socket
try:
socket.create_connection(('{address}', {port}), timeout=5)
except OSError:
print('refused')
else:
raise AssertionError('unlisted port was reachable')
"#,
port = destination.port()
),
);
assert!(output.status.success(), "{output:?}");
assert_eq!(stdout(&output), "refused\n");
server.set_nonblocking(true).unwrap();
assert_eq!(
server.accept().unwrap_err().kind(),
std::io::ErrorKind::WouldBlock
);
}
#[test]
fn allow_tcp_refuses_an_unresolved_host_before_running_the_command() {
let settings =
settings_allowing_tcp("unresolved", &["database.crabpot.invalid:5432".to_owned()]);
let output = run_script(&settings, "print('ran')");
assert_eq!(output.status.code(), Some(125), "{output:?}");
assert!(output.stdout.is_empty(), "{output:?}");
let error = String::from_utf8_lossy(&output.stderr);
assert!(error.contains("database.crabpot.invalid"), "{error}");
assert!(error.contains("cannot resolve"), "{error}");
}
fn settings_with_unix_sockets(case: &str, sockets: &[&str]) -> PathBuf {
let path = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(format!("allow-unix-{case}.json"));
fs::write(
&path,
serde_json::json!({
"network": {"allowedDomains": [], "deniedDomains": [], "allowUnixSockets": sockets},
"filesystem": {"denyRead": [], "allowRead": [], "allowWrite": [], "denyWrite": []}
})
.to_string(),
)
.unwrap();
path
}
fn listening(case: &str, name: &str) -> (PathBuf, std::os::unix::net::UnixListener) {
let path = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(format!("{case}-{name}"));
let _ = fs::remove_file(&path);
let listener = std::os::unix::net::UnixListener::bind(&path).expect("a socket to listen on");
(path, listener)
}
#[test]
fn a_listed_unix_socket_is_reachable_from_the_command() {
let (live, _held) = listening("listed", "live.sock");
let settings = settings_with_unix_sockets("listed", &[live.to_str().unwrap()]);
let output = run_script_alone(
&settings,
&format!(
r#"
import socket
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect({live:?})
print('connected')
"#
),
);
assert!(output.status.success(), "{output:?}");
assert_eq!(stdout(&output), "connected\n");
}
#[test]
fn a_unix_socket_the_list_leaves_out_is_refused() {
let (listed, _listed) = listening("unlisted", "listed.sock");
let (other, _other) = listening("unlisted", "other.sock");
let settings = settings_with_unix_sockets("unlisted", &[listed.to_str().unwrap()]);
let output = run_script_alone(
&settings,
&format!(
r#"
import socket
for name, path in (('listed', {listed:?}), ('other', {other:?})):
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
s.connect(path)
except OSError:
print(name, 'refused')
else:
print(name, 'connected')
"#
),
);
assert!(output.status.success(), "{output:?}");
assert_eq!(stdout(&output), "listed connected\nother refused\n");
}
#[test]
fn a_listed_directory_leaves_every_socket_beneath_it_reachable() {
let socks = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("listed-dir-socks");
fs::create_dir_all(&socks).unwrap();
let _ = fs::remove_file(socks.join("a.sock"));
let _held = std::os::unix::net::UnixListener::bind(socks.join("a.sock")).unwrap();
let settings = settings_with_unix_sockets("listed-dir", &[socks.to_str().unwrap()]);
let output = run_script_alone(
&settings,
&format!(
r#"
import socket
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect({a:?})
print('connected')
"#,
a = socks.join("a.sock")
),
);
assert!(output.status.success(), "{output:?}");
assert_eq!(stdout(&output), "connected\n");
}
#[test]
fn with_no_list_socket_af_unix_is_refused() {
let settings = settings_with_unix_sockets("none", &[]);
let output = run_script(
&settings,
r#"
import socket
try:
socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
except PermissionError:
print('refused')
else:
raise AssertionError('AF_UNIX was open with no list')
"#,
);
assert!(output.status.success(), "{output:?}");
assert_eq!(stdout(&output), "refused\n");
}