Skip to content

Commit ef7ec5f

Browse files
committed
Fix crawler to issue GETs when it should, instead of all POSTs.
1 parent 6ed76bb commit ef7ec5f

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

src/main/java/org/owasp/benchmark/tools/BenchmarkCrawler.java

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,28 @@
2121
import java.io.FileInputStream;
2222
import java.io.IOException;
2323
import java.io.InputStream;
24+
import java.security.KeyManagementException;
25+
import java.security.KeyStoreException;
26+
import java.security.NoSuchAlgorithmException;
2427
import java.text.DateFormat;
2528
import java.text.SimpleDateFormat;
2629
import java.util.Date;
2730
import java.util.List;
2831

32+
import javax.net.ssl.HostnameVerifier;
33+
import javax.net.ssl.SSLContext;
34+
2935
import org.apache.commons.lang.time.StopWatch;
3036
import org.apache.http.HttpEntity;
3137
import org.apache.http.client.methods.CloseableHttpResponse;
3238
import org.apache.http.client.methods.HttpPost;
3339
import org.apache.http.client.methods.HttpRequestBase;
40+
import org.apache.http.conn.ssl.NoopHostnameVerifier;
41+
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
42+
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
3443
import org.apache.http.impl.client.CloseableHttpClient;
3544
import org.apache.http.impl.client.HttpClients;
45+
import org.apache.http.ssl.SSLContextBuilder;
3646
import org.apache.http.util.EntityUtils;
3747

3848
import org.owasp.benchmark.helpers.Utils;
@@ -53,7 +63,7 @@ protected void init() {
5363
}
5464

5565
protected void crawl(InputStream http) throws Exception {
56-
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(Utils.getSSLFactory()).build();
66+
CloseableHttpClient httpclient = createAcceptSelfSignedCertificateClient();
5767
long start = System.currentTimeMillis();
5868

5969
List<AbstractTestCaseRequest> requests = Utils.parseHttpFile(http);
@@ -76,16 +86,37 @@ protected void crawl(InputStream http) throws Exception {
7686
+ " v" + testSuiteVersion + " took " + seconds + " seconds");
7787
}
7888

89+
// This method taken directly from: https://memorynotfound.com/ignore-certificate-errors-apache-httpclient/
90+
static CloseableHttpClient createAcceptSelfSignedCertificateClient()
91+
throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
92+
93+
// use the TrustSelfSignedStrategy to allow Self Signed Certificates
94+
SSLContext sslContext = SSLContextBuilder
95+
.create()
96+
.loadTrustMaterial(new TrustSelfSignedStrategy())
97+
.build();
98+
99+
// we can optionally disable hostname verification.
100+
// if you don't want to further weaken the security, you don't have to include this.
101+
HostnameVerifier allowAllHosts = new NoopHostnameVerifier();
102+
103+
// create an SSL Socket Factory to use the SSLContext with the trust self signed certificate strategy
104+
// and allow all hosts verifier.
105+
SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory(sslContext, allowAllHosts);
106+
107+
// finally create the HttpClient using HttpClient factory methods and assign the ssl socket factory
108+
return HttpClients
109+
.custom()
110+
.setSSLSocketFactory(connectionFactory)
111+
.build();
112+
}
113+
79114
/**
80-
* Issue the requested request, measure the time required to execute, then
81-
* output both to stdout and the global
82-
* variable timeString the URL tested, the time required to execute and the
83-
* response code.
115+
* Issue the requested request, measure the time required to execute, then output both to stdout and the
116+
* global variable timeString the URL tested, the time required to execute and the response code.
84117
*
85-
* @param httpclient
86-
* - The HTTP client to use to make the request
87-
* @param request
88-
* - THe HTTP request to issue
118+
* @param httpclient - The HTTP client to use to make the request
119+
* @param request - THe HTTP request to issue
89120
* @throws IOException
90121
*/
91122
protected ResponseInfo sendRequest(CloseableHttpClient httpclient, AbstractTestCaseRequest requestTC) {

src/main/java/org/owasp/benchmark/tools/ServletTestCaseRequest.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.http.NameValuePair;
2727
import org.apache.http.client.entity.UrlEncodedFormEntity;
2828
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
29+
import org.apache.http.client.methods.HttpGet;
2930
import org.apache.http.client.methods.HttpPost;
3031
import org.apache.http.client.methods.HttpRequestBase;
3132
import org.apache.http.message.BasicNameValuePair;
@@ -59,8 +60,9 @@ void buildQueryString() {
5960

6061
@Override
6162
HttpRequestBase createRequestInstance(String URL) {
62-
HttpPost httpPost = new HttpPost(URL);
63-
return httpPost;
63+
// If there are query parameters, this must be a GET, otherwise a POST.
64+
if (getQuery().length() == 0) return new HttpPost(URL);
65+
else return new HttpGet(URL);
6466
}
6567

6668
@Override
@@ -79,6 +81,10 @@ void buildCookies(HttpRequestBase request) {
7981
for (Node cookie : getCookies()) {
8082
String name = XMLCrawler.getAttributeValue("name", cookie);
8183
String value = XMLCrawler.getAttributeValue("value", cookie);
84+
// Note: URL encoding of a space becomes a +, which is OK for URL params, but
85+
// not in a cookie, as the + doesn't get decoded properly. So have to replace
86+
// all spaces with %20 instead (at least for NodeJS). Will this break Java?
87+
value = value.replaceAll(" ", "%20");
8288
request.addHeader("Cookie", name + "=" + URLEncoder.encode(value));
8389
}
8490
}
@@ -93,12 +99,14 @@ void buildBodyParameters(HttpRequestBase request) {
9399
NameValuePair nvp = new BasicNameValuePair(name, value);
94100
fields.add(nvp);
95101
}
96-
try {
97-
((HttpEntityEnclosingRequestBase) request).setEntity(new UrlEncodedFormEntity(fields));
98-
} catch (UnsupportedEncodingException e) {
99-
System.out.println("Error encoding URL." + e.getMessage());
102+
// Add the body parameters to the request if there were any
103+
if (fields.size() > 0) {
104+
try {
105+
((HttpEntityEnclosingRequestBase) request).setEntity(new UrlEncodedFormEntity(fields));
106+
} catch (UnsupportedEncodingException e) {
107+
System.out.println("Error encoding URL." + e.getMessage());
108+
}
100109
}
101-
102110
}
103111

104112
}

0 commit comments

Comments
 (0)