From 01f39c3abf58ee3e724b811b023457b3d7d8f6fd Mon Sep 17 00:00:00 2001 From: Arnab Nandy Date: Thu, 19 Nov 2026 23:36:14 +0530 Subject: [PATCH] Fix non-proxy host suffix wildcard sanitization Signed-off-by: Arnab Nandy --- sdk/clientcore/core/CHANGELOG.md | 3 ++ .../core/http/models/ProxyOptions.java | 16 +++----- .../core/http/models/ProxyOptionsTests.java | 41 +++++++++++++++++++ sdk/core/azure-core/CHANGELOG.md | 3 ++ .../com/azure/core/http/ProxyOptions.java | 16 +++----- .../azure/core/http/ProxyOptionsTests.java | 41 +++++++++++++++++++ 6 files changed, 100 insertions(+), 20 deletions(-) diff --git a/sdk/clientcore/core/CHANGELOG.md b/sdk/clientcore/core/CHANGELOG.md index 97e244bcd44e4..480671873abcd 100644 --- a/sdk/clientcore/core/CHANGELOG.md +++ b/sdk/clientcore/core/CHANGELOG.md @@ -14,6 +14,9 @@ ### Bugs Fixed +- Fixed `ProxyOptions` suffix wildcards in non-proxy hosts dropping a literal character, causing unintended hosts + to bypass the proxy, and fixed an exception for the `.*` entry. + - Restricted JDK HTTP proxy credentials to authentication challenges from the configured proxy host and port, preventing destination servers or other proxies from receiving them. - Fixed HTTP timeout resolution to prefer explicit builder values, then builder-specific configuration, then global diff --git a/sdk/clientcore/core/src/main/java/io/clientcore/core/http/models/ProxyOptions.java b/sdk/clientcore/core/src/main/java/io/clientcore/core/http/models/ProxyOptions.java index ad290e1e2243f..395ecc1b50e3d 100644 --- a/sdk/clientcore/core/src/main/java/io/clientcore/core/http/models/ProxyOptions.java +++ b/sdk/clientcore/core/src/main/java/io/clientcore/core/http/models/ProxyOptions.java @@ -106,6 +106,8 @@ public ProxyOptions setCredentials(String username, String password) { *

