]> git.99rst.org Git - openwrt-packages.git/blob
b0a5f9fc475de9c4d68508fba8f4429c0e67fabe
[openwrt-packages.git] /
1 commit dc2ee27c7a1908ca3157a10ad131f13644bcaea3
2 Author: Christopher Faulet <cfaulet@haproxy.com>
3 Date: Fri Jul 26 16:17:01 2019 +0200
4
5 BUG/MEDIUM: hlua: Check the calling direction in lua functions of the HTTP class
6
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
11 server.
12
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.
16
17 This issue was reported on Github (#190).
18
19 This patch must be backported to all versions since the 1.6.
20
21 (cherry picked from commit 84a6d5bc217a418db8efc4e76a0a32860db2c608)
22 Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
23
24 diff --git a/src/hlua.c b/src/hlua.c
25 index f9d1d699..21351cd6 100644
26 --- a/src/hlua.c
27 +++ b/src/hlua.c
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));
31
32 + if (htxn->dir != SMP_OPT_DIR_REQ)
33 + WILL_LJMP(lua_error(L));
34 +
35 return hlua_http_get_headers(L, htxn, &htxn->s->txn->req);
36 }
37
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));
41
42 + if (htxn->dir != SMP_OPT_DIR_RES)
43 + WILL_LJMP(lua_error(L));
44 +
45 return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp);
46 }
47
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));
51
52 + if (htxn->dir != SMP_OPT_DIR_REQ)
53 + WILL_LJMP(lua_error(L));
54 +
55 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR));
56 }
57
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));
61
62 + if (htxn->dir != SMP_OPT_DIR_RES)
63 + WILL_LJMP(lua_error(L));
64 +
65 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR));
66 }
67
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));
71
72 + if (htxn->dir != SMP_OPT_DIR_REQ)
73 + WILL_LJMP(lua_error(L));
74 +
75 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL));
76 }
77
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));
81
82 + if (htxn->dir != SMP_OPT_DIR_RES)
83 + WILL_LJMP(lua_error(L));
84 +
85 return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL));
86 }
87
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));
91
92 + if (htxn->dir != SMP_OPT_DIR_REQ)
93 + WILL_LJMP(lua_error(L));
94 +
95 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
96 }
97
98 @@ -5469,9 +5490,12 @@ __LJMP static int hlua_http_res_del_hdr(lua_State *L)
99 {
100 struct hlua_txn *htxn;
101
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));
105
106 + if (htxn->dir != SMP_OPT_DIR_RES)
107 + WILL_LJMP(lua_error(L));
108 +
109 return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
110 }
111
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));
115
116 + if (htxn->dir != SMP_OPT_DIR_REQ)
117 + WILL_LJMP(lua_error(L));
118 +
119 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
120 }
121
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));
125
126 + if (htxn->dir != SMP_OPT_DIR_RES)
127 + WILL_LJMP(lua_error(L));
128 +
129 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
130 }
131
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));
135
136 + if (htxn->dir != SMP_OPT_DIR_REQ)
137 + WILL_LJMP(lua_error(L));
138 +
139 hlua_http_del_hdr(L, htxn, &htxn->s->txn->req);
140 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req);
141 }
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));
145
146 + if (htxn->dir != SMP_OPT_DIR_RES)
147 + WILL_LJMP(lua_error(L));
148 +
149 hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp);
150 return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp);
151 }
152 @@ -5565,6 +5601,9 @@ static int hlua_http_req_set_meth(lua_State *L)
153 size_t name_len;
154 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
155
156 + if (htxn->dir != SMP_OPT_DIR_REQ)
157 + WILL_LJMP(lua_error(L));
158 +
159 lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1);
160 return 1;
161 }
162 @@ -5576,6 +5615,9 @@ static int hlua_http_req_set_path(lua_State *L)
163 size_t name_len;
164 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
165
166 + if (htxn->dir != SMP_OPT_DIR_REQ)
167 + WILL_LJMP(lua_error(L));
168 +
169 lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1);
170 return 1;
171 }
172 @@ -5587,6 +5629,9 @@ static int hlua_http_req_set_query(lua_State *L)
173 size_t name_len;
174 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
175
176 + if (htxn->dir != SMP_OPT_DIR_REQ)
177 + WILL_LJMP(lua_error(L));
178 +
179 /* Check length. */
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)
183 size_t name_len;
184 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
185
186 + if (htxn->dir != SMP_OPT_DIR_REQ)
187 + WILL_LJMP(lua_error(L));
188 +
189 lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1);
190 return 1;
191 }
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));
195
196 + if (htxn->dir != SMP_OPT_DIR_RES)
197 + WILL_LJMP(lua_error(L));
198 +
199 http_set_status(code, reason, htxn->s);
200 return 0;
201 }
git clone https://git.99rst.org/PROJECT