From d6465b907f238f50c20c51304723260c9aaa61ab Mon Sep 17 00:00:00 2001 From: Ujjwal Verma Date: Fri, 10 Feb 2017 01:26:06 +0530 Subject: [PATCH] Closes #1580 Gives warning when explicit proxy configured in transparent mode (#1996) --- mitmproxy/proxy/protocol/http.py | 15 +++++++++++---- test/mitmproxy/protocol/test_http1.py | 10 ++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/mitmproxy/proxy/protocol/http.py b/mitmproxy/proxy/protocol/http.py index b70afe333..da9a87813 100644 --- a/mitmproxy/proxy/protocol/http.py +++ b/mitmproxy/proxy/protocol/http.py @@ -131,7 +131,7 @@ class HTTPMode(enum.Enum): # At this point, we see only a subset of the proxy modes MODE_REQUEST_FORMS = { HTTPMode.regular: ("authority", "absolute"), - HTTPMode.transparent: ("relative"), + HTTPMode.transparent: ("relative",), HTTPMode.upstream: ("authority", "absolute"), } @@ -143,9 +143,16 @@ def validate_request_form(mode, request): ) allowed_request_forms = MODE_REQUEST_FORMS[mode] if request.first_line_format not in allowed_request_forms: - err_message = "Invalid HTTP request form (expected: %s, got: %s)" % ( - " or ".join(allowed_request_forms), request.first_line_format - ) + if mode == HTTPMode.transparent: + err_message = ( + "Mitmproxy received an {} request even though it is not running in regular mode. " + "This usually indicates a misconfiguration, please see " + "http://docs.mitmproxy.org/en/stable/modes.html for details." + ).format("HTTP CONNECT" if request.first_line_format == "authority" else "absolute-form") + else: + err_message = "Invalid HTTP request form (expected: %s, got: %s)" % ( + " or ".join(allowed_request_forms), request.first_line_format + ) raise exceptions.HttpException(err_message) diff --git a/test/mitmproxy/protocol/test_http1.py b/test/mitmproxy/protocol/test_http1.py index cd937adad..44a9effa3 100644 --- a/test/mitmproxy/protocol/test_http1.py +++ b/test/mitmproxy/protocol/test_http1.py @@ -30,6 +30,16 @@ class TestInvalidRequests(tservers.HTTPProxyTest): assert b"Invalid HTTP request form" in r.content +class TestProxyMisconfiguration(tservers.TransparentProxyTest): + + def test_absolute_request(self): + p = self.pathoc() + with p.connect(): + r = p.request("get:'http://localhost:%d/p/200'" % self.server.port) + assert r.status_code == 400 + assert b"misconfiguration" in r.content + + class TestExpectHeader(tservers.HTTPProxyTest): def test_simple(self):