* 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 '*'}. + * For example, {@code storage1*} bypasses the proxy for hosts beginning with {@code storage1}, and + * {@code 10.*} bypasses it for hosts beginning with {@code 10.} (the period is literal). * * @param nonProxyHosts Hosts that bypass the proxy. * @return the updated ProxyOptions object @@ -441,22 +443,16 @@ private static String sanitizeNonProxyHosts(String[] nonProxyHosts) { */ if (sanitizedNonProxyHost.endsWith(".")) { suffixWildcard = ".*?"; - sanitizedNonProxyHost = sanitizedNonProxyHost.substring(0, sanitizedNonProxyHost.length() - 2); + sanitizedNonProxyHost = sanitizedNonProxyHost.substring(0, sanitizedNonProxyHost.length() - 1); } else if (sanitizedNonProxyHost.endsWith(".?")) { suffixWildcard = ".*?"; - sanitizedNonProxyHost = sanitizedNonProxyHost.substring(0, sanitizedNonProxyHost.length() - 3); + 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() - 2); - } else if (sanitizedNonProxyHost.endsWith(".*?")) { - suffixWildcard = ".*?"; - sanitizedNonProxyHost = sanitizedNonProxyHost.substring(0, sanitizedNonProxyHost.length() - 4); - } else if (sanitizedNonProxyHost.endsWith(".*")) { + } else if (sanitizedNonProxyHost.endsWith("*")) { suffixWildcard = ".*?"; - sanitizedNonProxyHost = sanitizedNonProxyHost.substring(0, sanitizedNonProxyHost.length() - 3); + sanitizedNonProxyHost = sanitizedNonProxyHost.substring(0, sanitizedNonProxyHost.length() - 1); } try { diff --git a/sdk/clientcore/core/src/test/java/io/clientcore/core/http/models/ProxyOptionsTests.java b/sdk/clientcore/core/src/test/java/io/clientcore/core/http/models/ProxyOptionsTests.java index 9617412cd76d5..11bd0fc52638e 100644 --- a/sdk/clientcore/core/src/test/java/io/clientcore/core/http/models/ProxyOptionsTests.java +++ b/sdk/clientcore/core/src/test/java/io/clientcore/core/http/models/ProxyOptionsTests.java @@ -471,6 +471,47 @@ private static Stream nonProxyHostsSupplier() { return argumentsList.stream(); } + @ParameterizedTest + @MethodSource("nonProxyHostSuffixSupplier") + @Execution(ExecutionMode.SAME_THREAD) + public void nonProxyHostSuffix(String entry, String host, boolean expected) { + ProxyOptions codeOptions + = new ProxyOptions(ProxyOptions.Type.HTTP, InetSocketAddress.createUnresolved("localhost", 7777)) + .setNonProxyHosts(entry); + Configuration javaConfiguration = createJavaEnvConfiguration(7777, null, null, entry, false); + Configuration explicitConfiguration = createExplicitConfiguration(7777, null, null, entry); + Configuration environmentConfiguration + = setJavaSystemProxyPrerequisiteToTrue("http://localhost:7777", entry.replace('|', ','), true); + + ProxyOptions[] options = new ProxyOptions[] { + codeOptions, + fromConfiguration(javaConfiguration), + fromConfiguration(explicitConfiguration), + fromConfiguration(environmentConfiguration) }; + for (ProxyOptions option : options) { + Pattern pattern = compile(option.getNonProxyHosts(), Pattern.CASE_INSENSITIVE); + assertEquals(expected, pattern.matcher(host).matches(), + () -> String.format("Expected Pattern '%s' to match '%s': %s.", pattern.pattern(), host, expected)); + } + } + + private static Stream nonProxyHostSuffixSupplier() { + return Stream.of(Arguments.of("storage1*", "storage1.example.com", true), + Arguments.of("storage1*", "storage2.example.com", false), Arguments.of("10.*", "10.0.0.1", true), + Arguments.of("10.*", "100.64.0.1", false), Arguments.of("storage1*?", "storage1.example.com", true), + Arguments.of("storage1*?", "storage2.example.com", false), + Arguments.of("storage1.?", "storage1.example.com", true), + Arguments.of("storage1.?", "storage2.example.com", false), Arguments.of("192.168.", "192.168.0.1", true), + Arguments.of("192.168.", "192.16.0.1", false), Arguments.of("storage1.*", "storage1.example.com", true), + Arguments.of("storage1.*", "storage1other.example.com", false), + Arguments.of("storage1.*?", "storage1.example.com", true), + Arguments.of("storage1.*?", "storage1other.example.com", false), Arguments.of(".*", "example.com", true), + Arguments.of(".*?", "example.com", true), Arguments.of("*", "example.com", true), + Arguments.of("*?", "example.com", true), Arguments.of("a*", "abc", true), Arguments.of("a*", "bcd", false), + Arguments.of("storage1*|10.*", "storage2.example.com", false), + Arguments.of("storage1*|10.*", "10.0.0.1", true)); + } + @Test public void sanitizeNoProxyDoesNotSplitEscapedCommas() { String noProxy = "noproxy\\,withescapedcomma"; diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index c147c389a65f5..62b3b59f16171 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -12,6 +12,9 @@ ### Bugs Fixed +- Fixed `ProxyOptions` suffix wildcards in non-proxy hosts dropping a literal character, causing unintended hosts + to bypass the proxy, and fixed an exception for the `.*` entry. + - Fixed `RetryPolicy` synchronous retry backoff not restoring the thread's interrupt status after catching `InterruptedException`, which caused callers to silently lose the interruption signal. - Fixed `FluxUtil.toFluxByteBuffer` emitting each partial read backed by an array of the full chunk size, which caused diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/http/ProxyOptions.java b/sdk/core/azure-core/src/main/java/com/azure/core/http/ProxyOptions.java index 2c4be8dd4c62c..b5bedbe2ba5ee 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/http/ProxyOptions.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/http/ProxyOptions.java @@ -131,6 +131,8 @@ public ProxyOptions setCredentials(String username, String password) { *

* 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 '*'}. + * For example, {@code storage1*} bypasses the proxy for hosts beginning with {@code storage1}, and + * {@code 10.*} bypasses it for hosts beginning with {@code 10.} (the period is literal). * * @param nonProxyHosts Hosts that bypass the proxy. * @return the updated ProxyOptions object @@ -454,22 +456,16 @@ private static String sanitizeNonProxyHosts(String[] nonProxyHosts) { */ if (sanitizedNonProxyHost.endsWith(".")) { suffixWildcard = ".*?"; - sanitizedNonProxyHost = sanitizedNonProxyHost.substring(0, sanitizedNonProxyHost.length() - 2); + sanitizedNonProxyHost = sanitizedNonProxyHost.substring(0, sanitizedNonProxyHost.length() - 1); } else if (sanitizedNonProxyHost.endsWith(".?")) { suffixWildcard = ".*?"; - sanitizedNonProxyHost = sanitizedNonProxyHost.substring(0, sanitizedNonProxyHost.length() - 3); + 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() - 2); - } else if (sanitizedNonProxyHost.endsWith(".*?")) { - suffixWildcard = ".*?"; - sanitizedNonProxyHost = sanitizedNonProxyHost.substring(0, sanitizedNonProxyHost.length() - 4); - } else if (sanitizedNonProxyHost.endsWith(".*")) { + } else if (sanitizedNonProxyHost.endsWith("*")) { suffixWildcard = ".*?"; - sanitizedNonProxyHost = sanitizedNonProxyHost.substring(0, sanitizedNonProxyHost.length() - 3); + sanitizedNonProxyHost = sanitizedNonProxyHost.substring(0, sanitizedNonProxyHost.length() - 1); } try { diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/http/ProxyOptionsTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/http/ProxyOptionsTests.java index 49bb3269bcfb6..05693b0555e67 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/http/ProxyOptionsTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/http/ProxyOptionsTests.java @@ -491,6 +491,47 @@ private static Stream nonProxyHostsSupplier() { return argumentsList.stream(); } + @ParameterizedTest + @MethodSource("nonProxyHostSuffixSupplier") + @Execution(ExecutionMode.SAME_THREAD) + public void nonProxyHostSuffix(String entry, String host, boolean expected) { + ProxyOptions codeOptions + = new ProxyOptions(ProxyOptions.Type.HTTP, InetSocketAddress.createUnresolved("localhost", 7777)) + .setNonProxyHosts(entry); + Configuration javaConfiguration = createJavaEnvConfiguration(null, null, entry, false); + Configuration explicitConfiguration = createExplicitConfiguration(null, null, entry); + Configuration environmentConfiguration + = setJavaSystemProxyPrerequisiteToTrue("http://localhost:7777", entry.replace('|', ','), true); + + ProxyOptions[] options = new ProxyOptions[] { + codeOptions, + fromConfiguration(javaConfiguration), + fromConfiguration(explicitConfiguration), + fromConfiguration(environmentConfiguration) }; + for (ProxyOptions option : options) { + Pattern pattern = compile(option.getNonProxyHosts(), Pattern.CASE_INSENSITIVE); + assertEquals(expected, pattern.matcher(host).matches(), + () -> String.format("Expected Pattern '%s' to match '%s': %s.", pattern.pattern(), host, expected)); + } + } + + private static Stream nonProxyHostSuffixSupplier() { + return Stream.of(Arguments.of("storage1*", "storage1.example.com", true), + Arguments.of("storage1*", "storage2.example.com", false), Arguments.of("10.*", "10.0.0.1", true), + Arguments.of("10.*", "100.64.0.1", false), Arguments.of("storage1*?", "storage1.example.com", true), + Arguments.of("storage1*?", "storage2.example.com", false), + Arguments.of("storage1.?", "storage1.example.com", true), + Arguments.of("storage1.?", "storage2.example.com", false), Arguments.of("192.168.", "192.168.0.1", true), + Arguments.of("192.168.", "192.16.0.1", false), Arguments.of("storage1.*", "storage1.example.com", true), + Arguments.of("storage1.*", "storage1other.example.com", false), + Arguments.of("storage1.*?", "storage1.example.com", true), + Arguments.of("storage1.*?", "storage1other.example.com", false), Arguments.of(".*", "example.com", true), + Arguments.of(".*?", "example.com", true), Arguments.of("*", "example.com", true), + Arguments.of("*?", "example.com", true), Arguments.of("a*", "abc", true), Arguments.of("a*", "bcd", false), + Arguments.of("storage1*|10.*", "storage2.example.com", false), + Arguments.of("storage1*|10.*", "10.0.0.1", true)); + } + @Test public void sanitizeNoProxyDoesNotSplitEscapedCommas() { String noProxy = "noproxy\\,withescapedcomma";