Skip to content

Colota - Android GPS Location tracker

fix(sync): cache only successful DNS lookups for the private-host check

Max Dietrich90117c77ed80
diff --git a/apps/mobile/android/app/src/main/java/com/colota/sync/UrlSafety.kt b/apps/mobile/android/app/src/main/java/com/colota/sync/UrlSafety.kt
index a69ceeb0..d8d8baac 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/sync/UrlSafety.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/sync/UrlSafety.kt
@@ -5,6 +5,7 @@
package com.Colota.sync
+import androidx.annotation.VisibleForTesting
import java.net.InetAddress
import java.net.URL
import java.util.concurrent.ConcurrentHashMap
@@ -15,10 +16,10 @@ import java.util.concurrent.ConcurrentHashMap
*/
object UrlSafety {
- /** Per-hostname cache of the DNS lookup + private-range check. */
+ /** Per-hostname cache. Only successful lookups are cached: a failed one must not pin a host as public. */
private val privateHostCache = ConcurrentHashMap<String, Boolean>()
- /** Performs DNS resolution and caches the result. */
+ /** Performs DNS resolution and caches a successful lookup. */
fun isPrivateEndpoint(endpoint: String): Boolean {
val host = try {
URL(endpoint).host ?: return false
@@ -47,22 +48,31 @@ object UrlSafety {
* Matches Android's local network definition:
* loopback, site-local (RFC 1918), link-local, and CGNAT (100.64.0.0/10).
*/
- fun isPrivateHost(host: String): Boolean {
+ fun isPrivateHost(host: String): Boolean = isPrivateHost(host, InetAddress::getByName)
+
+ @VisibleForTesting
+ internal fun isPrivateHost(host: String, resolve: (String) -> InetAddress): Boolean {
if (host == "localhost") return true
- return privateHostCache.getOrPut(host) {
- try {
- val address = InetAddress.getByName(host)
- address.isAnyLocalAddress ||
- address.isLoopbackAddress ||
- address.isSiteLocalAddress ||
- address.isLinkLocalAddress ||
- isCgnatAddress(address)
- } catch (e: Exception) {
- false
- }
+ privateHostCache[host]?.let { return it }
+
+ val address = try {
+ resolve(host)
+ } catch (_: Exception) {
+ return false
}
+
+ val isPrivate = address.isAnyLocalAddress ||
+ address.isLoopbackAddress ||
+ address.isSiteLocalAddress ||
+ address.isLinkLocalAddress ||
+ isCgnatAddress(address)
+ privateHostCache[host] = isPrivate
+ return isPrivate
}
+ @VisibleForTesting
+ internal fun clearCache() = privateHostCache.clear()
+
/** Checks if the address falls in the CGNAT range 100.64.0.0/10. */
private fun isCgnatAddress(address: InetAddress): Boolean {
val bytes = address.address
diff --git a/apps/mobile/android/app/src/test/java/com/Colota/sync/UrlSafetyTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/sync/UrlSafetyTest.kt
index 550afac9..9c41b599 100644
--- a/apps/mobile/android/app/src/test/java/com/Colota/sync/UrlSafetyTest.kt
+++ b/apps/mobile/android/app/src/test/java/com/Colota/sync/UrlSafetyTest.kt
@@ -1,10 +1,18 @@
package com.Colota.sync
+import java.net.InetAddress
+import java.net.UnknownHostException
import org.junit.Assert.*
+import org.junit.Before
import org.junit.Test
class UrlSafetyTest {
+ @Before
+ fun setUp() {
+ UrlSafety.clearCache()
+ }
+
// --- isValidProtocol ---
@Test
@@ -129,15 +137,6 @@ class UrlSafetyTest {
assertFalse(UrlSafety.isPrivateEndpoint("https://example.com/api"))
}
- @Test
- fun `isPrivateEndpoint resolves hostname via DNS`() {
- // localhost resolves to 127.0.0.1 via InetAddress - proves the DNS path works.
- // Testing hostnames like "server.local" -> 192.168.x.x requires mDNS which
- // is not available in unit tests, but the code path is the same: InetAddress.getByName()
- // -> isSiteLocalAddress. The example.com test below proves public DNS resolution works.
- assertTrue(UrlSafety.isPrivateEndpoint("http://localhost:8080/api"))
- }
-
@Test
fun `isPrivateEndpoint returns false for unresolvable host`() {
assertFalse(UrlSafety.isPrivateEndpoint("http://this-host-does-not-exist-xyz.invalid/api"))
@@ -147,4 +146,41 @@ class UrlSafetyTest {
fun `isPrivateEndpoint returns false for malformed URL`() {
assertFalse(UrlSafety.isPrivateEndpoint("not-a-url"))
}
+
+ // --- private-host resolution ---
+
+ @Test
+ fun `localhost is private without consulting the resolver`() {
+ assertTrue(UrlSafety.isPrivateHost("localhost") { throw AssertionError("resolver must not be called") })
+ }
+
+ @Test
+ fun `a failed lookup does not pin a LAN host as public for the rest of the process`() {
+ val host = "nas.blip.test"
+ val lan = InetAddress.getByName("192.168.1.50")
+
+ assertFalse(UrlSafety.isPrivateHost(host) { throw UnknownHostException(it) })
+
+ assertTrue(UrlSafety.isPrivateHost(host) { lan })
+ assertTrue(UrlSafety.isValidProtocol("http://$host/api"))
+ }
+
+ @Test
+ fun `a resolved host is cached so a later lookup failure cannot flip the verdict`() {
+ val host = "nas.cached.test"
+ val lan = InetAddress.getByName("10.0.0.7")
+
+ assertTrue(UrlSafety.isPrivateHost(host) { lan })
+ assertTrue(UrlSafety.isPrivateHost(host) { throw UnknownHostException(it) })
+ }
+
+ @Test
+ fun `a host that resolves to a public address stays cached as public`() {
+ val host = "tracker.public.test"
+ val wan = InetAddress.getByName("93.184.216.34")
+
+ assertFalse(UrlSafety.isPrivateHost(host) { wan })
+ assertFalse(UrlSafety.isPrivateHost(host) { throw UnknownHostException(it) })
+ assertFalse(UrlSafety.isValidProtocol("http://$host/api"))
+ }
}
diff --git a/fastlane/metadata/android/en-US/changelogs/48.txt b/fastlane/metadata/android/en-US/changelogs/48.txt
index c60e0ea5..446b20db 100644
--- a/fastlane/metadata/android/en-US/changelogs/48.txt
+++ b/fastlane/metadata/android/en-US/changelogs/48.txt
@@ -5,3 +5,4 @@
- Switching auto-export off stops a running export
- Auto-export keeps its schedule after a backup restore
- An aborted backup restore starts tracking again instead of leaving it off
+- Uploads to a local server recover after a temporary DNS failure instead of waiting for an app restart