commit dc2ee27c7a1908ca3157a10ad131f13644bcaea3 Author: Christopher Faulet Date: Fri Jul 26 16:17:01 2019 +0200 BUG/MEDIUM: hlua: Check the calling direction in lua functions of the HTTP class It is invalid to manipulate responses from http-request rules or to manipulate requests from http-response rules. When http-request rules are evaluated, the connection to server is not yet established, so there is no response at all. And when http-response rules are evaluated, the request has already been sent to the server. Now, the calling direction is checked. So functions "txn.http:req_*" can now only be called from http-request rules and the functions "txn.http:res_*" can only be called from http-response rules. This issue was reported on Github (#190). This patch must be backported to all versions since the 1.6. (cherry picked from commit 84a6d5bc217a418db8efc4e76a0a32860db2c608) Signed-off-by: Christopher Faulet diff --git a/src/hlua.c b/src/hlua.c index f9d1d699..21351cd6 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -5346,6 +5346,9 @@ __LJMP static int hlua_http_req_get_headers(lua_State *L) MAY_LJMP(check_args(L, 1, "req_get_headers")); htxn = MAY_LJMP(hlua_checkhttp(L, 1)); + if (htxn->dir != SMP_OPT_DIR_REQ) + WILL_LJMP(lua_error(L)); + return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); } @@ -5356,6 +5359,9 @@ __LJMP static int hlua_http_res_get_headers(lua_State *L) MAY_LJMP(check_args(L, 1, "res_get_headers")); htxn = MAY_LJMP(hlua_checkhttp(L, 1)); + if (htxn->dir != SMP_OPT_DIR_RES) + WILL_LJMP(lua_error(L)); + return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); } @@ -5393,6 +5399,9 @@ __LJMP static int hlua_http_req_rep_hdr(lua_State *L) MAY_LJMP(check_args(L, 4, "req_rep_hdr")); htxn = MAY_LJMP(hlua_checkhttp(L, 1)); + if (htxn->dir != SMP_OPT_DIR_REQ) + WILL_LJMP(lua_error(L)); + return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); } @@ -5403,6 +5412,9 @@ __LJMP static int hlua_http_res_rep_hdr(lua_State *L) MAY_LJMP(check_args(L, 4, "res_rep_hdr")); htxn = MAY_LJMP(hlua_checkhttp(L, 1)); + if (htxn->dir != SMP_OPT_DIR_RES) + WILL_LJMP(lua_error(L)); + return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); } @@ -5413,6 +5425,9 @@ __LJMP static int hlua_http_req_rep_val(lua_State *L) MAY_LJMP(check_args(L, 4, "req_rep_hdr")); htxn = MAY_LJMP(hlua_checkhttp(L, 1)); + if (htxn->dir != SMP_OPT_DIR_REQ) + WILL_LJMP(lua_error(L)); + return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); } @@ -5423,6 +5438,9 @@ __LJMP static int hlua_http_res_rep_val(lua_State *L) MAY_LJMP(check_args(L, 4, "res_rep_val")); htxn = MAY_LJMP(hlua_checkhttp(L, 1)); + if (htxn->dir != SMP_OPT_DIR_RES) + WILL_LJMP(lua_error(L)); + return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL)); } @@ -5462,6 +5480,9 @@ __LJMP static int hlua_http_req_del_hdr(lua_State *L) MAY_LJMP(check_args(L, 2, "req_del_hdr")); htxn = MAY_LJMP(hlua_checkhttp(L, 1)); + if (htxn->dir != SMP_OPT_DIR_REQ) + WILL_LJMP(lua_error(L)); + return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); } @@ -5469,9 +5490,12 @@ __LJMP static int hlua_http_res_del_hdr(lua_State *L) { struct hlua_txn *htxn; - MAY_LJMP(check_args(L, 2, "req_del_hdr")); + MAY_LJMP(check_args(L, 2, "res_del_hdr")); htxn = MAY_LJMP(hlua_checkhttp(L, 1)); + if (htxn->dir != SMP_OPT_DIR_RES) + WILL_LJMP(lua_error(L)); + return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); } @@ -5523,6 +5547,9 @@ __LJMP static int hlua_http_req_add_hdr(lua_State *L) MAY_LJMP(check_args(L, 3, "req_add_hdr")); htxn = MAY_LJMP(hlua_checkhttp(L, 1)); + if (htxn->dir != SMP_OPT_DIR_REQ) + WILL_LJMP(lua_error(L)); + return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); } @@ -5533,6 +5560,9 @@ __LJMP static int hlua_http_res_add_hdr(lua_State *L) MAY_LJMP(check_args(L, 3, "res_add_hdr")); htxn = MAY_LJMP(hlua_checkhttp(L, 1)); + if (htxn->dir != SMP_OPT_DIR_RES) + WILL_LJMP(lua_error(L)); + return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); } @@ -5543,6 +5573,9 @@ static int hlua_http_req_set_hdr(lua_State *L) MAY_LJMP(check_args(L, 3, "req_set_hdr")); htxn = MAY_LJMP(hlua_checkhttp(L, 1)); + if (htxn->dir != SMP_OPT_DIR_REQ) + WILL_LJMP(lua_error(L)); + hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); } @@ -5554,6 +5587,9 @@ static int hlua_http_res_set_hdr(lua_State *L) MAY_LJMP(check_args(L, 3, "res_set_hdr")); htxn = MAY_LJMP(hlua_checkhttp(L, 1)); + if (htxn->dir != SMP_OPT_DIR_RES) + WILL_LJMP(lua_error(L)); + hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); } @@ -5565,6 +5601,9 @@ static int hlua_http_req_set_meth(lua_State *L) size_t name_len; const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); + if (htxn->dir != SMP_OPT_DIR_REQ) + WILL_LJMP(lua_error(L)); + lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1); return 1; } @@ -5576,6 +5615,9 @@ static int hlua_http_req_set_path(lua_State *L) size_t name_len; const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); + if (htxn->dir != SMP_OPT_DIR_REQ) + WILL_LJMP(lua_error(L)); + lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1); return 1; } @@ -5587,6 +5629,9 @@ static int hlua_http_req_set_query(lua_State *L) size_t name_len; const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); + if (htxn->dir != SMP_OPT_DIR_REQ) + WILL_LJMP(lua_error(L)); + /* Check length. */ if (name_len > trash.size - 1) { lua_pushboolean(L, 0); @@ -5611,6 +5656,9 @@ static int hlua_http_req_set_uri(lua_State *L) size_t name_len; const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); + if (htxn->dir != SMP_OPT_DIR_REQ) + WILL_LJMP(lua_error(L)); + lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1); return 1; } @@ -5622,6 +5670,9 @@ static int hlua_http_res_set_status(lua_State *L) unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); + if (htxn->dir != SMP_OPT_DIR_RES) + WILL_LJMP(lua_error(L)); + http_set_status(code, reason, htxn->s); return 0; }