1 commit dc2ee27c7a1908ca3157a10ad131f13644bcaea3
2 Author: Christopher Faulet <cfaulet@haproxy.com>
3 Date: Fri Jul 26 16:17:01 2019 +0200
5 BUG/MEDIUM: hlua: Check the calling direction in lua functions of the HTTP class
7 It is invalid to manipulate responses from http-request rules or to manipulate
8 requests from http-response rules. When http-request rules are evaluated, the
9 connection to server is not yet established, so there is no response at all. And
10 when http-response rules are evaluated, the request has already been sent to the
13 Now, the calling direction is checked. So functions "txn.http:req_*" can now
14 only be called from http-request rules and the functions "txn.http:res_*" can
15 only be called from http-response rules.
17 This issue was reported on Github (#190).
19 This patch must be backported to all versions since the 1.6.
21 (cherry picked from commit 84a6d5bc217a418db8efc4e76a0a32860db2c608)
22 Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
24 diff --git a/src/hlua.c b/src/hlua.c
25 index f9d1d699..21351cd6 100644
28 @@ -5346,6 +5346,9 @@ __LJMP static int hlua_http_req_get_headers(lua_State *L)
29 MAY_LJMP(check_args(L, 1, "req_get_headers"));
30 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
32 + if (htxn->dir != SMP_OPT_DIR_REQ)
33 + WILL_LJMP(lua_error(L));
35 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
38 @@ -5356,6 +5359,9 @@ __LJMP static int hlua_http_res_get_headers(lua_State *L)
39 MAY_LJMP(check_args(L, 1, "res_get_headers"));
40 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
42 + if (htxn->dir != SMP_OPT_DIR_RES)
43 + WILL_LJMP(lua_error(L));
45 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
48 @@ -5393,6 +5399,9 @@ __LJMP static int hlua_http_req_rep_hdr(lua_State *L)
49 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
50 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
52 + if (htxn->dir != SMP_OPT_DIR_REQ)
53 + WILL_LJMP(lua_error(L));
55 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
58 @@ -5403,6 +5412,9 @@ __LJMP static int hlua_http_res_rep_hdr(lua_State *L)
59 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
60 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
62 + if (htxn->dir != SMP_OPT_DIR_RES)
63 + WILL_LJMP(lua_error(L));
65 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
68 @@ -5413,6 +5425,9 @@ __LJMP static int hlua_http_req_rep_val(lua_State *L)
69 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
70 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
72 + if (htxn->dir != SMP_OPT_DIR_REQ)
73 + WILL_LJMP(lua_error(L));
75 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
78 @@ -5423,6 +5438,9 @@ __LJMP static int hlua_http_res_rep_val(lua_State *L)
79 MAY_LJMP(check_args(L, 4, "res_rep_val"));
80 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
82 + if (htxn->dir != SMP_OPT_DIR_RES)
83 + WILL_LJMP(lua_error(L));
85 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
88 @@ -5462,6 +5480,9 @@ __LJMP static int hlua_http_req_del_hdr(lua_State *L)
89 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
90 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
92 + if (htxn->dir != SMP_OPT_DIR_REQ)
93 + WILL_LJMP(lua_error(L));
95 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
98 @@ -5469,9 +5490,12 @@ __LJMP static int hlua_http_res_del_hdr(lua_State *L)
100 struct hlua_txn *htxn;
102 - MAY_LJMP(check_args(L, 2, "req_del_hdr"));
103 + MAY_LJMP(check_args(L, 2, "res_del_hdr"));
104 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
106 + if (htxn->dir != SMP_OPT_DIR_RES)
107 + WILL_LJMP(lua_error(L));
109 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
112 @@ -5523,6 +5547,9 @@ __LJMP static int hlua_http_req_add_hdr(lua_State *L)
113 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
114 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
116 + if (htxn->dir != SMP_OPT_DIR_REQ)
117 + WILL_LJMP(lua_error(L));
119 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
122 @@ -5533,6 +5560,9 @@ __LJMP static int hlua_http_res_add_hdr(lua_State *L)
123 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
124 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
126 + if (htxn->dir != SMP_OPT_DIR_RES)
127 + WILL_LJMP(lua_error(L));
129 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
132 @@ -5543,6 +5573,9 @@ static int hlua_http_req_set_hdr(lua_State *L)
133 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
134 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
136 + if (htxn->dir != SMP_OPT_DIR_REQ)
137 + WILL_LJMP(lua_error(L));
139 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
140 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
142 @@ -5554,6 +5587,9 @@ static int hlua_http_res_set_hdr(lua_State *L)
143 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
144 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
146 + if (htxn->dir != SMP_OPT_DIR_RES)
147 + WILL_LJMP(lua_error(L));
149 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
150 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
152 @@ -5565,6 +5601,9 @@ static int hlua_http_req_set_meth(lua_State *L)
154 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
156 + if (htxn->dir != SMP_OPT_DIR_REQ)
157 + WILL_LJMP(lua_error(L));
159 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
162 @@ -5576,6 +5615,9 @@ static int hlua_http_req_set_path(lua_State *L)
164 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
166 + if (htxn->dir != SMP_OPT_DIR_REQ)
167 + WILL_LJMP(lua_error(L));
169 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
172 @@ -5587,6 +5629,9 @@ static int hlua_http_req_set_query(lua_State *L)
174 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
176 + if (htxn->dir != SMP_OPT_DIR_REQ)
177 + WILL_LJMP(lua_error(L));
180 if (name_len > trash.size - 1) {
181 lua_pushboolean(L, 0);
182 @@ -5611,6 +5656,9 @@ static int hlua_http_req_set_uri(lua_State *L)
184 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
186 + if (htxn->dir != SMP_OPT_DIR_REQ)
187 + WILL_LJMP(lua_error(L));
189 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
192 @@ -5622,6 +5670,9 @@ static int hlua_http_res_set_status(lua_State *L)
193 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
194 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
196 + if (htxn->dir != SMP_OPT_DIR_RES)
197 + WILL_LJMP(lua_error(L));
199 http_set_status(code, reason, htxn->s);