Skip to content

Commit b77ba44

Browse files
committed
Add '-validation-server=...' option to test detected endpoints against automatically
1 parent 81ea090 commit b77ba44

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

src/com/denimgroup/threadfix/cli/endpoints/EndpointMain.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828

2929
import com.denimgroup.threadfix.data.entities.RouteParameter;
3030
import com.denimgroup.threadfix.data.entities.RouteParameterType;
31+
import com.denimgroup.threadfix.data.entities.WildcardEndpointPathNode;
3132
import com.denimgroup.threadfix.data.enums.FrameworkType;
3233
import com.denimgroup.threadfix.data.interfaces.Endpoint;
34+
import com.denimgroup.threadfix.data.interfaces.EndpointPathNode;
3335
import com.denimgroup.threadfix.framework.engine.framework.FrameworkCalculator;
3436
import com.denimgroup.threadfix.framework.engine.full.EndpointDatabase;
3537
import com.denimgroup.threadfix.framework.engine.full.EndpointDatabaseFactory;
@@ -46,7 +48,9 @@
4648
import org.codehaus.jackson.map.ObjectMapper;
4749

4850
import java.io.File;
51+
import java.io.FileNotFoundException;
4952
import java.io.IOException;
53+
import java.net.UnknownHostException;
5054
import java.util.Collection;
5155
import java.util.List;
5256
import java.util.Map;
@@ -74,6 +78,8 @@ enum Logging {
7478
static int totalDetectedEndpoints = 0;
7579
static int totalDetectedParameters = 0;
7680

81+
static String testUrlPath = null;
82+
7783
private static void println(String line) {
7884
if (printFormat != SIMPLE_JSON && printFormat != FULL_JSON) {
7985
System.out.println(line);
@@ -326,6 +332,9 @@ private static boolean checkArguments(String[] args) {
326332
path = path.substring(0, path.length() - 1);
327333
}
328334
pathListFile = path;
335+
} else if (arg.startsWith("-validation-server=")) {
336+
String[] parts = arg.split("=");
337+
testUrlPath = parts[1];
329338
} else {
330339
println("Received unsupported option " + arg + ", valid arguments are -lint, -debug, -simple-json, -json, -path-list-file, and -simple");
331340
return false;
@@ -446,6 +455,46 @@ private static List<Endpoint> listEndpoints(File rootFile, Collection<FrameworkT
446455
println("Failed to validate serialization for at least one of these endpoints");
447456
}
448457

458+
if (testUrlPath != null) {
459+
EndpointTester tester = new EndpointTester(testUrlPath);
460+
461+
println("Testing endpoints against server at: " + testUrlPath);
462+
463+
List<Endpoint> successfulEndpoints = list();
464+
List<Endpoint> failedEndpoints = list();
465+
List<Endpoint> allEndpoints = EndpointUtil.flattenWithVariants(endpoints);
466+
for (Endpoint endpoint : allEndpoints) {
467+
boolean skip = false;
468+
for (EndpointPathNode node : endpoint.getUrlPathNodes()) {
469+
if (node.getClass().equals(WildcardEndpointPathNode.class)) {
470+
skip = true;
471+
break;
472+
}
473+
}
474+
475+
if (skip) {
476+
continue;
477+
}
478+
479+
try {
480+
tester.test(endpoint);
481+
successfulEndpoints.add(endpoint);
482+
} catch (FileNotFoundException | UnknownHostException e) {
483+
failedEndpoints.add(endpoint);
484+
} catch (IOException e) {
485+
if (e.getMessage().contains("HTTP")) {
486+
successfulEndpoints.add(endpoint);
487+
}
488+
}
489+
}
490+
491+
for (Endpoint endpoint : failedEndpoints) {
492+
println("Failed: " + endpoint.getUrlPath() + "[" + endpoint.getHttpMethod() + "]");
493+
}
494+
495+
println(successfulEndpoints.size() + "/" + (successfulEndpoints.size() + failedEndpoints.size()) + " endpoints were queryable");
496+
}
497+
449498
int numMissingStartLine = 0;
450499
int numMissingEndLine = 0;
451500
int numSameLineRange = 0;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.denimgroup.threadfix.cli.endpoints;
2+
3+
import com.denimgroup.threadfix.data.interfaces.Endpoint;
4+
import com.denimgroup.threadfix.framework.util.PathUtil;
5+
6+
import java.io.IOException;
7+
import java.net.HttpURLConnection;
8+
import java.net.MalformedURLException;
9+
import java.net.URL;
10+
import java.net.URLConnection;
11+
12+
public class EndpointTester {
13+
String basePath;
14+
15+
public EndpointTester(String basePath) {
16+
this.basePath = basePath;
17+
}
18+
19+
public int test(Endpoint endpoint) throws IOException {
20+
URL url = new URL(PathUtil.combine(this.basePath, endpoint.getUrlPath()));
21+
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
22+
conn.setRequestMethod(endpoint.getHttpMethod());
23+
24+
conn.getInputStream().close();
25+
return conn.getResponseCode();
26+
}
27+
}

0 commit comments

Comments
 (0)