Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Use the CONNECT method URI as host fallback
Browse files Browse the repository at this point in the history
When a request is made for proxy interception using the `CONNECT` method but not including a `host` header, then a `NullPointerException` is thrown in `com.browserup.bup.mitm.manager.ImpersonatingMitmManager.getHostnameImpersonatingSslContext(String, SSLSession)` because the `hostnameToImpersonate` is null.

As a fallback, use the CONNECT URI as a host.
  • Loading branch information
candrews committed Apr 27, 2021
1 parent 26714e2 commit e3507b7
Showing 1 changed file with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.common.net.HostAndPort;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;

import java.net.URI;
Expand Down Expand Up @@ -42,6 +43,11 @@ public static String getHostFromRequest(HttpRequest httpRequest) {
host = parseHostHeader(httpRequest, false);
}

// if there was not a Host header, and the method is CONNECT, use that as the host
if (host == null || host.isEmpty()) {
host = hostFromConnect(httpRequest, false);
}

return host;
}

Expand All @@ -53,15 +59,26 @@ public static String getHostFromRequest(HttpRequest httpRequest) {
* @return host and port of the request
*/
public static String getHostAndPortFromRequest(HttpRequest httpRequest) {
String host = null;
if (startsWithHttpOrHttps(httpRequest.uri())) {
try {
return getHostAndPortFromUri(httpRequest.uri());
host = getHostAndPortFromUri(httpRequest.uri());
} catch (URISyntaxException e) {
// the URI could not be parsed, so return the host and port in the Host header
}
}

return parseHostHeader(httpRequest, true);
// if there was no host in the URI, attempt to grab the host from the Host header
if (host == null || host.isEmpty()) {
host = parseHostHeader(httpRequest, true);
}

// if there was not Host header, and the method is CONNECT, use that as the host
if (host == null || host.isEmpty()) {
host = hostFromConnect(httpRequest, true);
}

return host;
}

/**
Expand Down Expand Up @@ -125,4 +142,24 @@ private static String parseHostHeader(HttpRequest httpRequest, boolean includePo
return null;
}
}

/**
* Retrieves the host and, optionally, the port from the specified request's URI if the method is CONNECT.
*
* @param httpRequest HTTP request
* @param includePort when true, include the port
* @return the host and, optionally, the port specified in the request's URI
*/
private static String hostFromConnect(HttpRequest httpRequest, boolean includePort) {
if (HttpMethod.CONNECT.equals(httpRequest.method())) {
if (includePort) {
return httpRequest.uri();
} else {
HostAndPort parsedHostAndPort = HostAndPort.fromString(httpRequest.uri());
return parsedHostAndPort.getHost();
}
} else {
return null;
}
}
}

0 comments on commit e3507b7

Please sign in to comment.