Modern Apps git

← Modern-Apps.git

web: fail closed on leading-zero IPv4 octets in the LAN classifier

Commit
aeb86943dae264b0d595abee5cdef0dc35bccbd0
Author
Vayun Mathur <[email protected]>
When
2026-11-08 04:22 UTC (28 days ago)
Parents
4aa1516
LocalNetwork.parseIpv4 read a leading-zero octet as decimal, but the
WHATWG URL parser WebView follows reads it as octal. http://010.0.0.1/
therefore classified LAN (decimal 10.0.0.1) while the renderer connects
to 8.0.0.1, letting the cleartext gate allow a public-host request.

Reject octets with a leading zero so parseIpv4 returns null; such hosts
fall through to NUMERIC_ONLY and classify PUBLIC, i.e. fail closed, in
line with the existing treatment of other non-canonical numeric forms
(2130706433, 10.1). IPv4-mapped IPv6 tails inherit the fix through the
same parser and also classify PUBLIC. A lone '0' octet (0.0.0.0) is
unaffected and stays LAN.

Side effect: UrlCleaner.httpsUpgrade no longer treats leading-zero
hosts as IP literals, so it upgrades them to https instead of leaving
cleartext alone — the safe direction.

Verified red-then-green: new 'leading-zero octets fail closed' test in
LocalNetworkTest failed with expected:<PUBLIC> but was:<LAN> before the
fix; focused LocalNetwork/LanPolicy/UrlCleaner runs and the full
:web:testDevUnitTest suite (336 tests, 0 failures) pass after.

Diff

web/src/main/java/com/vayunmathur/web/domain/LocalNetwork.kt
diff --git a/web/src/main/java/com/vayunmathur/web/domain/LocalNetwork.kt b/web/src/main/java/com/vayunmathur/web/domain/LocalNetwork.ktindex 97fc334953..3ed1afc565 100644--- a/web/src/main/java/com/vayunmathur/web/domain/LocalNetwork.kt+++ b/web/src/main/java/com/vayunmathur/web/domain/LocalNetwork.kt@@ -99,6 +99,10 @@ object LocalNetwork {         for (i in 0..3) {             val part = parts[i]             if (part.isEmpty() || part.length > 3 || !part.all { it in '0'..'9' }) return null+            // The URL parser reads a leading-zero part as octal (`010` is 8), so a+            // decimal reading here would classify a different address than the one+            // the renderer connects to. Reject and let the caller fail closed.+            if (part.length > 1 && part[0] == '0') return null             val value = part.toInt()             if (value > 255) return null             octets[i] = value
web/src/test/java/com/vayunmathur/web/domain/LocalNetworkTest.kt
diff --git a/web/src/test/java/com/vayunmathur/web/domain/LocalNetworkTest.kt b/web/src/test/java/com/vayunmathur/web/domain/LocalNetworkTest.ktindex af6b2722eb..d573509797 100644--- a/web/src/test/java/com/vayunmathur/web/domain/LocalNetworkTest.kt+++ b/web/src/test/java/com/vayunmathur/web/domain/LocalNetworkTest.kt@@ -83,6 +83,18 @@ class LocalNetworkTest {         assertEquals(HostKind.PUBLIC, kind("192.168.1.256"))     } +    @Test+    fun `leading-zero octets fail closed`() {+        // The URL parser reads a leading-zero part as octal, so `010.0.0.1`+        // resolves to 8.0.0.1, not 10.0.0.1. Decimal classification here would+        // call it LAN while the renderer connects elsewhere — fail closed.+        assertEquals(HostKind.PUBLIC, kind("010.0.0.1"))+        assertEquals(HostKind.PUBLIC, kind("192.168.001.1"))+        assertEquals(HostKind.PUBLIC, kind("::ffff:010.0.0.1"))+        // A lone zero is not an octal prefix; this-network stays LAN.+        assertEquals(HostKind.LAN, kind("0.0.0.0"))+    }+     // -------------------------------------------------------------------- IPv6      @Test