Skip to content

Don't set 'content-length' with 'transfer-encoding' - #4827

Merged
mhils merged 2 commits into
mitmproxy:mainfrom
matthewhughes934:fix-crash-when-modifying-chunked-response
Sep 28, 2021
Merged

mhils merged 2 commits into
mitmproxy:mainfrom
matthewhughes934:fix-crash-when-modifying-chunked-response

Conversation

@matthewhughes934

Copy link
Copy Markdown
Contributor

When updating the response content for a response, avoid adding the
'content-length' header if the response contains a 'transfer-encoding'
header, from the spec [1]:

When a message does not have a Transfer-Encoding header field, a
Content-Length header field can provide the anticipated size, as a
decimal number of octets, for a potential payload body

Note the 'transfer-encoding' header is not used with HTTP/2

https://httpwg.org/specs/rfc7230.html#header.content-length

Description

Checklist

  • I have updated tests where applicable.
  • I have added an entry to the CHANGELOG.

Comment thread mitmproxy/http.py Outdated
del self.headers["content-encoding"]
self.raw_content = value
self.headers["content-length"] = str(len(self.raw_content))
if not "transfer-encoding" in self.headers:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not sure if this should be: if self.is_http2 or not "transfer-encoding" in self.headers: since we probably still want to set content-length if (the unused/unsupported) "transfer-encoding" header is given in a HTTP/2 request. What do you think?

@mhils mhils Sep 27, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTTP/2 does not have transfer-encoding headers (we would reject an incoming request with one), so I think it's really good enough to do the check below.

@mhils

mhils commented Sep 27, 2021

Copy link
Copy Markdown
Member

Thanks for tackling this!

Let's copy this check here so that things are consistent:

if "chunked" in headers.get("transfer-encoding", "").lower():

A stylistic device I like to use for cases like this is to have an explicit pass case with documentation: That emphasizes the intent here and also makes sure that we have test coverage for both cases:

if "chunked" in headers.get("transfer-encoding", "").lower(): 
    pass  # http://tools.ietf.org/html/rfc7230#section-3.3: do not add a content-length header to a chunked message.
else:
    self.headers["content-length"] = str(len(self.raw_content))

@mhils

mhils commented Sep 27, 2021

Copy link
Copy Markdown
Member

I've pushed the second part for #4824 on top of your commit so that we have everything in one place. :)

@matthewhughes934

Copy link
Copy Markdown
Contributor Author

Thanks for the review!

I've pushed the second part for #4824 on top of your commit so that we have everything in one place. :)

Perfect, thanks

A stylistic device I like to use for cases like this is to have an explicit pass case with documentation

Good idea, done with c5036d7

Let's copy this check here so that things are consistent:

I think these are separate checks: I said in the issue that this was only for 'chunked' transfer encodings, but I believe it's for any request with a transfer-encoding set

@mhils

mhils commented Sep 28, 2021

Copy link
Copy Markdown
Member

Ah, yes, you are right. Thanks for checking this so thoroughly! 🍰

matthewhughes934 and others added 2 commits September 28, 2021 18:31
When updating the response content for a response, avoid adding the
'content-length' header if the response contains a 'transfer-encoding'
header, from the spec [1]:

> When a message does not have a Transfer-Encoding header field, a
Content-Length header field can provide the anticipated size, as a
decimal number of octets, for a potential payload body

Note the 'transfer-encoding' header is not used with HTTP/2

https://httpwg.org/specs/rfc7230.html#header.content-length
@mhils
mhils force-pushed the fix-crash-when-modifying-chunked-response branch from c5036d7 to 4a3fefd Compare September 28, 2021 16:31
@mhils
mhils enabled auto-merge September 28, 2021 16:31
@mhils
mhils disabled auto-merge September 28, 2021 16:38
@mhils
mhils merged commit 1919c0d into mitmproxy:main Sep 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants