From e7d281c578aa7f6c9ed1842d37212896b9eab082 Mon Sep 17 00:00:00 2001 From: Bole1155 <49867651+Fred1155@users.noreply.github.com> Date: Thu, 30 Jan 2025 15:51:54 -0800 Subject: [PATCH] allow single "=" as query (#5823) * allow single = as query * unit test added * changelog added * changed the behavior * minor change * more test cases added --- .../bugfix-AWSSDKforJavav2-466bf4c.json | 6 ++++ .../http/SdkHttpRequestResponseTest.java | 20 +++++++++++++ .../awssdk/utils/http/SdkHttpUtils.java | 1 + .../amazon/awssdk/utils/SdkHttpUtilsTest.java | 30 +++++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 .changes/next-release/bugfix-AWSSDKforJavav2-466bf4c.json diff --git a/.changes/next-release/bugfix-AWSSDKforJavav2-466bf4c.json b/.changes/next-release/bugfix-AWSSDKforJavav2-466bf4c.json new file mode 100644 index 000000000000..955d13a1637f --- /dev/null +++ b/.changes/next-release/bugfix-AWSSDKforJavav2-466bf4c.json @@ -0,0 +1,6 @@ +{ + "type": "bugfix", + "category": "AWS SDK for Java v2", + "contributor": "", + "description": "Fixed an issue in SdkHttpUtils used in SdkHttpFullRequest where constructing with a query string consisting of a single \"=\" would throw an ArrayIndexOutOfBoundsException." +} \ No newline at end of file diff --git a/http-client-spi/src/test/java/software/amazon/awssdk/http/SdkHttpRequestResponseTest.java b/http-client-spi/src/test/java/software/amazon/awssdk/http/SdkHttpRequestResponseTest.java index 09d87c647d23..d48837f43ee0 100644 --- a/http-client-spi/src/test/java/software/amazon/awssdk/http/SdkHttpRequestResponseTest.java +++ b/http-client-spi/src/test/java/software/amazon/awssdk/http/SdkHttpRequestResponseTest.java @@ -412,6 +412,16 @@ public void testSdkHttpFullRequestBuilderUriWithQueryParamWithoutValue() { assertThat(actual.getUri()).hasToString(expected); } + @Test + public void testSdkHttpFullRequestBuilderUriWithQueryParamWithoutValueWithEqual() { + final String original = "https://github.com/aws/aws-sdk-for-java-v2?foo="; + final String expected = "https://github.com/aws/aws-sdk-for-java-v2?foo"; + URI myUri = URI.create(original); + + SdkHttpRequest actual = SdkHttpRequest.builder().method(SdkHttpMethod.POST).uri(myUri).build(); + assertThat(actual.getUri()).hasToString(expected); + } + @Test public void testSdkHttpRequestBuilderNoQueryParams() { URI uri = URI.create("https://github.com/aws/aws-sdk-java-v2/issues/2034"); @@ -444,6 +454,16 @@ public void testSdkHttpRequestBuilderUriWithQueryParamWithoutValue() { assertThat(actual.getUri()).hasToString(expected); } + @Test + public void testSdkHttpRequestWithSingleEqualQuery() { + final String original = "http://example.com?="; + final String expected = "http://example.com?"; + URI myUri = URI.create(original); + + SdkHttpRequest actual = SdkHttpRequest.builder().method(SdkHttpMethod.POST).uri(myUri).build(); + assertThat(actual.getUri()).hasToString(expected); + } + private interface BuilderProxy { BuilderProxy setValue(String key, String value); diff --git a/utils/src/main/java/software/amazon/awssdk/utils/http/SdkHttpUtils.java b/utils/src/main/java/software/amazon/awssdk/utils/http/SdkHttpUtils.java index 51f03585c51b..02a402d0db4d 100644 --- a/utils/src/main/java/software/amazon/awssdk/utils/http/SdkHttpUtils.java +++ b/utils/src/main/java/software/amazon/awssdk/utils/http/SdkHttpUtils.java @@ -405,6 +405,7 @@ public static Map> uriParams(URI uri) { return splitQueryString(uri.getRawQuery()) .stream() .map(s -> s.split("=")) + .map(s -> s.length == 0 ? new String[] {""} : s) .map(s -> s.length == 1 ? new String[] { s[0], null } : s) .collect(groupingBy(a -> urlDecode(a[0]), mapping(a -> urlDecode(a[1]), toList()))); } diff --git a/utils/src/test/java/software/amazon/awssdk/utils/SdkHttpUtilsTest.java b/utils/src/test/java/software/amazon/awssdk/utils/SdkHttpUtilsTest.java index 7a3445004575..c32aa7094917 100644 --- a/utils/src/test/java/software/amazon/awssdk/utils/SdkHttpUtilsTest.java +++ b/utils/src/test/java/software/amazon/awssdk/utils/SdkHttpUtilsTest.java @@ -238,6 +238,36 @@ public void uriParams() throws URISyntaxException { entry("decoded&Part", Arrays.asList("equals=val"))); } + @Test + void uriParamsWithSingleEqualQuery() { + URI uri = URI.create("http://example.com?="); + Map> uriParams = SdkHttpUtils.uriParams(uri); + assertThat(uriParams).containsKey(""); + assertThat(uriParams.get("")) + .isNotNull() + .hasSize(1) + .containsNull(); + } + @Test + void uriParamsWithSingleEqualWithValueQuery() { + URI uri = URI.create("http://example.com?=nokeyvalue"); + Map> uriParams = SdkHttpUtils.uriParams(uri); + assertThat(uriParams).containsKey(""); + assertThat(uriParams.get("")) + .isNotNull() + .hasSize(1) + .contains("nokeyvalue"); + } + @Test + void uriParamsWithWithEmptyValueQuery() { + URI uri = URI.create("http://example.com?novaluekey="); + Map> uriParams = SdkHttpUtils.uriParams(uri); + assertThat(uriParams).containsKey("novaluekey"); + assertThat(uriParams.get("novaluekey")) + .hasSize(1) + .containsNull(); + } + @Test void parseSingleNonProxyHost(){ String singleHostName = "singleHost" ;