-
-
Notifications
You must be signed in to change notification settings - Fork 762
fix: Perform additional security checks on overrideServerUrl API #2408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+134
−3
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
src/main/java/io/appium/java_client/internal/DirectConnectUrlSafety.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.appium.java_client.internal; | ||
|
|
||
| import org.openqa.selenium.SessionNotCreatedException; | ||
|
|
||
| import java.net.InetAddress; | ||
| import java.net.URL; | ||
| import java.net.UnknownHostException; | ||
|
|
||
| /** | ||
| * Validates URLs used with {@code overrideServerUrl} (for example after a {@code directConnect} | ||
| * response). Refuses the override when any resolved address is loopback, link-local (including | ||
| * typical cloud metadata IPv4 link-local space), unspecified, or multicast. | ||
| * | ||
| * <p>This is not a full "public internet only" policy: RFC 1918 private space, shared | ||
| * address space ({@code 100.64.0.0/10}), IPv6 unique-local ({@code fc00::/7}), and other addresses | ||
| * outside the checks above are still accepted. Stricter control belongs at the application or | ||
| * network layer (allowlists, egress rules, etc.). | ||
| */ | ||
| public final class DirectConnectUrlSafety { | ||
|
|
||
| private DirectConnectUrlSafety() { | ||
| } | ||
|
|
||
| /** | ||
| * Ensures the given URL's host does not resolve to loopback, link-local, unspecified, or | ||
| * multicast addresses (see class documentation for what is still allowed). | ||
| * | ||
| * @param url candidate server URL | ||
| * @throws SessionNotCreatedException if the host is missing, cannot be resolved, or resolves | ||
| * to any address in the disallowed categories | ||
| */ | ||
| public static void requireSafeOverrideTarget(URL url) throws SessionNotCreatedException { | ||
| String host = url.getHost(); | ||
| if (host == null || host.isEmpty()) { | ||
| throw new SessionNotCreatedException( | ||
| "Refusing to override the server URL: the URL must include a non-empty host."); | ||
| } | ||
|
|
||
| final InetAddress[] addresses; | ||
| try { | ||
| addresses = InetAddress.getAllByName(host); | ||
| } catch (UnknownHostException e) { | ||
| throw new SessionNotCreatedException( | ||
| "Refusing to override the server URL: cannot resolve host '" + host + "'.", e); | ||
| } | ||
|
|
||
| for (InetAddress address : addresses) { | ||
| if (isDisallowed(address)) { | ||
| throw new SessionNotCreatedException(String.format( | ||
| "Refusing to override the server URL: host '%s' resolves to %s, which is not " | ||
| + "allowed (loopback, link-local, unspecified, or multicast address).", | ||
| host, | ||
| address.getHostAddress())); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static boolean isDisallowed(InetAddress address) { | ||
| return address.isLoopbackAddress() | ||
| || address.isLinkLocalAddress() | ||
| || address.isAnyLocalAddress() | ||
| || address.isMulticastAddress(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/test/java/io/appium/java_client/internal/DirectConnectUrlSafetyTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package io.appium.java_client.internal; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
| import org.openqa.selenium.SessionNotCreatedException; | ||
|
|
||
| import java.net.MalformedURLException; | ||
| import java.net.URL; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| class DirectConnectUrlSafetyTest { | ||
|
|
||
| @Test | ||
| void allowsDocumentationNetIpLiteral() throws MalformedURLException { | ||
| // RFC 5737 TEST-NET-3; parsed locally without DNS | ||
| assertDoesNotThrow(() -> DirectConnectUrlSafety.requireSafeOverrideTarget( | ||
| new URL("https://203.0.113.1/wd/hub"))); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "[{index}] {0}") | ||
| @ValueSource(strings = { | ||
| "https://127.0.0.1:4443/wd/hub", | ||
| "https://localhost:4443/wd/hub", | ||
| "https://169.254.169.254/wd/hub", | ||
| "https://[::1]:4443/wd/hub", | ||
| "https://0.0.0.0:4443/wd/hub", | ||
| "https://[::]:4443/wd/hub", | ||
| "https://224.0.0.1:4443/wd/hub", | ||
| }) | ||
| void rejectsDisallowedOverrideTargets(String urlSpec) throws MalformedURLException { | ||
| assertThrows(SessionNotCreatedException.class, | ||
| () -> DirectConnectUrlSafety.requireSafeOverrideTarget(new URL(urlSpec))); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isDisallowedrejects unspecified (any-local) and multicast addresses, but the new tests only cover loopback and link-local cases. Add unit tests that assert rejection for0.0.0.0/[::](unspecified) and a multicast literal (e.g.,224.0.0.1) to keep this security-sensitive logic fully covered.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed