Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hostname lookup from Sep10Config validation #1042

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions core/src/main/java/org/stellar/anchor/util/NetUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,25 @@ 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 !hostnameLookup || isHostnameResolvable(serverPort);
}
switch (tokens.length) {
case 2:
String strPort = tokens[1];
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;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions core/src/test/kotlin/org/stellar/anchor/util/NetUtilTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ internal class NetUtilTest {
]
)
fun `test valid server port with isServerPortValid`(testValue: String) {
assertTrue(isServerPortValid(testValue))
assertTrue(isServerPortValid(testValue, false))
}

@ParameterizedTest
Expand All @@ -147,7 +147,7 @@ internal class NetUtilTest {
]
)
fun `test bad server port with isServerPortValid`(testValue: String?) {
assertFalse(isServerPortValid(testValue))
assertFalse(isServerPortValid(testValue, true))
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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",
Expand Down
Loading