@@ -1929,9 +1929,26 @@ fn is_cloud_metadata_ip(ip: IpAddr) -> bool {
19291929#[ cfg( any( target_os = "linux" , test) ) ]
19301930fn detect_trusted_host_gateway ( ) -> Option < IpAddr > {
19311931 let contents = std:: fs:: read_to_string ( "/etc/hosts" ) . ok ( ) ?;
1932- let ip = parse_hosts_file_for_host ( & contents, "host.openshell.internal" )
1933- . into_iter ( )
1934- . next ( ) ?;
1932+ let ips = parse_hosts_file_for_host ( & contents, "host.openshell.internal" ) ;
1933+
1934+ // Multiple distinct IPs for the alias is unexpected — compute drivers
1935+ // always inject exactly one. Warn loudly so operators can diagnose the
1936+ // inconsistency; we still proceed with the first entry rather than
1937+ // disabling the exemption entirely, because the mismatch guard in
1938+ // resolve_and_check_trusted_gateway() will reject any runtime resolution
1939+ // that returns a different IP.
1940+ if ips. len ( ) > 1 {
1941+ warn ! (
1942+ ips = ?ips,
1943+ "host.openshell.internal has {} distinct IPs in /etc/hosts; \
1944+ expected exactly one. Using first entry. \
1945+ Connections resolving to any other IP will be rejected.",
1946+ ips. len( )
1947+ ) ;
1948+ }
1949+
1950+ let ip = ips. into_iter ( ) . next ( ) ?;
1951+
19351952 if is_cloud_metadata_ip ( ip) {
19361953 warn ! (
19371954 %ip,
@@ -3964,6 +3981,93 @@ mod tests {
39643981 assert ! ( !( is_always_blocked_ip( ip) && !is_link_local_ip( ip) ) ) ;
39653982 }
39663983
3984+ // -- parse_hosts_file_for_host: multi-entry / duplicate scenarios --
3985+
3986+ #[ test]
3987+ fn test_parse_hosts_file_single_entry ( ) {
3988+ // Normal driver-injected case: exactly one IP for the alias.
3989+ let contents = "169.254.1.2\t host.openshell.internal host.containers.internal\n " ;
3990+ let ips = parse_hosts_file_for_host ( contents, "host.openshell.internal" ) ;
3991+ assert_eq ! ( ips, vec![ IpAddr :: V4 ( Ipv4Addr :: new( 169 , 254 , 1 , 2 ) ) ] ) ;
3992+ }
3993+
3994+ #[ test]
3995+ fn test_parse_hosts_file_duplicate_same_ip_deduplicated ( ) {
3996+ // Same IP on two separate lines for the same alias — deduplicated to one.
3997+ let contents = "169.254.1.2\t host.openshell.internal\n \
3998+ 169.254.1.2\t host.openshell.internal\n ";
3999+ let ips = parse_hosts_file_for_host ( contents, "host.openshell.internal" ) ;
4000+ assert_eq ! (
4001+ ips,
4002+ vec![ IpAddr :: V4 ( Ipv4Addr :: new( 169 , 254 , 1 , 2 ) ) ] ,
4003+ "identical IPs across lines must be deduplicated"
4004+ ) ;
4005+ }
4006+
4007+ #[ test]
4008+ fn test_parse_hosts_file_multiple_distinct_ips ( ) {
4009+ // Two distinct IPs for the same alias — both returned, first entry wins
4010+ // in detect_trusted_host_gateway(), second would cause mismatch rejection
4011+ // in resolve_and_check_trusted_gateway().
4012+ let contents = "169.254.1.2\t host.openshell.internal\n \
4013+ 169.254.1.3\t host.openshell.internal\n ";
4014+ let ips = parse_hosts_file_for_host ( contents, "host.openshell.internal" ) ;
4015+ assert_eq ! ( ips. len( ) , 2 , "two distinct IPs must both be returned" ) ;
4016+ assert_eq ! ( ips[ 0 ] , IpAddr :: V4 ( Ipv4Addr :: new( 169 , 254 , 1 , 2 ) ) ) ;
4017+ assert_eq ! ( ips[ 1 ] , IpAddr :: V4 ( Ipv4Addr :: new( 169 , 254 , 1 , 3 ) ) ) ;
4018+ }
4019+
4020+ #[ test]
4021+ fn test_parse_hosts_file_first_entry_wins_on_ambiguity ( ) {
4022+ // detect_trusted_host_gateway() pins to the first entry via .next().
4023+ // Verify the ordering guarantee: first line wins.
4024+ let contents = "169.254.1.3\t host.openshell.internal\n \
4025+ 169.254.1.2\t host.openshell.internal\n ";
4026+ let ips = parse_hosts_file_for_host ( contents, "host.openshell.internal" ) ;
4027+ assert_eq ! (
4028+ ips[ 0 ] ,
4029+ IpAddr :: V4 ( Ipv4Addr :: new( 169 , 254 , 1 , 3 ) ) ,
4030+ "first line must be first in the returned vec"
4031+ ) ;
4032+ }
4033+
4034+ #[ test]
4035+ fn test_parse_hosts_file_ignores_other_aliases_on_same_line ( ) {
4036+ // An entry with multiple aliases — only the matching alias counts.
4037+ let contents =
4038+ "169.254.1.2\t host.containers.internal host.openshell.internal host.docker.internal\n " ;
4039+ let ips = parse_hosts_file_for_host ( contents, "host.openshell.internal" ) ;
4040+ assert_eq ! ( ips, vec![ IpAddr :: V4 ( Ipv4Addr :: new( 169 , 254 , 1 , 2 ) ) ] ) ;
4041+ // Non-matching aliases on the same line do not produce extra entries.
4042+ let ips2 = parse_hosts_file_for_host ( contents, "host.docker.internal" ) ;
4043+ assert_eq ! ( ips2, vec![ IpAddr :: V4 ( Ipv4Addr :: new( 169 , 254 , 1 , 2 ) ) ] ) ;
4044+ }
4045+
4046+ #[ test]
4047+ fn test_parse_hosts_file_alias_not_present ( ) {
4048+ let contents = "127.0.0.1\t localhost\n \
4049+ ::1\t \t localhost\n ";
4050+ let ips = parse_hosts_file_for_host ( contents, "host.openshell.internal" ) ;
4051+ assert ! ( ips. is_empty( ) ) ;
4052+ }
4053+
4054+ #[ test]
4055+ fn test_parse_hosts_file_comment_lines_skipped ( ) {
4056+ let contents = "# 169.254.1.2 host.openshell.internal\n \
4057+ 169.254.1.2\t host.openshell.internal\n ";
4058+ let ips = parse_hosts_file_for_host ( contents, "host.openshell.internal" ) ;
4059+ // Commented-out line must not produce an entry.
4060+ assert_eq ! ( ips, vec![ IpAddr :: V4 ( Ipv4Addr :: new( 169 , 254 , 1 , 2 ) ) ] ) ;
4061+ }
4062+
4063+ #[ test]
4064+ fn test_parse_hosts_file_inline_comment_stripped ( ) {
4065+ // Anything after '#' on a data line is treated as a comment.
4066+ let contents = "169.254.1.2\t host.openshell.internal # injected by driver\n " ;
4067+ let ips = parse_hosts_file_for_host ( contents, "host.openshell.internal" ) ;
4068+ assert_eq ! ( ips, vec![ IpAddr :: V4 ( Ipv4Addr :: new( 169 , 254 , 1 , 2 ) ) ] ) ;
4069+ }
4070+
39674071 // -- resolve_and_check_trusted_gateway --
39684072
39694073 #[ tokio:: test]
@@ -4074,55 +4178,63 @@ mod tests {
40744178 }
40754179 }
40764180
4077- #[ test]
4078- fn test_trusted_gateway_rejects_loopback_as_trusted_gw ( ) {
4181+ #[ tokio :: test]
4182+ async fn test_trusted_gateway_rejects_loopback_as_trusted_gw ( ) {
40794183 // 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 .
4184+ // a loopback IP, resolve_and_check_trusted_gateway must reject it .
4185+ // Using an IP literal as the host bypasses DNS and gives a deterministic
4186+ // resolved address, allowing us to exercise the actual function .
40834187 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" ) ;
4188+ let result = resolve_and_check_trusted_gateway ( "127.0.0.1" , 8080 , loopback, 0 ) . await ;
4189+ assert ! ( result. is_err( ) , "loopback must be rejected" ) ;
4190+ let err = result. unwrap_err ( ) ;
40984191 assert ! (
4099- err. unwrap_err ( ) . contains( "always-blocked non-link-local" ) ,
4100- "expected always-blocked rejection"
4192+ err. contains( "always-blocked non-link-local" ) ,
4193+ "expected always-blocked rejection, got: {err} "
41014194 ) ;
41024195 }
41034196
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 .
4197+ #[ tokio :: test]
4198+ async fn test_trusted_gateway_rejects_unspecified_as_trusted_gw ( ) {
4199+ // Defense-in-depth: 0.0.0.0 as trusted_gw must be rejected.
4200+ // IP literal resolves to 0.0.0.0 directly, bypassing DNS .
41084201 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" ) ;
4202+ let result = resolve_and_check_trusted_gateway ( "0.0.0.0" , 8080 , unspecified, 0 ) . await ;
4203+ assert ! ( result. is_err( ) , "unspecified must be rejected" ) ;
4204+ let err = result. unwrap_err ( ) ;
4205+ assert ! (
4206+ err. contains( "always-blocked non-link-local" ) ,
4207+ "expected always-blocked rejection, got: {err}"
4208+ ) ;
4209+ }
4210+
4211+ #[ tokio:: test]
4212+ async fn test_trusted_gateway_rejects_ip_literal_mismatch ( ) {
4213+ // If the requested IP literal doesn't match trusted_gw, the mismatch
4214+ // guard fires. This exercises the full resolution→validation path.
4215+ let trusted_gw = IpAddr :: V4 ( Ipv4Addr :: new ( 169 , 254 , 1 , 2 ) ) ;
4216+ let other_ip = "10.0.0.1" ; // RFC1918, resolves as a literal
4217+ let result = resolve_and_check_trusted_gateway ( other_ip, 8080 , trusted_gw, 0 ) . await ;
4218+ assert ! ( result. is_err( ) , "IP mismatch must be rejected" ) ;
4219+ let err = result. unwrap_err ( ) ;
4220+ assert ! (
4221+ err. contains( "does not match trusted host gateway" ) ,
4222+ "expected mismatch rejection, got: {err}"
4223+ ) ;
4224+ }
4225+
4226+ #[ tokio:: test]
4227+ async fn test_trusted_gateway_rejects_cloud_metadata_literal ( ) {
4228+ // Cloud metadata IP as a literal address — must be rejected even when
4229+ // it matches trusted_gw (which detect_trusted_host_gateway prevents,
4230+ // but this is the defense-in-depth layer).
4231+ let metadata = IpAddr :: V4 ( Ipv4Addr :: new ( 169 , 254 , 169 , 254 ) ) ;
4232+ let result = resolve_and_check_trusted_gateway ( "169.254.169.254" , 80 , metadata, 0 ) . await ;
4233+ assert ! ( result. is_err( ) , "cloud metadata IP must be rejected" ) ;
4234+ let err = result. unwrap_err ( ) ;
41234235 assert ! (
4124- err. unwrap_err ( ) . contains( "always-blocked non-link-local " ) ,
4125- "expected always-blocked rejection"
4236+ err. contains( "cloud metadata " ) ,
4237+ "expected cloud-metadata rejection, got: {err} "
41264238 ) ;
41274239 }
41284240
0 commit comments