Skip to content

Commit

Permalink
Merge pull request #2434 from Ludy87/security_fix_1
Browse files Browse the repository at this point in the history
Security fix: Server-Side Request Forgery
  • Loading branch information
Frooodle authored Dec 12, 2024
2 parents fc514ee + c3f88f7 commit c6980e9
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/main/java/stirling/software/SPDF/utils/GeneralUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,45 @@ public static boolean isValidURL(String urlStr) {

public static boolean isURLReachable(String urlStr) {
try {
// Parse the URL
URL url = URI.create(urlStr).toURL();

// Allow only http and https protocols
String protocol = url.getProtocol();
if (!protocol.equals("http") && !protocol.equals("https")) {
return false; // Disallow other protocols
}

// Check if the host is a local address
String host = url.getHost();
if (isLocalAddress(host)) {
return false; // Exclude local addresses
}

// Check if the URL is reachable
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("HEAD");
// connection.setConnectTimeout(5000); // Set connection timeout
// connection.setReadTimeout(5000); // Set read timeout
int responseCode = connection.getResponseCode();
return (200 <= responseCode && responseCode <= 399);
} catch (MalformedURLException e) {
return false;
} catch (IOException e) {
return false;
} catch (Exception e) {
return false; // Return false in case of any exception
}
}

private static boolean isLocalAddress(String host) {
try {
// Resolve DNS to IP address
InetAddress address = InetAddress.getByName(host);

// Check for local addresses
return address.isAnyLocalAddress() || // Matches 0.0.0.0 or similar
address.isLoopbackAddress() || // Matches 127.0.0.1 or ::1
address.isSiteLocalAddress() || // Matches private IPv4 ranges: 192.168.x.x, 10.x.x.x, 172.16.x.x to 172.31.x.x
address.getHostAddress().startsWith("fe80:"); // Matches link-local IPv6 addresses
} catch (Exception e) {
return false; // Return false for invalid or unresolved addresses
}
}

Expand Down

0 comments on commit c6980e9

Please sign in to comment.