From 345d10c94a85b2f53a161699c740f9b98d12c976 Mon Sep 17 00:00:00 2001 From: Jamie Li Date: Sat, 5 Aug 2023 16:56:55 +0800 Subject: [PATCH 1/3] Remove Sep10Config hostname lookup for validation --- .../src/main/java/org/stellar/anchor/util/NetUtil.java | 10 +++++----- .../anchor/platform/config/PropertySep10Config.java | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/stellar/anchor/util/NetUtil.java b/core/src/main/java/org/stellar/anchor/util/NetUtil.java index b8047fea6e..dc0ba639c6 100644 --- a/core/src/main/java/org/stellar/anchor/util/NetUtil.java +++ b/core/src/main/java/org/stellar/anchor/util/NetUtil.java @@ -35,11 +35,11 @@ public static boolean isUrlValid(String url) { } } - public static boolean isServerPortValid(String serverPort) { + public static boolean isServerPortValid(String serverPort, boolean hostnameLookup) { if (isEmpty(serverPort)) return false; String[] tokens = Strings.split(serverPort, ":"); if (tokens == null) { - return isHostnameValid(serverPort); + return true; } switch (tokens.length) { case 2: @@ -47,13 +47,13 @@ public static boolean isServerPortValid(String serverPort) { try { int port = Integer.parseInt(strPort); if (port > 65535 || port < 0) { - return false; + return !hostnameLookup || isHostnameResolvable(serverPort); } } catch (NumberFormatException ex) { return false; } case 1: - return isHostnameValid(tokens[0]); + return !hostnameLookup || isHostnameResolvable(serverPort); case 0: default: return false; @@ -68,7 +68,7 @@ public static String getDomainFromURL(String strUri) throws MalformedURLExceptio return uri.getHost() + ":" + uri.getPort(); } - static boolean isHostnameValid(String hostname) { + static boolean isHostnameResolvable(String hostname) { try { InetAddress.getAllByName(hostname); return true; diff --git a/platform/src/main/java/org/stellar/anchor/platform/config/PropertySep10Config.java b/platform/src/main/java/org/stellar/anchor/platform/config/PropertySep10Config.java index d59d5f9025..162fea0190 100644 --- a/platform/src/main/java/org/stellar/anchor/platform/config/PropertySep10Config.java +++ b/platform/src/main/java/org/stellar/anchor/platform/config/PropertySep10Config.java @@ -119,7 +119,7 @@ void validateConfig(Errors errors) { homeDomain, iaex)); } - if (!NetUtil.isServerPortValid(homeDomain)) { + if (!NetUtil.isServerPortValid(homeDomain, false)) { errors.rejectValue( "homeDomain", "sep10-home-domain-invalid", @@ -139,7 +139,7 @@ void validateConfig(Errors errors) { webAuthDomain, iaex)); } - if (!NetUtil.isServerPortValid(webAuthDomain)) { + if (!NetUtil.isServerPortValid(webAuthDomain, false)) { errors.rejectValue( "webAuthDomain", "sep10-web-auth-domain-invalid", @@ -172,7 +172,7 @@ void validateClientAttribution(Errors errors) { if (!ListHelper.isEmpty(getDefaultAllowClientDomain())) { for (String clientDomain : getDefaultAllowClientDomain()) { - if (!NetUtil.isServerPortValid(clientDomain)) { + if (!NetUtil.isServerPortValid(clientDomain, false)) { errors.rejectValue( "clientAttributionAllowList", "sep10-client_attribution_allow_list_invalid", From 18308c2894946fc9a8f130f158a50aaa4b59eb11 Mon Sep 17 00:00:00 2001 From: Jamie Li Date: Mon, 7 Aug 2023 10:38:03 +0800 Subject: [PATCH 2/3] Fix test errors --- core/src/main/java/org/stellar/anchor/util/NetUtil.java | 2 +- core/src/test/kotlin/org/stellar/anchor/util/NetUtilTest.kt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/stellar/anchor/util/NetUtil.java b/core/src/main/java/org/stellar/anchor/util/NetUtil.java index dc0ba639c6..35e2e1ee06 100644 --- a/core/src/main/java/org/stellar/anchor/util/NetUtil.java +++ b/core/src/main/java/org/stellar/anchor/util/NetUtil.java @@ -39,7 +39,7 @@ public static boolean isServerPortValid(String serverPort, boolean hostnameLooku if (isEmpty(serverPort)) return false; String[] tokens = Strings.split(serverPort, ":"); if (tokens == null) { - return true; + return !hostnameLookup || isHostnameResolvable(serverPort); } switch (tokens.length) { case 2: diff --git a/core/src/test/kotlin/org/stellar/anchor/util/NetUtilTest.kt b/core/src/test/kotlin/org/stellar/anchor/util/NetUtilTest.kt index 0a39404fc6..593d890aa7 100644 --- a/core/src/test/kotlin/org/stellar/anchor/util/NetUtilTest.kt +++ b/core/src/test/kotlin/org/stellar/anchor/util/NetUtilTest.kt @@ -129,7 +129,7 @@ internal class NetUtilTest { ] ) fun `test valid server port with isServerPortValid`(testValue: String) { - assertTrue(isServerPortValid(testValue)) + assertTrue(isServerPortValid(testValue, false)) } @ParameterizedTest @@ -147,7 +147,7 @@ internal class NetUtilTest { ] ) fun `test bad server port with isServerPortValid`(testValue: String?) { - assertFalse(isServerPortValid(testValue)) + assertFalse(isServerPortValid(testValue, true)) } @ParameterizedTest From be9aa05199e77f4c6eba5f8390d265f4e3ceb869 Mon Sep 17 00:00:00 2001 From: Jamie Li Date: Mon, 7 Aug 2023 13:38:11 +0800 Subject: [PATCH 3/3] Fix test errors --- .../org/stellar/anchor/platform/config/Sep10ConfigTest.kt | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/platform/src/test/kotlin/org/stellar/anchor/platform/config/Sep10ConfigTest.kt b/platform/src/test/kotlin/org/stellar/anchor/platform/config/Sep10ConfigTest.kt index e34f7dc3f8..cec75ba3a6 100644 --- a/platform/src/test/kotlin/org/stellar/anchor/platform/config/Sep10ConfigTest.kt +++ b/platform/src/test/kotlin/org/stellar/anchor/platform/config/Sep10ConfigTest.kt @@ -177,9 +177,7 @@ class Sep10ConfigTest { value = [ "this-is-longer-than-64-bytes-which-is-the-maximum-length-for-a-web-auth-domain.stellar.org,sep10-web-auth-domain-too-long", - "stellar .org,sep10-web-auth-domain-invalid", - "abc,sep10-web-auth-domain-invalid", - "299.0.0.1,sep10-web-auth-domain-invalid", + "stellar.org:1000:1000,sep10-web-auth-domain-invalid", ] ) fun `test invalid web auth domains`(value: String, expectedErrorCode: String) { @@ -194,9 +192,6 @@ class Sep10ConfigTest { value = [ "this-is-longer-than-64-bytes-which-is-the-maximum-length-for-a-home-domain.stellar.org,sep10-home-domain-too-long", - "stellar .org,sep10-home-domain-invalid", - "abc,sep10-home-domain-invalid", - "299.0.0.1,sep10-home-domain-invalid", "http://stellar.org,sep10-home-domain-invalid", "https://stellar.org,sep10-home-domain-invalid", "://stellar.org,sep10-home-domain-invalid",