Skip to content

Commit 5617bcf

Browse files
committed
fix(sandbox,podman): bind proxied CONNECT tunnels to validated addresses
The CONNECT request sent to the corporate proxy carried the destination hostname, so the proxy resolved the name itself and the addresses that had passed SSRF and allowed_ips validation were discarded. Split-horizon DNS or rebinding at the proxy could then reach internal or otherwise unapproved destinations through a tunnel the supervisor logged as validated, and IP-range policy could not be enforced at all on proxied dials. CONNECT now targets a validated resolved address by default: the proxy performs no DNS resolution and the tunnel stays bound to the answer the supervisor checked. The hostname still travels inside the tunnel (TLS SNI, application Host), so destination servers behave normally. In split-horizon networks, operators point the gateway host at the corporate resolver so internal names validate to their internal addresses. For proxies whose ACLs filter on hostnames and reject IP CONNECT targets, a new proxy_connect_by_hostname opt-in (CLI --sandbox-proxy-connect-by-hostname, env OPENSHELL_SANDBOX_PROXY_CONNECT_BY_HOSTNAME, reserved OPENSHELL_UPSTREAM_PROXY_CONNECT_BY_HOSTNAME) restores hostname CONNECT, documented as re-opening proxy-side resolution and making the proxy's ACLs the effective egress control. Fail-closed pairing on both sides: the opt-in without a proxy, or any value other than 'true', is fatal. Adds regression coverage for the IP CONNECT request line (including IPv6 bracketing and hostname non-leakage), the hostname opt-in, and the config pairing rules in driver and supervisor. Signed-off-by: Philippe Martin <[email protected]>
1 parent f86d73c commit 5617bcf

10 files changed

Lines changed: 338 additions & 36 deletions

File tree

‎architecture/sandbox.md‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,21 @@ resolution-aware: an entry with a `:port` qualifier only bypasses that port,
121121
and IP/CIDR entries also match hostnames through their validated resolved
122122
addresses, with the direct dial limited to the addresses the entry contains. Only `http://` proxy URLs in explicit
123123
`http://host:port` form are supported — the scheme and port are both
124-
required, and a path, query, or fragment is rejected. Local DNS resolution and SSRF validation still run before the
125-
proxied dial; the CONNECT target sent to the corporate proxy is the requested
126-
hostname. The workload child's proxy variables are unaffected — they are
127-
always rewritten to point at the local policy proxy.
124+
required, and a path, query, or fragment is rejected. Local DNS resolution
125+
and SSRF validation still run before the proxied dial, and the CONNECT
126+
target sent to the corporate proxy is a validated resolved address, so the
127+
proxy performs no DNS resolution of its own and the tunnel stays bound to
128+
the answer that passed SSRF and `allowed_ips` validation. The hostname still
129+
travels inside the tunnel (TLS SNI, application `Host`). In split-horizon
130+
networks, point the gateway host at the corporate resolver so internal names
131+
validate to their internal addresses; the `proxy_connect_by_hostname`
132+
opt-in (reserved `OPENSHELL_UPSTREAM_PROXY_CONNECT_BY_HOSTNAME`) exists as a
133+
last resort for proxies whose ACLs filter on hostnames and reject IP CONNECT
134+
targets — with it, the proxy resolves the name itself and its ACLs become
135+
the effective egress control for proxied TLS. (Resolving through the proxy's
136+
own DNS view, e.g. DoH tunneled via CONNECT, is a possible future
137+
enhancement and out of scope.) The workload child's proxy variables are
138+
unaffected — they are always rewritten to point at the local policy proxy.
128139

129140
The configuration is fail-closed: a reserved variable that is present but
130141
invalid — a present-but-empty value, an unsupported or malformed proxy URL, an

‎crates/openshell-core/src/sandbox_env.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,17 @@ pub const UPSTREAM_PROXY_AUTH_FILE: &str = "OPENSHELL_UPSTREAM_PROXY_AUTH_FILE";
154154
/// Compute drivers write it in the required-variable tier from the driver's
155155
/// `proxy_auth_allow_insecure` setting.
156156
pub const UPSTREAM_PROXY_AUTH_ALLOW_INSECURE: &str = "OPENSHELL_UPSTREAM_PROXY_AUTH_ALLOW_INSECURE";
157+
158+
/// Explicit operator opt-out (`true`) that sends the destination *hostname*
159+
/// in the CONNECT request to the corporate proxy instead of a validated IP.
160+
///
161+
/// By default the supervisor CONNECTs to an address that already passed
162+
/// SSRF and `allowed_ips` validation, so the proxy performs no DNS
163+
/// resolution and the connection is bound to the validated answer. With
164+
/// this opt-out the proxy resolves the name itself: hostname-filtering
165+
/// proxy ACLs keep working, but a name whose resolution differs at the
166+
/// proxy (split-horizon DNS, rebinding) can reach destinations the sandbox
167+
/// policy never approved — the proxy's own ACLs become the effective
168+
/// egress control. Compute drivers write it in the required-variable tier
169+
/// from the driver's `proxy_connect_by_hostname` setting.
170+
pub const UPSTREAM_PROXY_CONNECT_BY_HOSTNAME: &str = "OPENSHELL_UPSTREAM_PROXY_CONNECT_BY_HOSTNAME";

