Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions sdk/clientcore/core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
- Fixed HTTP timeout resolution to prefer explicit builder values, then builder-specific configuration, then global
configuration, and finally SDK defaults. Global timeouts are resolved when needed rather than cached at class
initialization. Nonpositive JDK HTTP timeouts now disable the timeout, including the connection timeout.
- Fixed suffix wildcards in `ProxyOptions` non-proxy hosts removing the character before the wildcard, so `storage1*`
matched `storage2.example.com` and `10.*` matched `100.64.0.1`. This affected `setNonProxyHosts` as well as
`http.nonProxyHosts`, `http.proxy.non-proxy-hosts`, and `NO_PROXY`. Entries such as `.*` no longer throw
`StringIndexOutOfBoundsException`. ([#51341](https://github.com/Azure/azure-sdk-for-java/issues/51341))
- Fixed `ProxyOptions` non-proxy hosts with a leading or trailing dot, or a bracketed IPv6 address, matching other
hosts. `.example.com` now matches `example.com` and its subdomains but not `badexample.com`, `192.168.` no longer
matches `192.1689.0.1`, and `[::1]` is matched literally instead of as a regex character class.

### Other Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public final class ProxyOptions {

private static final Pattern UNESCAPED_PERIOD = Pattern.compile("(?<!\\\\)\\.");
private static final Pattern ANY = Pattern.compile("\\*");
private static final Pattern BRACKETED_IPV6_LITERAL = Pattern.compile("\\[[^\\[\\]]*:[^\\[\\]]*\\]");

private final InetSocketAddress address;
private final Type type;
Expand Down Expand Up @@ -105,7 +106,15 @@ public ProxyOptions setCredentials(String username, String password) {
* Sets the hosts which bypass the proxy.
* <p>
* The expected format of the passed string is a {@code '|'} delimited list of hosts which should bypass the proxy.
* Individual host strings may contain regex characters such as {@code '*'}.
* In each host, {@code '*'} is a wildcard and {@code '.'} is a literal dot. Other regex characters, such as
* character classes, keep their regex meaning. For example:
* <ul>
* <li>{@code *.example.com} matches any subdomain of {@code example.com}.</li>
* <li>{@code 10.*}, or {@code 10.}, matches hosts beginning with {@code 10.}, such as {@code 10.0.0.1} but not
* {@code 100.64.0.1}.</li>
* <li>{@code .example.com} matches {@code example.com} and its subdomains, but not {@code badexample.com}.</li>
* <li>{@code [::1]} matches the bracketed IPv6 address literally.</li>
* </ul>
*
* @param nonProxyHosts Hosts that bypass the proxy.
* @return the updated ProxyOptions object
Expand Down Expand Up @@ -412,69 +421,63 @@ private static String sanitizeNonProxyHosts(String[] nonProxyHosts) {
String sanitizedNonProxyHost = nonProxyHosts[i];

/*
* If the non-proxy host begins with either '.', '*', '.*', or any of the previous with a trailing '?'
* substring the non-proxy host and set the wildcard prefix.
* If the non-proxy host begins with '*', '.*', or either with a trailing '?', it matches any prefix. If it
* begins with '.', or '.?', it matches the domain and its subdomains, so '.example.com' matches
* 'example.com' and 'api.example.com' but not 'badexample.com'.
*/
if (sanitizedNonProxyHost.startsWith(".")) {
if (sanitizedNonProxyHost.startsWith(".*?")) {
prefixWildcard = ".*?";
sanitizedNonProxyHost = sanitizedNonProxyHost.substring(1);
} else if (sanitizedNonProxyHost.startsWith(".?")) {
prefixWildcard = ".*?";
sanitizedNonProxyHost = sanitizedNonProxyHost.substring(2);
} else if (sanitizedNonProxyHost.startsWith("*?")) {
sanitizedNonProxyHost = sanitizedNonProxyHost.substring(3);
} else if (sanitizedNonProxyHost.startsWith(".*") || sanitizedNonProxyHost.startsWith("*?")) {
prefixWildcard = ".*?";
sanitizedNonProxyHost = sanitizedNonProxyHost.substring(2);
} else if (sanitizedNonProxyHost.startsWith("*")) {
prefixWildcard = ".*?";
sanitizedNonProxyHost = sanitizedNonProxyHost.substring(1);
} else if (sanitizedNonProxyHost.startsWith(".*?")) {
prefixWildcard = ".*?";
sanitizedNonProxyHost = sanitizedNonProxyHost.substring(3);
} else if (sanitizedNonProxyHost.startsWith(".*")) {
prefixWildcard = ".*?";
} else if (sanitizedNonProxyHost.startsWith(".?")) {
prefixWildcard = "(?:.*?\\.)?";
sanitizedNonProxyHost = sanitizedNonProxyHost.substring(2);
} else if (sanitizedNonProxyHost.startsWith(".")) {
prefixWildcard = "(?:.*?\\.)?";
sanitizedNonProxyHost = sanitizedNonProxyHost.substring(1);
}

/*
* Same with the ending of the non-proxy host, if it has a suffix wildcard trim the non-proxy host and
* retain the suffix wildcard.
* If the non-proxy host ends with '*', '.', or either with a trailing '?', it matches any suffix. Only the
* '*' and '?' are trimmed. A '.' is a literal dot, so '10.*' matches '10.0.0.1' but not '100.64.0.1', and
* '192.168.' matches '192.168.0.1' but not '192.1689.0.1'.
*/
if (sanitizedNonProxyHost.endsWith(".")) {
suffixWildcard = ".*?";
sanitizedNonProxyHost = sanitizedNonProxyHost.substring(0, sanitizedNonProxyHost.length() - 2);
} else if (sanitizedNonProxyHost.endsWith(".?")) {
suffixWildcard = ".*?";
sanitizedNonProxyHost = sanitizedNonProxyHost.substring(0, sanitizedNonProxyHost.length() - 3);
} else if (sanitizedNonProxyHost.endsWith("*?")) {
suffixWildcard = ".*?";
sanitizedNonProxyHost = sanitizedNonProxyHost.substring(0, sanitizedNonProxyHost.length() - 3);
} else if (sanitizedNonProxyHost.endsWith("*")) {
if (sanitizedNonProxyHost.endsWith("*?")) {
suffixWildcard = ".*?";
sanitizedNonProxyHost = sanitizedNonProxyHost.substring(0, sanitizedNonProxyHost.length() - 2);
} else if (sanitizedNonProxyHost.endsWith(".*?")) {
} else if (sanitizedNonProxyHost.endsWith("*") || sanitizedNonProxyHost.endsWith(".?")) {
suffixWildcard = ".*?";
sanitizedNonProxyHost = sanitizedNonProxyHost.substring(0, sanitizedNonProxyHost.length() - 4);
} else if (sanitizedNonProxyHost.endsWith(".*")) {
sanitizedNonProxyHost = sanitizedNonProxyHost.substring(0, sanitizedNonProxyHost.length() - 1);
} else if (sanitizedNonProxyHost.endsWith(".")) {
suffixWildcard = ".*?";
sanitizedNonProxyHost = sanitizedNonProxyHost.substring(0, sanitizedNonProxyHost.length() - 3);
}

try {
// Sanitize the non-proxy host before any appending to prevent errant characters being added to the
// final response if the non-proxy host isn't a valid Pattern.
String attemptToSanitizeAsRegex = sanitizedNonProxyHost;
attemptToSanitizeAsRegex = UNESCAPED_PERIOD.matcher(attemptToSanitizeAsRegex).replaceAll("\\\\.");
attemptToSanitizeAsRegex = ANY.matcher(attemptToSanitizeAsRegex).replaceAll("\\.*?");
sanitizedNonProxyHost = Pattern.compile(attemptToSanitizeAsRegex).pattern();
} catch (PatternSyntaxException ex) {
/*
* Replace the non-proxy host with the sanitized value.
*
* The body of the non-proxy host is quoted to handle scenarios such a '127.0.0.1' or '*.somecloud.com'
* where without quoting the '.' in the string would be treated as the match any character instead of
* the literal '.' character.
*/
if (BRACKETED_IPV6_LITERAL.matcher(sanitizedNonProxyHost).matches()) {
// Match IPv6 literals such as '[::1]' as-is. As a regex, the brackets would be a character class.
sanitizedNonProxyHost = Pattern.quote(sanitizedNonProxyHost);
} else {
try {
// Sanitize the non-proxy host before any appending to prevent errant characters being added to
// the final response if the non-proxy host isn't a valid Pattern.
String attemptToSanitizeAsRegex = sanitizedNonProxyHost;
attemptToSanitizeAsRegex = UNESCAPED_PERIOD.matcher(attemptToSanitizeAsRegex).replaceAll("\\\\.");
attemptToSanitizeAsRegex = ANY.matcher(attemptToSanitizeAsRegex).replaceAll("\\.*?");
sanitizedNonProxyHost = Pattern.compile(attemptToSanitizeAsRegex).pattern();
} catch (PatternSyntaxException ex) {
/*
* Replace the non-proxy host with the sanitized value.
*
* The body of the non-proxy host is quoted to handle scenarios such a '127.0.0.1' or
* '*.somecloud.com' where without quoting the '.' in the string would be treated as the match any
* character instead of the literal '.' character.
*/
sanitizedNonProxyHost = Pattern.quote(sanitizedNonProxyHost);
}
}

sanitizedBuilder.append("(")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Stream;
Expand Down Expand Up @@ -485,6 +487,59 @@ public void sanitizeJavaHttpNonProxyHostsDoesNotSplitEscapedPipes() {
assertEquals("(" + nonProxyHosts + ")", ProxyOptions.sanitizeNoProxy(nonProxyHosts));
}

/**
* Tests that wildcards, leading and trailing dots, and IPv6 literals in non-proxy hosts only match the intended
* hosts, however the non-proxy hosts are configured. Hosts are matched the way HTTP clients match them: the whole
* host, ignoring case.
*/
@ParameterizedTest
@MethodSource("nonProxyHostsMatchOnlyIntendedHostsSupplier")
public void nonProxyHostsMatchOnlyIntendedHosts(String nonProxyHost, List<String> bypassedHosts,
List<String> proxiedHosts) {
String[] sanitizedNonProxyHosts = {
new ProxyOptions(ProxyOptions.Type.HTTP, InetSocketAddress.createUnresolved("localhost", 7777))
.setNonProxyHosts(nonProxyHost)
.getNonProxyHosts(),
fromConfiguration(createJavaEnvConfiguration(443, null, null, nonProxyHost, true), true).getNonProxyHosts(),
fromConfiguration(createExplicitConfiguration(443, null, null, nonProxyHost), true).getNonProxyHosts(),
fromConfiguration(setJavaSystemProxyPrerequisiteToTrue("http://localhost:7777", nonProxyHost, true), true)
.getNonProxyHosts() };

for (String sanitizedNonProxyHost : sanitizedNonProxyHosts) {
Pattern pattern = compile(sanitizedNonProxyHost, Pattern.CASE_INSENSITIVE);

for (String host : bypassedHosts) {
assertTrue(pattern.matcher(host).matches(), () -> String
.format("Expected '%s' (Pattern '%s') to match '%s'.", nonProxyHost, pattern.pattern(), host));
}

for (String host : proxiedHosts) {
assertFalse(pattern.matcher(host).matches(), () -> String
.format("Expected '%s' (Pattern '%s') not to match '%s'.", nonProxyHost, pattern.pattern(), host));
}
}
}

private static Stream<Arguments> nonProxyHostsMatchOnlyIntendedHostsSupplier() {
List<String> anyHost = Arrays.asList("example.com", "10.0.0.1", "[::1]");

return Stream.of(
Arguments.of("storage1*", Arrays.asList("storage1.example.com"), Arrays.asList("storage2.example.com")),
Arguments.of("storage1*?", Arrays.asList("storage1.example.com"), Arrays.asList("storage2.example.com")),
Arguments.of("storage1.?", Arrays.asList("storage1.example.com"), Arrays.asList("storage2.example.com")),
Arguments.of("storage1.*", Arrays.asList("storage1.example.com"),
Arrays.asList("storage1other.example.com")),
Arguments.of("10.*", Arrays.asList("10.0.0.1"), Arrays.asList("100.64.0.1")),
Arguments.of("192.168.", Arrays.asList("192.168.0.1"), Arrays.asList("192.16.0.1", "192.1689.0.1")),
Arguments.of(".example.com", Arrays.asList("example.com", "api.example.com"),
Arrays.asList("badexample.com")),
Arguments.of("[::1]", Arrays.asList("[::1]"), Arrays.asList("1")),
Arguments.of("[2001:db8::1]*", Arrays.asList("[2001:db8::1]"), Arrays.asList("2evil.example.com")),
Arguments.of(".*", anyHost, Collections.emptyList()), Arguments.of("*", anyHost, Collections.emptyList()),
Arguments.of("*?", anyHost, Collections.emptyList()),
Arguments.of(".*?", anyHost, Collections.emptyList()));
}

private static Configuration setJavaSystemProxyPrerequisiteToTrue(String proxy, String nonProxyHosts,
boolean isHttps) {
TestConfigurationSource testSource = new TestConfigurationSource().put(JAVA_SYSTEM_PROXY_PREREQUISITE, "true")
Expand Down
5 changes: 5 additions & 0 deletions sdk/core/azure-core-http-vertx/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
- Added `maximumHttpVersion` to select HTTP/1.1 or HTTP/2 with HTTP/1.1 fallback without changing defaults or mutating
caller-supplied Vert.x options.
- Added support for `HttpClientOptions.setMaximumHttpVersion` through the HTTP client provider.
- Added support for azure-core's `AZURE_NON_PROXY_PARSER_VERSION` setting. With version `2`, non-proxy hosts are
matched the same way as in the other HTTP clients, and the proxy is set on each request to a host that doesn't match
them. Version `1`, the default, still converts them to Vert.x's non-proxy host list, which can bypass the proxy for
the wrong hosts. For example, `10.*` bypasses the proxy for `100.64.0.1`, and entries without a wildcard are compared
case-sensitively.

### Breaking Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Pattern;

import static com.azure.core.http.vertx.implementation.VertxUtils.resetRequest;

Expand Down Expand Up @@ -90,6 +91,8 @@ class VertxHttpClient implements HttpClient {

final io.vertx.core.http.HttpClient client;
final HttpClientOptions buildOptions;
private final io.vertx.core.net.ProxyOptions proxyOptions;
private final Pattern nonProxyHosts;
private final Duration responseTimeout;
private final Duration writeTimeout;
private final ScheduledExecutorService writeTimeoutExecutor;
Expand All @@ -101,12 +104,18 @@ class VertxHttpClient implements HttpClient {
* @param client The Vert.x {@link io.vertx.core.http.HttpClient}
*/
VertxHttpClient(io.vertx.core.http.HttpClient client, HttpClientOptions buildOptions, Duration responseTimeout) {
this(client, buildOptions, responseTimeout, Duration.ZERO);
this(client, buildOptions, null, null, responseTimeout, Duration.ZERO);
}

VertxHttpClient(io.vertx.core.http.HttpClient client, HttpClientOptions buildOptions, Duration responseTimeout,
/*
* proxyOptions is set on each request whose host doesn't match nonProxyHosts. When it's null, requests use the
* proxy configured in buildOptions, if any.
*/
VertxHttpClient(io.vertx.core.http.HttpClient client, HttpClientOptions buildOptions,
io.vertx.core.net.ProxyOptions proxyOptions, Pattern nonProxyHosts, Duration responseTimeout,
Duration writeTimeout) {
this(client, buildOptions, responseTimeout, writeTimeout, SharedExecutorService.getInstance());
this(client, buildOptions, proxyOptions, nonProxyHosts, responseTimeout, writeTimeout,
SharedExecutorService.getInstance(), EXPECT_CONTINUE_TIMEOUT);
}

VertxHttpClient(io.vertx.core.http.HttpClient client, HttpClientOptions buildOptions, Duration responseTimeout,
Expand All @@ -116,9 +125,18 @@ class VertxHttpClient implements HttpClient {

// Visible for testing, so tests can make the body follow 100 Continue regardless of how slow the agent is.
VertxHttpClient(io.vertx.core.http.HttpClient client, HttpClientOptions buildOptions, Duration responseTimeout,
Duration writeTimeout, ScheduledExecutorService writeTimeoutExecutor, Duration expectContinueTimeout) {
this(client, buildOptions, null, null, responseTimeout, writeTimeout, writeTimeoutExecutor,
expectContinueTimeout);
}

private VertxHttpClient(io.vertx.core.http.HttpClient client, HttpClientOptions buildOptions,
io.vertx.core.net.ProxyOptions proxyOptions, Pattern nonProxyHosts, Duration responseTimeout,
Duration writeTimeout, ScheduledExecutorService writeTimeoutExecutor, Duration expectContinueTimeout) {
this.client = Objects.requireNonNull(client, "client cannot be null");
this.buildOptions = buildOptions;
this.proxyOptions = proxyOptions;
this.nonProxyHosts = nonProxyHosts;
this.responseTimeout = responseTimeout;
this.writeTimeout = writeTimeout;
this.writeTimeoutExecutor = Objects.requireNonNull(writeTimeoutExecutor, "writeTimeoutExecutor cannot be null");
Expand Down Expand Up @@ -171,7 +189,8 @@ private CompletableFuture<HttpResponse> sendInternal(HttpRequest request, Contex
ProgressReporter progressReporter = Contexts.with(context).getHttpRequestProgressReporter();

RequestOptions options = new RequestOptions().setMethod(HttpMethod.valueOf(request.getHttpMethod().name()))
.setAbsoluteURI(request.getUrl());
.setAbsoluteURI(request.getUrl())
.setProxyOptions(getProxyOptions(request.getUrl().getHost()));

// This is the shared design for sending a request and receiving a response in Vert.x. The design relies on
// using a Vert.x Promise and the handler methods to control the flow of the request-response cycle. The Promise
Expand Down Expand Up @@ -269,6 +288,18 @@ private CompletableFuture<HttpResponse> sendInternal(HttpRequest request, Contex
return requestFuture;
}

/*
* Gets the proxy for a request to the host, or null to use the proxy configured in buildOptions, if any. Hosts are
* matched the same way as the other HTTP clients: the whole host, ignoring case.
*/
io.vertx.core.net.ProxyOptions getProxyOptions(String host) {
if (proxyOptions == null || (nonProxyHosts != null && nonProxyHosts.matcher(host).matches())) {
return null;
}

return proxyOptions;
}

private void sendBody(ContextView contextView, HttpRequest azureRequest, ProgressReporter progressReporter,
HttpClientRequest vertxRequest, Promise<HttpResponse> promise, WriteTimeout writeWatcher) {
BinaryData body = azureRequest.getBodyAsBinaryData();
Expand Down
Loading
Loading