From efae44594f93a6ee6410f223a722887af544bfe3 Mon Sep 17 00:00:00 2001 From: Morgan Kleene Date: Thu, 18 Sep 2025 09:39:07 -0400 Subject: [PATCH 1/4] fix: parse hostnames with no ports correctly --- .../platform/sdk/AddressNormalizer.java | 19 ++++++++++++++----- .../platform/sdk/AddressNormalizerTest.java | 2 ++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java b/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java index 8e616e9c..50c1c9b2 100644 --- a/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java +++ b/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java @@ -22,11 +22,20 @@ static String normalizeAddress(String urlString, boolean usePlaintext) { final String scheme = usePlaintext ? "http" : "https"; if (uri.getHost() == null) { - // if there is no host then we are likely dealing with a host and port - try { - uri = new URI(scheme, null, uri.getScheme(), Integer.parseInt(uri.getSchemeSpecificPart()), null, null, null); - } catch (URISyntaxException e) { - throw new SDKException("error trying to create URL for host and port[" + urlString + "]", e); + // if there is no host and no scheme, then we assume the input is a bare hostname + if (uri.getScheme() == null) { + try { + uri = new URI(scheme, null, uri.getSchemeSpecificPart(), -1, null, null, null); + } catch (URISyntaxException e) { + throw new SDKException("error trying to create URL for hostname [" + urlString + "]", e); + } + } else { + // otherwise, we have a scheme but no host, so we assume the scheme is actually the host and the SSP is the port + try { + uri = new URI(scheme, null, uri.getScheme(), Integer.parseInt(uri.getSchemeSpecificPart()), null, null, null); + } catch (URISyntaxException e) { + throw new SDKException("error trying to create URL for host and port[" + urlString + "]", e); + } } } final int port; diff --git a/sdk/src/test/java/io/opentdf/platform/sdk/AddressNormalizerTest.java b/sdk/src/test/java/io/opentdf/platform/sdk/AddressNormalizerTest.java index 079eb97a..98316a0d 100644 --- a/sdk/src/test/java/io/opentdf/platform/sdk/AddressNormalizerTest.java +++ b/sdk/src/test/java/io/opentdf/platform/sdk/AddressNormalizerTest.java @@ -14,6 +14,7 @@ void testAddressNormalizationWithHTTPSClient() { // default to https if no scheme is provided assertThat(normalizeAddress("example.org:1234", false)).isEqualTo("https://example.org:1234"); assertThat(normalizeAddress("ftp://example.org", false)).isEqualTo("https://example.org:443"); + assertThat(normalizeAddress("keycloak.vm", false)).isEqualTo("https://keycloak.vm:443"); } @Test @@ -23,5 +24,6 @@ void testAddressNormaliationWithInsecureHTTPClient() { // default to http if no scheme is provided assertThat(normalizeAddress("example.org:1234", true)).isEqualTo("http://example.org:1234"); assertThat(normalizeAddress("sftp://example.org", true)).isEqualTo("http://example.org:80"); + assertThat(normalizeAddress("keycloak.vm", true)).isEqualTo("http://keycloak.vm:80"); } } From e89d1e22e868383dd09a8df8085a130f49130ee0 Mon Sep 17 00:00:00 2001 From: Morgan Kleene Date: Thu, 18 Sep 2025 09:42:10 -0400 Subject: [PATCH 2/4] Update AddressNormalizer.java --- .../main/java/io/opentdf/platform/sdk/AddressNormalizer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java b/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java index 50c1c9b2..84889c40 100644 --- a/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java +++ b/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java @@ -22,7 +22,7 @@ static String normalizeAddress(String urlString, boolean usePlaintext) { final String scheme = usePlaintext ? "http" : "https"; if (uri.getHost() == null) { - // if there is no host and no scheme, then we assume the input is a bare hostname + // if there is no host and no scheme, then we assume the input is a hostname with no port or scheme if (uri.getScheme() == null) { try { uri = new URI(scheme, null, uri.getSchemeSpecificPart(), -1, null, null, null); From 6ef09ed1804c3d9b9d4ccd07a5d3152bf01ee500 Mon Sep 17 00:00:00 2001 From: Morgan Kleene Date: Thu, 18 Sep 2025 10:22:09 -0400 Subject: [PATCH 3/4] catch the numberformatexception --- .../io/opentdf/platform/sdk/AddressNormalizer.java | 2 +- .../io/opentdf/platform/sdk/AddressNormalizerTest.java | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java b/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java index 50c1c9b2..3396488b 100644 --- a/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java +++ b/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java @@ -33,7 +33,7 @@ static String normalizeAddress(String urlString, boolean usePlaintext) { // otherwise, we have a scheme but no host, so we assume the scheme is actually the host and the SSP is the port try { uri = new URI(scheme, null, uri.getScheme(), Integer.parseInt(uri.getSchemeSpecificPart()), null, null, null); - } catch (URISyntaxException e) { + } catch (URISyntaxException | NumberFormatException e) { throw new SDKException("error trying to create URL for host and port[" + urlString + "]", e); } } diff --git a/sdk/src/test/java/io/opentdf/platform/sdk/AddressNormalizerTest.java b/sdk/src/test/java/io/opentdf/platform/sdk/AddressNormalizerTest.java index 98316a0d..9fb3de9a 100644 --- a/sdk/src/test/java/io/opentdf/platform/sdk/AddressNormalizerTest.java +++ b/sdk/src/test/java/io/opentdf/platform/sdk/AddressNormalizerTest.java @@ -5,6 +5,7 @@ import static io.opentdf.platform.sdk.AddressNormalizer.normalizeAddress; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.junit.Assert.assertThrows; class AddressNormalizerTest { @@ -26,4 +27,13 @@ void testAddressNormaliationWithInsecureHTTPClient() { assertThat(normalizeAddress("sftp://example.org", true)).isEqualTo("http://example.org:80"); assertThat(normalizeAddress("keycloak.vm", true)).isEqualTo("http://keycloak.vm:80"); } + + @Test + void testAddressNormalizationWithInvalidPort() { + var thrown = assertThrows(SDKException.class, () -> normalizeAddress("example.org:notaport", true)); + assertThat(thrown.getMessage()).contains("example.org:notaport"); + + thrown = assertThrows(SDKException.class, () -> normalizeAddress("http://example.org:notaport", true)); + assertThat(thrown.getMessage()).contains("http://example.org:notaport"); + } } From ce808c9e13b5704801e33a8db5b6e94f4714d763 Mon Sep 17 00:00:00 2001 From: Morgan Kleene Date: Thu, 18 Sep 2025 10:32:55 -0400 Subject: [PATCH 4/4] Update sdk/src/test/java/io/opentdf/platform/sdk/AddressNormalizerTest.java Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- .../java/io/opentdf/platform/sdk/AddressNormalizerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/src/test/java/io/opentdf/platform/sdk/AddressNormalizerTest.java b/sdk/src/test/java/io/opentdf/platform/sdk/AddressNormalizerTest.java index 9fb3de9a..154bf1e7 100644 --- a/sdk/src/test/java/io/opentdf/platform/sdk/AddressNormalizerTest.java +++ b/sdk/src/test/java/io/opentdf/platform/sdk/AddressNormalizerTest.java @@ -5,7 +5,7 @@ import static io.opentdf.platform.sdk.AddressNormalizer.normalizeAddress; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.assertThrows; class AddressNormalizerTest {