‎crates/openshell-driver-podman/README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ Podman resources after out-of-band container removal or label drift.
349349
| `OPENSHELL_SANDBOX_NO_PROXY` | `--sandbox-no-proxy` | unset | Comma-separated `NO_PROXY` list (hostnames, domain suffixes, IPs, CIDRs, each with an optional `:port` qualifier) dialed directly instead of through the corporate proxy. IP/CIDR entries also match hostnames through their validated DNS resolution. |
350350
| `OPENSHELL_SANDBOX_PROXY_AUTH_FILE` | `--sandbox-proxy-auth-file` | unset | Path to a file containing the proxy credentials as `user:pass`. Staged as a root-only Podman secret so credentials never appear in config or container metadata. Requires the insecure-auth acknowledgement below. |
351351
| `OPENSHELL_SANDBOX_PROXY_AUTH_ALLOW_INSECURE` | `--sandbox-proxy-auth-allow-insecure` | unset | Explicit acknowledgement (`true`) that the credential is sent as cleartext Basic auth over the plain-TCP connection to the `http://` proxy. Required when the auth file is set; rejected when it is not. |
352+
| `OPENSHELL_SANDBOX_PROXY_CONNECT_BY_HOSTNAME` | `--sandbox-proxy-connect-by-hostname` | unset | Send the destination hostname in CONNECT requests instead of a validated IP. Last resort for proxies whose ACLs filter on hostnames: the proxy then resolves the name itself, so sandbox SSRF/`allowed_ips` validation no longer binds the connection. |
352353

353354
Through the gateway, the same settings are the `https_proxy`, `no_proxy`, and
354355
`proxy_auth_file` keys under `[openshell.drivers.podman]`; see
@@ -367,6 +368,15 @@ credential. Setting `proxy_auth_file` therefore requires
367368
`proxy_auth_allow_insecure = true`; both the driver and the in-container
368369
supervisor reject credentials without that explicit acknowledgement.
369370

371+
CONNECT requests target a validated resolved IP by default, so the proxy
372+
performs no DNS resolution and the tunnel stays bound to the address that
373+
passed the sandbox's SSRF and `allowed_ips` checks; the hostname still
374+
travels inside the tunnel (TLS SNI, application `Host`). In split-horizon
375+
networks, point the gateway host at the corporate resolver. Set
376+
`proxy_connect_by_hostname = true` only when the proxy's ACLs filter on
377+
hostnames and reject IP CONNECT targets — it re-opens proxy-side DNS
378+
resolution, making the proxy's ACLs the effective egress control.
379+
370380
## Rootless-Specific Adaptations
371381

372382
The Podman driver is designed for rootless operation. The following adaptations

‎crates/openshell-driver-podman/src/config.rs‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ pub struct PodmanComputeConfig {
173173
/// rejected at startup. Set it only when the path to the proxy is a
174174
/// trusted network segment.
175175
pub proxy_auth_allow_insecure: Option<bool>,
176+
/// Send the destination *hostname* in CONNECT requests to the corporate
177+
/// proxy instead of a validated IP.
178+
///
179+
/// By default the supervisor CONNECTs to an address that already passed
180+
/// SSRF and `allowed_ips` validation, so the proxy performs no DNS
181+
/// resolution and the tunnel stays bound to the validated answer. Set
182+
/// this to `true` only when the proxy's ACLs filter on hostnames and
183+
/// reject IP CONNECT targets: the proxy then resolves the name itself,
184+
/// so a name that resolves differently at the proxy (split-horizon DNS,
185+
/// rebinding) can reach destinations the sandbox policy never approved,
186+
/// and the proxy's own ACLs become the effective egress control. Prefer
187+
/// pointing the gateway host at the corporate resolver so validated-IP
188+
/// CONNECT works in split-horizon networks.
189+
pub proxy_connect_by_hostname: Option<bool>,
176190
}
177191

