Skip to content

Commit 5afc55d

Browse files
committed
fix(net): add is_link_local_ip() helper and harden trusted-gateway SSRF
Extract a public is_link_local_ip(IpAddr) helper in openshell-core::net covering IPv4 169.254.0.0/16, IPv6 fe80::/10, and IPv4-mapped variants. Refactor the inline IPv6 fe80::/10 bitmask in is_always_blocked_ip() and is_internal_ip() to use the new helper, eliminating two copies of the same pattern. Harden the trusted-gateway SSRF exemption in proxy.rs: both detect_trusted_host_gateway() and resolve_and_check_trusted_gateway() now reject always-blocked non-link-local IPs (loopback, unspecified) with a warning. The exemption was intended only for link-local addresses used by rootless Podman with pasta; a /etc/hosts entry mapping host.openshell.internal to 127.0.0.1 or 0.0.0.0 previously bypassed the loopback/unspecified SSRF invariant. Add 169.254. prefix to the generate_security_notes() host checks in mechanistic_mapper and policy server, closing a gap where link-local literal hosts were not flagged as internal/private in advisory notes.
1 parent 8c37c08 commit 5afc55d

4 files changed

Lines changed: 213 additions & 7 deletions

File tree

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

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,31 @@
1212
1313
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
1414

15+
/// Check if an IP address is link-local.
16+
///
17+
/// Covers IPv4 `169.254.0.0/16`, IPv6 `fe80::/10`, and IPv4-mapped IPv6
18+
/// addresses whose embedded IPv4 address is link-local (`::ffff:169.254.x.x`).
19+
///
20+
/// This is a point-check helper used to build the always-blocked and
21+
/// trusted-gateway exemption predicates. For CIDR-range overlap checks see
22+
/// [`is_always_blocked_net`].
23+
pub fn is_link_local_ip(ip: IpAddr) -> bool {
24+
match ip {
25+
IpAddr::V4(v4) => v4.is_link_local(),
26+
IpAddr::V6(v6) => {
27+
// fe80::/10 — IPv6 link-local
28+
if (v6.segments()[0] & 0xffc0) == 0xfe80 {
29+
return true;
30+
}
31+
// ::ffff:169.254.x.x — IPv4-mapped link-local
32+
if let Some(v4) = v6.to_ipv4_mapped() {
33+
return v4.is_link_local();
34+
}
35+
false
36+
}
37+
}
38+
}
39+
1540
/// Check if an IP address is always blocked regardless of policy.
1641
///
1742
/// Loopback, link-local, and unspecified addresses are never allowed even when
@@ -24,13 +49,12 @@ pub fn is_always_blocked_ip(ip: IpAddr) -> bool {
2449
if v6.is_loopback() || v6.is_unspecified() {
2550
return true;
2651
}
27-
// fe80::/10 — IPv6 link-local
28-
if (v6.segments()[0] & 0xffc0) == 0xfe80 {
52+
if is_link_local_ip(IpAddr::V6(v6)) {
2953
return true;
3054
}
3155
// Check IPv4-mapped IPv6 (::ffff:x.x.x.x)
3256
if let Some(v4) = v6.to_ipv4_mapped() {
33-
return v4.is_loopback() || v4.is_link_local() || v4.is_unspecified();
57+
return v4.is_loopback() || v4.is_unspecified();
3458
}
3559
false
3660
}
@@ -138,8 +162,7 @@ pub fn is_internal_ip(ip: IpAddr) -> bool {
138162
if v6.is_loopback() || v6.is_unspecified() {
139163
return true;
140164
}
141-
// fe80::/10 — IPv6 link-local
142-
if (v6.segments()[0] & 0xffc0) == 0xfe80 {
165+
if is_link_local_ip(IpAddr::V6(v6)) {
143166
return true;
144167
}
145168
// fc00::/7 — IPv6 unique local addresses (ULA)
@@ -190,6 +213,69 @@ fn is_internal_v4(v4: Ipv4Addr) -> bool {
190213
mod tests {
191214
use super::*;
192215

216+
// -- is_link_local_ip --
217+
218+
#[test]
219+
fn test_link_local_ip_v4() {
220+
assert!(is_link_local_ip(IpAddr::V4(Ipv4Addr::new(169, 254, 0, 1))));
221+
assert!(is_link_local_ip(IpAddr::V4(Ipv4Addr::new(
222+
169, 254, 169, 254
223+
))));
224+
assert!(is_link_local_ip(IpAddr::V4(Ipv4Addr::new(
225+
169, 254, 255, 255
226+
))));
227+
}
228+
229+
#[test]
230+
fn test_link_local_ip_v6_fe80() {
231+
assert!(is_link_local_ip(IpAddr::V6(Ipv6Addr::new(
232+
0xfe80, 0, 0, 0, 0, 0, 0, 1
233+
))));
234+
// Upper boundary of fe80::/10 (febf:...)
235+
assert!(is_link_local_ip(IpAddr::V6(Ipv6Addr::new(
236+
0xfebf, 0, 0, 0, 0, 0, 0, 1
237+
))));
238+
}
239+
240+
#[test]
241+
fn test_link_local_ip_v6_mapped_v4() {
242+
let mapped = Ipv4Addr::new(169, 254, 1, 2).to_ipv6_mapped();
243+
assert!(is_link_local_ip(IpAddr::V6(mapped)));
244+
}
245+
246+
#[test]
247+
fn test_link_local_ip_not_loopback() {
248+
assert!(!is_link_local_ip(IpAddr::V4(Ipv4Addr::LOCALHOST)));
249+
assert!(!is_link_local_ip(IpAddr::V6(Ipv6Addr::LOCALHOST)));
250+
}
251+
252+
#[test]
253+
fn test_link_local_ip_not_unspecified() {
254+
assert!(!is_link_local_ip(IpAddr::V4(Ipv4Addr::UNSPECIFIED)));
255+
assert!(!is_link_local_ip(IpAddr::V6(Ipv6Addr::UNSPECIFIED)));
256+
}
257+
258+
#[test]
259+
fn test_link_local_ip_not_rfc1918() {
260+
assert!(!is_link_local_ip(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1))));
261+
assert!(!is_link_local_ip(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1))));
262+
assert!(!is_link_local_ip(IpAddr::V4(Ipv4Addr::new(172, 16, 0, 1))));
263+
}
264+
265+
#[test]
266+
fn test_link_local_ip_not_public() {
267+
assert!(!is_link_local_ip(IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8))));
268+
assert!(!is_link_local_ip(IpAddr::V6(Ipv6Addr::new(
269+
0x2001, 0x4860, 0x4860, 0, 0, 0, 0, 0x8888
270+
))));
271+
}
272+
273+
#[test]
274+
fn test_link_local_ip_not_v6_mapped_loopback() {
275+
let mapped = Ipv4Addr::LOCALHOST.to_ipv6_mapped();
276+
assert!(!is_link_local_ip(IpAddr::V6(mapped)));
277+
}
278+
193279
// -- is_always_blocked_ip --
194280

