|
| 1 | +package com.tinyengine.it.mcp.utils; |
| 2 | + |
| 3 | +import com.tinyengine.it.common.exception.ServiceException; |
| 4 | +import com.tinyengine.it.config.OpenAIConfig; |
| 5 | +import org.springframework.stereotype.Component; |
| 6 | + |
| 7 | +import java.net.*; |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Set; |
| 11 | +@Component |
| 12 | +public class UrlValidateUtil { |
| 13 | + private static final Set<String> LOOPBACK_HOSTS = Set.of("localhost", "127.0.0.1", "::1", "[::1]"); |
| 14 | + private final OpenAIConfig config; |
| 15 | + |
| 16 | + public UrlValidateUtil(OpenAIConfig config) { |
| 17 | + this.config = config; |
| 18 | + } |
| 19 | + |
| 20 | + |
| 21 | + public void validateFinalUrl(String finalUrl) { |
| 22 | + URI uri; |
| 23 | + try { |
| 24 | + uri = new URI(finalUrl); |
| 25 | + } catch (URISyntaxException e) { |
| 26 | + throw new ServiceException("400", "Invalid baseUrl format"); |
| 27 | + } |
| 28 | + |
| 29 | + String host = uri.getHost(); |
| 30 | + if (host == null || host.isEmpty()) { |
| 31 | + throw new ServiceException("400", "Invalid baseUrl: missing host"); |
| 32 | + } |
| 33 | + |
| 34 | + boolean isLoopback = LOOPBACK_HOSTS.contains(host.toLowerCase()); |
| 35 | + |
| 36 | + List<String> allowedHosts = config.getAllowedHosts(); |
| 37 | + |
| 38 | + if (allowedHosts == null || allowedHosts.isEmpty()) { |
| 39 | + if (!config.isAllowAnyHost()) { |
| 40 | + throw new ServiceException("500", "No AI allowed hosts configured"); |
| 41 | + } |
| 42 | + |
| 43 | + enforceHttpsAndIpCheck(uri, host); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + boolean matched = allowedHosts.stream() |
| 48 | + .anyMatch(allowed -> allowed.equalsIgnoreCase(host)); |
| 49 | + if (!matched) { |
| 50 | + throw new ServiceException("400", |
| 51 | + "Host not allowed: " + host + ". Allowed hosts: " + allowedHosts); |
| 52 | + } |
| 53 | + |
| 54 | + if (isLoopback) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + enforceHttpsAndIpCheck(uri, host); |
| 59 | + } |
| 60 | + |
| 61 | + void enforceHttpsAndIpCheck(URI uri, String host) { |
| 62 | + String scheme = uri.getScheme(); |
| 63 | + if (scheme == null || !"https".equalsIgnoreCase(scheme)) { |
| 64 | + throw new ServiceException("400", "Only HTTPS protocol is allowed for custom baseUrl"); |
| 65 | + } |
| 66 | + |
| 67 | + try { |
| 68 | + InetAddress[] addresses = resolveHostAddresses(host); |
| 69 | + boolean hasBlockedAddress = Arrays.stream(addresses).anyMatch(this::isBlockedAddress); |
| 70 | + if (hasBlockedAddress) { |
| 71 | + throw new ServiceException("400", "Internal network addresses are not allowed"); |
| 72 | + } |
| 73 | + } catch (UnknownHostException e) { |
| 74 | + throw new ServiceException("400", "Unable to resolve host: " + host); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + InetAddress[] resolveHostAddresses(String host) throws UnknownHostException { |
| 79 | + return InetAddress.getAllByName(host); |
| 80 | + } |
| 81 | + |
| 82 | + boolean isBlockedAddress(InetAddress address) { |
| 83 | + if (address.isLoopbackAddress() |
| 84 | + || address.isSiteLocalAddress() |
| 85 | + || address.isLinkLocalAddress() |
| 86 | + || address.isAnyLocalAddress() |
| 87 | + || address.isMulticastAddress()) { |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + if (address instanceof Inet4Address) { |
| 92 | + return isBlockedIpv4((Inet4Address) address); |
| 93 | + } |
| 94 | + if (address instanceof Inet6Address) { |
| 95 | + return isBlockedIpv6((Inet6Address) address); |
| 96 | + } |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + private boolean isBlockedIpv4(Inet4Address address) { |
| 101 | + byte[] octets = address.getAddress(); |
| 102 | + int first = octets[0] & 0xFF; |
| 103 | + int second = octets[1] & 0xFF; |
| 104 | + int third = octets[2] & 0xFF; |
| 105 | + |
| 106 | + if (first == 0) { |
| 107 | + return true; |
| 108 | + } |
| 109 | + if (first == 100 && second >= 64 && second <= 127) { |
| 110 | + return true; |
| 111 | + } |
| 112 | + if (first == 192 && second == 0 && third == 0) { |
| 113 | + return true; |
| 114 | + } |
| 115 | + if (first == 192 && second == 0 && third == 2) { |
| 116 | + return true; |
| 117 | + } |
| 118 | + if (first == 198 && (second == 18 || second == 19)) { |
| 119 | + return true; |
| 120 | + } |
| 121 | + if (first == 198 && second == 51 && third == 100) { |
| 122 | + return true; |
| 123 | + } |
| 124 | + if (first == 203 && second == 0 && third == 113) { |
| 125 | + return true; |
| 126 | + } |
| 127 | + return first >= 240; |
| 128 | + } |
| 129 | + |
| 130 | + private boolean isBlockedIpv6(Inet6Address address) { |
| 131 | + byte[] octets = address.getAddress(); |
| 132 | + int first = octets[0] & 0xFF; |
| 133 | + int second = octets[1] & 0xFF; |
| 134 | + |
| 135 | + if ((first & 0xFE) == 0xFC) { |
| 136 | + return true; |
| 137 | + } |
| 138 | + if (first == 0x20 && second == 0x01) { |
| 139 | + int third = octets[2] & 0xFF; |
| 140 | + int fourth = octets[3] & 0xFF; |
| 141 | + if (third == 0x0D && fourth == 0xB8) { |
| 142 | + return true; |
| 143 | + } |
| 144 | + } |
| 145 | + return first == 0xFF; |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments