Skip to content

Commit f46e14e

Browse files
committed
Not validating path and query params when returning the full URL, to avoid issues with noncompliant HTTP clients
1 parent c68d0af commit f46e14e

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

browsermob-core-littleproxy/src/main/java/net/lightbody/bmp/filters/HttpsAwareFiltersAdapter.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,18 @@ public String getFullUrl(HttpRequest modifiedRequest) {
5858
}
5959

6060
// To get the full URL, we need to retrieve the Scheme, Host + Port, Path, and Query Params from the request.
61-
// Scheme: the scheme (HTTP/HTTPS) may or may not be part of the request, and so must be generated based on the
62-
// type of connection.
61+
// If the request URI starts with http:// or https://, it is already a full URL and can be returned directly.
62+
if (BrowserMobHttpUtil.startsWithHttpOrHttps(modifiedRequest.getUri())) {
63+
return modifiedRequest.getUri();
64+
}
65+
66+
// The URI did not include the scheme and host, so examine the request to obtain them:
67+
// Scheme: the scheme (HTTP/HTTPS) are based on the type of connection, obtained from isHttps()
6368
// Host and Port: available for HTTP and HTTPS requests using the getHostAndPort() helper method.
64-
// Path + Query Params + Fragment: these elements are contained in the HTTP request for both HTTP and HTTPS
69+
// Path + Query Params: since the request URI doesn't start with the scheme, we can safely assume that the URI
70+
// contains only the path and query params.
6571
String hostAndPort = getHostAndPort(modifiedRequest);
66-
String path = BrowserMobHttpUtil.getRawPathAndParamsFromRequest(modifiedRequest);
72+
String path = modifiedRequest.getUri();
6773
String url;
6874
if (isHttps()) {
6975
url = "https://" + hostAndPort + path;

browsermob-core/src/main/java/net/lightbody/bmp/util/BrowserMobHttpUtil.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,18 +246,19 @@ public static String getHostAndPortFromRequest(HttpRequest httpRequest) {
246246
*
247247
* @param httpRequest HTTP request
248248
* @return the unescaped path + query string from the HTTP request
249+
* @throws URISyntaxException if the path could not be parsed (due to invalid characters in the URI, etc.)
249250
*/
250-
public static String getRawPathAndParamsFromRequest(HttpRequest httpRequest) {
251+
public static String getRawPathAndParamsFromRequest(HttpRequest httpRequest) throws URISyntaxException {
251252
// if this request's URI contains a full URI (including scheme, host, etc.), strip away the non-path components
252253
if (startsWithHttpOrHttps(httpRequest.getUri())) {
253-
try {
254-
return getRawPathAndParamsFromUri(httpRequest.getUri());
255-
} catch (URISyntaxException e) {
256-
// could not parse the URI, so fall through and return the URI as-is
257-
}
258-
}
254+
return getRawPathAndParamsFromUri(httpRequest.getUri());
255+
} else {
256+
// to provide consistent validation behavior for URIs that contain a scheme and those that don't, attempt to parse
257+
// the URI, even though we discard the parsed URI object
258+
new URI(httpRequest.getUri());
259259

260-
return httpRequest.getUri();
260+
return httpRequest.getUri();
261+
}
261262
}
262263

263264
/**

0 commit comments

Comments
 (0)