|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! IPv4-mapped destinations must retain their IPv4 policy and DNS identity. |
| 5 | +
|
| 6 | +use super::*; |
| 7 | + |
| 8 | +fn engine_for(host: &str) -> OpaEngine { |
| 9 | + native_identity_engine( |
| 10 | + include_str!("../../../data/sandbox-policy.rego"), |
| 11 | + &policy_with_native_executable_paths(&POLICY_DNS_OPEN_POLICY.replace("db.example", host)), |
| 12 | + ) |
| 13 | + .unwrap() |
| 14 | +} |
| 15 | + |
| 16 | +async fn probe( |
| 17 | + destination: SocketAddr, |
| 18 | + store: Option<&Arc<ResolvedEndpointStore>>, |
| 19 | + engine: &OpaEngine, |
| 20 | +) -> ( |
| 21 | + Option<TransparentOpen>, |
| 22 | + TcpOpenDecision, |
| 23 | + Option<DenialEvent>, |
| 24 | +) { |
| 25 | + let (open, completion) = staged_curl_open(destination, engine.current_generation()); |
| 26 | + let (denial_tx, mut denial_rx) = mpsc::unbounded_channel(); |
| 27 | + let accepted = preauthorize_transparent_open( |
| 28 | + open, |
| 29 | + store, |
| 30 | + engine, |
| 31 | + &BinaryIdentityCache::new(), |
| 32 | + None, |
| 33 | + None, |
| 34 | + false, |
| 35 | + Some(&denial_tx), |
| 36 | + None, |
| 37 | + ) |
| 38 | + .await; |
| 39 | + let decision = completion.await.unwrap(); |
| 40 | + assert_eq!(accepted.is_some(), decision == TcpOpenDecision::RelayReady); |
| 41 | + ( |
| 42 | + accepted.and_then(|(_, _, _, transparent)| transparent), |
| 43 | + decision, |
| 44 | + denial_rx.try_recv().ok(), |
| 45 | + ) |
| 46 | +} |
| 47 | + |
| 48 | +#[tokio::test] |
| 49 | +async fn allowed_ipv4_endpoint_accepts_mapped_form() { |
| 50 | + let engine = engine_for("203.0.113.7"); |
| 51 | + let store = policy_dns_test_store(); |
| 52 | + for dns in [None, Some(&store)] { |
| 53 | + for destination in ["203.0.113.7:5432", "[::ffff:203.0.113.7]:5432"] { |
| 54 | + let (transparent, decision, denial) = |
| 55 | + probe(destination.parse().unwrap(), dns, &engine).await; |
| 56 | + assert_eq!(decision, TcpOpenDecision::RelayReady, "{destination}"); |
| 57 | + assert_eq!( |
| 58 | + transparent.unwrap().destination, |
| 59 | + "203.0.113.7:5432".parse().unwrap() |
| 60 | + ); |
| 61 | + assert!(denial.is_none()); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +#[tokio::test] |
| 67 | +async fn policy_dns_recovers_hostname_for_mapped_form() { |
| 68 | + let engine = engine_for("db.example"); |
| 69 | + let store = policy_dns_test_store(); |
| 70 | + let record = publish_mapping( |
| 71 | + &store, |
| 72 | + "db.example", |
| 73 | + "database", |
| 74 | + 5432, |
| 75 | + engine.current_generation(), |
| 76 | + ); |
| 77 | + let IpAddr::V4(ipv4) = record.synthetic_address else { |
| 78 | + panic!("test publishes an IPv4 mapping"); |
| 79 | + }; |
| 80 | + for ip in [IpAddr::V4(ipv4), IpAddr::V6(ipv4.to_ipv6_mapped())] { |
| 81 | + let destination = SocketAddr::new(ip, 5432); |
| 82 | + let (transparent, decision, denial) = probe(destination, Some(&store), &engine).await; |
| 83 | + assert_eq!(decision, TcpOpenDecision::RelayReady, "{destination}"); |
| 84 | + assert!(denial.is_none()); |
| 85 | + let transparent = transparent.unwrap(); |
| 86 | + assert_eq!( |
| 87 | + transparent.destination, |
| 88 | + SocketAddr::new(IpAddr::V4(ipv4), 5432) |
| 89 | + ); |
| 90 | + let (decision, connector) = transparent.authorization.unwrap(); |
| 91 | + assert_eq!(decision.intent.destination.host, "db.example"); |
| 92 | + assert_eq!(connector.addrs(), &["203.0.113.8:5432".parse().unwrap()]); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +#[tokio::test] |
| 97 | +async fn denial_reports_canonical_ipv4_host() { |
| 98 | + let engine = engine_for("203.0.113.7"); |
| 99 | + let store = policy_dns_test_store(); |
| 100 | + let mut hosts = Vec::new(); |
| 101 | + for destination in ["203.0.113.8:5432", "[::ffff:203.0.113.8]:5432"] { |
| 102 | + let (_, decision, denial) = |
| 103 | + probe(destination.parse().unwrap(), Some(&store), &engine).await; |
| 104 | + assert_eq!( |
| 105 | + decision, |
| 106 | + TcpOpenDecision::Denied(TcpOpenDenial::PolicyDenied) |
| 107 | + ); |
| 108 | + let denial = denial.expect("unapproved endpoint produces a policy proposal"); |
| 109 | + assert_eq!(denial.denial_stage, "transparent_tcp_connect"); |
| 110 | + hosts.push(denial.host); |
| 111 | + } |
| 112 | + assert_eq!(hosts, ["203.0.113.8", "203.0.113.8"]); |
| 113 | +} |
| 114 | + |
| 115 | +#[tokio::test] |
| 116 | +async fn metadata_stays_blocked_in_both_forms() { |
| 117 | + let store = policy_dns_test_store(); |
| 118 | + for policy_host in ["169.254.169.254", "::ffff:169.254.169.254"] { |
| 119 | + let engine = engine_for(policy_host); |
| 120 | + for destination in ["169.254.169.254:5432", "[::ffff:169.254.169.254]:5432"] { |
| 121 | + let (_, decision, denial) = |
| 122 | + probe(destination.parse().unwrap(), Some(&store), &engine).await; |
| 123 | + assert!(matches!(decision, TcpOpenDecision::Denied(_))); |
| 124 | + assert!( |
| 125 | + denial.is_none(), |
| 126 | + "blocked metadata must not become a proposal" |
| 127 | + ); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +#[tokio::test] |
| 133 | +async fn native_ipv6_preserves_scope_and_flowinfo() { |
| 134 | + let engine = engine_for("2001:db8::7"); |
| 135 | + let destination = SocketAddr::V6(std::net::SocketAddrV6::new( |
| 136 | + "2001:db8::7".parse().unwrap(), |
| 137 | + 5432, |
| 138 | + 42, |
| 139 | + 7, |
| 140 | + )); |
| 141 | + let (transparent, decision, denial) = probe(destination, None, &engine).await; |
| 142 | + assert_eq!(decision, TcpOpenDecision::RelayReady); |
| 143 | + assert_eq!(transparent.unwrap().destination, destination); |
| 144 | + assert!(denial.is_none()); |
| 145 | +} |
0 commit comments