Closes #1580 Gives warning when explicit proxy configured in transparent mode (#1996)

This commit is contained in:
Ujjwal Verma 2017-02-10 01:26:06 +05:30 committed by Maximilian Hils
parent 380ff50e57
commit d6465b907f
2 changed files with 21 additions and 4 deletions

View File

@ -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)

View File

@ -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):