Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes: modify ngx.read_body api limitation in HTTP/2 and HTTP/3 #2237

Merged

Conversation

swananan
Copy link
Contributor

I hereby granted the copyright of the changes in this pull request
to the authors of this lua-nginx-module project.

#2174 mentioned an issue where there is a chance of blocking the ngx.read_body API when using HTTP/2. As a result, #2174 disabled the ngx.read_body API in HTTP/2. However, #2211 also suggested that perhaps we should not completely disable the ngx.read_body API in HTTP/2, as many HTTP/2 requests could still benefit from this.

To address this, I attempted to define the minimum limitations for using ngx.read_body in HTTP/2. From what I understand, ngx.read_body is based on the Nginx core to handle the client's body, and the Nginx core typically reads the entire client body before calling the callback registered by the lua-nginx-module. There are two scenarios in which HTTP/2 can trigger this callback. First, when the Content-Length header is used, Nginx core can determine the exact number of bytes to read from the client side. The second scenario is when an HTTP/2 request sends the entire body and immediately closes the HTTP/2 stream. Additionally, HTTP/2 does not support the chunked transfer encoding.

The first scenario is relatively straightforward to detect in the lua-nginx-module. However, the second scenario is challenging to identify because Nginx core takes over the remaining part, and the lua-nginx-module can only wait to be called by the Nginx core. Therefore, in this pull request, I propose to only allow HTTP/2 requests that carry the Content-Length header. So in the second scenario, If someone want to use ngx.read_body, need to add the Content-Length header.

I also considered adding a timeout option for the read_body API. However, I decided against it for two reasons:

Nginx already has a timeout option for reading the client's body, which is configured using client_body_timeout. This configuration terminates the HTTP request when a timeout occurs. Introducing another timeout option, which would keep the rest of the Lua thread logic running, seemed redundant.

Secondly, I believe that scenarios causing the blocking of the ngx.read_body API should not use ngx.read_body. Adding a timeout option may not immediately expose the real problem.

It's worth noting that HTTP/3 faces a similar problem, and this pr implemented the same solution for it.

if (r->http_version == NGX_HTTP_VERSION_30
&& r->headers_in.content_length_n < 0)
{
ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
Copy link

Choose a reason for hiding this comment

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

http3?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, HTTP/3 has same problem.

@@ -2724,7 +2724,7 @@ lua_need_request_body

**phase:** *depends on usage*

Due to the stream processing feature of HTTP2, it does not support HTTP2 connection.
Due to the stream processing feature of HTTP/2 or HTTP/3, this configuration could potentially block the entire request. Therefore, this configuration is effective only when HTTP/2 or HTTP/3 requests send content-length header. For requests with versions lower than HTTP/2, this configuration can still be used without any problems.
Copy link
Contributor

@oowl oowl Oct 17, 2023

Choose a reason for hiding this comment

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

We have discussed this problem and reached an agreement that enables read_body API in the HTTP2 request contain the content-length header
And why chunk-transfer not enabled? from RFC

In https://greenbytes.de/tech/webdav/rfc7540.html#rfc.section.8.1.p.4:

    HTTP/2 uses DATA frames to carry message payloads. The chunked transfer encoding defined in Section 4.1 of [RFC7230] MUST NOT be used in HTTP/2.

Copy link
Contributor

Choose a reason for hiding this comment

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

@zhuizhuhaomeng Hi, Can you help us take a look at this PR?

@swananan swananan changed the title Modify read body api limitation in HTTP/2 and HTTP/3 changes: modify ngx.read_body api limitation in HTTP/2 and HTTP/3 Oct 17, 2023
@swananan swananan force-pushed the modify_read_body_api_limitation branch from 432bed9 to 4af043a Compare October 29, 2023 03:06
@zhuizhuhaomeng zhuizhuhaomeng merged commit 86bea01 into openresty:master Nov 5, 2023
2 checks passed
ogercyprien added a commit to opendatasoft/lua-resty-waf that referenced this pull request Jan 31, 2024
…ength header

This check ensures that the underlying lua-nginx-module will not crash
when trying to fetch the body of a POST request made with HTTP2 or HTTP3
with an unspecified Content-Length header.
See openresty/lua-nginx-module#2237
ogercyprien added a commit to opendatasoft/lua-resty-waf that referenced this pull request Feb 1, 2024
This check ensures that the underlying lua-nginx-module will not crash
when trying to fetch the body of a POST request made with HTTP2 or HTTP3
with an unspecified Content-Length header.
See openresty/lua-nginx-module#2237
@oowl oowl mentioned this pull request Feb 26, 2024
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.

4 participants