flask icon indicating copy to clipboard operation
flask copied to clipboard

bad content-length default

Open reteptilian opened this issue 11 years ago • 27 comments

In werkzeug's WSGIRequestHandler, CONTENT-LENGTH defaults to an empty string ('') if the client did not provide a Content-Length header.

This is causing me problems in my flask web service, which is trying to proxy requests. The user creates a request without Content-Length, flask gives it to me and then I pass it along to another server, which then returns HTTP 400 due to the bad Content-Length that werkzeug has inserted. According to RFC 2616, Content-Length must be a number.

Initially I thought this should be considered a werkzeug bug but maybe the empty string was intended to be werkzeug's signifier for a non-existent Content-Length, rather than an actual Content-Length value that could be used directly. If that's the case, then I would modify flask so that Content-Length isn't included in the incoming request's headers if it is the empty string.

Bottom line, I believe that a client request that is missing a Content-Length header should not result in an incoming flask request with the Content-Length equal to the empty string.

reteptilian avatar Dec 19 '14 20:12 reteptilian

I don't think the WSGI environment Werkzeug generates is wrong... according to https://www.python.org/dev/peps/pep-0333/#environ-variables, the empty string and the nonexistent value should be treated symonymously:

The following variables must be present, unless their value would be an empty string, in which case they may be omitted, except as otherwise noted below.

[...] May be empty or absent.

So either behavior is fine, I myself don't see a reason why we couldn't change Werkzeug's behavior to omit Content-Type (and also Content-Length) if they're == ''.

untitaker avatar Dec 19 '14 20:12 untitaker

The beginner friendly tag might be too generous, but I'm trying to tag some stuff besides documentation and this seems like a reasonably self contained fix. That is, if we can get to a consensus on whether we actually want to change Werkzeug's behavior as you suggested @untitaker.

keyan avatar Mar 29 '15 23:03 keyan

Is it the way you are creating the response? I have tested it by generating a response header without a Content-Length and then running it through flask which routes it through nginx. (Not sure exactly when this occurs naturally in Flask)

Result: When you throw the response just in flask alone, it works fine (follows PEP-333). But when you try to send it through nginx it'll crash when reading the response because it does not like what is passed, the empty content-length (follows RFC 2616). For improved compatibility with other applications, I agree with @untitaker that Flask omit empty Content-Type and or Content-Length.

KWinston avatar Mar 29 '15 23:03 KWinston

We should let this @mitsuhiko decide... maybe Werkzeug's current behavior is also due to other software's behavior.

untitaker avatar Mar 30 '15 18:03 untitaker

And what did he decide? I'd be glad to start contributing with some code.

omarkhd avatar Apr 23 '15 23:04 omarkhd

We did not yet contact him.

I'm glad that you want to help @omarkhd -- feel free to ping mitsuhiko on the #pocoo IRC channel!

untitaker avatar Apr 24 '15 06:04 untitaker

For anyone seeking a quick fix solution to this error, take a look at the Proposed Solution section under Issue 1289. Link to Proposed Solution

KWinston avatar Apr 24 '15 06:04 KWinston

I'm fine with the proposed solution, but please let @mitsuhiko confirm it, on the unlikely off-chance that this behavior is actually intended.

untitaker avatar Apr 24 '15 07:04 untitaker

@KWinston can I take this solution? Its the better way to solve the problem?

matheuspoleza avatar May 13 '16 21:05 matheuspoleza

@matheuspoleza yes, you can use the solution I provided. The proposed solution should stop your application from crashing if you are having issues with CONTENT-LENGTH not being a number.

KWinston avatar May 14 '16 07:05 KWinston

@KWinston ok, I'll do that

matheuspoleza avatar May 14 '16 15:05 matheuspoleza

@mitsuhiko : Is the proposed solution ok to implement? @KWinston : May I use your solution?

ferbecab03 avatar Dec 09 '16 16:12 ferbecab03

I do not understand the proposed solution. Where do we generate an empty content length string?

mitsuhiko avatar Dec 09 '16 16:12 mitsuhiko

In werkzeug's WSGIRequestHandler, CONTENT-LENGTH defaults to an empty string ('') if the client did not provide a Content-Length header.

When used with other applications that default to an empty string or a non-number passed through flask. It is more a compatibility issue with other apps.

Probably more of a documentation addition to incorporate this when using other apps while running flask?

ferbecab03 avatar Dec 09 '16 16:12 ferbecab03

I'm not sure I follow. Surely an empty string is a valid value for CONTENT_LENGTH so should this not be reported against whatever application fails?

mitsuhiko avatar Dec 09 '16 16:12 mitsuhiko

Can someone make a minimal test case of what is wrong and what is expected? I'm at a loss here. WSGIRequestHandler handles incoming data only. So that should be irrelevant here, no?

mitsuhiko avatar Dec 09 '16 16:12 mitsuhiko

@mitsuhiko according to my reading of rfc2616, you can either (1) have a content-length header, in which case it must be a number, or 2) if the header is not provided, it explains how to compute it. The situation here has the client does not provide the header ... but then flask/werkzeug artificially creates a header that is the empty string, which is not legal according to rfc 2616. I am traveling this weekend but can make a test case next week.

reteptilian avatar Dec 09 '16 17:12 reteptilian

The HTTP RFC is irrelevant here. An empty WSGI environ value is to be treated as nonexistent according to the WSGI PEP.

untitaker avatar Dec 09 '16 18:12 untitaker

However, if you find an abstraction over the WSGI env (within Flask/Werkzeug or outside) whose behavior differs when given an empty string instead of a nonexistent value, it's totally a bug.

untitaker avatar Dec 09 '16 18:12 untitaker

@reteptilian as @untitaker mentioned the rfc does not matter here because this is an internal header on the incoming side and we hide it. This really needs a clear testcase.

mitsuhiko avatar Dec 09 '16 18:12 mitsuhiko