|
| 1 | +package ai.openclaw.app.node |
| 2 | + |
| 3 | +import java.net.InetAddress |
| 4 | +import java.net.URI |
| 5 | + |
| 6 | +/** Shared policy for gateway-driven loads and WebView main-frame navigations. */ |
| 7 | +internal object CanvasNavigationPolicy { |
| 8 | + fun normalize(rawUrl: String): String { |
| 9 | + val trimmed = rawUrl.trim() |
| 10 | + if (trimmed.isBlank() || trimmed == "/") return "" |
| 11 | + return trimmed.takeUnless(::shouldBlock).orEmpty() |
| 12 | + } |
| 13 | + |
| 14 | + fun shouldBlock(rawUrl: String): Boolean { |
| 15 | + val trimmed = rawUrl.trim() |
| 16 | + if (trimmed.isBlank() || trimmed == "/") return false |
| 17 | + val target = parseTarget(trimmed) |
| 18 | + val isWebUrl = target.scheme == "http" || target.scheme == "https" |
| 19 | + if (isWebUrl && (target.host.isEmpty() || isMalformedWebHost(target.host))) return true |
| 20 | + return target.host.isNotEmpty() && isDeviceLocalHost(target.host) |
| 21 | + } |
| 22 | + |
| 23 | + /** WebView does not expose POST redirects to shouldOverrideUrlLoading; fail closed before dispatch. */ |
| 24 | + fun shouldBlockNonGetMainFrame( |
| 25 | + method: String, |
| 26 | + isForMainFrame: Boolean, |
| 27 | + ): Boolean = isForMainFrame && !method.equals("GET", ignoreCase = true) |
| 28 | + |
| 29 | + private data class Target( |
| 30 | + val scheme: String, |
| 31 | + val host: String, |
| 32 | + ) |
| 33 | + |
| 34 | + private fun parseTarget(rawUrl: String): Target { |
| 35 | + val parsed = runCatching { URI(rawUrl) }.getOrNull() |
| 36 | + val scheme = |
| 37 | + parsed |
| 38 | + ?.scheme |
| 39 | + ?.trim() |
| 40 | + ?.lowercase() |
| 41 | + .orEmpty() |
| 42 | + .ifBlank { rawScheme(rawUrl) } |
| 43 | + val host = |
| 44 | + parsed |
| 45 | + ?.host |
| 46 | + ?.trim() |
| 47 | + .orEmpty() |
| 48 | + .ifBlank { authorityHost(parsed?.rawAuthority ?: rawAuthority(rawUrl)) } |
| 49 | + return Target(scheme = scheme, host = percentDecodeAscii(host)) |
| 50 | + } |
| 51 | + |
| 52 | + private fun rawScheme(rawUrl: String): String { |
| 53 | + val schemeSeparator = rawUrl.indexOf("://") |
| 54 | + val colonSeparator = rawUrl.indexOf(':') |
| 55 | + val end = |
| 56 | + when { |
| 57 | + schemeSeparator > 0 -> schemeSeparator |
| 58 | + colonSeparator > 0 -> colonSeparator |
| 59 | + else -> return "" |
| 60 | + } |
| 61 | + val candidate = rawUrl.substring(0, end).trim().lowercase() |
| 62 | + return candidate |
| 63 | + .takeIf { |
| 64 | + it.all { char -> |
| 65 | + char in 'a'..'z' || char in '0'..'9' || char == '+' || char == '-' || char == '.' |
| 66 | + } |
| 67 | + }.orEmpty() |
| 68 | + } |
| 69 | + |
| 70 | + private fun rawAuthority(rawUrl: String): String? { |
| 71 | + val schemeSeparator = rawUrl.indexOf("://") |
| 72 | + if (schemeSeparator < 0) return null |
| 73 | + val authorityStart = schemeSeparator + 3 |
| 74 | + val authorityEnd = |
| 75 | + rawUrl |
| 76 | + .indexOfAny(charArrayOf('/', '\\', '?', '#'), startIndex = authorityStart) |
| 77 | + .takeIf { it >= 0 } |
| 78 | + ?: rawUrl.length |
| 79 | + return rawUrl.substring(authorityStart, authorityEnd) |
| 80 | + } |
| 81 | + |
| 82 | + private fun authorityHost(rawAuthority: String?): String { |
| 83 | + val authority = rawAuthority?.trim().orEmpty() |
| 84 | + if (authority.isEmpty()) return "" |
| 85 | + val hostPort = authority.substringAfterLast('@') |
| 86 | + if (hostPort.startsWith("[")) { |
| 87 | + return hostPort.substringAfter('[').substringBefore(']') |
| 88 | + } |
| 89 | + return if (hostPort.count { it == ':' } == 1) hostPort.substringBefore(':') else hostPort |
| 90 | + } |
| 91 | + |
| 92 | + private fun percentDecodeAscii(value: String): String { |
| 93 | + if (!value.contains('%')) return value |
| 94 | + val out = StringBuilder(value.length) |
| 95 | + var index = 0 |
| 96 | + while (index < value.length) { |
| 97 | + if (value[index] == '%' && index + 2 < value.length) { |
| 98 | + val byte = value.substring(index + 1, index + 3).toIntOrNull(16) |
| 99 | + if (byte != null) { |
| 100 | + out.append(byte.toChar()) |
| 101 | + index += 3 |
| 102 | + continue |
| 103 | + } |
| 104 | + } |
| 105 | + out.append(value[index]) |
| 106 | + index += 1 |
| 107 | + } |
| 108 | + return out.toString() |
| 109 | + } |
| 110 | + |
| 111 | + private fun isMalformedWebHost(rawHost: String): Boolean = |
| 112 | + // Chromium applies UTS #46 before resolving special-scheme hosts. Reject raw Unicode here so |
| 113 | + // compatibility characters cannot become localhost or a loopback IP after this check. |
| 114 | + rawHost.any { char -> |
| 115 | + char <= ' ' || |
| 116 | + char.code > 0x7f || |
| 117 | + char == '/' || |
| 118 | + char == '\\' || |
| 119 | + char == '?' || |
| 120 | + char == '#' || |
| 121 | + char == '@' |
| 122 | + } || |
| 123 | + rawHost.contains('%') |
| 124 | + |
| 125 | + private fun isDeviceLocalHost(rawHost: String): Boolean { |
| 126 | + var host = |
| 127 | + rawHost |
| 128 | + .trim() |
| 129 | + .lowercase() |
| 130 | + .trim('[', ']') |
| 131 | + .trimEnd('.') |
| 132 | + host = host.substringBefore('%') |
| 133 | + if (host == "localhost" || host.endsWith(".localhost")) return true |
| 134 | + |
| 135 | + parseWebViewIpv4Address(host)?.let { address -> |
| 136 | + return address == 0L || ((address ushr 24) and 0xffL) == 127L |
| 137 | + } |
| 138 | + |
| 139 | + if (!host.contains(':') || !host.all(::isIpv6LiteralChar)) return false |
| 140 | + val address = runCatching { InetAddress.getByName(host).address }.getOrNull() ?: return false |
| 141 | + if (address.size == 4) { |
| 142 | + return address.all { it == 0.toByte() } || address[0] == 127.toByte() |
| 143 | + } |
| 144 | + if (address.size != 16) return false |
| 145 | + if (address.all { it == 0.toByte() }) return true |
| 146 | + if (address.copyOfRange(0, 15).all { it == 0.toByte() } && address[15] == 1.toByte()) return true |
| 147 | + |
| 148 | + val mappedPrefix = |
| 149 | + address.copyOfRange(0, 10).all { it == 0.toByte() } && |
| 150 | + address[10] == 0xff.toByte() && |
| 151 | + address[11] == 0xff.toByte() |
| 152 | + val compatiblePrefix = address.copyOfRange(0, 12).all { it == 0.toByte() } |
| 153 | + return (mappedPrefix || compatiblePrefix) && |
| 154 | + (address[12] == 127.toByte() || address.copyOfRange(12, 16).all { it == 0.toByte() }) |
| 155 | + } |
| 156 | + |
| 157 | + private fun isIpv6LiteralChar(char: Char): Boolean = char == ':' || char == '.' || char in '0'..'9' || char.lowercaseChar() in 'a'..'f' |
| 158 | + |
| 159 | + /** Matches Chromium/WebView's accepted decimal, octal, hex, and shorthand IPv4 forms. */ |
| 160 | + private fun parseWebViewIpv4Address(rawHost: String): Long? { |
| 161 | + val host = rawHost.trim().lowercase().trimEnd('.') |
| 162 | + if (host.isEmpty() || host.contains(':') || host.contains('%')) return null |
| 163 | + val parts = host.split('.') |
| 164 | + if (parts.size !in 1..4 || parts.any { it.isEmpty() }) return null |
| 165 | + val numbers = parts.map { parseWebViewIpv4Number(it) ?: return null } |
| 166 | + if (numbers.dropLast(1).any { it > 255L }) return null |
| 167 | + val lastMax = |
| 168 | + when (numbers.size) { |
| 169 | + 1 -> 0xffffffffL |
| 170 | + 2 -> 0x00ffffffL |
| 171 | + 3 -> 0x0000ffffL |
| 172 | + else -> 0xffL |
| 173 | + } |
| 174 | + val last = numbers.last() |
| 175 | + if (last > lastMax) return null |
| 176 | + return when (numbers.size) { |
| 177 | + 1 -> last |
| 178 | + 2 -> (numbers[0] shl 24) or last |
| 179 | + 3 -> (numbers[0] shl 24) or (numbers[1] shl 16) or last |
| 180 | + else -> (numbers[0] shl 24) or (numbers[1] shl 16) or (numbers[2] shl 8) or last |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private fun parseWebViewIpv4Number(raw: String): Long? { |
| 185 | + val normalized = raw.trim().lowercase() |
| 186 | + if (normalized.isEmpty()) return null |
| 187 | + val (digits, radix) = |
| 188 | + when { |
| 189 | + normalized.startsWith("0x") -> normalized.drop(2) to 16 |
| 190 | + normalized.length > 1 && normalized.startsWith("0") -> normalized.drop(1) to 8 |
| 191 | + else -> normalized to 10 |
| 192 | + } |
| 193 | + if (digits.isEmpty()) return 0L |
| 194 | + return digits.toLongOrNull(radix) |
| 195 | + } |
| 196 | +} |
0 commit comments