You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Received both a Transfer-Encoding and a Content-Length header, refusing as recommended in RFC 7230 Section 3.3.3. #4799
Starting with the release of mitmproxy 7.0.3, mitmproxy does not accept requests or responses anymore that include both a Content-Length and a Transfer-Encoding header. This follows the recommendation in RFC 7230 (emphasis mine):
If a message is received with both a Transfer-Encoding and a
Content-Length header field, the Transfer-Encoding overrides the
Content-Length. Such a message might indicate an attempt to
perform request smuggling (Section 9.5) or response splitting
(Section 9.4) and ought to be handled as an error. A sender MUST
remove the received Content-Length field prior to forwarding such
a message downstream.
We don't have any telemetry/analytics in mitmproxy, so we don't know how widespread requests with both headers are. To err on the side of being safe, we have decided to reject such requests for now. If this affects you, please comment below. If we find that messages with both headers are much more prevalent than we anticipate, we may return to a less strict handling.
changed the title [-][Placeholder][/-][+]Received both a Transfer-Encoding and a Content-Length header, refusing as recommended in RFC 7230 Section 3.3.3.[/+]on Sep 16, 2021
Well, this is mildly annoying. In my case, the server is sending only the Content-Encoding header, but once I intercept the response and modify it, Content-Length header gets added by mitmweb and I get this error
Thanks for the feedback @cotneit! @matthewhughes934 has PR ready at #4824 that addresses this, we should have another bug fix release for this particular issue in a few days.
While this covers (some) HTTP desync scenarios, the behavior is a bit harsh for an interception proxy use case (not only if you operate mitmproxy, while testing for desync vulns).
I found a request - hitting this logic - in the wild (originating from Knowunity Android App):
As "aborting" such requests does not fit my mitmproxy use-cases, I tried to work around this via a requestheaders hook:
def requestheaders(self, flow: http.HTTPFlow):
cl: Union[None, str] = flow.request.headers.get("Content-Length")
te: Union[None, str] = flow.request.headers.get("Transfer-Encoding")
if cl is not None and te is not None and te.lower().find("chunked") >= 0:
ctx.log.warn("Found CE and TL header in request to: " + flow.request.url)
# CL and TE header, while TE allows "chunked" encoding
# --> remove CL
del flow.request.headers["Content-Length"]
Unfortunately, this didn't work out for me
Add up - the unmodified headers of such a request, as capture from the error hook:
Solved this for myself via monkey-patching, for now. Making it work from within the requestheaders hook, would be better IMHO.
def load(self, loader):
from mitmproxy.net.http import http1
self.old_expected_http_body_size = http1.expected_http_body_size
# monkey patch 'expected_body_size' to remove CL header in case it appears along with 'TE: ...chunked...'
def new_expected_http_body_size(request: http.Request, response: Optional[http.Response] = None) -> Optional[int]:
cl: Union[None, str] = request.headers.get("Content-Length")
te: Union[None, str] = request.headers.get("Transfer-Encoding")
if cl is not None and te is not None and te.lower().find("chunked") >= 0:
ctx.log.warn("Found CE and TL header in request to: " + request.url)
# CL and TE header, while TE allows "chunked" encoding
# --> remove CL
del request.headers["Content-Length"]
return self.old_expected_http_body_size(request=request, response=response)
http1.expected_http_body_size = new_expected_http_body_size
def done(self):
if self.old_expected_http_body_size:
http1.expected_http_body_size = self.old_expected_http_body_size
Resulting request which got CL header removed (errored out in previous screenshot):
Note: I accidentally leaked the Basic-Auth realm in a previous screenshot, but consider this less of an issue for this "special service". Thus, I leave the screenshot "as is".
Thanks for sharing your workaround! If we find that there are more examples of this in practice, we should add a mitmproxy option that disables this check.
$ mitmdump --set validate_inbound_headers=False
/home/user/venv/bin/mitmdump: Boolean must be "true", "false", or have the value omitted (a synonym for "true").
Activity
[-][Placeholder][/-][+]Received both a Transfer-Encoding and a Content-Length header, refusing as recommended in RFC 7230 Section 3.3.3.[/+]cotneit commentedon Sep 28, 2021
Well, this is mildly annoying. In my case, the server is sending only the
Content-Encodingheader, but once I intercept the response and modify it,Content-Lengthheader gets added by mitmweb and I get this errormhils commentedon Sep 28, 2021
Thanks for the feedback @cotneit! @matthewhughes934 has PR ready at #4824 that addresses this, we should have another bug fix release for this particular issue in a few days.
mhils commentedon Sep 28, 2021
7.0.4 is out, which fixes this issue. :)
mame82 commentedon Nov 11, 2021
While this covers (some) HTTP desync scenarios, the behavior is a bit harsh for an interception proxy use case (not only if you operate mitmproxy, while testing for desync vulns).
I found a request - hitting this logic - in the wild (originating from

KnowunityAndroid App):As "aborting" such requests does not fit my mitmproxy use-cases, I tried to work around this via a
requestheadershook:Unfortunately, this didn't work out for me
Add up - the unmodified headers of such a request, as capture from the

errorhook:mame82 commentedon Nov 11, 2021
check_body_sizeinternallyexpected_http_body_size, while the latter raises the ValueError in caseTEandCLheaders exist in the request.Could this be prevented via (optional)
requestheadershook from above, if the hook is placed before thecheck_body_sizecall, here: https://github.com/mitmproxy/mitmproxy/blob/7e24e77ac4d72183c2181b46364d3df3be6aed6a/mitmproxy/proxy/layers/http/__init__.py#L198L201 ?mame82 commentedon Nov 11, 2021
Solved this for myself via monkey-patching, for now. Making it work from within the
requestheadershook, would be better IMHO.Resulting request which got CL header removed (errored out in previous screenshot):

Note: I accidentally leaked the Basic-Auth realm in a previous screenshot, but consider this less of an issue for this "special service". Thus, I leave the screenshot "as is".
mhils commentedon Nov 12, 2021
Thanks for sharing your workaround! If we find that there are more examples of this in practice, we should add a mitmproxy option that disables this check.
mitmproxy/mitmproxy/proxy/layers/http/_http1.py
Lines 236 to 237 in df32d61
mhils commentedon Mar 19, 2022
mitmproxy 8 now has a
validate_inbound_headersoption which allows to disable this check. 😃DUOLabs333 commentedon Apr 21, 2022
Still fails for me even with the option with https://redmine.lighttpd.net/boards/2/topics/7425.
mhils commentedon Apr 22, 2022
@DUOLabs333: The following works fine for me.
DUOLabs333 commentedon Apr 22, 2022
Oh, I put False, should that still work?
mhils commentedon Apr 22, 2022
DUOLabs333 commentedon Apr 22, 2022
Weird, it allowed it for me, I'll try again.
DUOLabs333 commentedon Apr 22, 2022
Yes, it works for me now. Don't know why it didn't say anything the last time.