Skip to content

Commit c291112

Browse files
committed
Merge pull request lightbody#352 from jekh/require-method-when-blacklisting-connect
Require HTTP method to blacklist CONNECTs
2 parents cf308ab + 7ba5adc commit c291112

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.netty.channel.ChannelHandlerContext;
44
import io.netty.handler.codec.http.DefaultFullHttpResponse;
55
import io.netty.handler.codec.http.HttpHeaders;
6+
import io.netty.handler.codec.http.HttpMethod;
67
import io.netty.handler.codec.http.HttpObject;
78
import io.netty.handler.codec.http.HttpRequest;
89
import io.netty.handler.codec.http.HttpResponse;
@@ -37,6 +38,11 @@ public HttpResponse clientToProxyRequest(HttpObject httpObject) {
3738
String url = getFullUrl(httpRequest);
3839

3940
for (BlacklistEntry entry : blacklistedUrls) {
41+
if (HttpMethod.CONNECT.equals(httpRequest.getMethod()) && entry.getHttpMethodPatern() == null) {
42+
// do not allow CONNECTs to be blacklisted unless a method pattern is explicitly specified
43+
continue;
44+
}
45+
4046
if (entry.matches(url, httpRequest.getMethod().name())) {
4147
HttpResponseStatus status = HttpResponseStatus.valueOf(entry.getStatusCode());
4248
HttpResponse resp = new DefaultFullHttpResponse(httpRequest.getProtocolVersion(), status);

browsermob-core-littleproxy/src/test/groovy/net/lightbody/bmp/proxy/BlacklistTest.groovy

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,31 @@ class BlacklistTest extends MockServerTest {
175175
assertThat("Expected blacklisted response to contain 0-length body", blacklistedResponseBody, isEmptyOrNullString())
176176
}
177177
}
178+
179+
@Test
180+
void testBlacklistDoesNotApplyToCONNECT() {
181+
mockServer.when(request()
182+
.withMethod("GET")
183+
.withPath("/connectNotBlacklisted"),
184+
Times.unlimited())
185+
.respond(response()
186+
.withStatusCode(200)
187+
.withBody("success"))
188+
189+
proxy = new BrowserMobProxyServer()
190+
proxy.setTrustAllServers(true)
191+
proxy.start()
192+
int proxyPort = proxy.getPort()
193+
194+
// HTTP CONNECTs should not be blacklisted unless the method is explicitly specified
195+
proxy.blacklistRequests("https://localhost:${mockServerPort}", 405)
196+
197+
ProxyServerTest.getNewHttpClient(proxyPort).withCloseable {
198+
CloseableHttpResponse response = it.execute(new HttpGet("https://localhost:${mockServerPort}/connectNotBlacklisted"))
199+
assertEquals("Expected to receive response from mock server after successful CONNECT", 200, response.getStatusLine().getStatusCode())
200+
201+
String responseBody = IOUtils.toStringAndClose(response.getEntity().getContent())
202+
assertEquals("Expected to receive HTTP 200 and success message from server", "success", responseBody)
203+
}
204+
}
178205
}

0 commit comments

Comments
 (0)