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

Commit e3507b7

Browse files
committed
Use the CONNECT method URI as host fallback
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.
1 parent 26714e2 commit e3507b7

File tree

1 file changed

+39
-2
lines changed
  • browserup-proxy-mitm/src/main/java/com/browserup/bup/util

1 file changed

+39
-2
lines changed

browserup-proxy-mitm/src/main/java/com/browserup/bup/util/HttpUtil.java

+39-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.google.common.net.HostAndPort;
88
import io.netty.handler.codec.http.HttpHeaderNames;
99
import io.netty.handler.codec.http.HttpHeaders;
10+
import io.netty.handler.codec.http.HttpMethod;
1011
import io.netty.handler.codec.http.HttpRequest;
1112

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

46+
// if there was not a Host header, and the method is CONNECT, use that as the host
47+
if (host == null || host.isEmpty()) {
48+
host = hostFromConnect(httpRequest, false);
49+
}
50+
4551
return host;
4652
}
4753

@@ -53,15 +59,26 @@ public static String getHostFromRequest(HttpRequest httpRequest) {
5359
* @return host and port of the request
5460
*/
5561
public static String getHostAndPortFromRequest(HttpRequest httpRequest) {
62+
String host = null;
5663
if (startsWithHttpOrHttps(httpRequest.uri())) {
5764
try {
58-
return getHostAndPortFromUri(httpRequest.uri());
65+
host = getHostAndPortFromUri(httpRequest.uri());
5966
} catch (URISyntaxException e) {
6067
// the URI could not be parsed, so return the host and port in the Host header
6168
}
6269
}
6370

64-
return parseHostHeader(httpRequest, true);
71+
// if there was no host in the URI, attempt to grab the host from the Host header
72+
if (host == null || host.isEmpty()) {
73+
host = parseHostHeader(httpRequest, true);
74+
}
75+
76+
// if there was not Host header, and the method is CONNECT, use that as the host
77+
if (host == null || host.isEmpty()) {
78+
host = hostFromConnect(httpRequest, true);
79+
}
80+
81+
return host;
6582
}
6683

6784
/**
@@ -125,4 +142,24 @@ private static String parseHostHeader(HttpRequest httpRequest, boolean includePo
125142
return null;
126143
}
127144
}
145+
146+
/**
147+
* Retrieves the host and, optionally, the port from the specified request's URI if the method is CONNECT.
148+
*
149+
* @param httpRequest HTTP request
150+
* @param includePort when true, include the port
151+
* @return the host and, optionally, the port specified in the request's URI
152+
*/
153+
private static String hostFromConnect(HttpRequest httpRequest, boolean includePort) {
154+
if (HttpMethod.CONNECT.equals(httpRequest.method())) {
155+
if (includePort) {
156+
return httpRequest.uri();
157+
} else {
158+
HostAndPort parsedHostAndPort = HostAndPort.fromString(httpRequest.uri());
159+
return parsedHostAndPort.getHost();
160+
}
161+
} else {
162+
return null;
163+
}
164+
}
128165
}

0 commit comments

Comments
 (0)