Kestrel: Unlimited request body content-length if no content-length supplied #65043
Unanswered
Danielku15
asked this question in
Q&A
Replies: 1 comment 4 replies
-
|
Hi, In HTTP/1.1, if a request has neither Content-Length nor Transfer-Encoding: chunked, the body length is defined as zero. Kestrel is enforcing that interpretation deliberately to avoid request smuggling and connection desync issues. Treating “connection close” as an implicit unlimited body is explicitly discouraged in RFC 9110, even if some clients do it in practice. Kestrel isn’t being inflexible. It’s enforcing HTTP correctness and security. If the client can’t be changed, HTTP/1.1 + Kestrel is the wrong abstraction boundary for this scenario. Practical options:
|
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey folks. 👋
Kestrel seem to have a behavior regarding the Content-Length of a request body which is causing limitations in some streaming scenarios.
Very concrete my problem is here located here in Kestrel:
aspnetcore/src/Servers/Kestrel/Core/src/Internal/Http/Http1MessageBody.cs
Line 215 in 5e25f15
It prevents any reading of the body even though there might be data which is not explicitly defined by a content length.
Looking at the HTTP spec, it is not 100% strictly defined whether there still could be a body even though the client does not send a Content-Length header. Clients should send the header but it's not a must.
https://www.rfc-editor.org/rfc/rfc9110.html#section-8.6
In my use-case this is exactly happening: A client streams a blob without specifying the length as it is not basically unlimited. Unfortunately in this domain (GNSS) this is a common practice and widely implemented in the clients. This is due to (IMO) poor application level protocol specs from the responsible consortium. Hence I cannot change the client side.
Unfortunately the logic of of handling the body stream is buried very deep in Kestrel, everything is internal and there are no obvious extension points. I was hoping to get some community/collaborator assistance here or even get the ball rolling for some adaptions in Kestrel.
Goal 1: I would like to reuse Kestrel as HTTP server in my ASP.net core app with all its networking and communication infrastructure.
Goal 2: I want to read the unlimited length request body using the
BodyReadereven though the client is using NEITHER chunked encoding, nor specifying aContent-Lengthheader.Problem 1: The whole HTTP protocol implementations are internal and there is no way to extend the related body creation using.
Problem 2: Compiling the whole kestrel library myself is not feasible due to the complexity in this project and patching out the related bit is not feasible from a maintenance perspective.
Problem 3: Patching the method with tools like Harmony seems very risky.
Idea 1: The easiest for me would be if Kestrel would allow reading of the body in such cases. e.g. it could simply use a
new Http1ContentUnlimitedLengthMessageBody(context, keepAlive)which is similar toHttp1ContentLengthMessageBodybut without an upper limit (e.g. long.MaxValue).Idea 2: Kestrel could provide some callback which allows me to modify the
HttpRequestHeadersbefore constructing the body. I could simply injectlong.MaxValue.Idea 3: Kestrel could expose the HTTP protocol implementations (e.g. via separate package having the types public?) + a factory constructing the connections here.
My current, very ugly workaround is to obtain a reader to the underlying connection like this:
Any help and feedback is highly appreciated.
Beta Was this translation helpful? Give feedback.
All reactions