Skip to content

MITM crash when modifying request with 'chunked' content encoding #4824

Closed

Description

@matthewhughes934

Problem Description

Traceback:

127.0.0.1:55064: mitmproxy has crashed!
<---snip--->
ValueError: Received both a Transfer-Encoding and a Content-Length header, refusing as recommended in RFC 7230 Section 3.3.3. See https://github.com/mitmproxy/mitmproxy/issues/4799 for detail

Steps to reproduce the behavior:

  1. Run either mitmproxy or mitmweb configured to intercept requests from http://anglesharp.azurewebsites.net/Chunked (just a convenient site that should always return chunked responses)
  2. Intercept a request from the above URL and modify the response in anyway, e.g. via
$ curl --output /dev/null --silent \
    --cacert ~/.mitmproxy/mitmproxy-ca-cert.pem \
    --proxy 127.0.0.1:8080 \
    http://anglesharp.azurewebsites.net/Chunked
  1. Resume sending the response

System Information

Paste the output of "mitmproxy --version" here.

$ mitmproxy --version
Mitmproxy: 8.0.0.dev (+114, commit 7e24e77)
Python:    3.8.6
OpenSSL:   OpenSSL 1.1.1l  24 Aug 2021
Platform:  Linux-5.10.0-0.bpo.8-amd64-x86_64-with-glibc2.28

More Details

Following up from #4811 (comment).

At a glance, the issue looks to be at https://github.com/mitmproxy/mitmproxy/blob/main/mitmproxy/http.py#L375 where the content-length header is unconditionally set whenever content is updated. Perhaps this just needs a simple if ce != "chunked" guard? With another glance at the spec, chunked encoding specifies the chunk size within the body of each chunk and I'm assuming once we have a response all these chunks have already been decoded, so we don't have to worry about updating this for a modified response

Activity

  1. mhils commented on Sep 24, 2021

    @mhils
    Member

    Thanks, this is an excellent catch. I think the guard you are proposing is one correct piece of the puzzle. The other part is that we should only validate inbound requests, it's fine if we send Content-Length + Transfer-Encoding upstream if a CL header is added by a user.

  2. added and removed
    kind/triage
    on Sep 25, 2021
  3. matthewhughes934 commented on Sep 26, 2021

    @matthewhughes934
    ContributorAuthor

    I think the guard you are proposing is one correct piece of the puzzle

    👍 adding with #4827

  4. matthewhughes934 commented on Sep 28, 2021

    @matthewhughes934
    ContributorAuthor

    Closing as this is fixed with the PR I linked above.

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