#![cfg(target_os = "linux")]
use std::fs;
use std::io::{BufRead, BufReader, Read, Write};
use std::net::TcpListener;
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf;
use std::process::{Child, Command, ExitStatus, Output, Stdio};
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
use nix::sys::signal::{SigSet, Signal};
const CRABPOT: &str = env!("CARGO_BIN_EXE_crabpot");
fn crabpot(args: &[&str]) -> Output {
Command::new(CRABPOT)
.env("HOME", env!("CARGO_TARGET_TMPDIR"))
.args(args)
.output()
.unwrap()
}
#[test]
fn the_helper_runs_the_command_and_passes_its_exit_status() {
let direct = crabpot(&["--crabpot-helper", "init", "sh", "-c", "echo ran; exit 7"]);
assert_eq!(direct.status.code(), Some(7), "{direct:?}");
assert_eq!(direct.stdout, b"ran\n");
for code in [0, 1, 5, 125] {
let sandboxed = crabpot(&[
"--",
"sh",
"-c",
&format!("test ! -e /proc/self/fd/3 || exit 99; exit {code}"),
]);
assert_eq!(sandboxed.status.code(), Some(code), "{sandboxed:?}");
}
let missing = crabpot(&["--crabpot-helper", "init", "/no/such/program"]);
assert_eq!(missing.status.code(), Some(1), "{missing:?}");
assert!(
String::from_utf8_lossy(&missing.stderr).contains("/no/such/program"),
"{missing:?}"
);
}
#[test]
fn a_bwrap_that_ends_without_reporting_a_pid_exits_125() {
bwrap_that_never_starts_the_helper(false);
}
#[test]
fn a_bwrap_that_reports_a_pid_but_never_starts_the_helper_exits_125() {
bwrap_that_never_starts_the_helper(true);
}
fn bwrap_that_never_starts_the_helper(reports_pid: bool) {
let root = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(format!(
"bwrap-without-helper-{reports_pid}-{}",
std::process::id()
));
fs::create_dir_all(&root).unwrap();
let bwrap = root.join("bwrap");
fs::write(
&bwrap,
r#"#!/bin/sh
case "$1" in
--version) echo 'bubblewrap 0.9.0'; exit 0 ;;
--unshare-user) exit 0 ;;
esac
if [ "$CRABPOT_TEST_BWRAP_REPORT" = 1 ]; then
echo '{"child-pid": 4242}' >&8
fi
echo 'bwrap ended without starting the helper' >&2
exit "$CRABPOT_TEST_BWRAP_EXIT"
"#,
)
.unwrap();
fs::set_permissions(&bwrap, fs::Permissions::from_mode(0o700)).unwrap();
let settings = root.join("settings.json");
fs::write(
&settings,
r#"{
"network": {"allowedDomains": [], "deniedDomains": []},
"filesystem": {"denyRead": [], "allowRead": [], "allowWrite": [], "denyWrite": []}
}"#,
)
.unwrap();
for code in [0, 1, 7] {
let output = Command::new(CRABPOT)
.env("HOME", &root)
.env("PATH", &root)
.env("CRABPOT_TEST_BWRAP_EXIT", code.to_string())
.env(
"CRABPOT_TEST_BWRAP_REPORT",
if reports_pid { "1" } else { "0" },
)
.arg("--settings")
.arg(&settings)
.args(["--", "/bin/sh", "-c", "echo ran"])
.output()
.unwrap();
assert_eq!(
output.status.code(),
Some(125),
"bwrap exit {code}: {output:?}"
);
assert!(output.stdout.is_empty(), "{output:?}");
assert!(
String::from_utf8_lossy(&output.stderr)
.contains("bwrap ended without starting the helper"),
"{output:?}"
);
}
fs::remove_dir_all(root).unwrap();
}
#[test]
fn the_command_sees_the_sandbox_environment() {
let output = Command::new(CRABPOT)
.env("HOME", env!("CARGO_TARGET_TMPDIR"))
.env("CLAUDE_CODE_TMPDIR", "/tmp/claude-integration")
.args([
"--",
"sh",
"-c",
r#"printf '%s\n' "$SANDBOX_RUNTIME" "$TMPDIR""#,
])
.output()
.unwrap();
assert!(output.status.success(), "{output:?}");
assert_eq!(
String::from_utf8_lossy(&output.stdout),
"1\n/tmp/claude-integration\n"
);
}
#[test]
fn the_helper_passes_its_arguments_untouched() {
let args = ["-d", "--", "--crabpot", "--crabpot-helper", "a b", ""];
let mut helper = vec![
"--crabpot-helper",
"init",
"sh",
"-c",
r#"printf '[%s]\n' "$@""#,
"sh",
];
helper.extend(args);
let output = crabpot(&helper);
assert!(output.status.success(), "{output:?}");
assert_eq!(
String::from_utf8_lossy(&output.stdout),
"[-d]\n[--]\n[--crabpot]\n[--crabpot-helper]\n[a b]\n[]\n"
);
}
#[test]
fn the_binary_is_bound_read_only_inside_the_sandbox() {
let script =
r#"awk -v p="$0" '$5 == p { split($6, o, ","); print o[1] }' /proc/self/mountinfo"#;
let output = crabpot(&["--", "sh", "-c", script, CRABPOT]);
assert!(output.status.success(), "{output:?}");
assert_eq!(String::from_utf8_lossy(&output.stdout), "ro\n");
}
fn in_helper(script: &str) -> Output {
let output = crabpot(&["--crabpot-helper", "init", "sh", "-c", script]);
assert!(output.status.success(), "{output:?}");
output
}
fn stdout(output: &Output) -> String {
String::from_utf8_lossy(&output.stdout).into_owned()
}
#[test]
fn the_command_sees_itself_as_a_child_of_pid_1() {
let output = in_helper("echo $$ $PPID");
assert_eq!(stdout(&output), "2 1\n");
}
#[test]
fn proc_shows_only_the_sandbox_pids() {
let output = in_helper(r#"for p in /proc/[0-9]*; do echo "${p#/proc/}"; done"#);
assert_eq!(stdout(&output), "1\n2\n");
}
#[test]
fn an_orphaned_grandchild_is_reaped() {
let script = r#"
pid=$(sh -c 'sh -c "exit 0" >/dev/null & echo $!')
i=0
while [ -e /proc/$pid ] && [ $i -lt 2000000 ]; do i=$((i + 1)); done
if [ -e /proc/$pid ]; then echo zombie; else echo reaped; fi
"#;
let output = in_helper(script);
assert_eq!(stdout(&output), "reaped\n");
}
#[test]
fn the_user_namespace_fallback_runs_when_unshare_is_refused() {
if stdout(&in_helper("id -u")) == "0\n" {
return;
}
let output = in_helper("cat /proc/self/setgroups");
assert_eq!(stdout(&output), "deny\n");
}
#[test]
fn the_process_is_not_dumpable() {
let output = in_helper(
r#"
cat /proc/$$/environ >/dev/null || exit 1
cat /proc/1/environ >/dev/null 2>&1 && echo readable || echo refused
"#,
);
assert_eq!(stdout(&output), "refused\n");
}
#[test]
fn ambient_capabilities_are_cleared() {
let output = in_helper("grep CapAmb /proc/self/status");
assert_eq!(stdout(&output), "CapAmb:\t0000000000000000\n");
}
#[test]
fn the_command_runs_under_the_seccomp_filters_with_no_new_privs() {
let output = in_helper("grep -E '^(NoNewPrivs|Seccomp):' /proc/self/status");
assert_eq!(stdout(&output), "NoNewPrivs:\t1\nSeccomp:\t2\n");
}
fn spawn_helper(script: &str) -> Child {
Command::new(CRABPOT)
.env("HOME", env!("CARGO_TARGET_TMPDIR"))
.args(["--crabpot-helper", "init", "sh", "-c", script])
.stdout(Stdio::piped())
.spawn()
.unwrap()
}
fn send(signal: &str, helper: &Child) {
let sent = Command::new("kill")
.args([format!("-{signal}"), helper.id().to_string()])
.status()
.unwrap();
assert!(sent.success());
}
fn signal_a_ready_command(signal: &str) -> (String, ExitStatus) {
let script = format!("trap 'echo trapped; exit 3' {signal}; echo ready; sleep 30 & wait");
let mut helper = spawn_helper(&script);
let mut stdout = BufReader::new(helper.stdout.take().unwrap());
let mut line = String::new();
stdout.read_line(&mut line).unwrap();
assert_eq!(line, "ready\n");
send(signal, &helper);
let mut rest = String::new();
stdout.read_to_string(&mut rest).unwrap();
(rest, helper.wait().unwrap())
}
#[test]
fn sigterm_reaches_the_command() {
let (rest, status) = signal_a_ready_command("TERM");
assert_eq!(rest, "trapped\n");
assert_eq!(status.code(), Some(3));
}
#[test]
fn sigint_reaches_the_command() {
let (rest, status) = signal_a_ready_command("INT");
assert_eq!(rest, "trapped\n");
assert_eq!(status.code(), Some(3));
}
#[test]
fn sigusr1_reaches_the_command() {
let (rest, status) = signal_a_ready_command("USR1");
assert_eq!(rest, "trapped\n");
assert_eq!(status.code(), Some(3));
}
#[test]
fn sigusr2_reaches_the_command() {
let (rest, status) = signal_a_ready_command("USR2");
assert_eq!(rest, "trapped\n");
assert_eq!(status.code(), Some(3));
}
const LAUNCHER_WAIT: Duration = Duration::from_secs(30);
fn within_wait(
lines: &mpsc::Receiver<String>,
stderr: &mpsc::Receiver<String>,
launcher: &mut Child,
what: &str,
) -> String {
match lines.recv_timeout(LAUNCHER_WAIT) {
Ok(text) => text,
Err(_) => {
let _ = launcher.kill();
let stderr = stderr
.recv_timeout(Duration::from_secs(2))
.unwrap_or_else(|_| "(still open after the launcher was killed)".to_owned());
panic!("the launcher gave no {what} in {LAUNCHER_WAIT:?}; stderr: {stderr}");
}
}
}
fn signal_a_ready_launcher(signal: &str) -> (String, ExitStatus) {
let script = format!("trap 'echo trapped; exit 3' {signal}; echo ready; sleep 30 & wait");
let mut launcher = Command::new(CRABPOT)
.env("HOME", env!("CARGO_TARGET_TMPDIR"))
.args(["--", "sh", "-c", &script])
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap();
let stdout = launcher.stdout.take().unwrap();
let mut stderr_pipe = launcher.stderr.take().unwrap();
let (sender, lines) = mpsc::channel();
thread::spawn(move || {
let mut stdout = BufReader::new(stdout);
let mut line = String::new();
let _ = stdout.read_line(&mut line);
let _ = sender.send(line);
let mut rest = String::new();
let _ = stdout.read_to_string(&mut rest);
let _ = sender.send(rest);
});
let (stderr_sender, stderr) = mpsc::channel();
thread::spawn(move || {
let mut text = String::new();
let _ = stderr_pipe.read_to_string(&mut text);
let _ = stderr_sender.send(text);
});
let ready = within_wait(&lines, &stderr, &mut launcher, "ready line");
assert_eq!(ready, "ready\n");
send(signal, &launcher);
let rest = within_wait(&lines, &stderr, &mut launcher, "end after the signal");
(rest, launcher.wait().unwrap())
}
#[test]
fn sigterm_through_the_launcher_reaches_the_command() {
let (rest, status) = signal_a_ready_launcher("TERM");
assert_eq!(rest, "trapped\n");
assert_eq!(status.code(), Some(3));
}
#[test]
fn sigint_through_the_launcher_reaches_the_command() {
let (rest, status) = signal_a_ready_launcher("INT");
assert_eq!(rest, "trapped\n");
assert_eq!(status.code(), Some(3));
}
#[test]
fn a_signal_death_gives_128_plus_the_signal() {
let script = "kill -KILL $$";
let direct = crabpot(&["--crabpot-helper", "init", "sh", "-c", script]);
let sandboxed = crabpot(&["--", "sh", "-c", script]);
assert_eq!(direct.status.code(), Some(137), "{direct:?}");
assert_eq!(sandboxed.status.code(), Some(137), "{sandboxed:?}");
}
#[test]
fn a_signal_during_startup_is_not_lost() {
let mut term = SigSet::empty();
term.add(Signal::SIGTERM);
term.thread_block().unwrap();
let helper = spawn_helper("exec sleep 60");
send("TERM", &helper);
term.thread_unblock().unwrap();
let (sender, receiver) = mpsc::channel();
thread::spawn(move || sender.send(helper.wait_with_output()));
let output = receiver
.recv_timeout(Duration::from_secs(20))
.expect("the helper did not end")
.unwrap();
assert_eq!(output.status.code(), Some(143), "{output:?}");
}
fn in_sandbox(script: &str) -> Output {
let output = crabpot(&["--", "bash", "-c", script]);
assert!(output.status.success(), "{output:?}");
output
}
fn helper_in_bwrap(options: &[&str], script: &str) -> Output {
let proxy = crabpot_core::proxy::Proxy::start(&[], &[], &[], false, None).unwrap();
let bwrap = crabpot_linux::deps::check_here().unwrap();
let wrapped = crabpot_linux::wrap::words(
&bwrap,
&crabpot_core::config::Config::default(),
std::path::Path::new(CRABPOT),
proxy.mux_socket(),
crabpot_linux::deps::is_root_with_setfcap_here(),
)
.unwrap();
Command::new(&wrapped.words[0])
.args(&wrapped.words[1..])
.args([
"--",
"sh",
"-c",
"readlink /proc/self/ns/net; exec \"$@\"",
"sh",
CRABPOT,
"--crabpot-helper",
"init",
"--relay",
])
.arg(proxy.mux_socket())
.args(options)
.args(["python3", "-c", script])
.output()
.unwrap()
}
#[test]
fn loopback_addresses_are_dialable_in_a_new_network_namespace_with_the_proxy() {
let output = helper_in_bwrap(
&[
"--loopback",
"10.9.9.9",
"--loopback",
"10.9.9.10",
"--loopback",
"fd00::5",
],
r#"
import os, socket
print(os.readlink('/proc/self/ns/net'))
for family, address in [(socket.AF_INET, '10.9.9.9'), (socket.AF_INET, '10.9.9.10'), (socket.AF_INET6, 'fd00::5')]:
with socket.socket(family, socket.SOCK_STREAM) as server:
server.settimeout(5)
server.bind((address, 0))
server.listen()
with socket.create_connection((address, server.getsockname()[1]), timeout=5) as client:
connection, _ = server.accept()
with connection:
connection.settimeout(5)
client.sendall(b'ping')
assert connection.recv(4) == b'ping'
print('dialed ' + address)
with socket.create_connection(('127.0.0.1', 3128), timeout=5) as proxy:
proxy.sendall(b'CONNECT example.com:443 HTTP/1.1\r\nHost: example.com:443\r\n\r\n')
with proxy.makefile('rb') as response:
assert response.readline() == b'HTTP/1.1 407 Proxy Authentication Required\r\n'
print('proxy reached')
assert sorted(p for p in os.listdir('/proc') if p.isdigit()) == ['1', '2']
"#,
);
assert!(output.status.success(), "{output:?}");
let text = stdout(&output);
let lines: Vec<_> = text.lines().collect();
assert_ne!(lines[0], lines[1], "{text}");
assert_eq!(
&lines[2..],
[
"dialed 10.9.9.9",
"dialed 10.9.9.10",
"dialed fd00::5",
"proxy reached"
]
);
}
#[test]
fn without_loopback_the_helper_keeps_bwraps_network_namespace() {
let output = helper_in_bwrap(&[], "import os; print(os.readlink('/proc/self/ns/net'))");
assert!(output.status.success(), "{output:?}");
let text = stdout(&output);
let lines: Vec<_> = text.lines().collect();
assert_eq!(lines.len(), 2, "{text}");
assert_eq!(lines[0], lines[1], "{text}");
}
#[test]
fn repeated_loopback_addresses_are_configured_once() {
let output = helper_in_bwrap(
&[
"--loopback",
"10.9.9.9",
"--loopback",
"10.9.9.9",
"--loopback",
"fd00::5",
"--loopback",
"fd00::5",
],
r#"
import socket
for family, address in [(socket.AF_INET, '10.9.9.9'), (socket.AF_INET6, 'fd00::5')]:
with socket.socket(family, socket.SOCK_STREAM) as server:
server.bind((address, 0))
print('bound ' + address)
"#,
);
assert_eq!(output.status.code(), Some(125), "{output:?}");
assert!(
String::from_utf8_lossy(&output.stderr).contains("10.9.9.9"),
"{output:?}"
);
assert!(!stdout(&output).contains("ran"), "{output:?}");
}
#[test]
fn the_relay_forwards_an_http_connect_to_the_proxy() {
let output = in_sandbox(
r#"exec 3<>/dev/tcp/localhost/3128 || exit 2
printf 'CONNECT example.com:443 HTTP/1.1\r\nHost: example.com:443\r\n\r\n' >&3
head -n 1 <&3"#,
);
assert_eq!(
stdout(&output),
"HTTP/1.1 407 Proxy Authentication Required\r\n"
);
}
#[test]
fn the_relay_forwards_socks_to_the_proxy() {
let output = in_sandbox(
r#"exec 3<>/dev/tcp/localhost/1080 || exit 2
printf '\005\001\000' >&3
head -c 2 <&3 | od -An -tx1"#,
);
assert_eq!(stdout(&output).trim(), "05 ff");
}
#[test]
fn a_relay_that_cannot_bind_makes_the_helper_exit_125() {
let _held = TcpListener::bind(("::", 3128)).or_else(|_| TcpListener::bind(("0.0.0.0", 3128)));
let output = crabpot(&[
"--crabpot-helper",
"init",
"--relay",
"/no/such/proxy.sock",
"echo",
"ran",
]);
assert_eq!(output.status.code(), Some(125), "{output:?}");
assert!(
output.stdout.is_empty(),
"the command must not run: {output:?}"
);
assert!(
String::from_utf8_lossy(&output.stderr).contains("3128"),
"{output:?}"
);
}
fn settings_allowing(case: &str, allowed: &[&str]) -> PathBuf {
let list: Vec<String> = allowed.iter().map(|host| format!("\"{host}\"")).collect();
let path = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(format!("connect-{case}.json"));
fs::write(
&path,
format!(
r#"{{"network": {{"allowedDomains": [{}], "deniedDomains": []}}, "filesystem": {{"denyRead": [], "allowRead": [], "allowWrite": [], "denyWrite": []}}}}"#,
list.join(", ")
),
)
.unwrap();
path
}
fn in_sandbox_allowing(case: &str, allowed: &[&str], script: &str) -> Output {
let settings = settings_allowing(case, allowed);
crabpot(&[
"-s",
settings.to_str().unwrap(),
"--",
"sh",
"-c",
script,
CRABPOT,
])
}
#[test]
fn the_command_sees_the_ssh_proxy_command_and_its_credential() {
let output = crabpot(&[
"--",
"sh",
"-c",
r#"printf '%s\n' "$GIT_SSH_COMMAND" "$SRT_SSH_PROXY_BINARY" "$SRT_SSH_PROXY_CREDENTIAL""#,
]);
assert!(output.status.success(), "{output:?}");
let text = stdout(&output);
let lines: Vec<&str> = text.lines().collect();
assert_eq!(lines.len(), 3, "{output:?}");
assert!(
lines[0].contains("--crabpot-helper connect 3128")
&& lines[0].contains("SRT_SSH_PROXY_BINARY"),
"{}",
lines[0]
);
assert_eq!(
lines[1], CRABPOT,
"the binary the command expands, on the environment and not in its text"
);
assert!(!lines[2].is_empty(), "the credential is set to read");
}
#[test]
fn connect_mode_tunnels_to_an_allowed_host() {
let held = TcpListener::bind(("127.0.0.1", 0)).unwrap();
let port = held.local_addr().unwrap().port();
let answered = thread::spawn(move || {
let (mut client, _) = held.accept().unwrap();
let mut asked = String::new();
BufReader::new(&mut client).read_line(&mut asked).unwrap();
client.write_all(format!("got:{asked}").as_bytes()).unwrap();
});
let script =
format!(r#"printf 'ping\n' | "$0" --crabpot-helper connect 3128 127.0.0.1 {port}"#);
let output = in_sandbox_allowing("allowed", &["127.0.0.1"], &script);
let seen = format!(
"stdout: {}stderr: {}",
stdout(&output),
String::from_utf8_lossy(&output.stderr)
);
assert!(output.status.success(), "{seen}");
assert_eq!(stdout(&output), "got:ping\n", "{seen}");
answered.join().unwrap();
}
#[test]
fn connect_mode_reports_a_refused_host() {
let script = r#""$0" --crabpot-helper connect 3128 not-allowed.example 80"#;
let output = in_sandbox_allowing("refused", &["127.0.0.1"], script);
assert_eq!(output.status.code(), Some(1), "{output:?}");
assert!(output.stdout.is_empty(), "{output:?}");
let text = String::from_utf8_lossy(&output.stderr);
assert!(text.contains("not-allowed.example:80"), "{text}");
assert!(text.contains("host is not on the allow list"), "{text}");
}