Skip to content

Commit c8434ad

Browse files
committed
test(sandbox): cover the SSRF-to-TLS composition across the proxy tunnel
Existing tests exercised validated-IP CONNECT and the upstream-TLS helper independently, but not the full boundary. Add an end-to-end regression that stands up a fake corporate proxy tunneling to a fake TLS server and drives the real path: connect_via_validated CONNECTs to the validated address, the proxy splices the tunnel, and tls_connect_upstream verifies the upstream certificate against the original hostname carried in SNI. It asserts the CONNECT authority is the validated IP and never the hostname, that verification succeeds for the matching hostname, and that a mismatched hostname is rejected — proving a rebinding or split-horizon substitution behind the proxy cannot pass certificate verification. Signed-off-by: Philippe Martin <[email protected]>
1 parent 80e4ac3 commit c8434ad

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

‎crates/openshell-supervisor-network/src/upstream_proxy.rs‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,118 @@ mod tests {
15551555
assert_eq!(err.kind(), ErrorKind::InvalidInput);
15561556
}
15571557

1558+
#[tokio::test]
1559+
async fn validated_ip_connect_tunnels_to_upstream_tls_verified_by_hostname() {
1560+
// End-to-end composition of the SSRF-to-TLS boundary: a validated
1561+
// address is CONNECTed to the corporate proxy (never the hostname),
1562+
// the proxy tunnels to the destination, and upstream TLS is verified
1563+
// against the original hostname carried in SNI. Covers both a
1564+
// matching-hostname success and a mismatched-certificate rejection.
1565+
use crate::l7::tls;
1566+
use std::sync::{Arc, Mutex};
1567+
use tokio::io::copy_bidirectional;
1568+
1569+
const SERVER_HOSTNAME: &str = "upstream.example.test";
1570+
1571+
let _ = rustls::crypto::ring::default_provider().install_default();
1572+
1573+
// Trusted CA; the client config trusts it, and the fake upstream
1574+
// server presents a leaf for SERVER_HOSTNAME signed by it.
1575+
let ca = tls::SandboxCa::generate().unwrap();
1576+
let client_config = tls::build_upstream_client_config(ca.cert_pem());
1577+
let tls_state = Arc::new(tls::ProxyTlsState::new(
1578+
tls::CertCache::new(ca),
1579+
tls::build_upstream_client_config(""),
1580+
));
1581+
1582+
// Fake upstream TLS server: accepts tunneled connections and completes
1583+
// a TLS handshake presenting the SERVER_HOSTNAME cert.
1584+
let tls_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
1585+
let tls_addr = tls_listener.local_addr().unwrap();
1586+
tokio::spawn(async move {
1587+
while let Ok((conn, _)) = tls_listener.accept().await {
1588+
let state = tls_state.clone();
1589+
tokio::spawn(async move {
1590+
let _ = tls::tls_terminate_client(conn, &state, SERVER_HOSTNAME).await;
1591+
});
1592+
}
1593+
});
1594+
1595+
// Fake corporate proxy: records each CONNECT request line and splices
1596+
// the tunnel to the upstream TLS server.
1597+
let proxy_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
1598+
let proxy_addr = proxy_listener.local_addr().unwrap();
1599+
let connect_lines: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
1600+
{
1601+
let connect_lines = connect_lines.clone();
1602+
tokio::spawn(async move {
1603+
while let Ok((mut client, _)) = proxy_listener.accept().await {
1604+
let connect_lines = connect_lines.clone();
1605+
tokio::spawn(async move {
1606+
let request = read_request(&mut client).await;
1607+
connect_lines
1608+
.lock()
1609+
.unwrap()
1610+
.push(request.lines().next().unwrap_or_default().to_string());
1611+
client
1612+
.write_all(b"HTTP/1.1 200 Connection established\r\n\r\n")
1613+
.await
1614+
.unwrap();
1615+
if let Ok(mut upstream) = TcpStream::connect(tls_addr).await {
1616+
let _ = copy_bidirectional(&mut client, &mut upstream).await;
1617+
}
1618+
});
1619+
}
1620+
});
1621+
}
1622+
1623+
let endpoint = endpoint_for(proxy_addr, None);
1624+
1625+
// Success: CONNECT to the validated IP, TLS verified against the
1626+
// requested hostname.
1627+
let stream =
1628+
connect_via_validated(&endpoint, SERVER_HOSTNAME, tls_addr.port(), &[tls_addr])
1629+
.await
1630+
.unwrap();
1631+
tls::tls_connect_upstream(stream, SERVER_HOSTNAME, &client_config)
1632+
.await
1633+
.expect("TLS must verify for the requested hostname");
1634+
1635+
// The proxy saw the validated IP in the CONNECT authority, never the
1636+
// hostname — the tunnel is bound to the SSRF-validated address.
1637+
{
1638+
let lines = connect_lines.lock().unwrap();
1639+
let line = lines.last().expect("proxy received a CONNECT");
1640+
assert!(
1641+
line.starts_with(&format!("CONNECT {}:", tls_addr.ip())),
1642+
"CONNECT must target the validated IP: {line}"
1643+
);
1644+
assert!(
1645+
!line.contains(SERVER_HOSTNAME),
1646+
"hostname must not appear in the CONNECT authority: {line}"
1647+
);
1648+
}
1649+
1650+
// Mismatch: the same validated tunnel, but verifying a different
1651+
// hostname must fail — the server cert is only valid for
1652+
// SERVER_HOSTNAME, so a rebinding/split-horizon substitution cannot
1653+
// pass certificate verification.
1654+
let stream = connect_via_validated(
1655+
&endpoint,
1656+
"wrong.example.test",
1657+
tls_addr.port(),
1658+
&[tls_addr],
1659+
)
1660+
.await
1661+
.unwrap();
1662+
assert!(
1663+
tls::tls_connect_upstream(stream, "wrong.example.test", &client_config)
1664+
.await
1665+
.is_err(),
1666+
"TLS must reject a certificate that does not match the requested hostname"
1667+
);
1668+
}
1669+
15581670
#[tokio::test]
15591671
async fn connect_via_sends_hostname_on_operator_opt_in() {
15601672
let (addr, handle) = fake_proxy("HTTP/1.1 200 Connection established\r\n\r\n").await;

0 commit comments

Comments
 (0)