Skip to content

Commit e5248aa

Browse files
bugfix: fixed HTTP HEAD request smuggling issue.
1 parent 6394deb commit e5248aa

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

src/ngx_http_lua_util.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,12 @@ ngx_http_lua_send_chain_link(ngx_http_request_t *r, ngx_http_lua_ctx_t *ctx,
599599
if (r->header_only) {
600600
ctx->eof = 1;
601601

602+
if (!r->request_body && r == r->main) {
603+
if (ngx_http_discard_request_body(r) != NGX_OK) {
604+
return NGX_ERROR;
605+
}
606+
}
607+
602608
if (ctx->buffering) {
603609
return ngx_http_lua_send_http10_headers(r, ctx);
604610
}

t/020-subrequest.t

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3527,3 +3527,83 @@ HTTP/1.1 400 Bad Request
35273527
[error]
35283528
--- skip_nginx
35293529
3: < 1.21.1
3530+
3531+
3532+
3533+
=== TEST 83: avoid request smuggling of HEAD req
3534+
--- config
3535+
location /capture {
3536+
server_tokens off;
3537+
more_clear_headers Date;
3538+
3539+
content_by_lua_block {
3540+
ngx.say("Hello")
3541+
}
3542+
}
3543+
3544+
location /t {
3545+
content_by_lua_block {
3546+
local req = [[
3547+
HEAD /capture HTTP/1.1
3548+
Host: test.com
3549+
Content-Length: 63
3550+
3551+
GET /capture HTTP/1.1
3552+
Host: test.com
3553+
X: GET /bar HTTP/1.0
3554+
3555+
]]
3556+
3557+
local sock = ngx.socket.tcp()
3558+
sock:settimeout(1000)
3559+
3560+
local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_SERVER_PORT)
3561+
if not ok then
3562+
ngx.say("failed to connect: ", err)
3563+
return
3564+
end
3565+
3566+
local bytes, err = sock:send(req)
3567+
if not bytes then
3568+
ngx.say("failed to send req: ", err)
3569+
return
3570+
end
3571+
3572+
ngx.say("req bytes: ", bytes)
3573+
3574+
local n_resp = 0
3575+
3576+
local reader = sock:receiveuntil("\r\n")
3577+
while true do
3578+
local line, err = reader()
3579+
if line then
3580+
ngx.say(line)
3581+
if line == "0" then
3582+
n_resp = n_resp + 1
3583+
end
3584+
3585+
if n_resp >= 2 then
3586+
break
3587+
end
3588+
3589+
else
3590+
ngx.say("err: ", err)
3591+
break
3592+
end
3593+
end
3594+
3595+
sock:close()
3596+
}
3597+
}
3598+
--- request
3599+
GET /t
3600+
--- response_body
3601+
req bytes: 117
3602+
HTTP/1.1 200 OK
3603+
Server: nginx
3604+
Content-Type: text/plain
3605+
Connection: keep-alive
3606+
3607+
err: timeout
3608+
--- error_log
3609+
lua tcp socket read timed out

0 commit comments

Comments
 (0)