Skip to content

Commit 0f11525

Browse files
committed
use InetAddress functionality instead of hand-crafted verification
1 parent 8a6afaf commit 0f11525

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

src/main/java/com/datadoghq/workshops/samplevulnerablejavaapp/DomainTestService.java

+10-22
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,30 @@
66
import org.springframework.stereotype.Service;
77

88
import java.io.IOException;
9-
import java.util.concurrent.TimeUnit;
10-
import java.util.regex.Matcher;
11-
import java.util.regex.Pattern;
9+
import java.net.InetAddress;
10+
import java.net.UnknownHostException;
1211

1312
@Service
1413
public class DomainTestService {
1514

1615
final static int timeoutMs = 10_000;
17-
final static Pattern domainValidationRegex = Pattern.compile("^((?!-))(xn--)?[a-z0-9][a-z0-9-_]{0,61}[a-z0-9]{0,1}\\.(xn--)?([a-z0-9\\-]{1,61}|[a-z0-9-]{1,30}\\.[a-z]{2,})", Pattern.CASE_INSENSITIVE);
1816

1917
public String testDomain(String domainName) throws DomainTestException {
20-
if (!isValidDomainName(domainName)) {
18+
InetAddress address;
19+
try {
20+
address = InetAddress.getByName(domainName);
21+
} catch (UnknownHostException e) {
2122
throw new InvalidDomainException("Invalid domain name: " + domainName + " - don't try to hack us!");
2223
}
2324

2425
try {
25-
//TODO use ProcessBuilder which looks cleaner
26-
Process process = Runtime.getRuntime().exec(new String[] {"sh", "-c", "ping -c 1 " + domainName});
27-
if (!process.waitFor(timeoutMs, TimeUnit.MILLISECONDS)) {
28-
throw new UnableToTestDomainException("Timed out pinging domain");
29-
}
30-
int exitCode = process.exitValue();
31-
if (exitCode != 0) {
32-
String stderr = new String(process.getErrorStream().readAllBytes(), StandardCharsets.UTF_8);
33-
throw new UnableToTestDomainException("Ping returned exit status " + exitCode + ": " + stderr);
26+
boolean reachable = address.isReachable(timeoutMs);
27+
if (!reachable) {
28+
throw new UnableToTestDomainException("The domain " + domainName + "is not reachable!");
3429
}
35-
return new String(process.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
30+
return "The domain " + domainName + "is reachable!";
3631
} catch (IOException e) {
3732
throw new UnableToTestDomainException("Internal error while testing domain: " + e.getMessage());
38-
} catch (InterruptedException e) {
39-
throw new UnableToTestDomainException("Timed out pinging domain");
4033
}
4134
}
42-
43-
static boolean isValidDomainName(String domainName) {
44-
Matcher matcher = domainValidationRegex.matcher(domainName);
45-
return matcher.find();
46-
}
4735
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
package com.datadoghq.workshops.samplevulnerablejavaapp;
22

3+
import com.datadoghq.workshops.samplevulnerablejavaapp.exception.DomainTestException;
34
import org.junit.jupiter.api.Assertions;
45
import org.junit.jupiter.api.Test;
56

67
public class DomainTestServiceTests {
78
@Test
89
void testValidDomain() {
910
String domain = "google.com";
10-
Assertions.assertTrue(DomainTestService.isValidDomainName(domain));
11+
DomainTestService testService = new DomainTestService();
12+
13+
try {
14+
testService.testDomain(domain);
15+
} catch (DomainTestException e) {
16+
Assertions.fail();
17+
}
1118
}
1219

1320
@Test
1421
void testInvalidDomain() {
1522
String domain = "exec script.sh";
16-
Assertions.assertFalse(DomainTestService.isValidDomainName(domain));
23+
DomainTestService testService = new DomainTestService();
24+
25+
try {
26+
testService.testDomain(domain);
27+
Assertions.fail();
28+
} catch (DomainTestException ignored) { }
1729
}
1830
}

0 commit comments

Comments
 (0)