generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 184
Fixing regex bypass issue #4336
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
Open
zane-neo
wants to merge
4
commits into
opensearch-project:main
Choose a base branch
from
zane-neo:fix-regex-bypass
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 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
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
111 changes: 111 additions & 0 deletions
111
common/src/main/java/org/opensearch/ml/common/httpclient/ValidatingHttpClient.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,111 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.httpclient; | ||
|
||
import java.net.Inet4Address; | ||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.util.Arrays; | ||
import java.util.Locale; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import lombok.extern.log4j.Log4j2; | ||
import software.amazon.awssdk.http.async.AsyncExecuteRequest; | ||
import software.amazon.awssdk.http.async.SdkAsyncHttpClient; | ||
|
||
@Log4j2 | ||
public class ValidatingHttpClient implements SdkAsyncHttpClient { | ||
|
||
private final SdkAsyncHttpClient delegate; | ||
private final AtomicBoolean connectorPrivateIpEnabled; | ||
|
||
protected ValidatingHttpClient(SdkAsyncHttpClient client, AtomicBoolean connectorPrivateIpEnabled) { | ||
this.delegate = client; | ||
this.connectorPrivateIpEnabled = connectorPrivateIpEnabled; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Void> execute(AsyncExecuteRequest request) { | ||
String protocol = request.request().protocol(); | ||
String host = request.request().host(); | ||
int port = request.request().port(); | ||
try { | ||
validate(protocol, host, port, connectorPrivateIpEnabled); | ||
return delegate.execute(request); | ||
} catch (Exception e) { | ||
log.error("Failed to validate request!", e); | ||
throw new IllegalArgumentException(e.getMessage(), e); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
delegate.close(); | ||
} | ||
|
||
/** | ||
* Validate the input parameters, such as protocol, host and port. | ||
* @param protocol The protocol supported in remote inference, currently only http and https are supported. | ||
* @param host The host name of the remote inference server, host must be a valid ip address or domain name and must not be localhost. | ||
* @param port The port number of the remote inference server, port number must be in range [0, 65536]. | ||
* @param connectorPrivateIpEnabled The port number of the remote inference server, port number must be in range [0, 65536]. | ||
* @throws UnknownHostException Allow to use private IP or not. | ||
*/ | ||
public void validate(String protocol, String host, int port, AtomicBoolean connectorPrivateIpEnabled) throws UnknownHostException { | ||
if (protocol != null && !"http".equalsIgnoreCase(protocol) && !"https".equalsIgnoreCase(protocol)) { | ||
log.error("Remote inference protocol is not http or https: {}", protocol); | ||
throw new IllegalArgumentException("Protocol is not http or https: " + protocol); | ||
} | ||
// When port is not specified, the default port is -1, and we need to set it to 80 or 443 based on protocol. | ||
if (port == -1) { | ||
if (protocol == null || "http".equals(protocol.toLowerCase(Locale.getDefault()))) { | ||
port = 80; | ||
} else { | ||
port = 443; | ||
} | ||
} | ||
if (port < 0 || port > 65536) { | ||
log.error("Remote inference port out of range: {}", port); | ||
throw new IllegalArgumentException("Port out of range: " + port); | ||
} | ||
validateIp(host, connectorPrivateIpEnabled); | ||
} | ||
|
||
private void validateIp(String hostName, AtomicBoolean connectorPrivateIpEnabled) throws UnknownHostException { | ||
InetAddress[] addresses = InetAddress.getAllByName(hostName); | ||
if (connectorPrivateIpEnabled != null && !connectorPrivateIpEnabled.get() && hasPrivateIpAddress(addresses)) { | ||
log.error("Remote inference host name has private ip address: {}", hostName); | ||
throw new IllegalArgumentException("Remote inference host name has private ip address: " + hostName); | ||
} | ||
} | ||
|
||
private boolean hasPrivateIpAddress(InetAddress[] ipAddress) { | ||
for (InetAddress ip : ipAddress) { | ||
if (ip instanceof Inet4Address) { | ||
byte[] bytes = ip.getAddress(); | ||
if (bytes.length != 4) { | ||
return true; | ||
} else { | ||
if (isPrivateIPv4(bytes)) { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
return Arrays.stream(ipAddress).anyMatch(x -> x.isSiteLocalAddress() || x.isLoopbackAddress() || x.isAnyLocalAddress()); | ||
} | ||
|
||
private boolean isPrivateIPv4(byte[] bytes) { | ||
int first = bytes[0] & 0xff; | ||
int second = bytes[1] & 0xff; | ||
|
||
// 127.0.0.1, 10.x.x.x, 172.16-31.x.x, 192.168.x.x, 169.254.x.x | ||
return (first == 10) | ||
|| (first == 172 && second >= 16 && second <= 31) | ||
|| (first == 192 && second == 168) | ||
|| (first == 169 && second == 254); | ||
} | ||
} |
Oops, something went wrong.
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.
can we use bool here? what's the reason to use atomicBoolean
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.
Agree that boolean is better, I tried to change all the occurrence of this
connectorPrivateIpEnabled
to boolean all the way back to where it's defined, but there's more UTs need to be changed. But changing to boolean is correct direction, let me try again this change.