195281
#[test]

‎crates/openshell-sandbox/src/mechanistic_mapper.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,13 @@ fn generate_security_notes(host: &str, port: u16, is_ssrf: bool) -> String {
295295
);
296296
}
297297

298-
// Check for private IP patterns in the host.
298+
// Check for private/reserved IP patterns in the host.
299299
if host.starts_with("10.")
300300
|| host.starts_with("172.")
301301
|| host.starts_with("192.168.")
302302
|| host == "localhost"
303303
|| host.starts_with("127.")
304+
|| host.starts_with("169.254.")
304305
{
305306
notes.push(format!(
306307
"Destination '{host}' appears to be an internal/private address."

‎crates/openshell-sandbox/src/proxy.rs‎

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::policy_local::{POLICY_LOCAL_HOST, PolicyLocalContext};
1212
use crate::provider_credentials::ProviderCredentialState;
1313
use crate::secrets::{SecretResolver, rewrite_header_line};
1414
use miette::{IntoDiagnostic, Result};
15-
use openshell_core::net::{is_always_blocked_ip, is_internal_ip};
15+
use openshell_core::net::{is_always_blocked_ip, is_internal_ip, is_link_local_ip};
1616
use openshell_ocsf::{
1717
ActionId, ActivityId, DispositionId, Endpoint, HttpActivityBuilder, HttpRequest,
1818
NetworkActivityBuilder, Process, SeverityId, StatusId, Url as OcsfUrl, ocsf_emit,
@@ -1940,6 +1940,18 @@ fn detect_trusted_host_gateway() -> Option<IpAddr> {
19401940
);
19411941
return None;
19421942
}
1943+
// The exemption exists solely for link-local IPs used by rootless Podman
1944+
// with pasta. Loopback (127.x), unspecified (0.0.0.0), and other
1945+
// always-blocked non-link-local addresses are never legitimate host
1946+
// gateway IPs and must not receive the exemption.
1947+
if is_always_blocked_ip(ip) && !is_link_local_ip(ip) {
1948+
warn!(
1949+
%ip,
1950+
"host.openshell.internal maps to an always-blocked non-link-local IP; \
1951+
trusted-gateway SSRF exemption disabled"
1952+
);
1953+
return None;
1954+
}
19431955
Some(ip)
19441956
}
19451957

@@ -1992,6 +2004,18 @@ async fn resolve_and_check_trusted_gateway(
19922004
addr.ip()
19932005
));
19942006
}
2007+
// Defense-in-depth: even if the resolved IP matches trusted_gw, reject
2008+
// always-blocked non-link-local addresses (loopback, unspecified). This
2009+
// catches the case where detect_trusted_host_gateway somehow admitted a
2010+
// bad IP, or where trusted_gw was set to a loopback/unspecified address
2011+
// through a code path we haven't anticipated.
2012+
if is_always_blocked_ip(addr.ip()) && !is_link_local_ip(addr.ip()) {
2013+
return Err(format!(
2014+
"{host} resolves to always-blocked non-link-local address {}, \
2015+
connection rejected",
2016+
addr.ip()
2017+
));
2018+
}
19952019
}
19962020
Ok(addrs)
19972021
}
@@ -3898,6 +3922,48 @@ mod tests {
38983922
assert!(ips.is_empty());
38993923
}
39003924

