Skip to content

Commit 2615baf

Browse files
GordonMcKinneyzhuizhuhaomeng
authored andcommitted
bugfix: segment fault when get header via ngx.req.raw_header with malformed requests.
Co-authored-by: lijunlong@openresty.com
1 parent 8cf9c63 commit 2615baf

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/ngx_http_lua_headers.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ ngx_http_lua_ngx_req_raw_header(lua_State *L)
167167
size = 0;
168168
b = c->buffer;
169169

170+
if (mr->request_line.len == 0) {
171+
/* return empty string on invalid request */
172+
lua_pushlstring(L, "", 0);
173+
return 1;
174+
}
175+
170176
if (mr->request_line.data[mr->request_line.len] == CR) {
171177
line_break_len = 2;
172178

t/104-req-raw-header.t

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,3 +1017,36 @@ client sent invalid header line: "\x20..." while reading client request headers
10171017
[error]
10181018
--- skip_nginx
10191019
3: < 1.21.1
1020+
1021+
1022+
1023+
=== TEST 35: bugfix: invalid http request
1024+
--- log_level: error
1025+
--- http_config
1026+
lua_package_path "../lua-resty-core/lib/?.lua;;";
1027+
--- config
1028+
location /t {
1029+
content_by_lua_block {
1030+
local sock = ngx.socket.tcp()
1031+
local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_SERVER_PORT)
1032+
if not ok then
1033+
ngx.log(ngx.ERR, "failed to connect to memc: ", err)
1034+
return
1035+
end
1036+
sock:send("\n")
1037+
sock:close()
1038+
1039+
ngx.say("OK")
1040+
}
1041+
}
1042+
1043+
log_by_lua_block {
1044+
local h = ngx.req.raw_header()
1045+
}
1046+
1047+
--- request
1048+
GET /t
1049+
--- response_body
1050+
OK
1051+
--- no_error_log
1052+
[error]

0 commit comments

Comments
 (0)