Skip to content

Commit 1847e9a

Browse files
authored
fix(android): retain canvas pins for equivalent numeric hosts (#130835)
1 parent fe2af30 commit 1847e9a

2 files changed

Lines changed: 57 additions & 35 deletions

File tree

‎apps/android/app/src/main/java/ai/openclaw/app/gateway/GatewaySession.kt‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,9 +1971,7 @@ class GatewaySession(
19711971
val scheme = parsed?.scheme ?: "http"
19721972
val port = parsed?.port?.takeIf { it > 0 } ?: if (scheme.equals("https", ignoreCase = true)) 443 else 80
19731973
val usesFallbackHost = host.isBlank() || isLoopbackGatewayHost(host)
1974-
val gatewayOrigin = "http://${formatGatewayAuthority(endpoint.host.trim().trimEnd('.'), endpoint.port)}".toHttpUrlOrNull()
1975-
val surfaceOrigin = "http://${formatGatewayAuthority(host.trimEnd('.'), port)}".toHttpUrlOrNull()
1976-
val isGatewayAuthority = usesFallbackHost || (gatewayOrigin != null && surfaceOrigin == gatewayOrigin)
1974+
val isGatewayAuthority = usesFallbackHost || endpoint.matchesGatewayAuthority(host, port)
19771975
val path = parsed?.rawPath.orEmpty()
19781976
val capability = path.removePrefix("/__openclaw__/cap/")
19791977
val isRootCapability = path != capability && capability.isNotEmpty() && !capability.contains('/')
@@ -2174,7 +2172,15 @@ internal fun shouldPauseGatewayReconnectAfterAuthFailure(
21742172
}
21752173
}
21762174

2177-
/** Builds the gateway WebSocket URL from endpoint authority and TLS policy. */
2175+
private fun GatewayEndpoint.matchesGatewayAuthority(
2176+
surfaceHost: String,
2177+
surfacePort: Int,
2178+
): Boolean =
2179+
"http://${formatGatewayAuthority(host.trim().trimEnd('.'), port)}".toHttpUrlOrNull()?.let {
2180+
it == "http://${formatGatewayAuthority(surfaceHost.trim().trimEnd('.'), surfacePort)}".toHttpUrlOrNull()
2181+
} == true
2182+
2183+
/** Retains pins for the same TLS authority, canonicalizing numeric hosts without adding DNS aliases. */
21782184
internal fun gatewayTlsFingerprintForCanvasSurface(
21792185
fingerprint: String?,
21802186
surfaceUrl: String,
@@ -2185,11 +2191,8 @@ internal fun gatewayTlsFingerprintForCanvasSurface(
21852191
val surface = runCatching { java.net.URI(surfaceUrl) }.getOrNull() ?: return null
21862192
if (!surface.scheme.equals("https", ignoreCase = true)) return null
21872193
val surfaceHost = surface.host?.trim()?.trimEnd('.') ?: return null
2188-
val gatewayHost = endpoint.host.trim().trimEnd('.')
2189-
val surfacePort = surface.port.takeIf { it > 0 } ?: 443
2190-
return fingerprint.takeIf {
2191-
surfaceHost.equals(gatewayHost, ignoreCase = true) && surfacePort == endpoint.port
2192-
}
2194+
if (!surfaceHost.equals(endpoint.host.trim().trimEnd('.'), ignoreCase = true) && ':' !in surfaceHost && ':' !in endpoint.host) return null
2195+
return fingerprint.takeIf { endpoint.matchesGatewayAuthority(surfaceHost, surface.port.takeIf { it > 0 } ?: 443) }
21932196
}
21942197

21952198
internal fun buildGatewayWebSocketUrl(

‎apps/android/app/src/test/java/ai/openclaw/app/gateway/GatewaySessionInvokeTest.kt‎

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -169,33 +169,50 @@ class GatewaySessionInvokeTest {
169169
@Test
170170
fun canvasRoutePinsOnlyTheConnectedTlsEndpoint() {
171171
val fingerprint = "ab".repeat(32)
172-
val endpoint = GatewayEndpoint.manual(host = "gateway.example", port = 7443)
173-
174-
assertEquals(
175-
fingerprint,
176-
gatewayTlsFingerprintForCanvasSurface(
177-
fingerprint = fingerprint,
178-
surfaceUrl = "https://gateway.example:7443/__openclaw__/cap/token",
179-
endpoint = endpoint,
180-
isTlsConnection = true,
181-
),
182-
)
183-
assertNull(
184-
gatewayTlsFingerprintForCanvasSurface(
185-
fingerprint = fingerprint,
186-
surfaceUrl = "https://canvas.example:7443/__openclaw__/cap/token",
187-
endpoint = endpoint,
188-
isTlsConnection = true,
189-
),
190-
)
191-
assertNull(
192-
gatewayTlsFingerprintForCanvasSurface(
193-
fingerprint = fingerprint,
194-
surfaceUrl = "https://gateway.example:9443/__openclaw__/cap/token",
195-
endpoint = endpoint,
196-
isTlsConnection = true,
197-
),
172+
173+
data class RouteCase(
174+
val host: String,
175+
val surfaceOrigin: String,
176+
val matches: Boolean,
177+
val port: Int = 7443,
178+
val tls: Boolean = true,
179+
val pin: String? = fingerprint,
198180
)
181+
val cases =
182+
listOf(
183+
RouteCase("gateway.example", "https://gateway.example:7443", true),
184+
RouteCase("GATEWAY.example.", "https://gateway.EXAMPLE:7443", true),
185+
RouteCase(" gateway.example. ", "https://gateway.example.:7443", true),
186+
RouteCase("gateway.example", "https://gateway.example", true, port = 443),
187+
RouteCase("192.0.2.10", "https://192.0.2.10:7443", true),
188+
RouteCase("[2001:db8::10]", "https://[2001:db8::10]:7443", true),
189+
RouteCase("gateway.example", "https://canvas.example:7443", false),
190+
RouteCase("gateway.example", "https://gateway.example:9443", false),
191+
RouteCase("gateway.example", "http://gateway.example:7443", false),
192+
RouteCase("localhost", "https://127.0.0.1:7443", false),
193+
RouteCase("bücher.example", "https://xn--bcher-kva.example:7443", false),
194+
RouteCase("192.0.2.10", "https://192.0.2.11:7443", false),
195+
RouteCase("2001:db8::10", "https://[2001:db8::11]:7443", false),
196+
RouteCase("::ffff:192.0.2.10", "https://192.0.2.11:7443", false),
197+
RouteCase("gateway.example", "https://gateway.example:7443", false, tls = false),
198+
RouteCase("gateway.example", "https://gateway.example:7443", false, pin = null),
199+
RouteCase("::ffff:192.0.2.10", "https://192.0.2.10:7443", true),
200+
RouteCase("192.0.2.10", "https://[::ffff:192.0.2.10]:7443", true),
201+
RouteCase("2001:db8::10", "https://[2001:db8::10]:7443", true),
202+
RouteCase("2001:0db8:0:0:0:0:0:10", "https://[2001:db8::10]:7443", true),
203+
)
204+
for (case in cases) {
205+
assertEquals(
206+
"Gateway ${case.host}:${case.port}, surface ${case.surfaceOrigin}",
207+
fingerprint.takeIf { case.matches },
208+
gatewayTlsFingerprintForCanvasSurface(
209+
fingerprint = case.pin,
210+
surfaceUrl = "${case.surfaceOrigin}/__openclaw__/cap/token",
211+
endpoint = GatewayEndpoint.manual(host = case.host, port = case.port),
212+
isTlsConnection = case.tls,
213+
),
214+
)
215+
}
199216
}
200217

201218
@Test
@@ -349,6 +366,8 @@ class GatewaySessionInvokeTest {
349366
val explicitRoutes =
350367
listOf(
351368
"https://canvas.example:9443$capabilityPath",
369+
"https://CANVAS.example.:443$capabilityPath",
370+
"http://CANVAS.example.:80$capabilityPath",
352371
"http://canvas.example:${server.port}$capabilityPath",
353372
"$origin$contextPath$capabilityPath",
354373
"$origin/custom$capabilityPath",

0 commit comments

Comments
 (0)