From 78905dd587aa5076da50e7afab0224f26ae0d759 Mon Sep 17 00:00:00 2001 From: austinried <4966622+austinried@users.noreply.github.com> Date: Sun, 15 Nov 2026 00:14:48 +0000 Subject: [PATCH] net: distinguish unreachable VPN routes from exclusions Only RTN_THROW continues routing onto the physical LAN. RTN_UNICAST and RTN_UNREACHABLE do not, including for .local names and IPv6-only unreachable routes. Regression tests failed against the previous predicate and now pass. --- .../subtracks/data/net/LocalNetworkAccess.kt | 10 +++---- .../net/LocalNetworkAccessRobolectricTest.kt | 29 +++++++++++++++++-- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/com/subtracks/data/net/LocalNetworkAccess.kt b/app/src/main/kotlin/com/subtracks/data/net/LocalNetworkAccess.kt index 45a58e1a..5af74e5e 100644 --- a/app/src/main/kotlin/com/subtracks/data/net/LocalNetworkAccess.kt +++ b/app/src/main/kotlin/com/subtracks/data/net/LocalNetworkAccess.kt @@ -37,7 +37,7 @@ class LocalNetworkAccess( vpnRoutes.mayFallThroughToLan(linkAddresses) } else { resolved.any { candidate -> - linkAddresses.any { it.contains(candidate) } && !vpnRoutes.routesThroughVpn(candidate) + linkAddresses.any { it.contains(candidate) } && vpnRoutes.fallsThroughToLan(candidate) } } } @@ -65,17 +65,17 @@ internal fun isLocalHostname(host: String): Boolean { private fun LinkAddress.contains(address: InetAddress): Boolean = IpPrefix(this.address, prefixLength).contains(address) @RequiresApi(37) -private fun List.routesThroughVpn(address: InetAddress): Boolean = +private fun List.fallsThroughToLan(address: InetAddress): Boolean = filter { it.matches(address) } .maxByOrNull { it.destination.prefixLength } - ?.type == RouteInfo.RTN_UNICAST + ?.let { it.type == RouteInfo.RTN_THROW } ?: true @RequiresApi(37) private fun List.mayFallThroughToLan(linkAddresses: List): Boolean = any { route -> - route.type != RouteInfo.RTN_UNICAST && + route.type == RouteInfo.RTN_THROW && linkAddresses.any { link -> route.matches(link.address) || link.contains(route.destination.address) } - } || linkAddresses.any { !routesThroughVpn(it.address) } + } || linkAddresses.any { fallsThroughToLan(it.address) } private val NetworkCapabilities.isPhysicalLan: Boolean get() = diff --git a/app/src/test/kotlin/com/subtracks/data/net/LocalNetworkAccessRobolectricTest.kt b/app/src/test/kotlin/com/subtracks/data/net/LocalNetworkAccessRobolectricTest.kt index 4f8d7cc2..619aa4a4 100644 --- a/app/src/test/kotlin/com/subtracks/data/net/LocalNetworkAccessRobolectricTest.kt +++ b/app/src/test/kotlin/com/subtracks/data/net/LocalNetworkAccessRobolectricTest.kt @@ -128,6 +128,28 @@ class LocalNetworkAccessRobolectricTest { assertTrue(checker().requiresPermission("http://music.local")) } + @Test + fun skipsAnIpv6AddressThatIsUnreachableThroughTheVpn() = + runTest { + activateUnreachableIpv6Vpn() + assertFalse(checker().requiresPermission("http://[fd00::5]")) + } + + @Test + fun skipsDotLocalWhenIpv6IsUnreachableThroughTheVpn() = + runTest { + activateUnreachableIpv6Vpn() + assertFalse(checker().requiresPermission("http://music.local")) + } + + private fun activateUnreachableIpv6Vpn() { + addWifiNetwork("fd00::2", 64) + activateVpn( + route("0.0.0.0", 0), + route("::", 0, RouteInfo.RTN_UNREACHABLE), + ) + } + private fun activateWifi() = activate(ConnectivityManager.TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI) private fun activateVpn(vararg routes: RouteInfo) = @@ -150,10 +172,13 @@ class LocalNetworkAccessRobolectricTest { configure(manager().activeNetwork!!, transport, address, prefix, routes) } - private fun addWifiNetwork() { + private fun addWifiNetwork( + address: String = "10.0.0.2", + prefix: Int = 24, + ) { val network = ShadowNetwork.newInstance(7) shadowOf(manager()).addNetwork(network, info(ConnectivityManager.TYPE_WIFI)) - configure(network, NetworkCapabilities.TRANSPORT_WIFI, "10.0.0.2", 24) + configure(network, NetworkCapabilities.TRANSPORT_WIFI, address, prefix) } private fun configure(