Skip to content

Colota - Android GPS Location tracker

fix: use native DNS resolution for test connection protocol and privacy checks

Max Dietrich41a4f5a52a3a
diff --git a/apps/docs/docs/api-reference.md b/apps/docs/docs/api-reference.md
index 47359595..765dfdec 100644
--- a/apps/docs/docs/api-reference.md
+++ b/apps/docs/docs/api-reference.md
@@ -152,7 +152,7 @@ Failed items stay in the queue indefinitely until they succeed. The queue can be
## Network Requirements
- **HTTPS required** for all public endpoints
-- **HTTP allowed** for private/local addresses - enforced at the native layer via DNS resolution at request time
+- **HTTP allowed** for private/local addresses - enforced via DNS resolution at both sync time and Test Connection
- Non-standard ports are supported (e.g., `https://my-server.com:8443/api`)
- Self-signed certificates are supported - see [Server Settings](/docs/configuration/server-settings#endpoint-url) for setup instructions
diff --git a/apps/docs/docs/development/permissions.md b/apps/docs/docs/development/permissions.md
index c4599dff..32dfc954 100644
--- a/apps/docs/docs/development/permissions.md
+++ b/apps/docs/docs/development/permissions.md
@@ -90,7 +90,7 @@ Starting with Android 13, apps must request notification permission explicitly.
android.permission.ACCESS_LOCAL_NETWORK
```
-Starting with Android 17, apps need this permission to connect to devices on the local network. Colota requests it when you use **Test Connection** with a private/local IP endpoint (e.g. `192.168.x.x`, `10.x.x.x`, `172.16-31.x.x`, or `100.64.x.x`). Loopback addresses (`localhost` / `127.0.0.1`) do not require this permission. If your server is a public HTTPS endpoint, this permission is never requested.
+Starting with Android 17, apps need this permission to connect to devices on the local network. Colota requests it when you use **Test Connection** with a private/local endpoint - this includes IP addresses (e.g. `192.168.x.x`, `10.x.x.x`, `172.16-31.x.x`, `100.64.x.x`) and hostnames that resolve to private IPs via DNS (e.g. `server.local`). Loopback addresses (`localhost` / `127.0.0.1`) do not require this permission. If your server is a public HTTPS endpoint, this permission is never requested.
:::note[Android 16]
diff --git a/apps/mobile/android/app/src/main/java/com/colota/bridge/LocationServiceModule.kt b/apps/mobile/android/app/src/main/java/com/colota/bridge/LocationServiceModule.kt
index 48bfd419..8a739926 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/bridge/LocationServiceModule.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/bridge/LocationServiceModule.kt
@@ -854,6 +854,16 @@ class LocationServiceModule(reactContext: ReactApplicationContext) :
}
}
+ @ReactMethod
+ fun isValidEndpointProtocol(endpoint: String, promise: Promise) = executeAsync(promise) {
+ networkManager.isValidProtocol(endpoint)
+ }
+
+ @ReactMethod
+ fun isPrivateEndpoint(endpoint: String, promise: Promise) = executeAsync(promise) {
+ networkManager.isPrivateEndpoint(endpoint)
+ }
+
@ReactMethod
fun isNetworkAvailable(promise: Promise) {
promise.resolve(networkManager.isNetworkAvailable())
diff --git a/apps/mobile/android/app/src/main/java/com/colota/sync/NetworkManager.kt b/apps/mobile/android/app/src/main/java/com/colota/sync/NetworkManager.kt
index d8573cf3..0a9cdac3 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/sync/NetworkManager.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/sync/NetworkManager.kt
@@ -96,7 +96,7 @@ class NetworkManager(private val context: Context) {
return@withContext false
}
- if (!isValidProtocol(url)) {
+ if (!isValidProtocol(resolvedEndpoint)) {
AppLogger.e(TAG, "Protocol blocked or invalid: $endpoint")
return@withContext false
}
@@ -187,15 +187,28 @@ class NetworkManager(private val context: Context) {
}
/**
- * Validates protocol and enforces HTTPS for public hosts.
+ * Returns true if the endpoint's host resolves to a private/local address.
+ * Performs DNS resolution and caches the result.
*/
- private fun isValidProtocol(url: URL): Boolean {
+ fun isPrivateEndpoint(endpoint: String): Boolean {
+ val host = try {
+ URL(endpoint).host ?: return false
+ } catch (_: Exception) { return false }
+ return isPrivateHost(host)
+ }
+
+ /**
+ * Validates that the endpoint uses an allowed protocol.
+ * HTTPS is required for public hosts; HTTP is only allowed for private/local addresses.
+ * Performs DNS resolution to detect hostnames that resolve to private IPs.
+ */
+ fun isValidProtocol(endpoint: String): Boolean {
+ val url = try { URL(endpoint) } catch (_: Exception) { return false }
val protocol = url.protocol.lowercase()
val host = url.host ?: return false
if (protocol != "http" && protocol != "https") return false
- // HTTP only allowed for local dev (localhost, 192.168.x.x, 10.x.x.x)
if (protocol == "http" && !isPrivateHost(host)) {
return false
}
diff --git a/apps/mobile/android/app/src/test/java/com/Colota/sync/NetworkManagerTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/sync/NetworkManagerTest.kt
index 6a0fe622..87e93519 100644
--- a/apps/mobile/android/app/src/test/java/com/Colota/sync/NetworkManagerTest.kt
+++ b/apps/mobile/android/app/src/test/java/com/Colota/sync/NetworkManagerTest.kt
@@ -33,94 +33,168 @@ class NetworkManagerTest {
@Test
fun `isValidProtocol accepts https for public host`() {
- assertTrue(invokeIsValidProtocol("https://example.com/api"))
+ val manager = createNetworkManagerViaReflection()
+ assertTrue(manager.isValidProtocol("https://example.com/api"))
}
@Test
fun `isValidProtocol rejects http for public host`() {
- assertFalse(invokeIsValidProtocol("http://example.com/api"))
+ val manager = createNetworkManagerViaReflection()
+ assertFalse(manager.isValidProtocol("http://example.com/api"))
}
@Test
fun `isValidProtocol accepts http for localhost`() {
- assertTrue(invokeIsValidProtocol("http://localhost:8080/api"))
+ val manager = createNetworkManagerViaReflection()
+ assertTrue(manager.isValidProtocol("http://localhost:8080/api"))
}
@Test
fun `isValidProtocol accepts http for 127_0_0_1`() {
- assertTrue(invokeIsValidProtocol("http://127.0.0.1:3000/api"))
+ val manager = createNetworkManagerViaReflection()
+ assertTrue(manager.isValidProtocol("http://127.0.0.1:3000/api"))
}
@Test
fun `isValidProtocol accepts http for 192_168 address`() {
- assertTrue(invokeIsValidProtocol("http://192.168.1.100/api"))
+ val manager = createNetworkManagerViaReflection()
+ assertTrue(manager.isValidProtocol("http://192.168.1.100/api"))
}
@Test
fun `isValidProtocol accepts http for 10_x address`() {
- assertTrue(invokeIsValidProtocol("http://10.0.0.1/api"))
+ val manager = createNetworkManagerViaReflection()
+ assertTrue(manager.isValidProtocol("http://10.0.0.1/api"))
+ }
+
+ @Test
+ fun `isValidProtocol accepts http for 172_16 range`() {
+ val manager = createNetworkManagerViaReflection()
+ assertTrue(manager.isValidProtocol("http://172.16.0.1/api"))
+ assertTrue(manager.isValidProtocol("http://172.31.255.255/api"))
+ }
+
+ @Test
+ fun `isValidProtocol rejects http for 172_32 (outside private range)`() {
+ val manager = createNetworkManagerViaReflection()
+ assertFalse(manager.isValidProtocol("http://172.32.0.1/api"))
+ }
+
+ @Test
+ fun `isValidProtocol accepts http for CGNAT range`() {
+ val manager = createNetworkManagerViaReflection()
+ assertTrue(manager.isValidProtocol("http://100.64.0.1/api"))
+ assertTrue(manager.isValidProtocol("http://100.127.255.255/api"))
}
@Test
fun `isValidProtocol rejects ftp protocol`() {
- assertFalse(invokeIsValidProtocol("ftp://example.com/file"))
+ val manager = createNetworkManagerViaReflection()
+ assertFalse(manager.isValidProtocol("ftp://example.com/file"))
+ }
+
+ @Test
+ fun `isValidProtocol returns false for malformed URL`() {
+ val manager = createNetworkManagerViaReflection()
+ assertFalse(manager.isValidProtocol("not-a-url"))
}
- @Test(expected = java.net.MalformedURLException::class)
- fun `isValidProtocol rejects javascript protocol via URL parsing`() {
- // javascript:// is not a valid URL, so URL() throws before we even get to validation
- invokeIsValidProtocol("javascript://example.com")
+ @Test
+ fun `isValidProtocol returns false for empty string`() {
+ val manager = createNetworkManagerViaReflection()
+ assertFalse(manager.isValidProtocol(""))
}
@Test
fun `isValidProtocol accepts https for any host`() {
- assertTrue(invokeIsValidProtocol("https://192.168.1.1/api"))
- assertTrue(invokeIsValidProtocol("https://localhost/api"))
+ val manager = createNetworkManagerViaReflection()
+ assertTrue(manager.isValidProtocol("https://192.168.1.1/api"))
+ assertTrue(manager.isValidProtocol("https://localhost/api"))
+ }
+
+ // --- isPrivateEndpoint (public wrapper) ---
+
+ @Test
+ fun `isPrivateEndpoint returns true for private IP endpoint`() {
+ val manager = createNetworkManagerViaReflection()
+ assertTrue(manager.isPrivateEndpoint("http://192.168.1.1/api"))
}
- // --- isPrivateHost ---
+ @Test
+ fun `isPrivateEndpoint returns true for localhost`() {
+ val manager = createNetworkManagerViaReflection()
+ assertTrue(manager.isPrivateEndpoint("http://localhost:8080/api"))
+ }
@Test
- fun `isPrivateHost returns true for localhost`() {
- assertTrue(invokeIsPrivateHost("localhost"))
+ fun `isPrivateEndpoint returns false for public host`() {
+ val manager = createNetworkManagerViaReflection()
+ assertFalse(manager.isPrivateEndpoint("https://example.com/api"))
}
@Test
- fun `isPrivateHost returns true for 127_0_0_1`() {
- assertTrue(invokeIsPrivateHost("127.0.0.1"))
+ fun `isPrivateEndpoint returns true for 127_0_0_1`() {
+ val manager = createNetworkManagerViaReflection()
+ assertTrue(manager.isPrivateEndpoint("http://127.0.0.1/api"))
}
@Test
- fun `isPrivateHost returns true for 192_168 address`() {
- assertTrue(invokeIsPrivateHost("192.168.0.1"))
- assertTrue(invokeIsPrivateHost("192.168.255.255"))
+ fun `isPrivateEndpoint returns true for 10_x address`() {
+ val manager = createNetworkManagerViaReflection()
+ assertTrue(manager.isPrivateEndpoint("http://10.0.0.1/api"))
+ assertTrue(manager.isPrivateEndpoint("http://10.255.255.255/api"))
}
@Test
- fun `isPrivateHost returns true for 10_x address`() {
- assertTrue(invokeIsPrivateHost("10.0.0.1"))
- assertTrue(invokeIsPrivateHost("10.255.255.255"))
+ fun `isPrivateEndpoint returns true for 172_16 range`() {
+ val manager = createNetworkManagerViaReflection()
+ assertTrue(manager.isPrivateEndpoint("http://172.16.0.1/api"))
+ assertTrue(manager.isPrivateEndpoint("http://172.31.255.255/api"))
}
@Test
- fun `isPrivateHost returns true for 172_16 range`() {
- assertTrue(invokeIsPrivateHost("172.16.0.1"))
- assertTrue(invokeIsPrivateHost("172.31.255.255"))
+ fun `isPrivateEndpoint returns true for CGNAT range`() {
+ val manager = createNetworkManagerViaReflection()
+ assertTrue(manager.isPrivateEndpoint("http://100.64.0.1/api"))
+ assertTrue(manager.isPrivateEndpoint("http://100.127.255.255/api"))
}
@Test
- fun `isPrivateHost returns false for public IP`() {
- assertFalse(invokeIsPrivateHost("8.8.8.8"))
+ fun `isPrivateEndpoint returns true regardless of protocol`() {
+ val manager = createNetworkManagerViaReflection()
+ assertTrue(manager.isPrivateEndpoint("http://192.168.1.1/api"))
+ assertTrue(manager.isPrivateEndpoint("https://192.168.1.1/api"))
+ assertTrue(manager.isPrivateEndpoint("http://localhost/api"))
+ assertTrue(manager.isPrivateEndpoint("https://localhost/api"))
}
@Test
- fun `isPrivateHost returns false for public domain`() {
- assertFalse(invokeIsPrivateHost("example.com"))
+ fun `isPrivateEndpoint returns false for public domain`() {
+ val manager = createNetworkManagerViaReflection()
+ assertFalse(manager.isPrivateEndpoint("http://example.com/api"))
+ assertFalse(manager.isPrivateEndpoint("https://example.com/api"))
}
@Test
- fun `isPrivateHost returns false for unresolvable host`() {
- assertFalse(invokeIsPrivateHost("this-host-does-not-exist-xyz.invalid"))
+ 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.
+ val manager = createNetworkManagerViaReflection()
+ assertTrue(manager.isPrivateEndpoint("http://localhost:8080/api"))
+ }
+
+ @Test
+ fun `isPrivateEndpoint returns false for unresolvable host`() {
+ val manager = createNetworkManagerViaReflection()
+ assertFalse(manager.isPrivateEndpoint("http://this-host-does-not-exist-xyz.invalid/api"))
+ }
+
+ @Test
+ fun `isPrivateEndpoint returns false for malformed URL`() {
+ val manager = createNetworkManagerViaReflection()
+ assertFalse(manager.isPrivateEndpoint("not-a-url"))
}
// --- maskSensitiveHeaderValue ---
@@ -454,22 +528,6 @@ class NetworkManagerTest {
// --- Reflection helpers to access private methods ---
- private fun invokeIsValidProtocol(urlStr: String): Boolean {
- val url = java.net.URL(urlStr)
- val method = NetworkManager::class.java.getDeclaredMethod("isValidProtocol", java.net.URL::class.java)
- method.isAccessible = true
- // Need an instance — create with mocked context
- val manager = createNetworkManagerViaReflection()
- return method.invoke(manager, url) as Boolean
- }
-
- private fun invokeIsPrivateHost(host: String): Boolean {
- val method = NetworkManager::class.java.getDeclaredMethod("isPrivateHost", String::class.java)
- method.isAccessible = true
- val manager = createNetworkManagerViaReflection()
- return method.invoke(manager, host) as Boolean
- }
-
private fun invokeMask(headerName: String, headerValue: String): String {
val method = NetworkManager::class.java.getDeclaredMethod(
"maskSensitiveHeaderValue", String::class.java, String::class.java
diff --git a/apps/mobile/src/components/features/settings/ConnectionSettings.tsx b/apps/mobile/src/components/features/settings/ConnectionSettings.tsx
index 4ee719a8..cd31b0c7 100644
--- a/apps/mobile/src/components/features/settings/ConnectionSettings.tsx
+++ b/apps/mobile/src/components/features/settings/ConnectionSettings.tsx
@@ -3,12 +3,12 @@
* Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
*/
-import React, { useState, useCallback } from "react"
+import React, { useState, useCallback, useEffect, useRef } from "react"
import { Text, StyleSheet, TextInput, Switch, View, Pressable } from "react-native"
import { CheckCircle, ChevronRight } from "lucide-react-native"
import { Settings, ThemeColors } from "../../../types/global"
import NativeLocationService from "../../../services/NativeLocationService"
-import { isPrivateHost, isEndpointAllowed } from "../../../utils/settingsValidation"
+import { isEndpointAllowed } from "../../../utils/settingsValidation"
import { buildTraccarJsonPayload, isTraccarJsonFormat } from "../../../utils/apiPayload"
import { ensureLocalNetworkPermission } from "../../../services/LocationServicePermission"
import { fonts } from "../../../styles/typography"
@@ -39,7 +39,20 @@ export function ConnectionSettings({
const [testing, setTesting] = useState(false)
const [testResponse, setTestResponse] = useState<string | null>(null)
const [testError, setTestError] = useState(false)
+ const [endpointPrivate, setEndpointPrivate] = useState(false)
const timeout = useTimeout()
+ const pendingCheck = useRef(0)
+
+ useEffect(() => {
+ if (!endpointInput || !endpointInput.startsWith("http://")) {
+ setEndpointPrivate(false)
+ return
+ }
+ const id = ++pendingCheck.current
+ NativeLocationService.isPrivateEndpoint(endpointInput).then((isPrivate) => {
+ if (id === pendingCheck.current) setEndpointPrivate(isPrivate)
+ })
+ }, [endpointInput])
const handleOfflineModeChange = useCallback(
async (enabled: boolean) => {
@@ -122,7 +135,15 @@ export function ConnectionSettings({
if (fieldMap.bear) payload[fieldMap.bear] = recentLocation.bearing ?? 0
if (fieldMap.tst) payload[fieldMap.tst] = Math.floor(Date.now() / 1000)
- if (isPrivateHost(endpointInput)) {
+ const protocolAllowed = await NativeLocationService.isValidEndpointProtocol(endpointInput)
+ if (!protocolAllowed) {
+ setTestResponse("HTTPS is required for public endpoints. HTTP is only allowed for private/local addresses.")
+ setTestError(true)
+ return
+ }
+
+ const isPrivate = await NativeLocationService.isPrivateEndpoint(endpointInput)
+ if (isPrivate) {
const granted = await ensureLocalNetworkPermission()
if (!granted) {
setTestResponse("Local network permission required to reach this server")
@@ -265,7 +286,7 @@ export function ConnectionSettings({
</Text>
)}
- {endpointInput.startsWith("http://") && !isPrivateHost(endpointInput) && (
+ {endpointInput.startsWith("http://") && !endpointPrivate && (
<Text style={[styles.httpWarning, { color: colors.warning }]}>
HTTP only allowed for private IPs / localhost
</Text>
diff --git a/apps/mobile/src/components/features/settings/__tests__/ConnectionSettings.test.tsx b/apps/mobile/src/components/features/settings/__tests__/ConnectionSettings.test.tsx
index 04d40872..a05d24a8 100644
--- a/apps/mobile/src/components/features/settings/__tests__/ConnectionSettings.test.tsx
+++ b/apps/mobile/src/components/features/settings/__tests__/ConnectionSettings.test.tsx
@@ -52,6 +52,8 @@ const mockClearQueue = jest.fn().mockResolvedValue(5)
const mockGetMostRecentLocation = jest.fn().mockResolvedValue(null)
const mockGetAuthHeaders = jest.fn().mockResolvedValue({})
const mockIsNetworkAvailable = jest.fn().mockResolvedValue(true)
+const mockIsValidEndpointProtocol = jest.fn().mockResolvedValue(true)
+const mockIsEndpointPrivate = jest.fn().mockResolvedValue(false)
jest.mock("../../../../services/NativeLocationService", () => ({
__esModule: true,
@@ -61,7 +63,9 @@ jest.mock("../../../../services/NativeLocationService", () => ({
clearQueue: (...args: any[]) => mockClearQueue(...args),
getMostRecentLocation: (...args: any[]) => mockGetMostRecentLocation(...args),
getAuthHeaders: (...args: any[]) => mockGetAuthHeaders(...args),
- isNetworkAvailable: (...args: any[]) => mockIsNetworkAvailable(...args)
+ isNetworkAvailable: (...args: any[]) => mockIsNetworkAvailable(...args),
+ isValidEndpointProtocol: (...args: any[]) => mockIsValidEndpointProtocol(...args),
+ isPrivateEndpoint: (...args: any[]) => mockIsEndpointPrivate(...args)
}
}))
@@ -71,7 +75,6 @@ jest.mock("../../../../services/modalService", () => ({
}))
jest.mock("../../../../utils/settingsValidation", () => ({
- isPrivateHost: () => false,
isEndpointAllowed: () => true
}))
@@ -310,4 +313,41 @@ describe("ConnectionSettings", () => {
})
})
})
+
+ describe("test connection HTTPS enforcement", () => {
+ const location = {
+ latitude: 52.5,
+ longitude: 13.4,
+ accuracy: 10,
+ altitude: 50,
+ speed: 0,
+ battery: 80,
+ batteryStatus: 2,
+ bearing: 0
+ }
+
+ it("blocks when native protocol check rejects endpoint", async () => {
+ mockGetMostRecentLocation.mockResolvedValue(location)
+ mockIsValidEndpointProtocol.mockResolvedValue(false)
+ const { getByText } = renderComponent({}, "http://example.com/api")
+
+ fireEvent.press(getByText("Test Connection"))
+
+ await waitFor(() => {
+ expect(getByText(/HTTPS is required for public endpoints/)).toBeTruthy()
+ })
+ })
+
+ it("allows when native protocol check accepts endpoint", async () => {
+ mockGetMostRecentLocation.mockResolvedValue(location)
+ mockIsValidEndpointProtocol.mockResolvedValue(true)
+ const { queryByText, getByText } = renderComponent({}, "https://example.com/api")
+
+ fireEvent.press(getByText("Test Connection"))
+
+ await waitFor(() => {
+ expect(queryByText(/HTTPS is required/)).toBeNull()
+ })
+ })
+ })
})
diff --git a/apps/mobile/src/services/NativeLocationService.ts b/apps/mobile/src/services/NativeLocationService.ts
index a1cb6a61..d0cadcd3 100644
--- a/apps/mobile/src/services/NativeLocationService.ts
+++ b/apps/mobile/src/services/NativeLocationService.ts
@@ -490,6 +490,29 @@ class NativeLocationService {
// NETWORK
// ============================================================================
+ /**
+ * Validates that the endpoint uses an allowed protocol.
+ * HTTPS is required for public hosts; HTTP is only allowed for private/local addresses.
+ * Uses DNS resolution on the native side to detect hostnames that resolve to private IPs.
+ */
+ static async isValidEndpointProtocol(endpoint: string): Promise<boolean> {
+ this.ensureModule()
+ return this.safeExecute(
+ () => LocationServiceModule.isValidEndpointProtocol(endpoint),
+ false,
+ "isValidEndpointProtocol failed"
+ )
+ }
+
+ /**
+ * Returns true if the endpoint's host resolves to a private/local address via native DNS.
+ * Used to decide whether to prompt for local network permission (Android 15+).
+ */
+ static async isPrivateEndpoint(endpoint: string): Promise<boolean> {
+ this.ensureModule()
+ return this.safeExecute(() => LocationServiceModule.isPrivateEndpoint(endpoint), false, "isPrivateEndpoint failed")
+ }
+
/**
* Checks if the device has an active internet connection
*/
diff --git a/apps/mobile/src/services/__tests__/NativeLocationService.test.ts b/apps/mobile/src/services/__tests__/NativeLocationService.test.ts
index e3fa87d6..6450be44 100644
--- a/apps/mobile/src/services/__tests__/NativeLocationService.test.ts
+++ b/apps/mobile/src/services/__tests__/NativeLocationService.test.ts
@@ -74,6 +74,8 @@ jest.mock("react-native", () => ({
saveAuthConfig: jest.fn().mockResolvedValue(true),
getAuthHeaders: jest.fn().mockResolvedValue({}),
isNetworkAvailable: jest.fn().mockResolvedValue(true),
+ isValidEndpointProtocol: jest.fn().mockResolvedValue(true),
+ isPrivateEndpoint: jest.fn().mockResolvedValue(false),
getActiveProfile: jest.fn().mockResolvedValue(null)
},
BuildConfigModule: {
diff --git a/apps/mobile/src/utils/__tests__/settingsValidation.test.ts b/apps/mobile/src/utils/__tests__/settingsValidation.test.ts
index bc5c6db9..c634106f 100644
--- a/apps/mobile/src/utils/__tests__/settingsValidation.test.ts
+++ b/apps/mobile/src/utils/__tests__/settingsValidation.test.ts
@@ -1,72 +1,4 @@
-import { isPrivateHost, isEndpointAllowed } from "../settingsValidation"
-
-describe("isPrivateHost", () => {
- describe("private IPs", () => {
- it("detects localhost IP", () => {
- expect(isPrivateHost("127.0.0.1")).toBe(true)
- })
-
- it("detects 10.x.x.x range", () => {
- expect(isPrivateHost("10.0.0.1")).toBe(true)
- expect(isPrivateHost("10.255.255.255")).toBe(true)
- })
-
- it("detects 192.168.x.x range", () => {
- expect(isPrivateHost("192.168.1.1")).toBe(true)
- expect(isPrivateHost("192.168.0.100")).toBe(true)
- })
-
- it("detects 172.16-31.x.x range", () => {
- expect(isPrivateHost("172.16.0.1")).toBe(true)
- expect(isPrivateHost("172.31.255.255")).toBe(true)
- })
-
- it("detects localhost hostname", () => {
- expect(isPrivateHost("localhost")).toBe(true)
- })
- })
-
- describe("public IPs and hosts", () => {
- it("rejects public IPs", () => {
- expect(isPrivateHost("8.8.8.8")).toBe(false)
- expect(isPrivateHost("1.1.1.1")).toBe(false)
- })
-
- it("rejects public hostnames", () => {
- expect(isPrivateHost("google.com")).toBe(false)
- expect(isPrivateHost("example.com")).toBe(false)
- })
-
- it("rejects 172.32+ (outside private range)", () => {
- expect(isPrivateHost("172.32.0.1")).toBe(false)
- })
- })
-
- describe("URL handling", () => {
- it("strips http protocol", () => {
- expect(isPrivateHost("http://192.168.1.1")).toBe(true)
- })
-
- it("strips https protocol", () => {
- expect(isPrivateHost("https://localhost")).toBe(true)
- })
-
- it("handles URLs with ports", () => {
- expect(isPrivateHost("192.168.1.1:3000")).toBe(true)
- expect(isPrivateHost("http://10.0.0.1:8080")).toBe(true)
- })
-
- it("handles URLs with paths", () => {
- expect(isPrivateHost("http://192.168.1.1/api/locations")).toBe(true)
- })
- })
-
- describe("edge cases", () => {
- it("returns false for empty string", () => {
- expect(isPrivateHost("")).toBe(false)
- })
- })
-})
+import { isEndpointAllowed } from "../settingsValidation"
describe("isEndpointAllowed", () => {
it("allows valid http and https URLs", () => {
diff --git a/apps/mobile/src/utils/settingsValidation.ts b/apps/mobile/src/utils/settingsValidation.ts
index 3caa3873..849c59fe 100644
--- a/apps/mobile/src/utils/settingsValidation.ts
+++ b/apps/mobile/src/utils/settingsValidation.ts
@@ -16,34 +16,7 @@ export function findDuplicates(values: string[]): Set<string> {
return dupes
}
-function isPrivateIP(host: string): boolean {
- const parts = host.split(".")
- if (parts.length !== 4) return false
- const octets = parts.map(Number)
- if (octets.some((o) => isNaN(o) || o < 0 || o > 255)) return false
- const [a, b] = octets
- return (
- a === 127 || // Loopback (127.0.0.0/8)
- a === 10 || // RFC 1918 (10.0.0.0/8)
- (a === 192 && b === 168) || // RFC 1918 (192.168.0.0/16)
- (a === 172 && b >= 16 && b <= 31) || // RFC 1918 (172.16.0.0/12)
- (a === 100 && b >= 64 && b <= 127) || // CGNAT (100.64.0.0/10)
- (a === 169 && b === 254) // Link-local (169.254.0.0/16)
- )
-}
-
-export function isPrivateHost(url: string) {
- try {
- const stripped = url.replace(/^https?:\/\//, "").split(/[/?#]/)[0]
- const host = stripped.split(":")[0]
- return isPrivateIP(host) || host === "localhost"
- } catch {
- return false
- }
-}
-
// Validates that a URL has a valid http(s):// scheme and hostname.
-// HTTP enforcement (private-IP restriction) happens at the native layer via DNS resolution.
export function isEndpointAllowed(url: string) {
return /^https?:\/\/[^/:]+/.test(url)
}