178192
pub const DEFAULT_HEALTH_CHECK_INTERVAL_SECS: u64 = 10;
@@ -305,6 +319,14 @@ impl PodmanComputeConfig {
305319
"proxy_auth_allow_insecure is set but no proxy_auth_file is configured".to_string(),
306320
));
307321
}
322+
323+
// The CONNECT-target mode only means something relative to a proxy
324+
// boundary the operator believed was in effect.
325+
if self.proxy_connect_by_hostname.is_some() && self.https_proxy.is_none() {
326+
return Err(crate::client::PodmanApiError::InvalidInput(
327+
"proxy_connect_by_hostname is set but no https_proxy is configured".to_string(),
328+
));
329+
}
308330
Ok(())
309331
}
310332

@@ -385,6 +407,7 @@ impl Default for PodmanComputeConfig {
385407
no_proxy: None,
386408
proxy_auth_file: None,
387409
proxy_auth_allow_insecure: None,
410+
proxy_connect_by_hostname: None,
388411
}
389412
}
390413
}
@@ -416,6 +439,7 @@ impl std::fmt::Debug for PodmanComputeConfig {
416439
.field("no_proxy", &self.no_proxy)
417440
.field("proxy_auth_file", &self.proxy_auth_file.is_some())
418441
.field("proxy_auth_allow_insecure", &self.proxy_auth_allow_insecure)
442+
.field("proxy_connect_by_hostname", &self.proxy_connect_by_hostname)
419443
.finish()
420444
}
421445
}
@@ -671,6 +695,28 @@ mod tests {
671695
}
672696
}
673697

