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

Fix Command Injection Vulnerability #7

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,30 @@
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.net.InetAddress;
import java.net.UnknownHostException;

@Service
public class DomainTestService {

final static int timeoutMs = 10_000;
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);

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

try {
//TODO use ProcessBuilder which looks cleaner
Process process = Runtime.getRuntime().exec(new String[] {"sh", "-c", "ping -c 1 " + domainName});
if (!process.waitFor(timeoutMs, TimeUnit.MILLISECONDS)) {
throw new UnableToTestDomainException("Timed out pinging domain");
}
int exitCode = process.exitValue();
if (exitCode != 0) {
String stderr = new String(process.getErrorStream().readAllBytes(), StandardCharsets.UTF_8);
throw new UnableToTestDomainException("Ping returned exit status " + exitCode + ": " + stderr);
boolean reachable = address.isReachable(timeoutMs);
if (!reachable) {
throw new UnableToTestDomainException("The domain " + domainName + "is not reachable!");
}
return new String(process.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
return "The domain " + domainName + "is reachable!";
} catch (IOException e) {
throw new UnableToTestDomainException("Internal error while testing domain: " + e.getMessage());
} catch (InterruptedException e) {
throw new UnableToTestDomainException("Timed out pinging domain");
}
}

static boolean isValidDomainName(String domainName) {
Matcher matcher = domainValidationRegex.matcher(domainName);
return matcher.find();
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
package com.datadoghq.workshops.samplevulnerablejavaapp;

import com.datadoghq.workshops.samplevulnerablejavaapp.exception.DomainTestException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class DomainTestServiceTests {
@Test
void testValidDomain() {
String domain = "google.com";
Assertions.assertTrue(DomainTestService.isValidDomainName(domain));
String domain = "localhost";
DomainTestService testService = new DomainTestService();

try {
testService.testDomain(domain);
} catch (DomainTestException e) {
Assertions.fail();
}
}

@Test
void testInvalidDomain() {
String domain = "exec script.sh";
Assertions.assertFalse(DomainTestService.isValidDomainName(domain));
DomainTestService testService = new DomainTestService();

try {
testService.testDomain(domain);
Assertions.fail();
} catch (DomainTestException ignored) { }
}
}