Skip to content

Commit cc8aaf4

Browse files
committed
test(network): drive the CONNECT handler tests with an in-process client
The child-process client (even fork-free) made the handler tests environment-sensitive: on CI runners with a busy or restricted /proc, resolving the child's socket ownership degraded into the whole-/proc fallback scan and a deny, which surfaced as a hang. The client is now an in-process TcpStream and the test policy allows current_exe(), so identity resolution binds the socket to the test process itself in the descendant scan — the same in-process pattern the passing resolve_process_identity tests rely on. The tls: skip test additionally asserts that the handler emitted no DenialEvent at any stage, so it can no longer pass vacuously on a policy or identity deny. Refusal budgets widened to 30s as a belt for slow runners; the refusals themselves return in milliseconds. Signed-off-by: Tony Luo <[email protected]>
1 parent f694fc0 commit cc8aaf4

1 file changed

Lines changed: 65 additions & 76 deletions

File tree

  • crates/openshell-supervisor-network/src

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

Lines changed: 65 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -7980,73 +7980,63 @@ network_policies:
79807980
}
79817981

79827982
/// Drives a real `CONNECT` through the full `handle_tcp_connection` entry
7983-
/// point and returns `(completed, client_response_bytes)`. `completed` is
7984-
/// `false` when the handler was still running at `budget` (e.g. a `tls:
7985-
/// skip` route that proceeded past the fail-closed gate and blocked on the
7986-
/// unroutable upstream connect).
7983+
/// point and returns `(completed, client_response_bytes, denial_stages)`.
7984+
/// `completed` is `false` when the handler was still running at `budget`
7985+
/// (e.g. a `tls: skip` route that proceeded past the fail-closed gate and
7986+
/// blocked on the unroutable upstream connect). `denial_stages` collects the
7987+
/// `denial_stage` of every `DenialEvent` the handler emitted — empty means
7988+
/// the connection was allowed and not blocked/refused at any stage.
79877989
///
7988-
/// A child copy of `/bin/bash` opens the connection via `/dev/tcp` so the
7989-
/// proxy's `/proc`-based process-identity binding resolves it against a
7990-
/// permissive policy (glob on the temp dir) — the same identity pattern the
7991-
/// hot-swap regression test uses. Callers gate on Linux; `evaluate_opa_tcp`
7992-
/// denies unconditionally without `/proc`.
7990+
/// The client is an in-process `TcpStream` (no child process), so the client
7991+
/// socket is owned solely by this test process. Identity resolution finds
7992+
/// that owner in the descendant scan (which includes the entrypoint PID
7993+
/// itself), binds to `current_exe()`, and never falls through to the
7994+
/// whole-`/proc` scan — the environment-sensitive path that made a forked
7995+
/// child flaky under a busy CI `/proc`. Callers gate on Linux;
7996+
/// `evaluate_opa_tcp` denies unconditionally without `/proc`.
79937997
async fn drive_connect_through_handler(
79947998
endpoint_yaml: &str,
79957999
connect_target: &str,
79968000
budget: std::time::Duration,
7997-
) -> (bool, Vec<u8>) {
7998-
use std::os::unix::fs::PermissionsExt;
7999-
use std::process::{Command, Stdio};
8000-
8001+
) -> (bool, Vec<u8>, Vec<String>) {
80018002
const POLICY_REGO: &str = include_str!("../data/sandbox-policy.rego");
80028003

8003-
let tmp = tempfile::TempDir::new().unwrap();
8004-
let bash = tmp.path().join("connect-bash");
8005-
std::fs::copy("/bin/bash", &bash).expect("copy /bin/bash");
8006-
std::fs::set_permissions(&bash, std::fs::Permissions::from_mode(0o755)).unwrap();
8007-
8008-
// Permissive policy: allow any binary under the temp dir (the child bash
8009-
// copy) to reach the endpoint. The binary glob matches the child's
8010-
// `/proc/<pid>/exe`; matching is by path, not by hash.
8004+
// Allow the current test binary — the in-process client's
8005+
// `/proc/<pid>/exe` — to reach the endpoint. Matching is by path.
8006+
let exe = std::env::current_exe().expect("current_exe");
80118007
let data = format!(
80128008
r#"network_policies:
80138009
test_allow:
80148010
name: test_allow
80158011
endpoints:
80168012
{endpoint_yaml} binaries:
8017-
- {{ path: "{glob}/*" }}
8013+
- {{ path: "{exe}" }}
80188014
"#,
8019-
glob = tmp.path().display(),
8015+
exe = exe.display(),
80208016
);
80218017
let engine = Arc::new(OpaEngine::from_strings(POLICY_REGO, &data).expect("load policy"));
80228018

80238019
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
80248020
let proxy_port = listener.local_addr().unwrap().port();
80258021

8026-
// The child connects, sends the CONNECT request, then reads the proxy's
8027-
// reply to EOF and prints it. Everything is a bash builtin (exec,
8028-
// printf, read) so NO external command is forked — a forked `cat`/`head`
8029-
// would inherit the socket fd, making the inode owned by two PIDs with
8030-
// different binaries, which the resolver correctly denies as ambiguous
8031-
// shared-socket ownership. `read -d ''` reads all bytes up to EOF into
8032-
// `resp`; on EOF it returns non-zero but `resp` still holds the reply.
8033-
let script = format!(
8034-
"exec 3<>/dev/tcp/127.0.0.1/{proxy_port}; \
8035-
printf 'CONNECT {connect_target} HTTP/1.1\\r\\nHost: {connect_target}\\r\\n\\r\\n' >&3; \
8036-
IFS= read -r -d '' resp <&3; printf '%s' \"$resp\"",
8037-
);
8038-
let child = Command::new(&bash)
8039-
.arg("-c")
8040-
.arg(&script)
8041-
.stdin(Stdio::null())
8042-
.stdout(Stdio::piped())
8043-
.stderr(Stdio::null())
8044-
.spawn()
8045-
.expect("spawn connect-bash child");
8022+
// In-process client: connect, send the CONNECT request, read the reply to
8023+
// EOF. The proxy closes the socket after writing a 503/403; a proceeding
8024+
// `tls: skip` route stalls on the upstream connect until the handler is
8025+
// dropped at `budget`, which closes the socket and ends the read.
8026+
let target = connect_target.to_string();
8027+
let client = tokio::spawn(async move {
8028+
let mut sock = TcpStream::connect(("127.0.0.1", proxy_port)).await.unwrap();
8029+
let req = format!("CONNECT {target} HTTP/1.1\r\nHost: {target}\r\n\r\n");
8030+
sock.write_all(req.as_bytes()).await.unwrap();
8031+
let mut buf = Vec::new();
8032+
let _ = sock.read_to_end(&mut buf).await;
8033+
buf
8034+
});
80468035

80478036
let (server, _peer) = listener.accept().await.unwrap();
80488037
let entrypoint_pid = Arc::new(AtomicU32::new(std::process::id()));
80498038
let cache = Arc::new(BinaryIdentityCache::new());
8039+
let (denial_tx, mut denial_rx) = mpsc::unbounded_channel();
80508040

80518041
let completed = tokio::time::timeout(
80528042
budget,
@@ -8055,25 +8045,28 @@ network_policies:
80558045
engine,
80568046
cache,
80578047
entrypoint_pid,
8058-
None, // tls_state — ephemeral CA unavailable
8059-
None, // inference_ctx
8060-
None, // policy_local_ctx
8061-
Arc::new(None), // trusted_host_gateway
8062-
None, // secret_resolver
8063-
None, // dynamic_credentials
8064-
None, // denial_tx
8065-
None, // activity_tx
8048+
None, // tls_state — ephemeral CA unavailable
8049+
None, // inference_ctx
8050+
None, // policy_local_ctx
8051+
Arc::new(None), // trusted_host_gateway
8052+
None, // secret_resolver
8053+
None, // dynamic_credentials
8054+
Some(denial_tx), // denial_tx — positive allow/deny signal
8055+
None, // activity_tx
80668056
)),
80678057
)
80688058
.await
80698059
.is_ok();
80708060

8071-
// Do not kill the child: it exits on its own once the server socket is
8072-
// dropped (normal return or timeout), at which point its read sees EOF
8073-
// and it prints whatever the proxy wrote. Killing it here would race
8074-
// that final read and truncate the captured response.
8075-
let output = child.wait_with_output().expect("collect child output");
8076-
(completed, output.stdout)
8061+
let stdout = client.await.expect("client task");
8062+
8063+
// The handler future is now finished or dropped, so its sender half is
8064+
// gone; drain every denial it emitted.
8065+
let mut denial_stages = Vec::new();
8066+
while let Ok(event) = denial_rx.try_recv() {
8067+
denial_stages.push(event.denial_stage);
8068+
}
8069+
(completed, stdout, denial_stages)
80778070
}
80788071

80798072
/// End-to-end regression for the gator finding on PR #2162: with no TLS
@@ -8086,17 +8079,15 @@ network_policies:
80868079
eprintln!("skipping: handler identity binding requires /proc (Linux)");
80878080
return;
80888081
}
8089-
if !std::path::Path::new("/bin/bash").exists() {
8090-
eprintln!("skipping: /bin/bash not available");
8091-
return;
8092-
}
80938082

80948083
// Public documentation IP (RFC 5737 TEST-NET-3): passes the SSRF check
80958084
// but is never actually connected — the fail-closed gate refuses first.
8096-
let (completed, stdout) = drive_connect_through_handler(
8085+
// The 30s budget is a generous belt against a slow CI runner; the refusal
8086+
// returns in milliseconds.
8087+
let (completed, stdout, _denials) = drive_connect_through_handler(
80978088
" - { host: \"203.0.113.10\", port: 443 }\n",
80988089
"203.0.113.10:443",
8099-
std::time::Duration::from_secs(10),
8090+
std::time::Duration::from_secs(30),
81008091
)
81018092
.await;
81028093

@@ -8125,15 +8116,11 @@ network_policies:
81258116
eprintln!("skipping: handler identity binding requires /proc (Linux)");
81268117
return;
81278118
}
8128-
if !std::path::Path::new("/bin/bash").exists() {
8129-
eprintln!("skipping: /bin/bash not available");
8130-
return;
8131-
}
81328119

8133-
let (completed, stdout) = drive_connect_through_handler(
8120+
let (completed, stdout, _denials) = drive_connect_through_handler(
81348121
" - { host: \"127.0.0.1\", port: 443 }\n",
81358122
"127.0.0.1:443",
8136-
std::time::Duration::from_secs(10),
8123+
std::time::Duration::from_secs(30),
81378124
)
81388125
.await;
81398126

@@ -8163,18 +8150,13 @@ network_policies:
81638150
eprintln!("skipping: handler identity binding requires /proc (Linux)");
81648151
return;
81658152
}
8166-
if !std::path::Path::new("/bin/bash").exists() {
8167-
eprintln!("skipping: /bin/bash not available");
8168-
return;
8169-
}
81708153

81718154
// `_completed` is intentionally ignored: whether the unroutable connect
81728155
// stalls (times out) or is rejected fast by a CI egress firewall, the
81738156
// invariant is the same — a `tls: skip` route is exempt from the
81748157
// fail-closed gate, so it proceeds to the tunnel connect and writes no
8175-
// 503/denial to the client beforehand. A wrongly-refused route would
8176-
// instead surface a 503, making `stdout` non-empty.
8177-
let (_completed, stdout) = drive_connect_through_handler(
8158+
// 503/denial to the client beforehand.
8159+
let (_completed, stdout, denial_stages) = drive_connect_through_handler(
81788160
" - { host: \"203.0.113.10\", port: 443, tls: skip }\n",
81798161
"203.0.113.10:443",
81808162
std::time::Duration::from_millis(800),
@@ -8186,6 +8168,13 @@ network_policies:
81868168
stdout.is_empty(),
81878169
"tls: skip must emit no refusal/denial before the tunnel connect; got: {resp:?}"
81888170
);
8171+
// Positive allow evidence, so this test cannot pass vacuously on a
8172+
// policy/identity deny (which would emit a DenialEvent): the handler
8173+
// must have emitted no denial at any stage.
8174+
assert!(
8175+
denial_stages.is_empty(),
8176+
"tls: skip must be allowed end-to-end, not denied at any stage; got: {denial_stages:?}"
8177+
);
81898178
}
81908179

81918180
/// Verifies the policy half of the Linux handler tests on every platform

0 commit comments

Comments
 (0)