Skip to content

Received both a Transfer-Encoding and a Content-Length header, refusing as recommended in RFC 7230 Section 3.3.3. #4799

Closed

Description

@mhils

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.

Activity

  1. 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
  2. cotneit commented on Sep 28, 2021

    @cotneit

    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

  3. mhils commented on Sep 28, 2021

    @mhils
    MemberAuthor

    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.

  4. mhils commented on Sep 28, 2021

    @mhils
    MemberAuthor

    7.0.4 is out, which fixes this issue. :)

  5. mame82 commented on Nov 11, 2021

    @mame82
    Contributor

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

    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:
    mitmproxy2

  6. mame82 commented on Nov 11, 2021

    @mame82
    Contributor

    check_body_size internally expected_http_body_size, while the latter raises the ValueError in case TE and CL headers exist in the request.

    Could this be prevented via (optional) requestheaders hook from above, if the hook is placed before the check_body_size call, here: https://github.com/mitmproxy/mitmproxy/blob/7e24e77ac4d72183c2181b46364d3df3be6aed6a/mitmproxy/proxy/layers/http/__init__.py#L198L201 ?

  7. mame82 commented on Nov 11, 2021

    @mame82
    Contributor

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

    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".

  8. mhils commented on Nov 12, 2021

    @mhils
    MemberAuthor

    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.

    self.request = http1.read_request_head(request_head)
    expected_body_size = http1.expected_http_body_size(self.request)
    is where things start to go haywire.

  9. mhils commented on Mar 19, 2022

    @mhils
    MemberAuthor

    mitmproxy 8 now has a validate_inbound_headers option which allows to disable this check. 😃

  10. DUOLabs333 commented on Apr 21, 2022

    @DUOLabs333
  11. mhils commented on Apr 22, 2022

    @mhils
    MemberAuthor

    @DUOLabs333: The following works fine for me.

    mitmdump --set validate_inbound_headers=false
    curl -x localhost:8080 -k https://redmine.lighttpd.net/boards/2/topics/7425
    
  12. DUOLabs333 commented on Apr 22, 2022

    @DUOLabs333

    Oh, I put False, should that still work?

  13. mhils commented on Apr 22, 2022

    @mhils
    MemberAuthor
    $ 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").
    
  14. DUOLabs333 commented on Apr 22, 2022

    @DUOLabs333

    Weird, it allowed it for me, I'll try again.

  15. DUOLabs333 commented on Apr 22, 2022

    @DUOLabs333

    Yes, it works for me now. Don't know why it didn't say anything the last time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions