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
SocketsHttpHandler does not enforce request Content-Length correctness #62258
Since this is a protocol violation and non-obvious to diagnose, I suggest that we always throw in case the Content-Length does not match the number of bytes sent. We already track the number of bytes sent (for telemetry purposes), it's just a matter of reacting to a mismatch.
I suggest that we always throw in case the Content-Length does not match the number of bytes sent.
Throwing seems reasonable, but the other question is what we send on the wire.
Seems like if the bytes sent < Content-Length, we should cancel the request (i.e. kill the HTTP/1.1 connection, send RST_STREAM for HTTP2, etc).
If bytes sent > Content-Length, things are less clear... the server may have processed the request fully up to Content-Length and reasonably assumed that the request is complete. What should we do on the client at this point? We definitely shouldn't send any more data on the wire past the Content-Length, since this will result in an invalid HTTP/1.1 connection, and for HTTP2 the result is essentially undefined. But should we also fail the request? If so, that means the user would never receive the response from the server, which seems problematic. Should we just ignore any data past Content-Length? This is a bit weird, but may be the best alternative.
Yeah, this sounds like a good thing to do on top of throwing - it may already happen as part of cleanup if we just throw, but worth confirming.
But should we also fail the request?
It indicates that the user's Content-Length calculation logic is wrong, so it seems like something one would want to know about.
It may lead to more failed requests in existing apps, but it only surfaces a preexisting issue that was certainly causing other failures/data corruption/potential request smuggling.
With HTTP/1.1, I bet even the first case with not enough data could lead to corruption if the server sends a complete response before attempting to read the entire request body (this would be unusual but certainly not unheard of). If that were to happen, the server could interpret the next request as the end of the previous response body.
Also, tearing down the connection for too much data might be even more effective than it seems at stopping the server from processing the bad HTTP/1.1 request. There's a pretty good chance you can catch this condition before Content-Length bytes are actually sent. And at least with Kestrel, the request body stream will start throwing as soon as a FIN is received even if it buffered a complete request body before then.
I think SocketsHttpHandler should tear down the HTTP/1.1 connection in both cases. That's what Kestrel does.
I was a bit concerned about this as it can be a way to do request stuffing. However as this is the client rather than the server, its not technically a security issue. Being silent about this is likely to cause application issues because there is some form of logic error on the client, and the server is likely to object.
I would suggest we throw in cases where there is this inconsistency. However it probably needs a check on which version of the runtime is targeted/vs run on as it could break older clients that were unaware of them having this problem.
Yarp will likely have a workaround but doesn't have one yet. For the most part, we're safe from sending too much content since Kestrel will only give us Content-Length bytes.
I was a bit concerned about this as it can be a way to do request stuffing. However as this is the client rather than the server, its not technically a security issue.
It's still an issue if you trust the user's input too much. Similar to newlines in header values that we fixed for .NET 6.0.
it could break older clients that were unaware of them having this problem.
How do you mean? If it throws it means they were already broken in some way (e.g. silent data corruption).
ghost added
in-prThere is an active PR which will close this issue when it is merged
Activity
geoffkizer commentedon Dec 2, 2021
You are specifically talking about Content-Length on the request, right?
This does seem like something we could do better about.
MihaZupan commentedon Dec 2, 2021
Yes
geoffkizer commentedon Dec 2, 2021
Throwing seems reasonable, but the other question is what we send on the wire.
Seems like if the bytes sent < Content-Length, we should cancel the request (i.e. kill the HTTP/1.1 connection, send RST_STREAM for HTTP2, etc).
If bytes sent > Content-Length, things are less clear... the server may have processed the request fully up to Content-Length and reasonably assumed that the request is complete. What should we do on the client at this point? We definitely shouldn't send any more data on the wire past the Content-Length, since this will result in an invalid HTTP/1.1 connection, and for HTTP2 the result is essentially undefined. But should we also fail the request? If so, that means the user would never receive the response from the server, which seems problematic. Should we just ignore any data past Content-Length? This is a bit weird, but may be the best alternative.
MihaZupan commentedon Dec 2, 2021
Yeah, this sounds like a good thing to do on top of throwing - it may already happen as part of cleanup if we just throw, but worth confirming.
It indicates that the user's
Content-Lengthcalculation logic is wrong, so it seems like something one would want to know about.It may lead to more failed requests in existing apps, but it only surfaces a preexisting issue that was certainly causing other failures/data corruption/potential request smuggling.
karelz commentedon Dec 2, 2021
Triage:
halter73 commentedon Dec 3, 2021
With HTTP/1.1, I bet even the first case with not enough data could lead to corruption if the server sends a complete response before attempting to read the entire request body (this would be unusual but certainly not unheard of). If that were to happen, the server could interpret the next request as the end of the previous response body.
Also, tearing down the connection for too much data might be even more effective than it seems at stopping the server from processing the bad HTTP/1.1 request. There's a pretty good chance you can catch this condition before
Content-Lengthbytes are actually sent. And at least with Kestrel, the request body stream will start throwing as soon as a FIN is received even if it buffered a complete request body before then.I think SocketsHttpHandler should tear down the HTTP/1.1 connection in both cases. That's what Kestrel does.
karelz commentedon Dec 7, 2021
Triage: Moving to Future as YARP has a workaround. @MihaZupan can you please confirm it is correct?
samsp-msft commentedon Dec 7, 2021
I was a bit concerned about this as it can be a way to do request stuffing. However as this is the client rather than the server, its not technically a security issue. Being silent about this is likely to cause application issues because there is some form of logic error on the client, and the server is likely to object.
I would suggest we throw in cases where there is this inconsistency. However it probably needs a check on which version of the runtime is targeted/vs run on as it could break older clients that were unaware of them having this problem.
MihaZupan commentedon Dec 7, 2021
Yarp will likely have a workaround but doesn't have one yet. For the most part, we're safe from sending too much content since Kestrel will only give us
Content-Lengthbytes.It's still an issue if you trust the user's input too much. Similar to newlines in header values that we fixed for .NET 6.0.
How do you mean? If it throws it means they were already broken in some way (e.g. silent data corruption).