|
| 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