|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package io.appium.java_client.internal; |
| 16 | + |
| 17 | +import org.openqa.selenium.SessionNotCreatedException; |
| 18 | + |
| 19 | +import java.net.InetAddress; |
| 20 | +import java.net.URL; |
| 21 | +import java.net.UnknownHostException; |
| 22 | + |
| 23 | +/** |
| 24 | + * Validates URLs used with {@code overrideServerUrl} (for example after a {@code directConnect} |
| 25 | + * response). Refuses the override when any resolved address is loopback, link-local (including |
| 26 | + * typical cloud metadata IPv4 link-local space), unspecified, or multicast. |
| 27 | + * |
| 28 | + * <p>This is not a full "public internet only" policy: RFC 1918 private space, shared |
| 29 | + * address space ({@code 100.64.0.0/10}), IPv6 unique-local ({@code fc00::/7}), and other addresses |
| 30 | + * outside the checks above are still accepted. Stricter control belongs at the application or |
| 31 | + * network layer (allowlists, egress rules, etc.). |
| 32 | + */ |
| 33 | +public final class DirectConnectUrlSafety { |
| 34 | + |
| 35 | + private DirectConnectUrlSafety() { |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Ensures the given URL's host does not resolve to loopback, link-local, unspecified, or |
| 40 | + * multicast addresses (see class documentation for what is still allowed). |
| 41 | + * |
| 42 | + * @param url candidate server URL |
| 43 | + * @throws SessionNotCreatedException if the host is missing, cannot be resolved, or resolves |
| 44 | + * to any address in the disallowed categories |
| 45 | + */ |
| 46 | + public static void requireSafeOverrideTarget(URL url) throws SessionNotCreatedException { |
| 47 | + String host = url.getHost(); |
| 48 | + if (host == null || host.isEmpty()) { |
| 49 | + throw new SessionNotCreatedException( |
| 50 | + "Refusing to override the server URL: the URL must include a non-empty host."); |
| 51 | + } |
| 52 | + |
| 53 | + final InetAddress[] addresses; |
| 54 | + try { |
| 55 | + addresses = InetAddress.getAllByName(host); |
| 56 | + } catch (UnknownHostException e) { |
| 57 | + throw new SessionNotCreatedException( |
| 58 | + "Refusing to override the server URL: cannot resolve host '" + host + "'.", e); |
| 59 | + } |
| 60 | + |
| 61 | + for (InetAddress address : addresses) { |
| 62 | + if (isDisallowed(address)) { |
| 63 | + throw new SessionNotCreatedException(String.format( |
| 64 | + "Refusing to override the server URL: host '%s' resolves to %s, which is not " |
| 65 | + + "allowed (loopback, link-local, unspecified, or multicast address).", |
| 66 | + host, |
| 67 | + address.getHostAddress())); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private static boolean isDisallowed(InetAddress address) { |
| 73 | + return address.isLoopbackAddress() |
| 74 | + || address.isLinkLocalAddress() |
| 75 | + || address.isAnyLocalAddress() |
| 76 | + || address.isMulticastAddress(); |
| 77 | + } |
| 78 | +} |
0 commit comments