From 30ad8ed6019555f10cd79fc954e1e532726ebf6b Mon Sep 17 00:00:00 2001 From: Morgan Kleene Date: Sat, 20 Sep 2025 07:26:26 -0400 Subject: [PATCH 1/4] and ips --- .../platform/sdk/AddressNormalizer.java | 28 ++++++++++++++----- .../platform/sdk/AddressNormalizerTest.java | 4 +++ 2 files changed, 25 insertions(+), 7 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 48a86ac7..477f81c3 100644 --- a/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java +++ b/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java @@ -6,6 +6,8 @@ import java.net.URI; import java.net.URISyntaxException; +import static java.lang.String.format; + class AddressNormalizer { private static final Logger logger = LoggerFactory.getLogger(AddressNormalizer.class); @@ -13,14 +15,9 @@ private AddressNormalizer(){ } static String normalizeAddress(String urlString, boolean usePlaintext) { - URI uri; - try { - uri = new URI(urlString); - } catch (URISyntaxException e) { - throw new SDKException("error trying to parse URL [" + urlString + "]", e); - } - final String scheme = usePlaintext ? "http" : "https"; + URI uri = getUri(urlString); + if (uri.getHost() == null) { // 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) { @@ -53,4 +50,21 @@ static String normalizeAddress(String urlString, boolean usePlaintext) { throw new SDKException("error creating KAS address", e); } } + + private static URI getUri(String urlString) { + URISyntaxException initialThrown = null; + try { + return new URI(urlString); + } catch (URISyntaxException e) { + // this can happen if there is no schema and the hostname is not a valid scheme, like if we havea + // an IP adddress + initialThrown = e; + } + + try { + return new URI(format("fake://%s", urlString)); + } catch (URISyntaxException e) { + throw new SDKException("error parsing url [" + urlString + "]", initialThrown); + } + } } 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 154bf1e7..f48022e3 100644 --- a/sdk/src/test/java/io/opentdf/platform/sdk/AddressNormalizerTest.java +++ b/sdk/src/test/java/io/opentdf/platform/sdk/AddressNormalizerTest.java @@ -16,6 +16,8 @@ void testAddressNormalizationWithHTTPSClient() { 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"); + assertThat(normalizeAddress("192.168.1.1:1234", false)).isEqualTo("https://192.168.1.1:1234"); + assertThat(normalizeAddress("192.168.1.1", false)).isEqualTo("https://192.168.1.1:443"); } @Test @@ -26,6 +28,8 @@ void testAddressNormaliationWithInsecureHTTPClient() { 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"); + assertThat(normalizeAddress("192.168.1.1:1234", true)).isEqualTo("http://192.168.1.1:1234"); + assertThat(normalizeAddress("192.168.1.1", true)).isEqualTo("http://192.168.1.1:80"); } @Test From c1ee44466f9d08fe570f0f5c63c97ec33c0d1c57 Mon Sep 17 00:00:00 2001 From: Morgan Kleene Date: Sat, 20 Sep 2025 07:34:49 -0400 Subject: [PATCH 2/4] add comments --- .../java/io/opentdf/platform/sdk/AddressNormalizer.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 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 477f81c3..1e2e4e6a 100644 --- a/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java +++ b/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java @@ -16,7 +16,7 @@ private AddressNormalizer(){ static String normalizeAddress(String urlString, boolean usePlaintext) { final String scheme = usePlaintext ? "http" : "https"; - URI uri = getUri(urlString); + URI uri = getInitialUri(urlString); if (uri.getHost() == null) { // if there is no host and no scheme, then we assume the input is a hostname with no port or scheme @@ -51,7 +51,10 @@ static String normalizeAddress(String urlString, boolean usePlaintext) { } } - private static URI getUri(String urlString) { + // tries to parse a URI that is possibly missing a scheme by first trying the input directly, and then + // trying again with a fake scheme if the first attempt fails. this is needed because URIs without a schema + // whose hostnames are not valid schemas will fail to parse + private static URI getInitialUri(String urlString) { URISyntaxException initialThrown = null; try { return new URI(urlString); From 5d7b94086ae71ca03cf3b2ffdb8f22a2975c5955 Mon Sep 17 00:00:00 2001 From: Morgan Kleene Date: Mon, 22 Sep 2025 10:34:48 -0400 Subject: [PATCH 3/4] Update AddressNormalizer.java --- sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java | 1 + 1 file changed, 1 insertion(+) 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 1e2e4e6a..39c0cb78 100644 --- a/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java +++ b/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java @@ -67,6 +67,7 @@ private static URI getInitialUri(String urlString) { try { return new URI(format("fake://%s", urlString)); } catch (URISyntaxException e) { + initialThrown.addSuppressed(e); throw new SDKException("error parsing url [" + urlString + "]", initialThrown); } } From 3f8cef5f173346a1c03d594eebd90301b03c25fd Mon Sep 17 00:00:00 2001 From: Morgan Kleene Date: Mon, 22 Sep 2025 16:25:43 -0400 Subject: [PATCH 4/4] coverage --- .../java/io/opentdf/platform/sdk/AddressNormalizer.java | 2 +- .../io/opentdf/platform/sdk/AddressNormalizerTest.java | 8 ++++++++ 2 files changed, 9 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 39c0cb78..93692323 100644 --- a/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java +++ b/sdk/src/main/java/io/opentdf/platform/sdk/AddressNormalizer.java @@ -15,9 +15,9 @@ private AddressNormalizer(){ } static String normalizeAddress(String urlString, boolean usePlaintext) { - final String scheme = usePlaintext ? "http" : "https"; URI uri = getInitialUri(urlString); + 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 hostname with no port or scheme if (uri.getScheme() == null) { 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 f48022e3..20f9817e 100644 --- a/sdk/src/test/java/io/opentdf/platform/sdk/AddressNormalizerTest.java +++ b/sdk/src/test/java/io/opentdf/platform/sdk/AddressNormalizerTest.java @@ -3,6 +3,8 @@ import org.junit.jupiter.api.Test; +import java.net.URISyntaxException; + import static io.opentdf.platform.sdk.AddressNormalizer.normalizeAddress; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -40,4 +42,10 @@ void testAddressNormalizationWithInvalidPort() { thrown = assertThrows(SDKException.class, () -> normalizeAddress("http://example.org:notaport", true)); assertThat(thrown.getMessage()).contains("http://example.org:notaport"); } + + @Test + void testInsaneAddressThrowsException() { + var thrown = assertThrows(SDKException.class, () -> normalizeAddress("1://&()&{$!//1//1", true)); + assertThat(thrown.getCause()).isInstanceOf(URISyntaxException.class); + } }