Skip to content

Commit 82d0c26

Browse files
committed
bugfix: using error codes (ngx.ERROR or >=300) in ngx.exit() in header_filter_by_lua* might lead to Lua stack overflow.
1 parent d427d9a commit 82d0c26

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/ngx_http_lua_headerfilterby.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ ngx_http_lua_header_filter_by_chunk(lua_State *L, ngx_http_request_t *r)
131131
dd("exited: %d, exit code: %d, old exit code: %d",
132132
(int) ctx->exited, (int) ctx->exit_code, (int) old_exit_code);
133133

134+
#if 1
135+
/* clear Lua stack */
136+
lua_settop(L, 0);
137+
#endif
138+
134139
if (ctx->exited && ctx->exit_code != old_exit_code) {
135140
if (ctx->exit_code == NGX_ERROR) {
136141
return NGX_ERROR;
@@ -147,9 +152,6 @@ ngx_http_lua_header_filter_by_chunk(lua_State *L, ngx_http_request_t *r)
147152
return NGX_DECLINED;
148153
}
149154

150-
/* clear Lua stack */
151-
lua_settop(L, 0);
152-
153155
return NGX_OK;
154156
}
155157

t/041-header-filter.t

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,3 +767,28 @@ GET /lua?a=1&b=2
767767
--- error_log eval
768768
qr/failed to load external Lua file ".*?test2\.lua": cannot open .*? No such file or directory/
769769

770+
771+
772+
=== TEST 41: filter finalize
773+
--- config
774+
error_page 582 = /bar;
775+
location = /t {
776+
echo ok;
777+
header_filter_by_lua '
778+
return ngx.exit(582)
779+
';
780+
}
781+
782+
location = /bar {
783+
echo hi;
784+
header_filter_by_lua '
785+
return ngx.exit(302)
786+
';
787+
}
788+
--- request
789+
GET /t
790+
--- response_body_like: 302 Found
791+
--- error_code: 302
792+
--- no_error_log
793+
[error]
794+

0 commit comments

Comments
 (0)