Skip to content

Commit b67b22a

Browse files
thibaultchazhuizhuhaomeng
authored andcommitted
feature: prevent calling ngx.exit() with invalid values.
1 parent 0b6da3b commit b67b22a

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

src/ngx_http_lua_control.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,14 @@ ngx_http_lua_ffi_exit(ngx_http_request_t *r, int status, u_char *err,
358358
{
359359
ngx_http_lua_ctx_t *ctx;
360360

361+
if (status == NGX_AGAIN || status == NGX_DONE) {
362+
*errlen = ngx_snprintf(err, *errlen,
363+
"bad argument to 'ngx.exit': does not accept "
364+
"NGX_AGAIN or NGX_DONE")
365+
- err;
366+
return NGX_ERROR;
367+
}
368+
361369
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
362370
if (ctx == NULL) {
363371
*errlen = ngx_snprintf(err, *errlen, "no request ctx found") - err;

t/005-exit.t

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,3 +740,82 @@ GET /lua
740740
--- response_body_like: 501 (?:Method )?Not Implemented
741741
--- no_error_log
742742
[error]
743+
744+
745+
746+
=== TEST 26: accepts NGX_OK
747+
--- config
748+
location = /t {
749+
content_by_lua_block {
750+
ngx.exit(ngx.OK)
751+
}
752+
}
753+
--- request
754+
GET /t
755+
--- response_body
756+
--- no_error_log
757+
[error]
758+
759+
760+
761+
=== TEST 27: accepts NGX_ERROR
762+
--- config
763+
location = /t {
764+
content_by_lua_block {
765+
ngx.exit(ngx.ERROR)
766+
}
767+
}
768+
--- request
769+
GET /t
770+
--- error_code:
771+
--- response_body
772+
--- no_error_log
773+
[error]
774+
775+
776+
777+
=== TEST 28: accepts NGX_DECLINED
778+
--- config
779+
location = /t {
780+
content_by_lua_block {
781+
ngx.exit(ngx.DECLINED)
782+
}
783+
}
784+
--- request
785+
GET /t
786+
--- error_code:
787+
--- response_body
788+
--- no_error_log
789+
[error]
790+
791+
792+
793+
=== TEST 29: refuses NGX_AGAIN
794+
--- config
795+
location = /t {
796+
content_by_lua_block {
797+
ngx.exit(ngx.AGAIN)
798+
}
799+
}
800+
--- request
801+
GET /t
802+
--- error_code: 500
803+
--- response_body_like: 500 Internal Server Error
804+
--- error_log eval
805+
qr/\[error\] .*? bad argument to 'ngx.exit': does not accept NGX_AGAIN or NGX_DONE/
806+
807+
808+
809+
=== TEST 30: refuses NGX_DONE
810+
--- config
811+
location = /t {
812+
content_by_lua_block {
813+
ngx.exit(ngx.DONE)
814+
}
815+
}
816+
--- request
817+
GET /t
818+
--- error_code: 500
819+
--- response_body_like: 500 Internal Server Error
820+
--- error_log eval
821+
qr/\[error\] .*? bad argument to 'ngx.exit': does not accept NGX_AGAIN or NGX_DONE/

0 commit comments

Comments
 (0)