698+
#[test]
699+
fn validate_proxy_config_accepts_connect_by_hostname_with_proxy() {
700+
for by_hostname in [Some(true), Some(false)] {
701+
let cfg = PodmanComputeConfig {
702+
https_proxy: Some("http://proxy.corp.com:8080".to_string()),
703+
proxy_connect_by_hostname: by_hostname,
704+
..PodmanComputeConfig::default()
705+
};
706+
assert!(cfg.validate_proxy_config().is_ok(), "{by_hostname:?}");
707+
}
708+
}
709+
710+
#[test]
711+
fn validate_proxy_config_rejects_connect_by_hostname_without_proxy() {
712+
let cfg = PodmanComputeConfig {
713+
proxy_connect_by_hostname: Some(true),
714+
..PodmanComputeConfig::default()
715+
};
716+
let err = cfg.validate_proxy_config().unwrap_err();
717+
assert!(err.to_string().contains("no https_proxy"), "{err}");
718+
}
719+
674720
#[test]
675721
fn validate_proxy_config_rejects_acknowledgement_without_auth_file() {
676722
for allow in [Some(true), Some(false)] {

‎crates/openshell-driver-podman/src/container.rs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,12 @@ fn build_env(
423423
.proxy_auth_allow_insecure
424424
.filter(|allowed| *allowed)
425425
.map(|_| "true".to_string());
426+
// Absent means the default validated-IP CONNECT binding; only the
427+
// explicit hostname opt-in is written through.
428+
let proxy_connect_by_hostname = config
429+
.proxy_connect_by_hostname
430+
.filter(|by_hostname| *by_hostname)
431+
.map(|_| "true".to_string());
426432
for (name, value) in [
427433
(
428434
openshell_core::sandbox_env::UPSTREAM_HTTPS_PROXY,
@@ -440,6 +446,10 @@ fn build_env(
440446
openshell_core::sandbox_env::UPSTREAM_PROXY_AUTH_ALLOW_INSECURE,
441447
&proxy_auth_allow_insecure,
442448
),
449+
(
450+
openshell_core::sandbox_env::UPSTREAM_PROXY_CONNECT_BY_HOSTNAME,
451+
&proxy_connect_by_hostname,
452+
),
443453
] {
444454
env.remove(name);
445455
if let Some(value) = value {
@@ -2618,6 +2628,32 @@ mod tests {
26182628
);
26192629
}
26202630

2631+
#[test]
2632+
fn container_spec_connect_by_hostname_written_only_on_opt_in() {
2633+
let sandbox = test_sandbox("proxy-id", "proxy-name");
2634+
let mut config = test_config();
2635+
config.https_proxy = Some("http://proxy.corp.com:8080".to_string());
2636+
2637+
// Default: no reserved variable, the supervisor uses validated-IP
2638+
// CONNECT binding.
2639+
let spec = build_container_spec(&sandbox, &config);
2640+
let env_map = spec["env"].as_object().expect("env should be an object");
2641+
assert!(
2642+
!env_map.contains_key(openshell_core::sandbox_env::UPSTREAM_PROXY_CONNECT_BY_HOSTNAME),
2643+
"hostname CONNECT must be absent without the operator opt-in"
2644+
);
2645+
2646+
config.proxy_connect_by_hostname = Some(true);
2647+
let spec = build_container_spec(&sandbox, &config);
2648+
let env_map = spec["env"].as_object().expect("env should be an object");
2649+
assert_eq!(
2650+
env_map
2651+
.get(openshell_core::sandbox_env::UPSTREAM_PROXY_CONNECT_BY_HOSTNAME)
2652+
.and_then(|v| v.as_str()),
2653+
Some("true")
2654+
);
2655+
}
2656+
26212657
#[test]
26222658
fn container_spec_omits_tls_without_config() {
26232659
let sandbox = test_sandbox("notls-id", "notls-name");

‎crates/openshell-driver-podman/src/main.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ struct Args {
126126
/// proxy. Required when `--sandbox-proxy-auth-file` is set.
127127
#[arg(long, env = "OPENSHELL_SANDBOX_PROXY_AUTH_ALLOW_INSECURE")]
128128
sandbox_proxy_auth_allow_insecure: Option<bool>,
129+
130+
/// Send the destination hostname in CONNECT requests to the corporate
131+
/// proxy instead of a validated IP. Only for proxies whose ACLs filter
132+
/// on hostnames: the proxy then resolves the name itself, so sandbox
133+
/// SSRF/`allowed_ips` validation no longer binds the connection.
134+
#[arg(long, env = "OPENSHELL_SANDBOX_PROXY_CONNECT_BY_HOSTNAME")]
135+
sandbox_proxy_connect_by_hostname: Option<bool>,
129136
}
130137

131138
#[tokio::main]
@@ -164,6 +171,7 @@ async fn main() -> Result<()> {
164171
no_proxy: args.sandbox_no_proxy,
165172
proxy_auth_file: args.sandbox_proxy_auth_file,
166173
proxy_auth_allow_insecure: args.sandbox_proxy_auth_allow_insecure,
174+
proxy_connect_by_hostname: args.sandbox_proxy_connect_by_hostname,
167175
..PodmanComputeConfig::default()
168176
})
169177
.await

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2955,9 +2955,13 @@ fn validate_declared_endpoint_resolved_addrs(
29552955
/// and an IP/CIDR entry that matches through resolution limits the direct
29562956
/// dial to the addresses it contains.
29572957
///
2958-
/// The CONNECT target sent to the corporate proxy is the client-requested
2959-
/// hostname, so hostname-filtering proxies and split-horizon DNS at the
2960-
/// proxy keep working.
2958+
/// The CONNECT target sent to the corporate proxy is a validated resolved
2959+
/// address, so the proxy performs no DNS resolution of its own and the
2960+
/// tunnel stays bound to the answer that passed SSRF and `allowed_ips`
2961+
/// validation; the hostname still travels inside the tunnel (TLS SNI,
2962+
/// application `Host`). The operator opt-in `proxy_connect_by_hostname`
2963+
/// sends the client-requested hostname instead, for proxies whose ACLs
2964+
/// filter on hostnames, at the cost of re-opening proxy-side resolution.
29612965
///
29622966
/// Both paths return a [`upstream_proxy::PrefixedStream`]: for proxied
29632967
/// dials it replays any tunneled bytes that arrived in the same read as the
@@ -2971,7 +2975,20 @@ async fn dial_upstream(
29712975
if let Some(cfg) = upstream_proxy.as_ref() {
29722976
return match cfg.decision(host_lc, port, addrs) {
29732977
upstream_proxy::ProxyDecision::Proxy(endpoint) => {
2974-
upstream_proxy::connect_via(endpoint, host_lc, port).await
2978+
let target = if cfg.connect_by_hostname() {
2979+
upstream_proxy::ConnectTarget::Hostname
2980+
} else {
2981+
// First validated address, matching the order-of-attempt
2982+
// the direct path's `TcpStream::connect` uses.
2983+
let ip = addrs.first().map(SocketAddr::ip).ok_or_else(|| {
2984+
std::io::Error::new(
2985+
std::io::ErrorKind::InvalidInput,
2986+
format!("no validated addresses to CONNECT to for {host_lc}"),
2987+
)
2988+
})?;
2989+
upstream_proxy::ConnectTarget::Ip(ip)
2990+
};
2991+
upstream_proxy::connect_via(endpoint, host_lc, port, target).await
29752992
}
29762993
upstream_proxy::ProxyDecision::Direct(direct_addrs) => {
29772994
Ok(upstream_proxy::PrefixedStream::without_prefix(

0 commit comments

Comments
 (0)