Skip to content

Commit c1d3b43

Browse files
fix(policy): classify advisory private-IP notes with the canonical is_internal_ip (#1777) (#1824)
generate_security_notes() flagged a destination as internal/private using naive string prefixes. host.starts_with("172.") matched all of 172.0.0.0/8 even though only 172.16.0.0/12 is private (RFC 1918), so the advisory flagged public addresses (172.15/172.32) and missed CGNAT (100.64.0.0/10), IPv6 ULA, and the other reserved ranges; it also flagged non-IP hostnames that merely start with 10./172.. The two copies of the check (openshell-sandbox mechanistic_mapper.rs and openshell-server grpc/policy.rs) now parse the host as an IP and defer to openshell-core net::is_internal_ip, the same RFC-accurate, unit-tested classifier the runtime SSRF enforcement path uses, so the proposed-rule advisory matches actual enforcement. Per review, is_internal_v4 now uses the stable Ipv4Addr predicates as far as they go: is_documentation() (RFC 5737) replaces the two manual TEST-NET-2/3 checks and also closes a gap, since 192.0.2.0/24 (TEST-NET-1) was previously unflagged, and is_broadcast() (RFC 919) is added. Manual checks remain only for the ranges without a stable predicate: CGNAT 100.64.0.0/10, IETF protocol assignments 192.0.0.0/24, and benchmarking 198.18.0.0/15. Adds regression coverage: both note suites now assert hostname-prefix false positives (10.example.com, 172.example.com) are not flagged and IPv6 ULA (fd00::1) is flagged; net.rs covers the newly classified TEST-NET-1 and broadcast addresses. Closes #1777 Signed-off-by: latenighthackathon <latenighthackathon@users.noreply.github.com> Co-authored-by: latenighthackathon <latenighthackathon@users.noreply.github.com>
1 parent 84c24a0 commit c1d3b43

3 files changed

Lines changed: 83 additions & 29 deletions

File tree

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,31 +191,33 @@ pub fn is_internal_ip(ip: IpAddr) -> bool {
191191
/// IPv4 internal address check covering RFC 1918, CGNAT (RFC 6598), and other
192192
/// special-use ranges that should never be reachable from sandbox egress.
193193
fn is_internal_v4(v4: Ipv4Addr) -> bool {
194-
if v4.is_loopback() || v4.is_private() || v4.is_link_local() || v4.is_unspecified() {
194+
// Prefer the stable `Ipv4Addr` predicates. `is_documentation()` (RFC 5737)
195+
// covers all three TEST-NET ranges, including 192.0.2.0/24 (TEST-NET-1) which
196+
// the previous manual checks missed.
197+
if v4.is_loopback()
198+
|| v4.is_private()
199+
|| v4.is_link_local()
200+
|| v4.is_unspecified()
201+
|| v4.is_documentation()
202+
|| v4.is_broadcast()
203+
{
195204
return true;
196205
}
197206
let octets = v4.octets();
198-
// 100.64.0.0/10 — CGNAT / shared address space (RFC 6598). Commonly used by
199-
// cloud VPC peering, Tailscale, and similar overlay networks.
207+
// The ranges below have no stable std predicate yet, so keep them manual.
208+
// 100.64.0.0/10 — CGNAT / shared address space (RFC 6598; `is_shared` is
209+
// unstable). Commonly used by cloud VPC peering, Tailscale, and similar overlays.
200210
if octets[0] == 100 && (octets[1] & 0xC0) == 64 {
201211
return true;
202212
}
203-
// 192.0.0.0/24 — IETF protocol assignments (RFC 6890)
213+
// 192.0.0.0/24 — IETF protocol assignments (RFC 6890; no stable predicate).
204214
if octets[0] == 192 && octets[1] == 0 && octets[2] == 0 {
205215
return true;
206216
}
207-
// 198.18.0.0/15 — benchmarking (RFC 2544)
217+
// 198.18.0.0/15 — benchmarking (RFC 2544; `is_benchmarking` is unstable).
208218
if octets[0] == 198 && (octets[1] & 0xFE) == 18 {
209219
return true;
210220
}
211-
// 198.51.100.0/24 — TEST-NET-2 (RFC 5737)
212-
if octets[0] == 198 && octets[1] == 51 && octets[2] == 100 {
213-
return true;
214-
}
215-
// 203.0.113.0/24 — TEST-NET-3 (RFC 5737)
216-
if octets[0] == 203 && octets[1] == 0 && octets[2] == 113 {
217-
return true;
218-
}
219221
false
220222
}
221223

@@ -567,10 +569,14 @@ mod tests {
567569
// 198.18.0.0/15 — benchmarking
568570
assert!(is_internal_ip(IpAddr::V4(Ipv4Addr::new(198, 18, 0, 1))));
569571
assert!(is_internal_ip(IpAddr::V4(Ipv4Addr::new(198, 19, 255, 255))));
572+
// 192.0.2.0/24 — TEST-NET-1 (RFC 5737), now covered via is_documentation()
573+
assert!(is_internal_ip(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1))));
570574
// 198.51.100.0/24 — TEST-NET-2
571575
assert!(is_internal_ip(IpAddr::V4(Ipv4Addr::new(198, 51, 100, 1))));
572576
// 203.0.113.0/24 — TEST-NET-3
573577
assert!(is_internal_ip(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 1))));
578+
// 255.255.255.255 — limited broadcast (RFC 919), via is_broadcast()
579+
assert!(is_internal_ip(IpAddr::V4(Ipv4Addr::BROADCAST)));
574580
}
575581

