openresty / openresty/lua-nginx-module

May ngx_http_lua_ngx_req_discard_body let lua handle the return code?

Open
#1,320 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
11.8k
Forks
2.1k
Avg merge
6h 1m
Merged PRs (30d)
6

Description

Hello,
When we use ngx.req.discard_body API. We have some error log like below:

[error] 27954#0: *44703042 client sent invalid chunked body, client: ***, server: ****, request: *****
[error] 27954#0: *44703042 lua entry thread aborted: runtime error: ***.lua:**: failed to discard request body
stack traceback:
coroutine 0:
    [C]: in function 'discard_body'
    ***.lua:**: in function <***.lua:**>, client: ****, server: ***, request: "***", host: "****"

As a result, nginx returns 500 to those clients.
After check out code in nginx and ngx-lua-module, I found ngx_http_lua_ngx_req_discard_body can only return two ways after call ngx_http_discard_request_body: ok or raise an error. When rc is NGX_HTTP_BAD_REQUEST it will raise an error which lead to send 500 to clients. In my opinion we should send 400 to clients in this case, rather than 500.

static int
ngx_http_lua_ngx_req_discard_body(lua_State *L)
{
    ngx_http_request_t          *r;
    ngx_int_t                    rc;
    int                          n;

    n = lua_gettop(L);

    if (n != 0) {
	return luaL_error(L, "expecting 0 arguments but seen %d", n);
    }

    r = ngx_http_lua_get_req(L);
    if (r == NULL) {
	return luaL_error(L, "request object not found");
    }

    ngx_http_lua_check_fake_request(L, r);

    rc = ngx_http_discard_request_body(r); // <==  rc = NGX_HTTP_BAD_REQUEST

    if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
	return luaL_error(L, "failed to discard request body");
    }

    return 0;
}

ngx_http_discard_request_body_filter {
    ....
    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
	          "client sent invalid chunked body");

    return NGX_HTTP_BAD_REQUEST;
    ....
}

Is there any way we can deal with it or we can work around it?

Thank you very much!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with ngx_http_lua_ngx_req_discard_body in the ngx-lua module and compare its handling of ngx_http_discard_request_body with the NGX_HTTP_BAD_REQUEST path shown in the issue. Review the nginx discard-body filter behavior and determine how the requested client-visible 400 response should be represented; done means the behavior is specified and covered by an appropriate regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, nginx
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.