3925+
#[test]
3926+
fn test_detect_trusted_host_gateway_rejects_loopback() {
3927+
// Loopback is always-blocked and not link-local — must not be trusted.
3928+
let ip = IpAddr::V4(Ipv4Addr::LOCALHOST);
3929+
assert!(!is_cloud_metadata_ip(ip));
3930+
assert!(is_always_blocked_ip(ip));
3931+
assert!(!is_link_local_ip(ip));
3932+
// The guard: always-blocked && !link-local → reject.
3933+
assert!(is_always_blocked_ip(ip) && !is_link_local_ip(ip));
3934+
}
3935+
3936+
#[test]
3937+
fn test_detect_trusted_host_gateway_rejects_unspecified() {
3938+
// Unspecified (0.0.0.0) must not be trusted.
3939+
let ip = IpAddr::V4(Ipv4Addr::UNSPECIFIED);
3940+
assert!(!is_cloud_metadata_ip(ip));
3941+
assert!(is_always_blocked_ip(ip));
3942+
assert!(!is_link_local_ip(ip));
3943+
assert!(is_always_blocked_ip(ip) && !is_link_local_ip(ip));
3944+
}
3945+
3946+
#[test]
3947+
fn test_detect_trusted_host_gateway_rejects_loopback_v6() {
3948+
let ip = IpAddr::V6(Ipv6Addr::LOCALHOST);
3949+
assert!(!is_cloud_metadata_ip(ip));
3950+
assert!(is_always_blocked_ip(ip));
3951+
assert!(!is_link_local_ip(ip));
3952+
assert!(is_always_blocked_ip(ip) && !is_link_local_ip(ip));
3953+
}
3954+
3955+
#[test]
3956+
fn test_detect_trusted_host_gateway_allows_link_local_non_metadata() {
3957+
// 169.254.1.2 is always-blocked (link-local) but IS link-local,
3958+
// so the guard should pass and allow the exemption.
3959+
let ip = IpAddr::V4(Ipv4Addr::new(169, 254, 1, 2));
3960+
assert!(!is_cloud_metadata_ip(ip));
3961+
assert!(is_always_blocked_ip(ip));
3962+
assert!(is_link_local_ip(ip));
3963+
// The guard does NOT fire — this IP is eligible for the exemption.
3964+
assert!(!(is_always_blocked_ip(ip) && !is_link_local_ip(ip)));
3965+
}
3966+
39013967
// -- resolve_and_check_trusted_gateway --
39023968

