Skip to content

Commit 5754050

Browse files
committed
Decide "the app's own origin" in one place, by scheme, host and effective port, with unit tests
The checks already compared the port; this moves them to one tested helper and fixes the three that did not use it: the renewal's returnTo compared raw ports (so ":443" written counted as foreign), a certificate error compared hosts only, and a base URL with its default port written was kept in a spelling location.origin never uses. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012xYtaUjbkB1hwLtK4TEzhq
1 parent fbd102c commit 5754050

6 files changed

Lines changed: 142 additions & 26 deletions

File tree

‎android-shell/README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ What is native:
8888
`OpenBotShellAndroid.postMessage(json)`, which `bridge.ts` calls; messages are accepted from the
8989
main frame on the base origin only. No `addJavascriptInterface`. The user agent ends with
9090
` OpenBotShell-Android/<version>`.
91+
- **"The app's own origin" is one rule.** Every check (the bridge, the lock, dialogs, media
92+
permissions, Back, the navigation policy, the last page a retry loads, `/sign`, the hosted sign-in,
93+
a certificate error) goes through `Origins.same`: scheme, host and effective port (443 or 80 when
94+
none is written), case ignored, a trailing-dot host or user info never matching
95+
(`OriginsTest`).
9196
- **Only the app's own origin locks the app.** A page of another site that fails to load (the
9297
identity provider, a link) sends the WebView back to the last page of the app; retries load the last
9398
page of the app, never another site's.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package ai.openbot.shell
2+
3+
import java.net.URI
4+
5+
/**
6+
* The one place the app decides whether two addresses share an origin: scheme, host and effective
7+
* port, as a browser compares them. Pure JVM (java.net.URI), so the unit tests cover it.
8+
*
9+
* - Scheme and host are compared without case; only http and https have an origin here.
10+
* - The port is the written one, else 443 for https and 80 for http, so `https://h` and
11+
* `https://h:443` are the same origin and `https://h:8443` is another.
12+
* - A host written with a trailing dot (`my.open.bot.`) is a different origin, as it is to Chromium
13+
* (`location.origin` keeps the dot).
14+
* - Anything that does not parse, has no host, or carries user info (`https://a@b/`) has no origin
15+
* and so never matches: every check fails closed.
16+
*/
17+
object Origins {
18+
data class Origin(val scheme: String, val host: String, val port: Int) {
19+
/** "scheme://host[:port]", the default port left out, as `location.origin` spells it. */
20+
override fun toString(): String =
21+
if (port == defaultPort(scheme)) "$scheme://$host" else "$scheme://$host:$port"
22+
}
23+
24+
fun of(url: String?): Origin? {
25+
if (url == null) return null
26+
val schemeEnd = url.indexOf("://")
27+
if (schemeEnd <= 0) return null
28+
val scheme = url.substring(0, schemeEnd).lowercase()
29+
if (scheme != "https" && scheme != "http") return null
30+
// Only the authority matters; parsing just it keeps a path or query that java.net.URI
31+
// would refuse (an unescaped "|" or space) from making an own page look foreign.
32+
val rest = url.substring(schemeEnd + 3)
33+
val end = rest.indexOfFirst { it == '/' || it == '?' || it == '#' }
34+
val authority = if (end == -1) rest else rest.substring(0, end)
35+
if (authority.isEmpty() || authority.contains('\\')) return null
36+
val uri = try {
37+
URI("$scheme://$authority/")
38+
} catch (error: Exception) {
39+
return null
40+
}
41+
if (uri.rawUserInfo != null) return null
42+
val host = uri.host?.lowercase()?.takeIf { it.isNotEmpty() } ?: return null
43+
val port = if (uri.port != -1) uri.port else defaultPort(scheme)
44+
return Origin(scheme, host, port)
45+
}
46+
47+
fun same(a: String?, b: String?): Boolean {
48+
val left = of(a) ?: return false
49+
return left == of(b)
50+
}
51+
52+
fun defaultPort(scheme: String): Int = if (scheme == "https") 443 else 80
53+
}