576582
#[test]

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

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//! The LLM-powered `PolicyAdvisor` (issue #205) wraps and enriches these
1313
//! mechanistic proposals with context-aware rationale and smarter grouping.
1414
15-
use openshell_core::net::{is_always_blocked_ip, is_known_metadata_hostname};
15+
use openshell_core::net::{is_always_blocked_ip, is_internal_ip, is_known_metadata_hostname};
1616
use openshell_core::proto::{
1717
DenialSummary, L7Allow, L7Rule, NetworkBinary, NetworkEndpoint, NetworkPolicyRule, PolicyChunk,
1818
};
@@ -293,14 +293,15 @@ fn generate_security_notes(host: &str, port: u16, is_ssrf: bool) -> String {
293293
);
294294
}
295295

296-
// Check for private/reserved IP patterns in the host.
297-
if host.starts_with("10.")
298-
|| host.starts_with("172.")
299-
|| host.starts_with("192.168.")
300-
|| host == "localhost"
301-
|| host.starts_with("127.")
302-
|| host.starts_with("169.254.")
303-
{
296+
// Flag destinations that are an internal/private address. Parse the host as
297+
// an IP literal and defer to the canonical RFC-accurate classifier
298+
// (openshell-core net::is_internal_ip) rather than naive string prefixes:
299+
// `starts_with("172.")` wrongly matched 172.0-15 / 172.32-255 (RFC 1918 is
300+
// only 172.16.0.0/12) and missed CGNAT (100.64.0.0/10), IPv6 ULA, etc. The
301+
// "localhost" hostname is not an IP literal, so it is checked separately.
302+
// See #1777.
303+
let resolves_internal = host.parse::<IpAddr>().is_ok_and(is_internal_ip);
304+
if resolves_internal || host == "localhost" {
304305
notes.push(format!(
305306
"Destination '{host}' appears to be an internal/private address."
306307
));
@@ -470,6 +471,29 @@ mod tests {
470471
assert!(notes.contains("SSRF"));
471472
}
472473

474+
#[test]
475+
fn test_security_notes_internal_ip_uses_canonical_classifier() {
476+
// RFC 1918 is 172.16.0.0/12 only: the old starts_with("172.") prefix
477+
// wrongly flagged 172.15/172.32 and missed CGNAT (100.64.0.0/10). #1777.
478+
assert!(generate_security_notes("172.16.0.1", 80, false).contains("internal/private"));
479+
assert!(!generate_security_notes("172.15.0.1", 80, false).contains("internal/private"));
480+
assert!(!generate_security_notes("172.32.0.1", 80, false).contains("internal/private"));
481+
assert!(generate_security_notes("100.64.0.1", 80, false).contains("internal/private"));
482+
assert!(generate_security_notes("10.0.0.1", 80, false).contains("internal/private"));
483+
assert!(generate_security_notes("192.168.1.1", 80, false).contains("internal/private"));
484+
assert!(generate_security_notes("127.0.0.1", 80, false).contains("internal/private"));
485+
assert!(generate_security_notes("localhost", 80, false).contains("internal/private"));
486+
assert!(!generate_security_notes("8.8.8.8", 80, false).contains("internal/private"));
487+
// Hostnames that merely start with a private-range prefix must NOT be
488+
// flagged: classification parses an IP literal, not a string prefix. #1824.
489+
assert!(!generate_security_notes("10.example.com", 80, false).contains("internal/private"));
490+
assert!(
491+
!generate_security_notes("172.example.com", 80, false).contains("internal/private")
492+
);
493+
// IPv6 ULA (fc00::/7, RFC 4193) is internal/private.
494+
assert!(generate_security_notes("fd00::1", 80, false).contains("internal/private"));
495+
}
496+
473497
#[test]
474498
fn test_generate_proposals_empty() {
475499
let proposals = generate_proposals(&[]);

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

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::ServerState;
1414
use crate::auth::principal::Principal;
1515
use crate::persistence::{DraftChunkRecord, ObjectId, ObjectName, ObjectType, PolicyRecord, Store};
1616
use crate::policy_store::PolicyStoreExt;
17+
use openshell_core::net::is_internal_ip;
1718
use openshell_core::proto::policy_merge_operation;
1819
use openshell_core::proto::setting_value;
1920
use openshell_core::proto::{
@@ -3171,13 +3172,15 @@ fn policy_record_to_revision(record: &PolicyRecord, include_policy: bool) -> San
31713172
fn generate_security_notes(host: &str, port: u16) -> String {
31723173
let mut notes = Vec::new();
31733174

3174-
if host.starts_with("10.")
3175-
|| host.starts_with("172.")
3176-
|| host.starts_with("192.168.")
3177-
|| host == "localhost"
3178-
|| host.starts_with("127.")
3179-
|| host.starts_with("169.254.")
3180-
{
3175+
// Flag destinations that are an internal/private address. Parse the host as
3176+
// an IP literal and defer to the canonical RFC-accurate classifier
3177+
// (openshell-core net::is_internal_ip) rather than naive string prefixes:
3178+
// `starts_with("172.")` wrongly matched 172.0-15 / 172.32-255 (RFC 1918 is
3179+
// only 172.16.0.0/12) and missed CGNAT (100.64.0.0/10), IPv6 ULA, etc. The
3180+
// "localhost" hostname is not an IP literal, so it is checked separately.
3181+
// See #1777.
3182+
let resolves_internal = host.parse::<IpAddr>().is_ok_and(is_internal_ip);
3183+
if resolves_internal || host == "localhost" {
31813184
notes.push(format!(
31823185
"Destination '{host}' appears to be an internal/private address."
31833186
));
@@ -3893,6 +3896,27 @@ mod tests {
38933896
request
38943897
}
38953898

3899+
#[test]
3900+
fn security_notes_use_canonical_internal_ip_classifier() {
3901+
// RFC 1918 is 172.16.0.0/12 only: the old starts_with("172.") prefix
3902+
// wrongly flagged 172.15/172.32 and missed CGNAT (100.64.0.0/10). #1777.
3903+
assert!(generate_security_notes("172.16.0.1", 80).contains("internal/private"));
3904+
assert!(!generate_security_notes("172.15.0.1", 80).contains("internal/private"));
3905+
assert!(!generate_security_notes("172.32.0.1", 80).contains("internal/private"));
3906+
assert!(generate_security_notes("100.64.0.1", 80).contains("internal/private"));
3907+
assert!(generate_security_notes("10.0.0.1", 80).contains("internal/private"));
3908+
assert!(generate_security_notes("192.168.1.1", 80).contains("internal/private"));
3909+
assert!(generate_security_notes("127.0.0.1", 80).contains("internal/private"));
3910+
assert!(generate_security_notes("localhost", 80).contains("internal/private"));
3911+
assert!(!generate_security_notes("8.8.8.8", 80).contains("internal/private"));
3912+
// Hostnames that merely start with a private-range prefix must NOT be
3913+
// flagged: classification parses an IP literal, not a string prefix. #1824.
3914+
assert!(!generate_security_notes("10.example.com", 80).contains("internal/private"));
3915+
assert!(!generate_security_notes("172.example.com", 80).contains("internal/private"));
3916+
// IPv6 ULA (fc00::/7, RFC 4193) is internal/private.
3917+
assert!(generate_security_notes("fd00::1", 80).contains("internal/private"));
3918+
}
3919+
38963920
#[test]
38973921
fn sandbox_caller_update_validation_allows_sandbox_policy_sync() {
38983922
let req = UpdateConfigRequest {

0 commit comments

Comments
 (0)