39033969
#[tokio::test]
@@ -4008,6 +4074,58 @@ mod tests {
40084074
}
40094075
}
40104076

4077+
#[test]
4078+
fn test_trusted_gateway_rejects_loopback_as_trusted_gw() {
4079+
// Defense-in-depth: even if detect_trusted_host_gateway somehow admitted
4080+
// a loopback IP, the always-blocked non-link-local guard must fire.
4081+
// We validate the guard logic inline (mirroring resolve_and_check internals)
4082+
// since DNS resolution of "localhost" is environment-dependent.
4083+
let loopback = IpAddr::V4(Ipv4Addr::LOCALHOST);
4084+
assert!(!is_cloud_metadata_ip(loopback));
4085+
assert!(is_always_blocked_ip(loopback));
4086+
assert!(!is_link_local_ip(loopback));
4087+
// The guard fires: always-blocked && !link-local → reject.
4088+
let err: Result<(), String> =
4089+
if is_always_blocked_ip(loopback) && !is_link_local_ip(loopback) {
4090+
Err(format!(
4091+
"host resolves to always-blocked non-link-local address {loopback}, \
4092+
connection rejected"
4093+
))
4094+
} else {
4095+
Ok(())
4096+
};
4097+
assert!(err.is_err(), "loopback must be rejected");
4098+
assert!(
4099+
err.unwrap_err().contains("always-blocked non-link-local"),
4100+
"expected always-blocked rejection"
4101+
);
4102+
}
4103+
4104+
#[test]
4105+
fn test_trusted_gateway_rejects_unspecified_as_trusted_gw() {
4106+
// Defense-in-depth: 0.0.0.0 as trusted_gw must be rejected even if
4107+
// the mismatch check passes.
4108+
let unspecified = IpAddr::V4(Ipv4Addr::UNSPECIFIED);
4109+
assert!(!is_cloud_metadata_ip(unspecified));
4110+
assert!(is_always_blocked_ip(unspecified));
4111+
assert!(!is_link_local_ip(unspecified));
4112+
// The guard fires: always-blocked && !link-local → reject.
4113+
let err: Result<(), String> =
4114+
if is_always_blocked_ip(unspecified) && !is_link_local_ip(unspecified) {
4115+
Err(format!(
4116+
"host resolves to always-blocked non-link-local address {unspecified}, \
4117+
connection rejected"
4118+
))
4119+
} else {
4120+
Ok(())
4121+
};
4122+
assert!(err.is_err(), "unspecified must be rejected");
4123+
assert!(
4124+
err.unwrap_err().contains("always-blocked non-link-local"),
4125+
"expected always-blocked rejection"
4126+
);
4127+
}
4128+
40114129
#[tokio::test]
40124130
async fn test_rejects_localhost_resolution() {
40134131
let result = resolve_and_reject_internal("localhost", 80, 0).await;

‎crates/openshell-server/src/grpc/policy.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,6 +2141,7 @@ fn generate_security_notes(host: &str, port: u16) -> String {
21412141
|| host.starts_with("192.168.")
21422142
|| host == "localhost"
21432143
|| host.starts_with("127.")
2144+
|| host.starts_with("169.254.")
21442145
{
21452146
notes.push(format!(
21462147
"Destination '{host}' appears to be an internal/private address."

0 commit comments

Comments
 (0)