feat: run sandbox transports in disposable workers

The control plane opened ssh connections itself, so a malicious ssh
server attacked the process that held the database, the master key, and
every room's keys. It now starts one worker per target and workspace.
The worker gets one target key on stdin, binds loopback behind a random
token, and serves exec, files, the pty, and the preview proxy. It has no
database and no master key, and a closed stdin ends it.

The preview HTTP client moved into the worker too. The app sees a typed
response, and the worker caps bodies, output tails, handshakes, and
transport waits.

Direct targets now reject loopback, private, link-local, multicast,
unspecified, carrier-grade NAT, and documentation addresses when a
target is created and again when its worker starts.
TOGETHER_ALLOW_PRIVATE_TARGETS=1 lifts the check for a server that runs
on one private network.
This commit is contained in:
vhaudiquet committed 2026-11-29 18:22:44 +01:00
1 parent a8f7e74ba0
commit fc684115b5
16 files changed
+1355 -208

No files matched your search

+110
View File
@@ -1,5 +1,7 @@
//! Small helpers shared by the API and the turn runner.
use std::net::{IpAddr, Ipv4Addr};
use rand::RngCore;
/// Milliseconds since the Unix epoch.
@@ -10,6 +12,82 @@ pub fn now_ms() -> i64 {
.unwrap_or(0)
}
/// Parses an IP literal, including a bracketed IPv6 address.
pub fn literal_ip(host: &str) -> Option<IpAddr> {
let host = host.trim().trim_start_matches('[').trim_end_matches(']');
host.parse().ok()
}
/// True when a target address is local. The list is the one a server
/// must not dial on behalf of a user: loopback, private, link-local,
/// multicast, unspecified, carrier-grade NAT, and the documentation
/// ranges. IPv4-mapped IPv6 follows its IPv4 part.
pub fn forbidden_target_ip(ip: IpAddr) -> bool {
match ip {
IpAddr::V4(v4) => forbidden_v4(v4),
IpAddr::V6(v6) => {
if let Some(v4) = v6.to_ipv4_mapped() {
return forbidden_v4(v4);
}
let s = v6.segments();
v6.is_loopback()
|| v6.is_unspecified()
|| v6.is_multicast()
|| (s[0] & 0xffc0) == 0xfe80
|| (s[0] & 0xfe00) == 0xfc00
|| (s[0] & 0xffc0) == 0xfec0
|| (s[0] == 0x2001 && s[1] == 0x0db8)
}
}
}
fn forbidden_v4(v4: Ipv4Addr) -> bool {
let o = v4.octets();
v4.is_loopback()
|| v4.is_private()
|| v4.is_link_local()
|| v4.is_broadcast()
|| v4.is_unspecified()
|| v4.is_multicast()
|| v4.is_documentation()
|| (o[0] == 100 && (o[1] & 0xc0) == 64)
}
/// Resolves a target and rejects it when any address is local. A
/// hostname that does not resolve is an error too: a target the server
/// cannot reach is not a target.
pub async fn check_target_address(host: &str, port: u16) -> Result<(), String> {
if let Some(ip) = literal_ip(host) {
return if forbidden_target_ip(ip) {
Err(format!(
"{host} is a local or private address; an operator can allow those with TOGETHER_ALLOW_PRIVATE_TARGETS=1"
))
} else {
Ok(())
};
}
let addrs = tokio::time::timeout(
std::time::Duration::from_secs(5),
tokio::net::lookup_host((host, port)),
)
.await
.map_err(|_| format!("could not resolve {host} in time"))?
.map_err(|err| format!("could not resolve {host}: {err}"))?
.collect::<Vec<_>>();
if addrs.is_empty() {
return Err(format!("{host} did not resolve to an address"));
}
for addr in addrs {
if forbidden_target_ip(addr.ip()) {
return Err(format!(
"{host} resolves to the local address {}; an operator can allow private targets with TOGETHER_ALLOW_PRIVATE_TARGETS=1",
addr.ip()
));
}
}
Ok(())
}
/// A URL-safe random token of `bytes` entropy, no padding.
pub fn random_token(bytes: usize) -> String {
use base64::Engine;
@@ -102,6 +180,38 @@ mod tests {
);
}
#[test]
fn local_target_addresses_are_forbidden() {
for text in [
"127.0.0.1",
"10.0.0.1",
"172.16.0.1",
"192.168.1.1",
"169.254.169.254",
"100.64.0.1",
"0.0.0.0",
"224.0.0.1",
"::1",
"fe80::1",
"fc00::1",
"::ffff:127.0.0.1",
] {
let ip = literal_ip(text).unwrap();
assert!(forbidden_target_ip(ip), "{text} should be forbidden");
}
for text in ["1.1.1.1", "2606:4700:4700::1111", "::ffff:1.1.1.1"] {
let ip = literal_ip(text).unwrap();
assert!(!forbidden_target_ip(ip), "{text} should be allowed");
}
}
#[tokio::test]
async fn a_literal_loopback_target_is_rejected() {
assert!(check_target_address("127.0.0.1", 22).await.is_err());
assert!(check_target_address("[::1]", 22).await.is_err());
assert!(check_target_address("1.1.1.1", 22).await.is_ok());
}
#[test]
fn mentions_need_a_word_boundary() {
assert!(mentions_agent("@rhema fix this"));