Skip to content

Commit 48da2fe

Browse files
committed
fix(network): bind hostname policy to supervisor DNS
Signed-off-by: Adrien Langou <[email protected]>
1 parent 6ab87ce commit 48da2fe

5 files changed

Lines changed: 114 additions & 52 deletions

File tree

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

Lines changed: 94 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4225,7 +4225,7 @@ async fn resolve_and_check_trusted_gateway(
42254225
"port {port} is a blocked control-plane port, connection rejected"
42264226
)));
42274227
}
4228-
let addrs = resolve_socket_addrs(host, port, entrypoint_pid)
4228+
let addrs = resolve_socket_addrs(host, port, entrypoint_pid, AddressBinding::IpConstrained)
42294229
.await
42304230
.map_err(DestinationCheckError::Resolution)?;
42314231
if addrs.is_empty() {
@@ -4345,16 +4345,31 @@ async fn resolve_from_sandbox_hosts(
43454345
None
43464346
}
43474347

4348-
async fn resolve_socket_addrs(
4348+
/// Controls whether untrusted sandbox hosts entries may participate in resolution.
4349+
///
4350+
/// `IpConstrained` is only valid when the caller verifies every returned IP
4351+
/// against an independent pin or CIDR before dialing.
4352+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4353+
enum AddressBinding {
4354+
NameAuthorized,
4355+
IpConstrained,
4356+
}
4357+
4358+
impl AddressBinding {
4359+
fn allows_sandbox_hosts(self) -> bool {
4360+
matches!(self, Self::IpConstrained)
4361+
}
4362+
}
4363+
4364+
async fn resolve_nonliteral_socket_addrs(
43494365
host: &str,
43504366
port: u16,
4351-
entrypoint_pid: u32,
4367+
address_binding: AddressBinding,
4368+
sandbox_hosts: Option<Vec<SocketAddr>>,
43524369
) -> std::result::Result<Vec<SocketAddr>, String> {
4353-
if let Some(addrs) = resolve_ip_literal(host, port) {
4354-
return Ok(addrs);
4355-
}
4356-
4357-
if let Some(addrs) = resolve_from_sandbox_hosts(host, port, entrypoint_pid).await {
4370+
if address_binding.allows_sandbox_hosts()
4371+
&& let Some(addrs) = sandbox_hosts
4372+
{
43584373
return Ok(addrs);
43594374
}
43604375

@@ -4376,6 +4391,25 @@ async fn resolve_socket_addrs(
43764391
Ok(addrs)
43774392
}
43784393

4394+
async fn resolve_socket_addrs(
4395+
host: &str,
4396+
port: u16,
4397+
entrypoint_pid: u32,
4398+
address_binding: AddressBinding,
4399+
) -> std::result::Result<Vec<SocketAddr>, String> {
4400+
if let Some(addrs) = resolve_ip_literal(host, port) {
4401+
return Ok(addrs);
4402+
}
4403+
4404+
let sandbox_hosts = if address_binding.allows_sandbox_hosts() {
4405+
resolve_from_sandbox_hosts(host, port, entrypoint_pid).await
4406+
} else {
4407+
None
4408+
};
4409+
4410+
resolve_nonliteral_socket_addrs(host, port, address_binding, sandbox_hosts).await
4411+
}
4412+
43794413
fn reject_internal_resolved_addrs(
43804414
host: &str,
43814415
addrs: &[SocketAddr],
@@ -4563,17 +4597,16 @@ async fn dial_transparent_upstream(
45634597
))
45644598
}
45654599

4566-
/// Resolve a host:port using sandbox `/etc/hosts` first (when available), then
4567-
/// reject if any resolved address is internal.
4600+
/// Resolve a host:port using the supervisor resolver, then reject if any
4601+
/// resolved address is internal.
45684602
///
45694603
/// Returns the resolved `SocketAddr` list on success. Returns an error string
45704604
/// if any resolved IP is in an internal range or if DNS resolution fails.
45714605
async fn resolve_and_reject_internal(
45724606
host: &str,
45734607
port: u16,
4574-
entrypoint_pid: u32,
45754608
) -> std::result::Result<Vec<SocketAddr>, DestinationCheckError> {
4576-
let addrs = resolve_socket_addrs(host, port, entrypoint_pid)
4609+
let addrs = resolve_socket_addrs(host, port, 0, AddressBinding::NameAuthorized)
45774610
.await
45784611
.map_err(DestinationCheckError::Resolution)?;
45794612
reject_internal_resolved_addrs(host, &addrs).map_err(DestinationCheckError::Denied)?;
@@ -4594,7 +4627,7 @@ async fn resolve_and_check_allowed_ips(
45944627
allowed_ips: &[ipnet::IpNet],
45954628
entrypoint_pid: u32,
45964629
) -> std::result::Result<Vec<SocketAddr>, DestinationCheckError> {
4597-
let addrs = resolve_socket_addrs(host, port, entrypoint_pid)
4630+
let addrs = resolve_socket_addrs(host, port, entrypoint_pid, AddressBinding::IpConstrained)
45984631
.await
45994632
.map_err(DestinationCheckError::Resolution)?;
46004633
validate_allowed_ips_for_resolved_addrs(host, port, &addrs, allowed_ips)
@@ -4606,13 +4639,15 @@ async fn resolve_and_check_allowed_ips(
46064639
///
46074640
/// Exact declared hostnames are the operator's trust signal, so RFC1918 and
46084641
/// other private ranges are allowed without a duplicated `allowed_ips` entry.
4609-
/// Loopback, link-local, unspecified, and control-plane ports remain blocked.
4642+
/// The supervisor resolver is authoritative because accepting the sandbox's
4643+
/// `/etc/hosts` here would let the workload choose the address authorized by
4644+
/// the hostname. Loopback, link-local, unspecified, and control-plane ports
4645+
/// remain blocked.
46104646
async fn resolve_and_check_declared_endpoint(
46114647
host: &str,
46124648
port: u16,
4613-
entrypoint_pid: u32,
46144649
) -> std::result::Result<Vec<SocketAddr>, DestinationCheckError> {
4615-
let addrs = resolve_socket_addrs(host, port, entrypoint_pid)
4650+
let addrs = resolve_socket_addrs(host, port, 0, AddressBinding::NameAuthorized)
46164651
.await
46174652
.map_err(DestinationCheckError::Resolution)?;
46184653
validate_declared_endpoint_resolved_addrs(host, port, &addrs)
@@ -11818,6 +11853,40 @@ network_policies:
1181811853

1181911854
// -- resolve_and_reject_internal --
1182011855

11856+
#[tokio::test]
11857+
async fn test_address_binding_controls_sandbox_hosts() {
11858+
let attacker_selected = resolve_from_hosts_file_contents(
11859+
"93.184.216.34 api.example.test\n",
11860+
"api.example.test",
11861+
80,
11862+
);
11863+
11864+
let name_authorized = resolve_nonliteral_socket_addrs(
11865+
"this-host-does-not-exist.invalid",
11866+
80,
11867+
AddressBinding::NameAuthorized,
11868+
Some(attacker_selected.clone()),
11869+
)
11870+
.await;
11871+
assert!(
11872+
name_authorized.is_err(),
11873+
"name-authorized resolution must ignore sandbox hosts"
11874+
);
11875+
11876+
let ip_constrained = resolve_nonliteral_socket_addrs(
11877+
"api.example.test",
11878+
80,
11879+
AddressBinding::IpConstrained,
11880+
Some(attacker_selected.clone()),
11881+
)
11882+
.await
11883+
.expect("IP-constrained resolution should accept sandbox hosts candidates");
11884+
assert_eq!(
11885+
ip_constrained, attacker_selected,
11886+
"IP-constrained callers validate the selected addresses independently"
11887+
);
11888+
}
11889+
1182111890
#[test]
1182211891
fn test_parse_hosts_file_for_host_handles_comments_invalid_rows_and_case() {
1182311892
let contents = r#"
@@ -11856,7 +11925,7 @@ network_policies:
1185611925
}
1185711926

1185811927
#[test]
11859-
fn test_resolve_from_hosts_file_contents_public_ip_passes_default_ssrf_check() {
11928+
fn test_resolve_from_hosts_file_contents_public_ip_is_not_internal() {
1186011929
let addrs =
1186111930
resolve_from_hosts_file_contents("93.184.216.34 example.local\n", "example.local", 80);
1186211931
assert!(reject_internal_resolved_addrs("example.local", &addrs).is_ok());
@@ -11883,7 +11952,7 @@ network_policies:
1188311952
}
1188411953

1188511954
#[test]
11886-
fn test_declared_endpoint_private_hosts_file_resolution_allowed() {
11955+
fn test_declared_endpoint_validator_allows_private_address() {
1188711956
let addrs = resolve_from_hosts_file_contents(
1188811957
"192.168.1.105 searxng.local\n",
1188911958
"searxng.local",
@@ -12395,7 +12464,7 @@ network_policies:
1239512464

1239612465
#[tokio::test]
1239712466
async fn test_rejects_localhost_resolution() {
12398-
let result = resolve_and_reject_internal("localhost", 80, 0).await;
12467+
let result = resolve_and_reject_internal("localhost", 80).await;
1239912468
assert!(result.is_err());
1240012469
let err = result.unwrap_err();
1240112470
assert!(
@@ -12406,7 +12475,7 @@ network_policies:
1240612475

1240712476
#[tokio::test]
1240812477
async fn test_rejects_loopback_ip_literal() {
12409-
let result = resolve_and_reject_internal("127.0.0.1", 443, 0).await;
12478+
let result = resolve_and_reject_internal("127.0.0.1", 443).await;
1241012479
assert!(result.is_err());
1241112480
let err = result.unwrap_err();
1241212481
assert!(
@@ -12417,7 +12486,7 @@ network_policies:
1241712486

1241812487
#[tokio::test]
1241912488
async fn test_rejects_metadata_ip() {
12420-
let result = resolve_and_reject_internal("169.254.169.254", 80, 0).await;
12489+
let result = resolve_and_reject_internal("169.254.169.254", 80).await;
1242112490
assert!(result.is_err());
1242212491
let err = result.unwrap_err();
1242312492
assert!(
@@ -12428,7 +12497,7 @@ network_policies:
1242812497

1242912498
#[tokio::test]
1243012499
async fn test_dns_failure_returns_error() {
12431-
let result = resolve_and_reject_internal("this-host-does-not-exist.invalid", 80, 0).await;
12500+
let result = resolve_and_reject_internal("this-host-does-not-exist.invalid", 80).await;
1243212501
assert!(result.is_err());
1243312502
let err = result.unwrap_err();
1243412503
assert!(
@@ -14117,7 +14186,7 @@ network_policies:
1411714186
async fn test_forward_public_ip_allowed_without_allowed_ips() {
1411814187
// Public IPs (e.g. dns.google -> 8.8.8.8) should pass through
1411914188
// resolve_and_reject_internal without needing allowed_ips.
14120-
let result = resolve_and_reject_internal("dns.google", 80, 0).await;
14189+
let result = resolve_and_reject_internal("dns.google", 80).await;
1412114190
assert!(
1412214191
result.is_ok(),
1412314192
"Public IP should be allowed without allowed_ips: {result:?}"
@@ -14137,7 +14206,7 @@ network_policies:
1413714206
#[tokio::test]
1413814207
async fn test_forward_private_ip_rejected_without_allowed_ips() {
1413914208
// Private IP literals should be rejected by resolve_and_reject_internal.
14140-
let result = resolve_and_reject_internal("10.0.0.1", 80, 0).await;
14209+
let result = resolve_and_reject_internal("10.0.0.1", 80).await;
1414114210
assert!(
1414214211
result.is_err(),
1414314212
"Private IP should be rejected without allowed_ips"
@@ -14208,7 +14277,7 @@ network_policies:
1420814277

1420914278
#[tokio::test]
1421014279
async fn test_resolve_reject_internal_fails_closed_on_nul_hostname() {
14211-
let result = resolve_and_reject_internal("evil.com\0.safe.com", 443, 0).await;
14280+
let result = resolve_and_reject_internal("evil.com\0.safe.com", 443).await;
1421214281
assert!(
1421314282
result.is_err(),
1421414283
"NUL-containing hostname should fail DNS resolution (fail closed)"

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -362,20 +362,16 @@ pub(crate) async fn validate_destination(
362362
DestinationDenial::from_check(error, DestinationDenialKind::AllowedIps)
363363
})?
364364
}
365-
AddressAuthorization::ExactDeclaredHost => {
366-
resolve_and_check_declared_endpoint(host, port, sandbox_entrypoint_pid)
367-
.await
368-
.map_err(|error| {
369-
DestinationDenial::from_check(error, DestinationDenialKind::DeclaredEndpoint)
370-
})?
371-
}
372-
AddressAuthorization::DefaultPublicOnly => {
373-
resolve_and_reject_internal(host, port, sandbox_entrypoint_pid)
374-
.await
375-
.map_err(|error| {
376-
DestinationDenial::from_check(error, DestinationDenialKind::InternalAddress)
377-
})?
378-
}
365+
AddressAuthorization::ExactDeclaredHost => resolve_and_check_declared_endpoint(host, port)
366+
.await
367+
.map_err(|error| {
368+
DestinationDenial::from_check(error, DestinationDenialKind::DeclaredEndpoint)
369+
})?,
370+
AddressAuthorization::DefaultPublicOnly => resolve_and_reject_internal(host, port)
371+
.await
372+
.map_err(|error| {
373+
DestinationDenial::from_check(error, DestinationDenialKind::InternalAddress)
374+
})?,
379375
AddressAuthorization::PinnedResolved(addresses) => addresses
380376
.iter()
381377
.copied()

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,8 @@ impl NoProxy {
219219
/// outside that range.
220220
///
221221
/// The automatic loopback bypass is derived from resolved addresses, never
222-
/// from the host string: `resolved` may come from the sandbox's own
223-
/// `/etc/hosts` (see `resolve_socket_addrs`), which the workload controls,
224-
/// so treating the name `localhost` as proof of loopback would let a
225-
/// spoofed mapping dial a policy-allowed public address while escaping the
226-
/// operator proxy. A spoofed `localhost` falls through to the entries below
227-
/// and is proxied unless an explicit `NO_PROXY` entry matches.
222+
/// from the host string. This keeps the bypass valid even for callers whose
223+
/// independently IP-constrained resolution may honor sandbox `/etc/hosts`.
228224
fn direct_addrs(
229225
&self,
230226
host: &str,
@@ -1782,11 +1778,10 @@ mod tests {
17821778
assert!(!bypasses(&cfg, "example.com"));
17831779
}
17841780

1785-
/// A sandbox controls its own `/etc/hosts`, and `resolve_socket_addrs`
1786-
/// consults it before DNS, so `localhost` can resolve to any address the
1787-
/// network policy happens to allow. The name must not authorize a direct
1788-
/// dial that escapes the operator proxy — only genuinely loopback
1789-
/// resolved addresses may.
1781+
/// Only genuinely loopback resolved addresses may authorize the automatic
1782+
/// direct-dial bypass. Callers with independently IP-constrained resolution
1783+
/// can still honor sandbox `/etc/hosts`, so this check must remain based on
1784+
/// the resolved address rather than the hostname.
17901785
#[test]
17911786
fn spoofed_localhost_resolution_does_not_bypass_proxy() {
17921787
let cfg = config_ok(&[(HTTPS_PROXY, "http://proxy:8080")]);

‎docs/security/best-practices.mdx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ After OPA policy allows a connection, the proxy resolves DNS and rejects undecla
133133
| Aspect | Detail |
134134
|---|---|
135135
| Default | The proxy blocks private IPs for undeclared, wildcard, and hostless endpoints, and for endpoints added through the policy advisor. Exact hostnames declared in user policy may resolve to private RFC 1918 addresses. Loopback (`127.0.0.0/8`), link-local (`169.254.0.0/16`), and unspecified (`0.0.0.0`) addresses are always blocked and cannot be overridden with `allowed_ips`. |
136-
| What you can change | Declare a known internal service as an exact `host` endpoint, or add `allowed_ips` (CIDR notation) to an endpoint to permit specific private IP ranges for wildcard or hostless policies. On Linux, the proxy consults the sandbox's `/etc/hosts` before DNS, so Kubernetes `hostAliases` can make LAN-only hostnames resolvable. Policies with `allowed_ips` entries that overlap loopback, link-local, or unspecified addresses fail to load with a clear validation error. |
136+
| What you can change | Declare a known internal service as an exact `host` endpoint, or add `allowed_ips` (CIDR notation) to an endpoint to permit specific private IP ranges for wildcard or hostless policies. The supervisor resolver is authoritative for hostname-only authorization. Linux explicit-proxy paths with access to the workload process namespace may use sandbox `/etc/hosts` entries, including Kubernetes `hostAliases`, for ordinary endpoints only when the endpoint has `allowed_ips`; every resolved address is then checked against the allowlist. Driver-managed host-gateway aliases use a separate trusted IP pin. Policies with `allowed_ips` entries that overlap loopback, link-local, or unspecified addresses fail to load with a clear validation error. |
137137
| Risk if relaxed | Without SSRF protection, a misconfigured policy could allow the agent to reach cloud metadata services (`169.254.169.254`), internal databases, or other infrastructure endpoints through DNS rebinding. |
138-
| Recommendation | Prefer exact hostname endpoints for stable internal services. Use `allowed_ips` when you need hostless or wildcard authorization, and scope the CIDR as narrowly as possible (for example, `10.0.5.20/32` for a single host). Loopback, link-local, and unspecified addresses are always blocked regardless of `allowed_ips`. `hostAliases` change resolution, not authorization: `host: searxng.local` with `/etc/hosts` mapping to `192.168.1.105` is trusted only when that exact hostname is declared by user policy. The policy advisor does not propose rules for always-blocked destinations and still requires a separate `allowed_ips` edit for private-IP endpoints. |
138+
| Recommendation | Prefer exact hostname endpoints for stable internal services. Use `allowed_ips` when you need hostless or wildcard authorization, and scope the CIDR as narrowly as possible (for example, `10.0.5.20/32` for a single host). Loopback, link-local, and unspecified addresses are always blocked regardless of `allowed_ips`. On a deployment that supports sandbox host aliases, declare both `host: searxng.local` and `allowed_ips: [192.168.1.105/32]` before relying on a Kubernetes `hostAliases` mapping; the alias changes resolution while the allowlist supplies the trusted address constraint. The policy advisor does not propose rules for always-blocked destinations and still requires a separate `allowed_ips` edit for private-IP endpoints. |
139139

140140
### Operator Approval
141141

‎skills/generate-sandbox-policy/SKILL.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ Use `allowed_ips` to pin the addresses an endpoint may reach. When it is set, ev
357357
- **Host + allowlist**: `host` + `allowed_ips` — domain must resolve to an IP in the allowlist
358358
- **Hostless allowlist**: `allowed_ips` only (no `host`) — any domain on the port is allowed if it resolves to an IP in the allowlist
359359

360+
The supervisor resolver is authoritative for hostname-only authorization. When a Linux explicit-proxy deployment exposes the workload process namespace to the supervisor, a sandbox `/etc/hosts` entry such as a Kubernetes `hostAliases` mapping is honored for an ordinary endpoint only if it sets `allowed_ips` to the exact IP or a narrow CIDR. OpenShell's driver-managed host-gateway aliases use a separate trusted IP pin.
361+
360362
Loopback (`127.0.0.0/8`), link-local (`169.254.0.0/16`), unspecified, and cloud metadata addresses are **always blocked** as upstream destinations regardless of `allowed_ips`.
361363

362364
The Google Cloud metadata emulator reserves `127.0.0.1:8174` in Linux sandboxes. OpenShell handles SDK discovery locally through the supervisor; do not add an `allowed_ips` exception or grant access to the host cloud metadata service. See the [Google provider documentation](https://docs.nvidia.com/openshell/latest/how-it-works/providers/google.md).

0 commit comments

Comments
 (0)