From 04774f5be397abe91035ffceb5bea5533677320b Mon Sep 17 00:00:00 2001 From: Ian Botsford <83236726+ianbotsf@users.noreply.github.com> Date: Thu, 16 May 2024 15:35:50 -0700 Subject: [PATCH] fix: respect prefix/suffix wildcards in nonProxyHosts (#1093) --- .../cbfc3f67-c8b3-49ad-8e06-0e9bdece01e9.json | 8 ++++++++ .../http/engine/EnvironmentProxySelector.kt | 4 ++++ .../http/engine/EnvironmentProxySelectorTest.kt | 16 ++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 .changes/cbfc3f67-c8b3-49ad-8e06-0e9bdece01e9.json diff --git a/.changes/cbfc3f67-c8b3-49ad-8e06-0e9bdece01e9.json b/.changes/cbfc3f67-c8b3-49ad-8e06-0e9bdece01e9.json new file mode 100644 index 000000000..e484bb175 --- /dev/null +++ b/.changes/cbfc3f67-c8b3-49ad-8e06-0e9bdece01e9.json @@ -0,0 +1,8 @@ +{ + "id": "cbfc3f67-c8b3-49ad-8e06-0e9bdece01e9", + "type": "bugfix", + "description": "Respect `*` wildcard in `http.nonProxyHosts` when used as prefix or suffix", + "issues": [ + "smithy-lang/smithy-kotlin#1092" + ] +} diff --git a/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/engine/EnvironmentProxySelector.kt b/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/engine/EnvironmentProxySelector.kt index 4d89d0146..253a5e6a3 100644 --- a/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/engine/EnvironmentProxySelector.kt +++ b/runtime/protocol/http-client/common/src/aws/smithy/kotlin/runtime/http/engine/EnvironmentProxySelector.kt @@ -105,6 +105,10 @@ internal data class NonProxyHost(val hostMatch: String, val port: Int? = null) { val name = url.host.toString() + // handle start/end wildcard cases + if (hostMatch.endsWith("*")) return name.startsWith(hostMatch.removeSuffix("*")) + if (hostMatch.startsWith("*")) return name.endsWith(hostMatch.removePrefix("*")) + if (hostMatch.length > name.length) return false val match = name.endsWith(hostMatch) diff --git a/runtime/protocol/http-client/common/test/aws/smithy/kotlin/runtime/http/engine/EnvironmentProxySelectorTest.kt b/runtime/protocol/http-client/common/test/aws/smithy/kotlin/runtime/http/engine/EnvironmentProxySelectorTest.kt index aa55651d6..0aa4db01c 100644 --- a/runtime/protocol/http-client/common/test/aws/smithy/kotlin/runtime/http/engine/EnvironmentProxySelectorTest.kt +++ b/runtime/protocol/http-client/common/test/aws/smithy/kotlin/runtime/http/engine/EnvironmentProxySelectorTest.kt @@ -41,6 +41,8 @@ class EnvironmentProxySelectorTest { "https.proxyPort" to "80", ) private val httpProxyProps = mapOf("http.proxyHost" to "test.proxy.aws") + private val wildcardPrefixNonProxyProps = mapOf("http.nonProxyHosts" to "*amazon.com") + private val wildcardSuffixNonProxyProps = mapOf("http.nonProxyHosts" to "amazon.co*") private val expectedProxyConfig = ProxyConfig.Http(Url.parse("http://test.proxy.aws")) @@ -58,6 +60,20 @@ class EnvironmentProxySelectorTest { TestCase(ProxyConfig.Direct, props = mapOf("http.nonProxyHosts" to "aws.amazon.com") + httpsProxyProps), TestCase(ProxyConfig.Direct, props = mapOf("http.nonProxyHosts" to ".amazon.com") + httpsProxyProps), + // no proxy w/ wildcards + + TestCase(ProxyConfig.Direct, "https://aws.amazon.com", props = wildcardPrefixNonProxyProps + httpsProxyProps), + TestCase(ProxyConfig.Direct, "https://blamazon.com", props = wildcardPrefixNonProxyProps + httpsProxyProps), + TestCase(ProxyConfig.Direct, "https://amazon.com", props = wildcardPrefixNonProxyProps + httpsProxyProps), + TestCase(expectedProxyConfig, "https://aws.com", props = wildcardPrefixNonProxyProps + httpsProxyProps), + TestCase(expectedProxyConfig, "https://amazon.com.br", props = wildcardPrefixNonProxyProps + httpsProxyProps), + + TestCase(ProxyConfig.Direct, "https://amazon.com", props = wildcardSuffixNonProxyProps + httpsProxyProps), + TestCase(ProxyConfig.Direct, "https://amazon.com.br", props = wildcardSuffixNonProxyProps + httpsProxyProps), + TestCase(ProxyConfig.Direct, "https://amazon.co.jp", props = wildcardSuffixNonProxyProps + httpsProxyProps), + TestCase(expectedProxyConfig, "https://aws.amazon.com", props = wildcardSuffixNonProxyProps + httpsProxyProps), + TestCase(expectedProxyConfig, "https://blamazon.com", props = wildcardSuffixNonProxyProps + httpsProxyProps), + // multiple no proxy hosts normalization TestCase(ProxyConfig.Direct, env = mapOf("no_proxy" to "example.com,.amazon.com") + httpsProxyEnv), TestCase(ProxyConfig.Direct, props = mapOf("http.noProxyHosts" to "example.com|.amazon.com") + httpsProxyProps),