‎android-shell/app/src/main/java/ai/openbot/shell/ShellConfig.kt‎

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,12 @@ class ShellConfig(
4747
fun url(pathAndQuery: String): String =
4848
origin + if (pathAndQuery.startsWith("/")) pathAndQuery else "/$pathAndQuery"
4949

50-
fun isOwnOrigin(uri: Uri?): Boolean {
51-
if (uri == null) return false
52-
val scheme = uri.scheme?.lowercase() ?: return false
53-
if (scheme != baseScheme) return false
54-
if ((uri.host?.lowercase() ?: "") != baseHost) return false
55-
return effectivePort(uri) == effectivePort(baseUrl)
56-
}
50+
/** Same scheme, host and effective port as the base URL (Origins.same). */
51+
fun isOwnOrigin(uri: Uri?): Boolean = uri != null && isOwnOrigin(uri.toString())
5752

58-
fun isOwnOrigin(url: String?): Boolean = url != null && isOwnOrigin(Uri.parse(url))
53+
fun isOwnOrigin(url: String?): Boolean = Origins.same(url, origin)
5954

6055
companion object {
61-
fun effectivePort(uri: Uri): Int = when {
62-
uri.port != -1 -> uri.port
63-
uri.scheme.equals("https", ignoreCase = true) -> 443
64-
else -> 80
65-
}
66-
6756
fun from(intent: Intent?): ShellConfig {
6857
var baseOverride: String? = null
6958
var diagnostics = false
@@ -84,16 +73,15 @@ class ShellConfig(
8473
)
8574
}
8675

87-
/** The override when it is an http(s) URL with a host, else BuildConfig.BASE_URL; origin only. */
76+
/**
77+
* The override when it is an http(s) URL with a host, else BuildConfig.BASE_URL; origin only,
78+
* spelled as `location.origin` spells it (lower case, default port left out), because the
79+
* marker script and the bridge's origin rule compare against that spelling.
80+
*/
8881
fun resolveBase(candidate: String?): Uri {
8982
for (value in listOfNotNull(candidate, BuildConfig.BASE_URL)) {
90-
val uri = Uri.parse(value)
91-
val scheme = uri.scheme?.lowercase()
92-
val host = uri.host?.lowercase()
93-
if ((scheme == "https" || scheme == "http") && !host.isNullOrEmpty()) {
94-
val authority = if (uri.port != -1) "$host:${uri.port}" else host
95-
return Uri.Builder().scheme(scheme).encodedAuthority(authority).build()
96-
}
83+
val origin = Origins.of(value) ?: continue
84+
return Uri.parse(origin.toString())
9785
}
9886
return Uri.parse("https://my.open.bot")
9987
}

‎android-shell/app/src/main/java/ai/openbot/shell/ShellUrls.kt‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ object ShellUrls {
5555
} catch (error: Exception) {
5656
return "/"
5757
}
58-
val base = URI(origin)
59-
val sameHost = uri.scheme.equals(base.scheme, ignoreCase = true) && uri.host.equals(base.host, ignoreCase = true) && uri.port == base.port
60-
if (!sameHost) return "/"
58+
if (!Origins.same(returnTo, origin)) return "/"
6159
val path = (uri.rawPath ?: "").ifEmpty { "/" }
6260
return path + (uri.rawQuery?.let { "?$it" } ?: "") + (uri.rawFragment?.let { "#$it" } ?: "")
6361
}

‎android-shell/app/src/main/java/ai/openbot/shell/WebController.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ class WebController(
571571
handler.cancel()
572572
diagnostics.log("ssl-error", mapOf("primaryError" to error.primaryError, "url" to Diagnostics.redact(error.url)))
573573
val loading = lastRequestedUrl
574-
if (loading != null && Uri.parse(loading).host == Uri.parse(error.url).host) failMainFrame("ssl ${error.primaryError}", error.url)
574+
if (Origins.same(loading, error.url)) failMainFrame("ssl ${error.primaryError}", error.url)
575575
}
576576

577577
override fun onRenderProcessGone(view: WebView, detail: RenderProcessGoneDetail): Boolean {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package ai.openbot.shell
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Assert.assertFalse
5+
import org.junit.Assert.assertNull
6+
import org.junit.Assert.assertTrue
7+
import org.junit.Test
8+
9+
/** Origins.same: scheme, host and effective port, failing closed on anything odd. */
10+
class OriginsTest {
11+
private val base = "https://my.open.bot"
12+
13+
@Test
14+
fun sameOriginWithPathQueryAndFragment() {
15+
assertTrue(Origins.same("https://my.open.bot/bots/b1?tab=chat#x", base))
16+
assertTrue(Origins.same("https://my.open.bot", base))
17+
assertTrue(Origins.same("https://my.open.bot/a b|c", base))
18+
}
19+
20+
@Test
21+
fun sameHostDifferentPortIsAnotherOrigin() {
22+
assertFalse(Origins.same("https://my.open.bot:8443/", base))
23+
assertFalse(Origins.same("http://localhost:3011/", "http://localhost:3010"))
24+
assertTrue(Origins.same("http://localhost:3010/x", "http://localhost:3010"))
25+
}
26+
27+
@Test
28+
fun differentSchemeIsAnotherOrigin() {
29+
assertFalse(Origins.same("http://my.open.bot/", base))
30+
assertFalse(Origins.same("ws://my.open.bot/", base))
31+
assertFalse(Origins.same("intent://my.open.bot/#Intent;end", base))
32+
}
33+
34+
@Test
35+
fun defaultPortWrittenOrNotIsTheSame() {
36+
assertTrue(Origins.same("https://my.open.bot:443/x", base))
37+
assertTrue(Origins.same("http://10.0.2.2:80/", "http://10.0.2.2"))
38+
assertFalse(Origins.same("https://my.open.bot:80/", base))
39+
assertEquals("https://my.open.bot", Origins.of("HTTPS://My.Open.Bot:443/path").toString())
40+
assertEquals("http://localhost:3010", Origins.of("http://localhost:3010").toString())
41+
}
42+
43+
@Test
44+
fun hostCaseIsIgnoredAndATrailingDotIsAnotherOrigin() {
45+
assertTrue(Origins.same("https://MY.Open.BOT/", base))
46+
assertTrue(Origins.same("HTTPS://my.open.bot/", base))
47+
assertFalse(Origins.same("https://my.open.bot./", base))
48+
}
49+
50+
@Test
51+
fun lookalikesAndOddInputFailClosed() {
52+
assertFalse(Origins.same("https://my.open.bot.evil.com/", base))
53+
assertFalse(Origins.same("https://evil.com/my.open.bot", base))
54+
assertFalse(Origins.same("https://my.open.bot@evil.com/", base))
55+
assertFalse(Origins.same("https://evil.com@my.open.bot/", base))
56+
assertFalse(Origins.same("https://evil.com\\@my.open.bot/", base))
57+
assertFalse(Origins.same("about:blank", base))
58+
assertFalse(Origins.same("null", base))
59+
assertFalse(Origins.same("", base))
60+
assertFalse(Origins.same(null, base))
61+
assertFalse(Origins.same(base, null))
62+
assertNull(Origins.of("https:///path"))
63+
}
64+
65+
@Test
66+
fun renewalReturnToUsesTheSameRule() {
67+
val intercepted = "$base/api/auth/hosted/sign-in?returnTo="
68+
assertEquals("/bots", ShellUrls.renewalTo(intercepted + ShellUrls.encode("https://my.open.bot:443/bots"), base))
69+
assertEquals("/", ShellUrls.renewalTo(intercepted + ShellUrls.encode("https://my.open.bot:8443/bots"), base))
70+
assertEquals("/", ShellUrls.renewalTo(intercepted + ShellUrls.encode("http://my.open.bot/bots"), base))
71+
}
72+
}

0 commit comments

Comments
 (0)