blob: b77fd9361dae7ae34d72ed30e0cff8b2d25a67ca [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
2 * HTTP actions
3 *
4 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/types.h>
14
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
18
Willy Tarreaudcc048a2020-06-04 19:11:43 +020019#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020020#include <haproxy/action.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020021#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/arg.h>
23#include <haproxy/capture-t.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020024#include <haproxy/cfgparse.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020025#include <haproxy/chunk.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020026#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020027#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020028#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020029#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020030#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020031#include <haproxy/log.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020032#include <haproxy/pattern.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020033#include <haproxy/pool.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020034#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020035#include <haproxy/sample.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020036#include <haproxy/stream_interface.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020037#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020038#include <haproxy/uri_auth-t.h>
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +020039#include <haproxy/uri_normalizer.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020040#include <haproxy/version.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020041
Willy Tarreau79e57332018-10-02 16:01:16 +020042
Christopher Faulet2eb53962020-01-14 14:47:34 +010043/* Release memory allocated by most of HTTP actions. Concretly, it releases
44 * <arg.http>.
45 */
46static void release_http_action(struct act_rule *rule)
47{
48 struct logformat_node *lf, *lfb;
49
Tim Duesterhused526372020-03-05 17:56:33 +010050 istfree(&rule->arg.http.str);
Christopher Faulet2eb53962020-01-14 14:47:34 +010051 if (rule->arg.http.re)
52 regex_free(rule->arg.http.re);
53 list_for_each_entry_safe(lf, lfb, &rule->arg.http.fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +020054 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +010055 release_sample_expr(lf->expr);
56 free(lf->arg);
57 free(lf);
58 }
59}
60
Christopher Faulet5cb513a2020-05-13 17:56:56 +020061/* Release memory allocated by HTTP actions relying on an http reply. Concretly,
62 * it releases <.arg.http_reply>
63 */
64static void release_act_http_reply(struct act_rule *rule)
65{
66 release_http_reply(rule->arg.http_reply);
67 rule->arg.http_reply = NULL;
68}
69
70
71/* Check function for HTTP actions relying on an http reply. The function
72 * returns 1 in success case, otherwise, it returns 0 and err is filled.
73 */
74static int check_act_http_reply(struct act_rule *rule, struct proxy *px, char **err)
75{
76 struct http_reply *reply = rule->arg.http_reply;
77
78 if (!http_check_http_reply(reply, px, err)) {
79 release_act_http_reply(rule);
80 return 0;
81 }
82 return 1;
83}
84
Willy Tarreau79e57332018-10-02 16:01:16 +020085
86/* This function executes one of the set-{method,path,query,uri} actions. It
87 * builds a string in the trash from the specified format string. It finds
Christopher Faulet2c22a692019-12-18 15:39:56 +010088 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +050089 * parse_set_req_line(). The replacement action is executed by the function
Christopher Faulete00d06c2019-12-16 17:18:42 +010090 * http_action_set_req_line(). On success, it returns ACT_RET_CONT. If an error
91 * occurs while soft rewrites are enabled, the action is canceled, but the rule
92 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau79e57332018-10-02 16:01:16 +020093 */
94static enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
95 struct session *sess, struct stream *s, int flags)
96{
97 struct buffer *replace;
Christopher Faulet13403762019-12-13 09:01:57 +010098 enum act_return ret = ACT_RET_CONT;
Willy Tarreau79e57332018-10-02 16:01:16 +020099
100 replace = alloc_trash_chunk();
101 if (!replace)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100102 goto fail_alloc;
Willy Tarreau79e57332018-10-02 16:01:16 +0200103
104 /* If we have to create a query string, prepare a '?'. */
Christopher Faulet2c22a692019-12-18 15:39:56 +0100105 if (rule->action == 2) // set-query
Willy Tarreau79e57332018-10-02 16:01:16 +0200106 replace->area[replace->data++] = '?';
107 replace->data += build_logline(s, replace->area + replace->data,
108 replace->size - replace->data,
Christopher Faulet96bff762019-12-17 13:46:18 +0100109 &rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200110
Christopher Faulet2c22a692019-12-18 15:39:56 +0100111 if (http_req_replace_stline(rule->action, replace->area, replace->data, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100112 goto fail_rewrite;
Willy Tarreau79e57332018-10-02 16:01:16 +0200113
Christopher Faulete00d06c2019-12-16 17:18:42 +0100114 leave:
Willy Tarreau79e57332018-10-02 16:01:16 +0200115 free_trash_chunk(replace);
116 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100117
118 fail_alloc:
119 if (!(s->flags & SF_ERR_MASK))
120 s->flags |= SF_ERR_RESOURCE;
121 ret = ACT_RET_ERR;
122 goto leave;
123
124 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +0200125 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100126 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200127 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100128 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200129 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100130 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200131 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100132
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100133 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100134 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100135 if (!(s->flags & SF_ERR_MASK))
136 s->flags |= SF_ERR_PRXCOND;
137 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100138 goto leave;
Willy Tarreau79e57332018-10-02 16:01:16 +0200139}
140
141/* parse an http-request action among :
142 * set-method
143 * set-path
Christopher Faulet312294f2020-09-02 17:17:44 +0200144 * set-pathq
Willy Tarreau79e57332018-10-02 16:01:16 +0200145 * set-query
146 * set-uri
147 *
148 * All of them accept a single argument of type string representing a log-format.
Christopher Faulet96bff762019-12-17 13:46:18 +0100149 * The resulting rule makes use of <http.fmt> to store the log-format list head,
Christopher Faulet2c22a692019-12-18 15:39:56 +0100150 * and <.action> to store the action type as an int (0=method, 1=path, 2=query,
151 * 3=uri). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreau79e57332018-10-02 16:01:16 +0200152 */
153static enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
154 struct act_rule *rule, char **err)
155{
156 int cur_arg = *orig_arg;
157
Willy Tarreau79e57332018-10-02 16:01:16 +0200158 switch (args[0][4]) {
159 case 'm' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100160 rule->action = 0; // set-method
Willy Tarreau79e57332018-10-02 16:01:16 +0200161 break;
162 case 'p' :
Christopher Faulet312294f2020-09-02 17:17:44 +0200163 if (args[0][8] == 'q')
164 rule->action = 4; // set-pathq
165 else
166 rule->action = 1; // set-path
Willy Tarreau79e57332018-10-02 16:01:16 +0200167 break;
168 case 'q' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100169 rule->action = 2; // set-query
Willy Tarreau79e57332018-10-02 16:01:16 +0200170 break;
171 case 'u' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100172 rule->action = 3; // set-uri
Willy Tarreau79e57332018-10-02 16:01:16 +0200173 break;
174 default:
175 memprintf(err, "internal error: unhandled action '%s'", args[0]);
176 return ACT_RET_PRS_ERR;
177 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100178 rule->action_ptr = http_action_set_req_line;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100179 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200180
181 if (!*args[cur_arg] ||
182 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
183 memprintf(err, "expects exactly 1 argument <format>");
184 return ACT_RET_PRS_ERR;
185 }
186
Christopher Faulet96bff762019-12-17 13:46:18 +0100187 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200188 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet96bff762019-12-17 13:46:18 +0100189 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP,
Willy Tarreau79e57332018-10-02 16:01:16 +0200190 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
191 return ACT_RET_PRS_ERR;
192 }
193
194 (*orig_arg)++;
195 return ACT_RET_PRS_OK;
196}
197
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200198/* This function executes the http-request normalize-uri action.
199 * `rule->action` is expected to be a value from `enum act_normalize_uri`.
200 *
201 * On success, it returns ACT_RET_CONT. If an error
202 * occurs while soft rewrites are enabled, the action is canceled, but the rule
203 * processing continue. Otherwsize ACT_RET_ERR is returned.
204 */
205static enum act_return http_action_normalize_uri(struct act_rule *rule, struct proxy *px,
206 struct session *sess, struct stream *s, int flags)
207{
208 enum act_return ret = ACT_RET_CONT;
209 struct htx *htx = htxbuf(&s->req.buf);
210 const struct ist uri = htx_sl_req_uri(http_get_stline(htx));
211 struct buffer *replace = alloc_trash_chunk();
212 enum uri_normalizer_err err = URI_NORMALIZER_ERR_INTERNAL_ERROR;
213
214 if (!replace)
215 goto fail_alloc;
216
217 switch ((enum act_normalize_uri) rule->action) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200218 case ACT_NORMALIZE_URI_PATH_MERGE_SLASHES: {
Tim Duesterhusd371e992021-04-15 21:45:58 +0200219 const struct ist path = http_get_path(uri);
220 struct ist newpath = ist2(replace->area, replace->size);
221
222 if (!isttest(path))
223 goto leave;
224
225 err = uri_normalizer_path_merge_slashes(iststop(path, '?'), &newpath);
226
227 if (err != URI_NORMALIZER_ERR_NONE)
228 break;
229
230 if (!http_replace_req_path(htx, newpath, 0))
231 goto fail_rewrite;
232
233 break;
234 }
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200235 case ACT_NORMALIZE_URI_PATH_STRIP_DOT: {
236 const struct ist path = http_get_path(uri);
237 struct ist newpath = ist2(replace->area, replace->size);
238
239 if (!isttest(path))
240 goto leave;
241
242 err = uri_normalizer_path_dot(iststop(path, '?'), &newpath);
243
244 if (err != URI_NORMALIZER_ERR_NONE)
245 break;
246
247 if (!http_replace_req_path(htx, newpath, 0))
248 goto fail_rewrite;
249
250 break;
251 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200252 case ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT:
253 case ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL: {
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200254 const struct ist path = http_get_path(uri);
255 struct ist newpath = ist2(replace->area, replace->size);
256
257 if (!isttest(path))
258 goto leave;
259
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200260 err = uri_normalizer_path_dotdot(iststop(path, '?'), rule->action == ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL, &newpath);
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200261
262 if (err != URI_NORMALIZER_ERR_NONE)
263 break;
264
265 if (!http_replace_req_path(htx, newpath, 0))
266 goto fail_rewrite;
267
268 break;
269 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200270 case ACT_NORMALIZE_URI_QUERY_SORT_BY_NAME: {
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200271 const struct ist path = http_get_path(uri);
272 struct ist newquery = ist2(replace->area, replace->size);
273
274 if (!isttest(path))
275 goto leave;
276
277 err = uri_normalizer_query_sort(istfind(path, '?'), '&', &newquery);
278
279 if (err != URI_NORMALIZER_ERR_NONE)
280 break;
281
282 if (!http_replace_req_query(htx, newquery))
283 goto fail_rewrite;
284
285 break;
286 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200287 case ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE:
288 case ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT: {
Tim Duesterhusa4071932021-04-15 21:46:02 +0200289 const struct ist path = http_get_path(uri);
290 struct ist newpath = ist2(replace->area, replace->size);
291
292 if (!isttest(path))
293 goto leave;
294
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200295 err = uri_normalizer_percent_upper(path, rule->action == ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT, &newpath);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200296
297 if (err != URI_NORMALIZER_ERR_NONE)
298 break;
299
300 if (!http_replace_req_path(htx, newpath, 1))
301 goto fail_rewrite;
302
303 break;
304 }
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +0200305 case ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED:
306 case ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT: {
307 const struct ist path = http_get_path(uri);
308 struct ist newpath = ist2(replace->area, replace->size);
309
310 if (!isttest(path))
311 goto leave;
312
313 err = uri_normalizer_percent_decode_unreserved(path, rule->action == ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT, &newpath);
314
315 if (err != URI_NORMALIZER_ERR_NONE)
316 break;
317
318 if (!http_replace_req_path(htx, newpath, 1))
319 goto fail_rewrite;
320
321 break;
322 }
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200323 case ACT_NORMALIZE_URI_FRAGMENT_STRIP: {
324 const struct ist path = http_get_path(uri);
325 struct ist newpath = ist2(replace->area, replace->size);
326
327 if (!isttest(path))
328 goto leave;
329
330 err = uri_normalizer_fragment_strip(path, &newpath);
331
332 if (err != URI_NORMALIZER_ERR_NONE)
333 break;
334
335 if (!http_replace_req_path(htx, newpath, 1))
336 goto fail_rewrite;
337
338 break;
339 }
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200340 case ACT_NORMALIZE_URI_FRAGMENT_ENCODE: {
341 const struct ist path = http_get_path(uri);
342 struct ist newpath = ist2(replace->area, replace->size);
343
344 if (!isttest(path))
345 goto leave;
346
347 err = uri_normalizer_fragment_encode(path, &newpath);
348
349 if (err != URI_NORMALIZER_ERR_NONE)
350 break;
351
352 if (!http_replace_req_path(htx, newpath, 1))
353 goto fail_rewrite;
354
355 break;
356 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200357 }
358
359 switch (err) {
360 case URI_NORMALIZER_ERR_NONE:
361 break;
362 case URI_NORMALIZER_ERR_INTERNAL_ERROR:
363 ret = ACT_RET_ERR;
364 break;
365 case URI_NORMALIZER_ERR_INVALID_INPUT:
366 ret = ACT_RET_INV;
367 break;
368 case URI_NORMALIZER_ERR_ALLOC:
369 goto fail_alloc;
370 }
371
372 leave:
373 free_trash_chunk(replace);
374 return ret;
375
376 fail_alloc:
377 if (!(s->flags & SF_ERR_MASK))
378 s->flags |= SF_ERR_RESOURCE;
379 ret = ACT_RET_ERR;
380 goto leave;
381
382 fail_rewrite:
383 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
384 if (s->flags & SF_BE_ASSIGNED)
385 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
386 if (sess->listener && sess->listener->counters)
387 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
388 if (objt_server(s->target))
389 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
390
391 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
392 ret = ACT_RET_ERR;
393 if (!(s->flags & SF_ERR_MASK))
394 s->flags |= SF_ERR_PRXCOND;
395 }
396 goto leave;
397}
398
399/* Parses the http-request normalize-uri action. It expects a single <normalizer>
400 * argument, corresponding too a value in `enum act_normalize_uri`.
401 *
402 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
403 */
404static enum act_parse_ret parse_http_normalize_uri(const char **args, int *orig_arg, struct proxy *px,
405 struct act_rule *rule, char **err)
406{
407 int cur_arg = *orig_arg;
408
409 rule->action_ptr = http_action_normalize_uri;
410 rule->release_ptr = NULL;
411
412 if (!*args[cur_arg]) {
413 memprintf(err, "missing argument <normalizer>");
414 return ACT_RET_PRS_ERR;
415 }
416
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200417 if (strcmp(args[cur_arg], "path-merge-slashes") == 0) {
Tim Duesterhusd371e992021-04-15 21:45:58 +0200418 cur_arg++;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200419
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200420 rule->action = ACT_NORMALIZE_URI_PATH_MERGE_SLASHES;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200421 }
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200422 else if (strcmp(args[cur_arg], "path-strip-dot") == 0) {
423 cur_arg++;
424
425 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOT;
426 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200427 else if (strcmp(args[cur_arg], "path-strip-dotdot") == 0) {
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200428 cur_arg++;
429
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200430 if (strcmp(args[cur_arg], "full") == 0) {
431 cur_arg++;
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200432 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL;
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200433 }
434 else if (!*args[cur_arg]) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200435 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT;
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200436 }
437 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
Tim Duesterhus2f413132021-05-10 23:21:20 +0200438 memprintf(err, "unknown argument '%s' for 'path-strip-dotdot' normalizer", args[cur_arg]);
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200439 return ACT_RET_PRS_ERR;
440 }
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200441 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200442 else if (strcmp(args[cur_arg], "query-sort-by-name") == 0) {
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200443 cur_arg++;
444
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200445 rule->action = ACT_NORMALIZE_URI_QUERY_SORT_BY_NAME;
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200446 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200447 else if (strcmp(args[cur_arg], "percent-to-uppercase") == 0) {
Tim Duesterhusa4071932021-04-15 21:46:02 +0200448 cur_arg++;
449
450 if (strcmp(args[cur_arg], "strict") == 0) {
451 cur_arg++;
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200452 rule->action = ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT;
Tim Duesterhusa4071932021-04-15 21:46:02 +0200453 }
454 else if (!*args[cur_arg]) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200455 rule->action = ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE;
Tim Duesterhusa4071932021-04-15 21:46:02 +0200456 }
457 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
Tim Duesterhus2f413132021-05-10 23:21:20 +0200458 memprintf(err, "unknown argument '%s' for 'percent-to-uppercase' normalizer", args[cur_arg]);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200459 return ACT_RET_PRS_ERR;
460 }
461 }
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +0200462 else if (strcmp(args[cur_arg], "percent-decode-unreserved") == 0) {
463 cur_arg++;
464
465 if (strcmp(args[cur_arg], "strict") == 0) {
466 cur_arg++;
467 rule->action = ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT;
468 }
469 else if (!*args[cur_arg]) {
470 rule->action = ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED;
471 }
472 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
473 memprintf(err, "unknown argument '%s' for 'percent-decode-unreserved' normalizer", args[cur_arg]);
474 return ACT_RET_PRS_ERR;
475 }
476 }
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200477 else if (strcmp(args[cur_arg], "fragment-strip") == 0) {
478 cur_arg++;
479
480 rule->action = ACT_NORMALIZE_URI_FRAGMENT_STRIP;
481 }
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200482 else if (strcmp(args[cur_arg], "fragment-encode") == 0) {
483 cur_arg++;
484
485 rule->action = ACT_NORMALIZE_URI_FRAGMENT_ENCODE;
486 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200487 else {
488 memprintf(err, "unknown normalizer '%s'", args[cur_arg]);
489 return ACT_RET_PRS_ERR;
490 }
491
492 *orig_arg = cur_arg;
493 return ACT_RET_PRS_OK;
494}
495
Willy Tarreau33810222019-06-12 17:44:02 +0200496/* This function executes a replace-uri action. It finds its arguments in
Christopher Faulet96bff762019-12-17 13:46:18 +0100497 * <rule>.arg.http. It builds a string in the trash from the format string
Willy Tarreau33810222019-06-12 17:44:02 +0200498 * previously filled by function parse_replace_uri() and will execute the regex
Christopher Faulet96bff762019-12-17 13:46:18 +0100499 * in <http.re> to replace the URI. It uses the format string present in
Christopher Faulet2c22a692019-12-18 15:39:56 +0100500 * <http.fmt>. The component to act on (path/uri) is taken from <.action> which
Christopher Faulet96bff762019-12-17 13:46:18 +0100501 * contains 1 for the path or 3 for the URI (values used by
502 * http_req_replace_stline()). On success, it returns ACT_RET_CONT. If an error
503 * occurs while soft rewrites are enabled, the action is canceled, but the rule
504 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau33810222019-06-12 17:44:02 +0200505 */
506static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px,
507 struct session *sess, struct stream *s, int flags)
508{
Christopher Faulet13403762019-12-13 09:01:57 +0100509 enum act_return ret = ACT_RET_CONT;
Willy Tarreau33810222019-06-12 17:44:02 +0200510 struct buffer *replace, *output;
511 struct ist uri;
512 int len;
513
514 replace = alloc_trash_chunk();
515 output = alloc_trash_chunk();
516 if (!replace || !output)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100517 goto fail_alloc;
Christopher Faulet12c28b62019-07-15 16:30:24 +0200518 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Willy Tarreau262c3f12019-12-17 06:52:51 +0100519
Christopher Faulet1fa0cc12020-09-02 11:10:38 +0200520 if (rule->action == 1) // replace-path
521 uri = iststop(http_get_path(uri), '?');
Christopher Faulet312294f2020-09-02 17:17:44 +0200522 else if (rule->action == 4) // replace-pathq
523 uri = http_get_path(uri);
Willy Tarreau262c3f12019-12-17 06:52:51 +0100524
Christopher Faulet96bff762019-12-17 13:46:18 +0100525 if (!regex_exec_match2(rule->arg.http.re, uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
Willy Tarreau33810222019-06-12 17:44:02 +0200526 goto leave;
527
Christopher Faulet96bff762019-12-17 13:46:18 +0100528 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200529
530 /* note: uri.ptr doesn't need to be zero-terminated because it will
531 * only be used to pick pmatch references.
532 */
533 len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch);
534 if (len == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100535 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200536
Christopher Faulet2c22a692019-12-18 15:39:56 +0100537 if (http_req_replace_stline(rule->action, output->area, len, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100538 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200539
Christopher Faulete00d06c2019-12-16 17:18:42 +0100540 leave:
Willy Tarreau33810222019-06-12 17:44:02 +0200541 free_trash_chunk(output);
542 free_trash_chunk(replace);
543 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100544
545 fail_alloc:
546 if (!(s->flags & SF_ERR_MASK))
547 s->flags |= SF_ERR_RESOURCE;
548 ret = ACT_RET_ERR;
549 goto leave;
550
551 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +0200552 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100553 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200554 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100555 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200556 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100557 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200558 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100559
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100560 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100561 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100562 if (!(s->flags & SF_ERR_MASK))
563 s->flags |= SF_ERR_PRXCOND;
564 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100565 goto leave;
Willy Tarreau33810222019-06-12 17:44:02 +0200566}
567
Christopher Faulet312294f2020-09-02 17:17:44 +0200568/* parse a "replace-uri", "replace-path" or "replace-pathq"
569 * http-request action.
Willy Tarreau33810222019-06-12 17:44:02 +0200570 * This action takes 2 arguments (a regex and a replacement format string).
Christopher Faulet2c22a692019-12-18 15:39:56 +0100571 * The resulting rule makes use of <.action> to store the action (1/3 for now),
Christopher Faulet96bff762019-12-17 13:46:18 +0100572 * <http.re> to store the compiled regex, and <http.fmt> to store the log-format
Willy Tarreau33810222019-06-12 17:44:02 +0200573 * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
574 */
575static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px,
576 struct act_rule *rule, char **err)
577{
578 int cur_arg = *orig_arg;
579 char *error = NULL;
580
Christopher Faulet312294f2020-09-02 17:17:44 +0200581 switch (args[0][8]) {
582 case 'p':
583 if (args[0][12] == 'q')
584 rule->action = 4; // replace-pathq, same as set-pathq
585 else
586 rule->action = 1; // replace-path, same as set-path
587 break;
588 case 'u':
Christopher Faulet2c22a692019-12-18 15:39:56 +0100589 rule->action = 3; // replace-uri, same as set-uri
Christopher Faulet312294f2020-09-02 17:17:44 +0200590 break;
591 default:
592 memprintf(err, "internal error: unhandled action '%s'", args[0]);
593 return ACT_RET_PRS_ERR;
594 }
Willy Tarreau262c3f12019-12-17 06:52:51 +0100595
Willy Tarreau33810222019-06-12 17:44:02 +0200596 rule->action_ptr = http_action_replace_uri;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100597 rule->release_ptr = release_http_action;
Willy Tarreau33810222019-06-12 17:44:02 +0200598
599 if (!*args[cur_arg] || !*args[cur_arg+1] ||
600 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
601 memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>");
602 return ACT_RET_PRS_ERR;
603 }
604
Christopher Faulet96bff762019-12-17 13:46:18 +0100605 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, &error))) {
Willy Tarreau33810222019-06-12 17:44:02 +0200606 memprintf(err, "failed to parse the regex : %s", error);
607 free(error);
608 return ACT_RET_PRS_ERR;
609 }
610
Christopher Faulet96bff762019-12-17 13:46:18 +0100611 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200612 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet96bff762019-12-17 13:46:18 +0100613 if (!parse_logformat_string(args[cur_arg + 1], px, &rule->arg.http.fmt, LOG_OPT_HTTP,
Willy Tarreau33810222019-06-12 17:44:02 +0200614 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
Christopher Faulet1337b322020-01-14 14:50:55 +0100615 regex_free(rule->arg.http.re);
Willy Tarreau33810222019-06-12 17:44:02 +0200616 return ACT_RET_PRS_ERR;
617 }
618
619 (*orig_arg) += 2;
620 return ACT_RET_PRS_OK;
621}
622
Willy Tarreau79e57332018-10-02 16:01:16 +0200623/* This function is just a compliant action wrapper for "set-status". */
624static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
625 struct session *sess, struct stream *s, int flags)
626{
Christopher Faulet96bff762019-12-17 13:46:18 +0100627 if (http_res_set_status(rule->arg.http.i, rule->arg.http.str, s) == -1) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200628 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100629 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200630 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100631 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200632 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100633 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200634 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100635
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100636 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100637 if (!(s->flags & SF_ERR_MASK))
638 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet692a6c22020-02-07 10:22:31 +0100639 return ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100640 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100641 }
642
Willy Tarreau79e57332018-10-02 16:01:16 +0200643 return ACT_RET_CONT;
644}
645
646/* parse set-status action:
647 * This action accepts a single argument of type int representing
648 * an http status code. It returns ACT_RET_PRS_OK on success,
649 * ACT_RET_PRS_ERR on error.
650 */
651static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
652 struct act_rule *rule, char **err)
653{
654 char *error;
655
656 rule->action = ACT_CUSTOM;
657 rule->action_ptr = action_http_set_status;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100658 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200659
660 /* Check if an argument is available */
661 if (!*args[*orig_arg]) {
662 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
663 return ACT_RET_PRS_ERR;
664 }
665
666 /* convert status code as integer */
Christopher Faulet96bff762019-12-17 13:46:18 +0100667 rule->arg.http.i = strtol(args[*orig_arg], &error, 10);
668 if (*error != '\0' || rule->arg.http.i < 100 || rule->arg.http.i > 999) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200669 memprintf(err, "expects an integer status code between 100 and 999");
670 return ACT_RET_PRS_ERR;
671 }
672
673 (*orig_arg)++;
674
675 /* set custom reason string */
Christopher Faulet96bff762019-12-17 13:46:18 +0100676 rule->arg.http.str = ist(NULL); // If null, we use the default reason for the status code.
Willy Tarreau79e57332018-10-02 16:01:16 +0200677 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
678 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
679 (*orig_arg)++;
Christopher Faulet96bff762019-12-17 13:46:18 +0100680 rule->arg.http.str.ptr = strdup(args[*orig_arg]);
681 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200682 (*orig_arg)++;
683 }
684
Christopher Fauletc20b3712020-01-27 15:51:56 +0100685 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200686 return ACT_RET_PRS_OK;
687}
688
689/* This function executes the "reject" HTTP action. It clears the request and
690 * response buffer without sending any response. It can be useful as an HTTP
691 * alternative to the silent-drop action to defend against DoS attacks, and may
692 * also be used with HTTP/2 to close a connection instead of just a stream.
693 * The txn status is unchanged, indicating no response was sent. The termination
Christopher Faulet90d22a82020-03-06 11:18:39 +0100694 * flags will indicate "PR". It always returns ACT_RET_ABRT.
Willy Tarreau79e57332018-10-02 16:01:16 +0200695 */
696static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
697 struct session *sess, struct stream *s, int flags)
698{
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100699 si_must_kill_conn(chn_prod(&s->req));
Willy Tarreau79e57332018-10-02 16:01:16 +0200700 channel_abort(&s->req);
701 channel_abort(&s->res);
Christopher Fauletd4a824e2020-03-06 15:07:09 +0100702 s->req.analysers &= AN_REQ_FLT_END;
703 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau79e57332018-10-02 16:01:16 +0200704
Willy Tarreau4781b152021-04-06 13:53:36 +0200705 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
706 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200707 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200708 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200709
710 if (!(s->flags & SF_ERR_MASK))
711 s->flags |= SF_ERR_PRXCOND;
712 if (!(s->flags & SF_FINST_MASK))
713 s->flags |= SF_FINST_R;
714
Christopher Faulet90d22a82020-03-06 11:18:39 +0100715 return ACT_RET_ABRT;
Willy Tarreau79e57332018-10-02 16:01:16 +0200716}
717
718/* parse the "reject" action:
719 * This action takes no argument and returns ACT_RET_PRS_OK on success,
720 * ACT_RET_PRS_ERR on error.
721 */
722static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
723 struct act_rule *rule, char **err)
724{
725 rule->action = ACT_CUSTOM;
726 rule->action_ptr = http_action_reject;
727 return ACT_RET_PRS_OK;
728}
729
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200730/* This function executes the "disable-l7-retry" HTTP action.
731 * It disables L7 retries (all retry except for a connection failure). This
732 * can be useful for example to avoid retrying on POST requests.
733 * It just removes the L7 retry flag on the stream_interface, and always
734 * return ACT_RET_CONT;
735 */
736static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px,
737 struct session *sess, struct stream *s, int flags)
738{
739 struct stream_interface *si = &s->si[1];
740
741 /* In theory, the SI_FL_L7_RETRY flags isn't set at this point, but
742 * let's be future-proof and remove it anyway.
743 */
744 si->flags &= ~SI_FL_L7_RETRY;
745 si->flags |= SI_FL_D_L7_RETRY;
746 return ACT_RET_CONT;
747}
748
749/* parse the "disable-l7-retry" action:
750 * This action takes no argument and returns ACT_RET_PRS_OK on success,
751 * ACT_RET_PRS_ERR on error.
752 */
753static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args,
754 int *orig_args, struct proxy *px,
755 struct act_rule *rule, char **err)
756{
757 rule->action = ACT_CUSTOM;
758 rule->action_ptr = http_req_disable_l7_retry;
759 return ACT_RET_PRS_OK;
760}
761
Willy Tarreau79e57332018-10-02 16:01:16 +0200762/* This function executes the "capture" action. It executes a fetch expression,
763 * turns the result into a string and puts it in a capture slot. It always
764 * returns 1. If an error occurs the action is cancelled, but the rule
765 * processing continues.
766 */
767static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
768 struct session *sess, struct stream *s, int flags)
769{
770 struct sample *key;
771 struct cap_hdr *h = rule->arg.cap.hdr;
772 char **cap = s->req_cap;
773 int len;
774
775 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
776 if (!key)
777 return ACT_RET_CONT;
778
779 if (cap[h->index] == NULL)
780 cap[h->index] = pool_alloc(h->pool);
781
782 if (cap[h->index] == NULL) /* no more capture memory */
783 return ACT_RET_CONT;
784
785 len = key->data.u.str.data;
786 if (len > h->len)
787 len = h->len;
788
789 memcpy(cap[h->index], key->data.u.str.area, len);
790 cap[h->index][len] = 0;
791 return ACT_RET_CONT;
792}
793
794/* This function executes the "capture" action and store the result in a
795 * capture slot if exists. It executes a fetch expression, turns the result
796 * into a string and puts it in a capture slot. It always returns 1. If an
797 * error occurs the action is cancelled, but the rule processing continues.
798 */
799static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
800 struct session *sess, struct stream *s, int flags)
801{
802 struct sample *key;
803 struct cap_hdr *h;
804 char **cap = s->req_cap;
805 struct proxy *fe = strm_fe(s);
806 int len;
807 int i;
808
809 /* Look for the original configuration. */
810 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
811 h != NULL && i != rule->arg.capid.idx ;
812 i--, h = h->next);
813 if (!h)
814 return ACT_RET_CONT;
815
816 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
817 if (!key)
818 return ACT_RET_CONT;
819
820 if (cap[h->index] == NULL)
821 cap[h->index] = pool_alloc(h->pool);
822
823 if (cap[h->index] == NULL) /* no more capture memory */
824 return ACT_RET_CONT;
825
826 len = key->data.u.str.data;
827 if (len > h->len)
828 len = h->len;
829
830 memcpy(cap[h->index], key->data.u.str.area, len);
831 cap[h->index][len] = 0;
832 return ACT_RET_CONT;
833}
834
835/* Check an "http-request capture" action.
836 *
837 * The function returns 1 in success case, otherwise, it returns 0 and err is
838 * filled.
839 */
840static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
841{
842 if (rule->action_ptr != http_action_req_capture_by_id)
843 return 1;
844
Baptiste Assmann19a69b32020-01-16 14:34:22 +0100845 /* capture slots can only be declared in frontends, so we can't check their
846 * existence in backends at configuration parsing step
847 */
848 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_req_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200849 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
850 rule->arg.capid.idx);
851 return 0;
852 }
853
854 return 1;
855}
856
Christopher Faulet2eb53962020-01-14 14:47:34 +0100857/* Release memory allocate by an http capture action */
858static void release_http_capture(struct act_rule *rule)
859{
860 if (rule->action_ptr == http_action_req_capture)
861 release_sample_expr(rule->arg.cap.expr);
862 else
863 release_sample_expr(rule->arg.capid.expr);
864}
865
Willy Tarreau79e57332018-10-02 16:01:16 +0200866/* parse an "http-request capture" action. It takes a single argument which is
867 * a sample fetch expression. It stores the expression into arg->act.p[0] and
868 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
869 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
870 */
871static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
872 struct act_rule *rule, char **err)
873{
874 struct sample_expr *expr;
875 struct cap_hdr *hdr;
876 int cur_arg;
877 int len = 0;
878
879 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
880 if (strcmp(args[cur_arg], "if") == 0 ||
881 strcmp(args[cur_arg], "unless") == 0)
882 break;
883
884 if (cur_arg < *orig_arg + 3) {
885 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
886 return ACT_RET_PRS_ERR;
887 }
888
889 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100890 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args, NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +0200891 if (!expr)
892 return ACT_RET_PRS_ERR;
893
894 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
895 memprintf(err,
896 "fetch method '%s' extracts information from '%s', none of which is available here",
897 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100898 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200899 return ACT_RET_PRS_ERR;
900 }
901
902 if (!args[cur_arg] || !*args[cur_arg]) {
903 memprintf(err, "expects 'len or 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100904 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200905 return ACT_RET_PRS_ERR;
906 }
907
908 if (strcmp(args[cur_arg], "len") == 0) {
909 cur_arg++;
910
911 if (!(px->cap & PR_CAP_FE)) {
912 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Christopher Faulet1337b322020-01-14 14:50:55 +0100913 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200914 return ACT_RET_PRS_ERR;
915 }
916
917 px->conf.args.ctx = ARGC_CAP;
918
919 if (!args[cur_arg]) {
920 memprintf(err, "missing length value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100921 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200922 return ACT_RET_PRS_ERR;
923 }
924 /* we copy the table name for now, it will be resolved later */
925 len = atoi(args[cur_arg]);
926 if (len <= 0) {
927 memprintf(err, "length must be > 0");
Christopher Faulet1337b322020-01-14 14:50:55 +0100928 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200929 return ACT_RET_PRS_ERR;
930 }
931 cur_arg++;
932
Willy Tarreau79e57332018-10-02 16:01:16 +0200933 hdr = calloc(1, sizeof(*hdr));
Remi Tricot-Le Bretona4bf8a02021-05-12 17:54:17 +0200934 if (!hdr) {
935 memprintf(err, "out of memory");
936 release_sample_expr(expr);
937 return ACT_RET_PRS_ERR;
938 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200939 hdr->next = px->req_cap;
940 hdr->name = NULL; /* not a header capture */
941 hdr->namelen = 0;
942 hdr->len = len;
943 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
944 hdr->index = px->nb_req_cap++;
945
946 px->req_cap = hdr;
947 px->to_log |= LW_REQHDR;
948
949 rule->action = ACT_CUSTOM;
950 rule->action_ptr = http_action_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100951 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200952 rule->arg.cap.expr = expr;
953 rule->arg.cap.hdr = hdr;
954 }
955
956 else if (strcmp(args[cur_arg], "id") == 0) {
957 int id;
958 char *error;
959
960 cur_arg++;
961
962 if (!args[cur_arg]) {
963 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100964 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200965 return ACT_RET_PRS_ERR;
966 }
967
968 id = strtol(args[cur_arg], &error, 10);
969 if (*error != '\0') {
970 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100971 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200972 return ACT_RET_PRS_ERR;
973 }
974 cur_arg++;
975
976 px->conf.args.ctx = ARGC_CAP;
977
978 rule->action = ACT_CUSTOM;
979 rule->action_ptr = http_action_req_capture_by_id;
980 rule->check_ptr = check_http_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100981 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200982 rule->arg.capid.expr = expr;
983 rule->arg.capid.idx = id;
984 }
985
986 else {
987 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100988 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200989 return ACT_RET_PRS_ERR;
990 }
991
992 *orig_arg = cur_arg;
993 return ACT_RET_PRS_OK;
994}
995
996/* This function executes the "capture" action and store the result in a
997 * capture slot if exists. It executes a fetch expression, turns the result
998 * into a string and puts it in a capture slot. It always returns 1. If an
999 * error occurs the action is cancelled, but the rule processing continues.
1000 */
1001static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
1002 struct session *sess, struct stream *s, int flags)
1003{
1004 struct sample *key;
1005 struct cap_hdr *h;
1006 char **cap = s->res_cap;
1007 struct proxy *fe = strm_fe(s);
1008 int len;
1009 int i;
1010
1011 /* Look for the original configuration. */
1012 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
1013 h != NULL && i != rule->arg.capid.idx ;
1014 i--, h = h->next);
1015 if (!h)
1016 return ACT_RET_CONT;
1017
1018 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
1019 if (!key)
1020 return ACT_RET_CONT;
1021
1022 if (cap[h->index] == NULL)
1023 cap[h->index] = pool_alloc(h->pool);
1024
1025 if (cap[h->index] == NULL) /* no more capture memory */
1026 return ACT_RET_CONT;
1027
1028 len = key->data.u.str.data;
1029 if (len > h->len)
1030 len = h->len;
1031
1032 memcpy(cap[h->index], key->data.u.str.area, len);
1033 cap[h->index][len] = 0;
1034 return ACT_RET_CONT;
1035}
1036
1037/* Check an "http-response capture" action.
1038 *
1039 * The function returns 1 in success case, otherwise, it returns 0 and err is
1040 * filled.
1041 */
1042static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
1043{
1044 if (rule->action_ptr != http_action_res_capture_by_id)
1045 return 1;
1046
Tim Duesterhusf3f4aa02020-07-03 13:43:42 +02001047 /* capture slots can only be declared in frontends, so we can't check their
1048 * existence in backends at configuration parsing step
1049 */
1050 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_rsp_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001051 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
1052 rule->arg.capid.idx);
1053 return 0;
1054 }
1055
1056 return 1;
1057}
1058
1059/* parse an "http-response capture" action. It takes a single argument which is
1060 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07001061 * the allocated hdr_cap struct of the preallocated id into arg->act.p[1].
Willy Tarreau79e57332018-10-02 16:01:16 +02001062 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1063 */
1064static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
1065 struct act_rule *rule, char **err)
1066{
1067 struct sample_expr *expr;
1068 int cur_arg;
1069 int id;
1070 char *error;
1071
1072 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
1073 if (strcmp(args[cur_arg], "if") == 0 ||
1074 strcmp(args[cur_arg], "unless") == 0)
1075 break;
1076
1077 if (cur_arg < *orig_arg + 3) {
1078 memprintf(err, "expects <expression> id <idx>");
1079 return ACT_RET_PRS_ERR;
1080 }
1081
1082 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001083 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args, NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +02001084 if (!expr)
1085 return ACT_RET_PRS_ERR;
1086
1087 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
1088 memprintf(err,
1089 "fetch method '%s' extracts information from '%s', none of which is available here",
1090 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01001091 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001092 return ACT_RET_PRS_ERR;
1093 }
1094
1095 if (!args[cur_arg] || !*args[cur_arg]) {
1096 memprintf(err, "expects 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +01001097 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001098 return ACT_RET_PRS_ERR;
1099 }
1100
1101 if (strcmp(args[cur_arg], "id") != 0) {
1102 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001103 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001104 return ACT_RET_PRS_ERR;
1105 }
1106
1107 cur_arg++;
1108
1109 if (!args[cur_arg]) {
1110 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +01001111 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001112 return ACT_RET_PRS_ERR;
1113 }
1114
1115 id = strtol(args[cur_arg], &error, 10);
1116 if (*error != '\0') {
1117 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001118 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001119 return ACT_RET_PRS_ERR;
1120 }
1121 cur_arg++;
1122
1123 px->conf.args.ctx = ARGC_CAP;
1124
1125 rule->action = ACT_CUSTOM;
1126 rule->action_ptr = http_action_res_capture_by_id;
1127 rule->check_ptr = check_http_res_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001128 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +02001129 rule->arg.capid.expr = expr;
1130 rule->arg.capid.idx = id;
1131
1132 *orig_arg = cur_arg;
1133 return ACT_RET_PRS_OK;
1134}
1135
Christopher Faulet81e20172019-12-12 16:40:30 +01001136/* Parse a "allow" action for a request or a response rule. It takes no argument. It
1137 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1138 */
1139static enum act_parse_ret parse_http_allow(const char **args, int *orig_arg, struct proxy *px,
1140 struct act_rule *rule, char **err)
1141{
1142 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +01001143 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +01001144 return ACT_RET_PRS_OK;
1145}
1146
Christopher Faulete0fca292020-01-13 21:49:03 +01001147/* Parse "deny" or "tarpit" actions for a request rule or "deny" action for a
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001148 * response rule. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on
1149 * error. It relies on http_parse_http_reply() to set
1150 * <.arg.http_reply>.
Christopher Faulet81e20172019-12-12 16:40:30 +01001151 */
Christopher Faulete0fca292020-01-13 21:49:03 +01001152static enum act_parse_ret parse_http_deny(const char **args, int *orig_arg, struct proxy *px,
1153 struct act_rule *rule, char **err)
Christopher Faulet81e20172019-12-12 16:40:30 +01001154{
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001155 int default_status;
1156 int cur_arg, arg = 0;
Christopher Faulet81e20172019-12-12 16:40:30 +01001157
1158 cur_arg = *orig_arg;
Christopher Faulete0fca292020-01-13 21:49:03 +01001159 if (rule->from == ACT_F_HTTP_REQ) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001160 if (strcmp(args[cur_arg - 1], "tarpit") == 0) {
Christopher Faulete0fca292020-01-13 21:49:03 +01001161 rule->action = ACT_HTTP_REQ_TARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001162 default_status = 500;
Christopher Faulet81e20172019-12-12 16:40:30 +01001163 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001164 else {
1165 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001166 default_status = 403;
Christopher Faulet81e20172019-12-12 16:40:30 +01001167 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001168 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001169 else {
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001170 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001171 default_status = 502;
Christopher Faulete0fca292020-01-13 21:49:03 +01001172 }
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001173
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001174 /* If no args or only a deny_status specified, fallback on the legacy
1175 * mode and use default error files despite the fact that
1176 * default-errorfiles is not used. Otherwise, parse an http reply.
1177 */
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001178
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001179 /* Prepare parsing of log-format strings */
1180 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001181
Christopher Faulet9467f182020-06-30 09:32:01 +02001182 if (!*(args[cur_arg]) || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001183 rule->arg.http_reply = http_parse_http_reply((const char *[]){"default-errorfiles", ""}, &arg, px, default_status, err);
1184 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001185 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001186
1187 if (strcmp(args[cur_arg], "deny_status") == 0) {
Christopher Faulet9467f182020-06-30 09:32:01 +02001188 if (!*(args[cur_arg+2]) || strcmp(args[cur_arg+2], "if") == 0 || strcmp(args[cur_arg+2], "unless") == 0) {
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001189 rule->arg.http_reply = http_parse_http_reply((const char *[]){"status", args[cur_arg+1], "default-errorfiles", ""},
1190 &arg, px, default_status, err);
1191 *orig_arg += 2;
1192 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001193 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001194 args[cur_arg] += 5; /* skip "deny_" for the parsing */
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001195 }
1196
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001197 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, default_status, err);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001198
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001199 end:
1200 if (!rule->arg.http_reply)
1201 return ACT_RET_PRS_ERR;
1202
1203 rule->flags |= ACT_FLAG_FINAL;
1204 rule->check_ptr = check_act_http_reply;
1205 rule->release_ptr = release_act_http_reply;
Christopher Faulet81e20172019-12-12 16:40:30 +01001206 return ACT_RET_PRS_OK;
1207}
1208
Christopher Fauletb3048832020-05-27 15:26:43 +02001209
1210/* This function executes a auth action. It builds an 401/407 HTX message using
1211 * the corresponding proxy's error message. On success, it returns
1212 * ACT_RET_ABRT. If an error occurs ACT_RET_ERR is returned.
1213 */
1214static enum act_return http_action_auth(struct act_rule *rule, struct proxy *px,
1215 struct session *sess, struct stream *s, int flags)
1216{
1217 struct channel *req = &s->req;
1218 struct channel *res = &s->res;
1219 struct htx *htx = htx_from_buf(&res->buf);
1220 struct http_reply *reply;
1221 const char *auth_realm;
1222 struct http_hdr_ctx ctx;
1223 struct ist hdr;
1224
1225 /* Auth might be performed on regular http-req rules as well as on stats */
1226 auth_realm = rule->arg.http.str.ptr;
1227 if (!auth_realm) {
1228 if (px->uri_auth && s->current_rule_list == &px->uri_auth->http_req_rules)
1229 auth_realm = STATS_DEFAULT_REALM;
1230 else
1231 auth_realm = px->id;
1232 }
1233
1234 if (!(s->txn->flags & TX_USE_PX_CONN)) {
1235 s->txn->status = 401;
1236 hdr = ist("WWW-Authenticate");
1237 }
1238 else {
1239 s->txn->status = 407;
1240 hdr = ist("Proxy-Authenticate");
1241 }
1242 reply = http_error_message(s);
1243 channel_htx_truncate(res, htx);
1244
1245 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
1246 goto fail;
1247
1248 /* Write the generic 40x message */
1249 if (http_reply_to_htx(s, htx, reply) == -1)
1250 goto fail;
1251
1252 /* Remove all existing occurrences of the XXX-Authenticate header */
1253 ctx.blk = NULL;
1254 while (http_find_header(htx, hdr, &ctx, 1))
1255 http_remove_header(htx, &ctx);
1256
1257 /* Now a the right XXX-Authenticate header */
1258 if (!http_add_header(htx, hdr, ist2(b_orig(&trash), b_data(&trash))))
1259 goto fail;
1260
1261 /* Finally forward the reply */
1262 htx_to_buf(htx, &res->buf);
1263 if (!http_forward_proxy_resp(s, 1))
1264 goto fail;
1265
1266 /* Note: Only eval on the request */
1267 s->logs.tv_request = now;
1268 req->analysers &= AN_REQ_FLT_END;
1269
1270 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02001271 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Fauletb3048832020-05-27 15:26:43 +02001272
1273 if (!(s->flags & SF_ERR_MASK))
1274 s->flags |= SF_ERR_LOCAL;
1275 if (!(s->flags & SF_FINST_MASK))
1276 s->flags |= SF_FINST_R;
1277
1278 stream_inc_http_err_ctr(s);
1279 return ACT_RET_ABRT;
1280
1281 fail:
1282 /* If an error occurred, remove the incomplete HTTP response from the
1283 * buffer */
1284 channel_htx_truncate(res, htx);
1285 return ACT_RET_ERR;
1286}
1287
Christopher Faulet81e20172019-12-12 16:40:30 +01001288/* Parse a "auth" action. It may take 2 optional arguments to define a "realm"
1289 * parameter. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1290 */
1291static enum act_parse_ret parse_http_auth(const char **args, int *orig_arg, struct proxy *px,
1292 struct act_rule *rule, char **err)
1293{
1294 int cur_arg;
1295
Christopher Fauletb3048832020-05-27 15:26:43 +02001296 rule->action = ACT_CUSTOM;
Christopher Faulet245cf792019-12-18 14:58:12 +01001297 rule->flags |= ACT_FLAG_FINAL;
Christopher Fauletb3048832020-05-27 15:26:43 +02001298 rule->action_ptr = http_action_auth;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001299 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001300
1301 cur_arg = *orig_arg;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001302 if (strcmp(args[cur_arg], "realm") == 0) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001303 cur_arg++;
1304 if (!*args[cur_arg]) {
1305 memprintf(err, "missing realm value.\n");
1306 return ACT_RET_PRS_ERR;
1307 }
Christopher Faulet96bff762019-12-17 13:46:18 +01001308 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1309 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001310 cur_arg++;
1311 }
1312
Christopher Fauletc20b3712020-01-27 15:51:56 +01001313 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001314 *orig_arg = cur_arg;
1315 return ACT_RET_PRS_OK;
1316}
1317
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001318/* This function executes a early-hint action. It adds an HTTP Early Hint HTTP
1319 * 103 response header with <.arg.http.str> name and with a value built
1320 * according to <.arg.http.fmt> log line format. If it is the first early-hint
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001321 * rule of series, the 103 response start-line is added first. At the end, if
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001322 * the next rule is not an early-hint rule or if it is the last rule, the EOH
1323 * block is added to terminate the response. On success, it returns
1324 * ACT_RET_CONT. If an error occurs while soft rewrites are enabled, the action
1325 * is canceled, but the rule processing continue. Otherwsize ACT_RET_ERR is
1326 * returned.
1327 */
1328static enum act_return http_action_early_hint(struct act_rule *rule, struct proxy *px,
1329 struct session *sess, struct stream *s, int flags)
1330{
1331 struct act_rule *prev_rule, *next_rule;
1332 struct channel *res = &s->res;
1333 struct htx *htx = htx_from_buf(&res->buf);
1334 struct buffer *value = alloc_trash_chunk();
1335 enum act_return ret = ACT_RET_CONT;
1336
1337 if (!(s->txn->req.flags & HTTP_MSGF_VER_11))
1338 goto leave;
1339
1340 if (!value) {
1341 if (!(s->flags & SF_ERR_MASK))
1342 s->flags |= SF_ERR_RESOURCE;
1343 goto error;
1344 }
1345
1346 /* get previous and next rules */
1347 prev_rule = LIST_PREV(&rule->list, typeof(rule), list);
1348 next_rule = LIST_NEXT(&rule->list, typeof(rule), list);
1349
1350 /* if no previous rule or previous rule is not early-hint, start a new response. Otherwise,
1351 * continue to add link to a previously started response */
1352 if (&prev_rule->list == s->current_rule_list || prev_rule->action_ptr != http_action_early_hint) {
1353 struct htx_sl *sl;
1354 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
1355 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
1356
1357 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
1358 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
1359 if (!sl)
1360 goto error;
1361 sl->info.res.status = 103;
1362 }
1363
1364 /* Add the HTTP Early Hint HTTP 103 response heade */
1365 value->data = build_logline(s, b_tail(value), b_room(value), &rule->arg.http.fmt);
1366 if (!htx_add_header(htx, rule->arg.http.str, ist2(b_head(value), b_data(value))))
1367 goto error;
1368
1369 /* if it is the last rule or the next one is not an early-hint, terminate the current
1370 * response. */
1371 if (&next_rule->list == s->current_rule_list || next_rule->action_ptr != http_action_early_hint) {
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001372 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
1373 /* If an error occurred during an Early-hint rule,
1374 * remove the incomplete HTTP 103 response from the
1375 * buffer */
1376 goto error;
1377 }
1378
Christopher Fauleta72a7e42020-01-28 09:28:11 +01001379 if (!http_forward_proxy_resp(s, 0))
1380 goto error;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001381 }
1382
1383 leave:
1384 free_trash_chunk(value);
1385 return ret;
1386
1387 error:
1388 /* If an error occurred during an Early-hint rule, remove the incomplete
1389 * HTTP 103 response from the buffer */
1390 channel_htx_truncate(res, htx);
1391 ret = ACT_RET_ERR;
1392 goto leave;
1393}
1394
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001395/* This function executes a set-header or add-header actions. It builds a string
1396 * in the trash from the specified format string. It finds the action to be
1397 * performed in <.action>, previously filled by function parse_set_header(). The
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001398 * replacement action is executed by the function http_action_set_header(). On
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001399 * success, it returns ACT_RET_CONT. If an error occurs while soft rewrites are
1400 * enabled, the action is canceled, but the rule processing continue. Otherwsize
1401 * ACT_RET_ERR is returned.
1402 */
1403static enum act_return http_action_set_header(struct act_rule *rule, struct proxy *px,
1404 struct session *sess, struct stream *s, int flags)
1405{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001406 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1407 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001408 enum act_return ret = ACT_RET_CONT;
1409 struct buffer *replace;
1410 struct http_hdr_ctx ctx;
1411 struct ist n, v;
1412
1413 replace = alloc_trash_chunk();
1414 if (!replace)
1415 goto fail_alloc;
1416
1417 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1418 n = rule->arg.http.str;
1419 v = ist2(replace->area, replace->data);
1420
1421 if (rule->action == 0) { // set-header
1422 /* remove all occurrences of the header */
1423 ctx.blk = NULL;
1424 while (http_find_header(htx, n, &ctx, 1))
1425 http_remove_header(htx, &ctx);
1426 }
1427
1428 /* Now add header */
1429 if (!http_add_header(htx, n, v))
1430 goto fail_rewrite;
1431
1432 leave:
1433 free_trash_chunk(replace);
1434 return ret;
1435
1436 fail_alloc:
1437 if (!(s->flags & SF_ERR_MASK))
1438 s->flags |= SF_ERR_RESOURCE;
1439 ret = ACT_RET_ERR;
1440 goto leave;
1441
1442 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001443 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001444 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001445 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001446 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001447 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001448 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001449 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001450
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001451 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001452 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001453 if (!(s->flags & SF_ERR_MASK))
1454 s->flags |= SF_ERR_PRXCOND;
1455 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001456 goto leave;
1457}
1458
Christopher Faulet81e20172019-12-12 16:40:30 +01001459/* Parse a "set-header", "add-header" or "early-hint" actions. It takes an
1460 * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK
1461 * on success, ACT_RET_PRS_ERR on error.
1462 *
1463 * Note: same function is used for the request and the response. However
1464 * "early-hint" rules are only supported for request rules.
1465 */
1466static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px,
1467 struct act_rule *rule, char **err)
1468{
Christopher Faulet81e20172019-12-12 16:40:30 +01001469 int cap, cur_arg;
1470
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001471 if (args[*orig_arg-1][0] == 'e') {
1472 rule->action = ACT_CUSTOM;
1473 rule->action_ptr = http_action_early_hint;
1474 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001475 else {
1476 if (args[*orig_arg-1][0] == 's')
1477 rule->action = 0; // set-header
1478 else
1479 rule->action = 1; // add-header
1480 rule->action_ptr = http_action_set_header;
1481 }
Christopher Faulet2eb53962020-01-14 14:47:34 +01001482 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001483
1484 cur_arg = *orig_arg;
1485 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1486 memprintf(err, "expects exactly 2 arguments");
1487 return ACT_RET_PRS_ERR;
1488 }
1489
Christopher Faulet81e20172019-12-12 16:40:30 +01001490
Christopher Faulet96bff762019-12-17 13:46:18 +01001491 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1492 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1493 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001494
1495 if (rule->from == ACT_F_HTTP_REQ) {
1496 px->conf.args.ctx = ARGC_HRQ;
1497 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1498 }
1499 else{
1500 px->conf.args.ctx = ARGC_HRS;
1501 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1502 }
1503
1504 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001505 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001506 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001507 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001508 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001509
1510 free(px->conf.lfs_file);
1511 px->conf.lfs_file = strdup(px->conf.args.file);
1512 px->conf.lfs_line = px->conf.args.line;
1513
1514 *orig_arg = cur_arg + 1;
1515 return ACT_RET_PRS_OK;
1516}
1517
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001518/* This function executes a replace-header or replace-value actions. It
1519 * builds a string in the trash from the specified format string. It finds
1520 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001521 * parse_replace_header(). The replacement action is executed by the function
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001522 * http_action_replace_header(). On success, it returns ACT_RET_CONT. If an error
1523 * occurs while soft rewrites are enabled, the action is canceled, but the rule
1524 * processing continue. Otherwsize ACT_RET_ERR is returned.
1525 */
1526static enum act_return http_action_replace_header(struct act_rule *rule, struct proxy *px,
1527 struct session *sess, struct stream *s, int flags)
1528{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001529 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1530 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001531 enum act_return ret = ACT_RET_CONT;
1532 struct buffer *replace;
1533 int r;
1534
1535 replace = alloc_trash_chunk();
1536 if (!replace)
1537 goto fail_alloc;
1538
1539 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1540
1541 r = http_replace_hdrs(s, htx, rule->arg.http.str, replace->area, rule->arg.http.re, (rule->action == 0));
1542 if (r == -1)
1543 goto fail_rewrite;
1544
1545 leave:
1546 free_trash_chunk(replace);
1547 return ret;
1548
1549 fail_alloc:
1550 if (!(s->flags & SF_ERR_MASK))
1551 s->flags |= SF_ERR_RESOURCE;
1552 ret = ACT_RET_ERR;
1553 goto leave;
1554
1555 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001556 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001557 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001558 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001559 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001560 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001561 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001562 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001563
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001564 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001565 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001566 if (!(s->flags & SF_ERR_MASK))
1567 s->flags |= SF_ERR_PRXCOND;
1568 }
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001569 goto leave;
1570}
1571
Christopher Faulet81e20172019-12-12 16:40:30 +01001572/* Parse a "replace-header" or "replace-value" actions. It takes an header name,
1573 * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on
1574 * success, ACT_RET_PRS_ERR on error.
1575 */
1576static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px,
1577 struct act_rule *rule, char **err)
1578{
1579 int cap, cur_arg;
1580
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001581 if (args[*orig_arg-1][8] == 'h')
1582 rule->action = 0; // replace-header
1583 else
1584 rule->action = 1; // replace-value
1585 rule->action_ptr = http_action_replace_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001586 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001587
1588 cur_arg = *orig_arg;
1589 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2]) {
1590 memprintf(err, "expects exactly 3 arguments");
1591 return ACT_RET_PRS_ERR;
1592 }
1593
Christopher Faulet96bff762019-12-17 13:46:18 +01001594 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1595 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
1596 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001597
1598 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001599 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, err))) {
Tim Duesterhused526372020-03-05 17:56:33 +01001600 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001601 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001602 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001603
1604 if (rule->from == ACT_F_HTTP_REQ) {
1605 px->conf.args.ctx = ARGC_HRQ;
1606 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1607 }
1608 else{
1609 px->conf.args.ctx = ARGC_HRS;
1610 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1611 }
1612
1613 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001614 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001615 istfree(&rule->arg.http.str);
Christopher Faulet1337b322020-01-14 14:50:55 +01001616 regex_free(rule->arg.http.re);
Christopher Faulet81e20172019-12-12 16:40:30 +01001617 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001618 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001619
1620 free(px->conf.lfs_file);
1621 px->conf.lfs_file = strdup(px->conf.args.file);
1622 px->conf.lfs_line = px->conf.args.line;
1623
1624 *orig_arg = cur_arg + 1;
1625 return ACT_RET_PRS_OK;
1626}
1627
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001628/* This function executes a del-header action with selected matching mode for
1629 * header name. It finds the matching method to be performed in <.action>, previously
1630 * filled by function parse_http_del_header(). On success, it returns ACT_RET_CONT.
1631 * Otherwise ACT_RET_ERR is returned.
1632 */
1633static enum act_return http_action_del_header(struct act_rule *rule, struct proxy *px,
1634 struct session *sess, struct stream *s, int flags)
1635{
1636 struct http_hdr_ctx ctx;
1637 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1638 struct htx *htx = htxbuf(&msg->chn->buf);
1639 enum act_return ret = ACT_RET_CONT;
1640
1641 /* remove all occurrences of the header */
1642 ctx.blk = NULL;
1643 switch (rule->action) {
1644 case PAT_MATCH_STR:
1645 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
1646 http_remove_header(htx, &ctx);
1647 break;
1648 case PAT_MATCH_BEG:
1649 while (http_find_pfx_header(htx, rule->arg.http.str, &ctx, 1))
1650 http_remove_header(htx, &ctx);
1651 break;
1652 case PAT_MATCH_END:
1653 while (http_find_sfx_header(htx, rule->arg.http.str, &ctx, 1))
1654 http_remove_header(htx, &ctx);
1655 break;
1656 case PAT_MATCH_SUB:
1657 while (http_find_sub_header(htx, rule->arg.http.str, &ctx, 1))
1658 http_remove_header(htx, &ctx);
1659 break;
1660 case PAT_MATCH_REG:
1661 while (http_match_header(htx, rule->arg.http.re, &ctx, 1))
1662 http_remove_header(htx, &ctx);
1663 break;
1664 default:
1665 return ACT_RET_ERR;
1666 }
1667 return ret;
1668}
1669
1670/* Parse a "del-header" action. It takes string as a required argument,
1671 * optional flag (currently only -m) and optional matching method of input string
1672 * with header name to be deleted. Default matching method is exact match (-m str).
1673 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001674 */
1675static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px,
1676 struct act_rule *rule, char **err)
1677{
1678 int cur_arg;
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001679 int pat_idx;
Christopher Faulet81e20172019-12-12 16:40:30 +01001680
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001681 /* set exact matching (-m str) as default */
1682 rule->action = PAT_MATCH_STR;
1683 rule->action_ptr = http_action_del_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001684 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001685
1686 cur_arg = *orig_arg;
1687 if (!*args[cur_arg]) {
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001688 memprintf(err, "expects at least 1 argument");
Christopher Faulet81e20172019-12-12 16:40:30 +01001689 return ACT_RET_PRS_ERR;
1690 }
1691
Christopher Faulet96bff762019-12-17 13:46:18 +01001692 rule->arg.http.str.ptr = strdup(args[cur_arg]);
1693 rule->arg.http.str.len = strlen(rule->arg.http.str.ptr);
Christopher Faulet81e20172019-12-12 16:40:30 +01001694 px->conf.args.ctx = (rule->from == ACT_F_HTTP_REQ ? ARGC_HRQ : ARGC_HRS);
1695
Maciej Zdeb6dee9962020-11-23 16:03:09 +00001696 LIST_INIT(&rule->arg.http.fmt);
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001697 if (strcmp(args[cur_arg+1], "-m") == 0) {
1698 cur_arg++;
1699 if (!*args[cur_arg+1]) {
1700 memprintf(err, "-m flag expects exactly 1 argument");
1701 return ACT_RET_PRS_ERR;
1702 }
1703
1704 cur_arg++;
1705 pat_idx = pat_find_match_name(args[cur_arg]);
1706 switch (pat_idx) {
1707 case PAT_MATCH_REG:
1708 if (!(rule->arg.http.re = regex_comp(rule->arg.http.str.ptr, 1, 1, err)))
1709 return ACT_RET_PRS_ERR;
1710 /* fall through */
1711 case PAT_MATCH_STR:
1712 case PAT_MATCH_BEG:
1713 case PAT_MATCH_END:
1714 case PAT_MATCH_SUB:
1715 rule->action = pat_idx;
1716 break;
1717 default:
1718 memprintf(err, "-m with unsupported matching method '%s'", args[cur_arg]);
1719 return ACT_RET_PRS_ERR;
1720 }
1721 }
1722
Christopher Faulet81e20172019-12-12 16:40:30 +01001723 *orig_arg = cur_arg + 1;
1724 return ACT_RET_PRS_OK;
1725}
1726
Christopher Faulet2eb53962020-01-14 14:47:34 +01001727/* Release memory allocated by an http redirect action. */
1728static void release_http_redir(struct act_rule *rule)
1729{
1730 struct logformat_node *lf, *lfb;
1731 struct redirect_rule *redir;
1732
1733 redir = rule->arg.redir;
Willy Tarreau2b718102021-04-21 07:32:39 +02001734 LIST_DELETE(&redir->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001735 if (redir->cond) {
1736 prune_acl_cond(redir->cond);
1737 free(redir->cond);
1738 }
1739 free(redir->rdr_str);
1740 free(redir->cookie_str);
1741 list_for_each_entry_safe(lf, lfb, &redir->rdr_fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001742 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001743 free(lf);
1744 }
1745 free(redir);
1746}
1747
Christopher Faulet81e20172019-12-12 16:40:30 +01001748/* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success,
1749 * ACT_RET_PRS_ERR on error.
1750 */
1751static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px,
1752 struct act_rule *rule, char **err)
1753{
1754 struct redirect_rule *redir;
1755 int dir, cur_arg;
1756
1757 rule->action = ACT_HTTP_REDIR;
Christopher Faulet245cf792019-12-18 14:58:12 +01001758 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001759 rule->release_ptr = release_http_redir;
Christopher Faulet81e20172019-12-12 16:40:30 +01001760
1761 cur_arg = *orig_arg;
1762
1763 dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1);
1764 if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL)
1765 return ACT_RET_PRS_ERR;
1766
1767 rule->arg.redir = redir;
1768 rule->cond = redir->cond;
1769 redir->cond = NULL;
1770
1771 /* skip all arguments */
1772 while (*args[cur_arg])
1773 cur_arg++;
1774
1775 *orig_arg = cur_arg;
1776 return ACT_RET_PRS_OK;
1777}
1778
Christopher Faulet046cf442019-12-17 15:45:23 +01001779/* This function executes a add-acl, del-acl, set-map or del-map actions. On
1780 * success, it returns ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1781 */
1782static enum act_return http_action_set_map(struct act_rule *rule, struct proxy *px,
1783 struct session *sess, struct stream *s, int flags)
1784{
1785 struct pat_ref *ref;
1786 struct buffer *key = NULL, *value = NULL;
1787 enum act_return ret = ACT_RET_CONT;
1788
1789 /* collect reference */
1790 ref = pat_ref_lookup(rule->arg.map.ref);
1791 if (!ref)
1792 goto leave;
1793
1794 /* allocate key */
1795 key = alloc_trash_chunk();
1796 if (!key)
1797 goto fail_alloc;
1798
1799 /* collect key */
1800 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
1801 key->area[key->data] = '\0';
1802
1803 switch (rule->action) {
1804 case 0: // add-acl
1805 /* add entry only if it does not already exist */
1806 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1807 if (pat_ref_find_elt(ref, key->area) == NULL)
1808 pat_ref_add(ref, key->area, NULL, NULL);
1809 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1810 break;
1811
1812 case 1: // set-map
1813 /* allocate value */
1814 value = alloc_trash_chunk();
1815 if (!value)
1816 goto fail_alloc;
1817
1818 /* collect value */
1819 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
1820 value->area[value->data] = '\0';
1821
1822 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1823 if (pat_ref_find_elt(ref, key->area) != NULL) {
1824 /* update entry if it exists */
1825 pat_ref_set(ref, key->area, value->area, NULL);
1826 }
1827 else {
1828 /* insert a new entry */
1829 pat_ref_add(ref, key->area, value->area, NULL);
1830 }
1831 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1832 break;
1833
1834 case 2: // del-acl
1835 case 3: // del-map
1836 /* returned code: 1=ok, 0=ko */
1837 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1838 pat_ref_delete(ref, key->area);
1839 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1840 break;
1841
1842 default:
1843 ret = ACT_RET_ERR;
1844 }
1845
1846
1847 leave:
1848 free_trash_chunk(key);
1849 free_trash_chunk(value);
1850 return ret;
1851
1852 fail_alloc:
1853 if (!(s->flags & SF_ERR_MASK))
1854 s->flags |= SF_ERR_RESOURCE;
1855 ret = ACT_RET_ERR;
1856 goto leave;
1857}
1858
Christopher Faulet2eb53962020-01-14 14:47:34 +01001859/* Release memory allocated by an http map/acl action. */
1860static void release_http_map(struct act_rule *rule)
1861{
1862 struct logformat_node *lf, *lfb;
1863
1864 free(rule->arg.map.ref);
1865 list_for_each_entry_safe(lf, lfb, &rule->arg.map.key, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001866 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001867 release_sample_expr(lf->expr);
1868 free(lf->arg);
1869 free(lf);
1870 }
1871 if (rule->action == 1) {
1872 list_for_each_entry_safe(lf, lfb, &rule->arg.map.value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001873 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001874 release_sample_expr(lf->expr);
1875 free(lf->arg);
1876 free(lf);
1877 }
1878 }
1879}
1880
Christopher Faulet81e20172019-12-12 16:40:30 +01001881/* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or
Christopher Faulet046cf442019-12-17 15:45:23 +01001882 * two log-format string as argument depending on the action. The action is
1883 * stored in <.action> as an int (0=add-acl, 1=set-map, 2=del-acl,
1884 * 3=del-map). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001885 */
1886static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px,
1887 struct act_rule *rule, char **err)
1888{
1889 int cap, cur_arg;
1890
Christopher Faulet046cf442019-12-17 15:45:23 +01001891 if (args[*orig_arg-1][0] == 'a') // add-acl
1892 rule->action = 0;
1893 else if (args[*orig_arg-1][0] == 's') // set-map
1894 rule->action = 1;
1895 else if (args[*orig_arg-1][4] == 'a') // del-acl
1896 rule->action = 2;
1897 else if (args[*orig_arg-1][4] == 'm') // del-map
1898 rule->action = 3;
1899 else {
1900 memprintf(err, "internal error: unhandled action '%s'", args[0]);
1901 return ACT_RET_PRS_ERR;
1902 }
1903 rule->action_ptr = http_action_set_map;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001904 rule->release_ptr = release_http_map;
Christopher Faulet81e20172019-12-12 16:40:30 +01001905
1906 cur_arg = *orig_arg;
Christopher Faulet046cf442019-12-17 15:45:23 +01001907 if (rule->action == 1 && (!*args[cur_arg] || !*args[cur_arg+1])) {
1908 /* 2 args for set-map */
Christopher Faulet81e20172019-12-12 16:40:30 +01001909 memprintf(err, "expects exactly 2 arguments");
1910 return ACT_RET_PRS_ERR;
1911 }
1912 else if (!*args[cur_arg]) {
Christopher Faulet046cf442019-12-17 15:45:23 +01001913 /* only one arg for other actions */
Christopher Faulet81e20172019-12-12 16:40:30 +01001914 memprintf(err, "expects exactly 1 arguments");
1915 return ACT_RET_PRS_ERR;
1916 }
1917
1918 /*
1919 * '+ 8' for 'set-map(' (same for del-map)
1920 * '- 9' for 'set-map(' + trailing ')' (same for del-map)
1921 */
1922 rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9);
1923
1924 if (rule->from == ACT_F_HTTP_REQ) {
1925 px->conf.args.ctx = ARGC_HRQ;
1926 cap = (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR;
1927 }
1928 else{
1929 px->conf.args.ctx = ARGC_HRS;
1930 cap = (px->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR;
1931 }
1932
1933 /* key pattern */
1934 LIST_INIT(&rule->arg.map.key);
Christopher Faulet1337b322020-01-14 14:50:55 +01001935 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err)) {
1936 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001937 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001938 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001939
Christopher Faulet046cf442019-12-17 15:45:23 +01001940 if (rule->action == 1) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001941 /* value pattern for set-map only */
1942 cur_arg++;
1943 LIST_INIT(&rule->arg.map.value);
Christopher Faulet1337b322020-01-14 14:50:55 +01001944 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err)) {
1945 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001946 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001947 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001948 }
1949
1950 free(px->conf.lfs_file);
1951 px->conf.lfs_file = strdup(px->conf.args.file);
1952 px->conf.lfs_line = px->conf.args.line;
1953
1954 *orig_arg = cur_arg + 1;
1955 return ACT_RET_PRS_OK;
1956}
1957
Christopher Fauletac98d812019-12-18 09:20:16 +01001958/* This function executes a track-sc* actions. On success, it returns
1959 * ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1960 */
1961static enum act_return http_action_track_sc(struct act_rule *rule, struct proxy *px,
1962 struct session *sess, struct stream *s, int flags)
1963{
1964 struct stktable *t;
1965 struct stksess *ts;
1966 struct stktable_key *key;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001967 void *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *ptr6;
Christopher Fauletac98d812019-12-18 09:20:16 +01001968 int opt;
1969
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001970 ptr1 = ptr2 = ptr3 = ptr4 = ptr5 = ptr6 = NULL;
Christopher Fauletac98d812019-12-18 09:20:16 +01001971 opt = ((rule->from == ACT_F_HTTP_REQ) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES) | SMP_OPT_FINAL;
1972
1973 t = rule->arg.trk_ctr.table.t;
Emeric Brun362d25e2021-03-10 16:58:03 +01001974
1975 if (stkctr_entry(&s->stkctr[rule->action]))
1976 goto end;
1977
Christopher Fauletac98d812019-12-18 09:20:16 +01001978 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, NULL);
1979
1980 if (!key)
1981 goto end;
1982 ts = stktable_get_entry(t, key);
1983 if (!ts)
1984 goto end;
1985
1986 stream_track_stkctr(&s->stkctr[rule->action], t, ts);
1987
1988 /* let's count a new HTTP request as it's the first time we do it */
1989 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1990 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
1991
1992 /* When the client triggers a 4xx from the server, it's most often due
1993 * to a missing object or permission. These events should be tracked
1994 * because if they happen often, it may indicate a brute force or a
1995 * vulnerability scan. Normally this is done when receiving the response
1996 * but here we're tracking after this ought to have been done so we have
1997 * to do it on purpose.
1998 */
1999 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 400) < 100) {
2000 ptr3 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2001 ptr4 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2002 }
2003
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002004 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 500) < 100 &&
2005 s->txn->status != 501 && s->txn->status != 505) {
2006 ptr5 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
2007 ptr6 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
2008 }
2009
2010 if (ptr1 || ptr2 || ptr3 || ptr4 || ptr5 || ptr6) {
Christopher Fauletac98d812019-12-18 09:20:16 +01002011 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2012
2013 if (ptr1)
2014 stktable_data_cast(ptr1, http_req_cnt)++;
2015 if (ptr2)
2016 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2017 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2018 if (ptr3)
2019 stktable_data_cast(ptr3, http_err_cnt)++;
2020 if (ptr4)
2021 update_freq_ctr_period(&stktable_data_cast(ptr4, http_err_rate),
2022 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002023 if (ptr5)
2024 stktable_data_cast(ptr5, http_fail_cnt)++;
2025 if (ptr6)
2026 update_freq_ctr_period(&stktable_data_cast(ptr6, http_fail_rate),
2027 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u, 1);
Christopher Fauletac98d812019-12-18 09:20:16 +01002028
2029 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2030
2031 /* If data was modified, we need to touch to re-schedule sync */
2032 stktable_touch_local(t, ts, 0);
2033 }
2034
2035 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT);
2036 if (sess->fe != s->be)
2037 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND);
2038
2039 end:
2040 return ACT_RET_CONT;
2041}
Christopher Faulet81e20172019-12-12 16:40:30 +01002042
Christopher Faulet2eb53962020-01-14 14:47:34 +01002043static void release_http_track_sc(struct act_rule *rule)
2044{
2045 release_sample_expr(rule->arg.trk_ctr.expr);
2046}
2047
Christopher Faulet81e20172019-12-12 16:40:30 +01002048/* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success,
2049 * ACT_RET_PRS_ERR on error.
2050 */
2051static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px,
2052 struct act_rule *rule, char **err)
2053{
2054 struct sample_expr *expr;
2055 unsigned int where;
2056 unsigned int tsc_num;
2057 const char *tsc_num_str;
2058 int cur_arg;
2059
2060 tsc_num_str = &args[*orig_arg-1][8];
2061 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1)
2062 return ACT_RET_PRS_ERR;
2063
2064 cur_arg = *orig_arg;
2065 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002066 err, &px->conf.args, NULL);
Christopher Faulet81e20172019-12-12 16:40:30 +01002067 if (!expr)
2068 return ACT_RET_PRS_ERR;
2069
2070 where = 0;
2071 if (px->cap & PR_CAP_FE)
2072 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
2073 if (px->cap & PR_CAP_BE)
2074 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
2075
2076 if (!(expr->fetch->val & where)) {
2077 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here",
2078 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01002079 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002080 return ACT_RET_PRS_ERR;
2081 }
2082
2083 if (strcmp(args[cur_arg], "table") == 0) {
2084 cur_arg++;
2085 if (!*args[cur_arg]) {
2086 memprintf(err, "missing table name");
Christopher Faulet1337b322020-01-14 14:50:55 +01002087 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002088 return ACT_RET_PRS_ERR;
2089 }
2090
2091 /* we copy the table name for now, it will be resolved later */
2092 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
2093 cur_arg++;
2094 }
2095
Christopher Fauletac98d812019-12-18 09:20:16 +01002096 rule->action = tsc_num;
Christopher Faulet81e20172019-12-12 16:40:30 +01002097 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +01002098 rule->action_ptr = http_action_track_sc;
Christopher Faulet2eb53962020-01-14 14:47:34 +01002099 rule->release_ptr = release_http_track_sc;
Christopher Faulet81e20172019-12-12 16:40:30 +01002100 rule->check_ptr = check_trk_action;
2101
2102 *orig_arg = cur_arg;
2103 return ACT_RET_PRS_OK;
2104}
2105
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002106static enum act_return action_timeout_set_stream_timeout(struct act_rule *rule,
2107 struct proxy *px,
2108 struct session *sess,
2109 struct stream *s,
2110 int flags)
2111{
2112 struct sample *key;
2113
2114 if (rule->arg.timeout.expr) {
2115 key = sample_fetch_as_type(px, sess, s, SMP_OPT_FINAL, rule->arg.timeout.expr, SMP_T_SINT);
2116 if (!key)
2117 return ACT_RET_CONT;
2118
2119 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(key->data.u.sint));
2120 }
2121 else {
2122 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(rule->arg.timeout.value));
2123 }
2124
2125 return ACT_RET_CONT;
2126}
2127
2128/* Parse a "set-timeout" action. Returns ACT_RET_PRS_ERR if parsing error.
2129 */
2130static enum act_parse_ret parse_http_set_timeout(const char **args,
2131 int *orig_arg,
2132 struct proxy *px,
2133 struct act_rule *rule, char **err)
2134{
2135 int cur_arg;
2136
2137 rule->action = ACT_CUSTOM;
2138 rule->action_ptr = action_timeout_set_stream_timeout;
2139 rule->release_ptr = release_timeout_action;
2140
2141 cur_arg = *orig_arg;
2142 if (!*args[cur_arg] || !*args[cur_arg + 1]) {
2143 memprintf(err, "expects exactly 2 arguments");
2144 return ACT_RET_PRS_ERR;
2145 }
2146
2147 if (!(px->cap & PR_CAP_BE)) {
2148 memprintf(err, "proxy '%s' has no backend capability", px->id);
2149 return ACT_RET_PRS_ERR;
2150 }
2151
2152 if (cfg_parse_rule_set_timeout(args, cur_arg,
2153 &rule->arg.timeout.value,
2154 &rule->arg.timeout.type,
2155 &rule->arg.timeout.expr,
2156 err,
2157 px->conf.args.file,
2158 px->conf.args.line, &px->conf.args) == -1) {
2159 return ACT_RET_PRS_ERR;
2160 }
2161
2162 *orig_arg = cur_arg + 2;
2163
2164 return ACT_RET_PRS_OK;
2165}
2166
Christopher Faulet46f95542019-12-20 10:07:22 +01002167/* This function executes a strict-mode actions. On success, it always returns
2168 * ACT_RET_CONT
2169 */
2170static enum act_return http_action_strict_mode(struct act_rule *rule, struct proxy *px,
2171 struct session *sess, struct stream *s, int flags)
2172{
2173 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
2174
2175 if (rule->action == 0) // strict-mode on
2176 msg->flags &= ~HTTP_MSGF_SOFT_RW;
2177 else // strict-mode off
2178 msg->flags |= HTTP_MSGF_SOFT_RW;
2179 return ACT_RET_CONT;
2180}
2181
2182/* Parse a "strict-mode" action. It returns ACT_RET_PRS_OK on success,
2183 * ACT_RET_PRS_ERR on error.
2184 */
2185static enum act_parse_ret parse_http_strict_mode(const char **args, int *orig_arg, struct proxy *px,
2186 struct act_rule *rule, char **err)
2187{
2188 int cur_arg;
2189
Christopher Faulet46f95542019-12-20 10:07:22 +01002190 cur_arg = *orig_arg;
2191 if (!*args[cur_arg]) {
2192 memprintf(err, "expects exactly 1 arguments");
2193 return ACT_RET_PRS_ERR;
2194 }
2195
2196 if (strcasecmp(args[cur_arg], "on") == 0)
2197 rule->action = 0; // strict-mode on
2198 else if (strcasecmp(args[cur_arg], "off") == 0)
2199 rule->action = 1; // strict-mode off
2200 else {
2201 memprintf(err, "Unexpected value '%s'. Only 'on' and 'off' are supported", args[cur_arg]);
2202 return ACT_RET_PRS_ERR;
2203 }
2204 rule->action_ptr = http_action_strict_mode;
2205
2206 *orig_arg = cur_arg + 1;
2207 return ACT_RET_PRS_OK;
2208}
2209
Christopher Faulet24231ab2020-01-24 17:44:23 +01002210/* This function executes a return action. It builds an HTX message from an
2211 * errorfile, an raw file or a log-format string, depending on <.action>
2212 * value. On success, it returns ACT_RET_ABRT. If an error occurs ACT_RET_ERR is
2213 * returned.
2214 */
2215static enum act_return http_action_return(struct act_rule *rule, struct proxy *px,
2216 struct session *sess, struct stream *s, int flags)
2217{
2218 struct channel *req = &s->req;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002219
Christopher Faulet2d36df22021-02-19 11:41:01 +01002220 s->txn->status = rule->arg.http_reply->status;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002221 if (http_reply_message(s, rule->arg.http_reply) == -1)
2222 return ACT_RET_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002223
Christopher Faulet24231ab2020-01-24 17:44:23 +01002224 if (rule->from == ACT_F_HTTP_REQ) {
2225 /* let's log the request time */
2226 s->logs.tv_request = now;
2227 req->analysers &= AN_REQ_FLT_END;
2228
2229 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002230 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002231 }
2232
2233 if (!(s->flags & SF_ERR_MASK))
2234 s->flags |= SF_ERR_LOCAL;
2235 if (!(s->flags & SF_FINST_MASK))
2236 s->flags |= ((rule->from == ACT_F_HTTP_REQ) ? SF_FINST_R : SF_FINST_H);
2237
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002238 return ACT_RET_ABRT;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002239}
2240
Christopher Faulet24231ab2020-01-24 17:44:23 +01002241/* Parse a "return" action. It returns ACT_RET_PRS_OK on success,
Christopher Faulet47e791e2020-05-13 14:36:55 +02002242 * ACT_RET_PRS_ERR on error. It relies on http_parse_http_reply() to set
2243 * <.arg.http_reply>.
Christopher Faulet24231ab2020-01-24 17:44:23 +01002244 */
2245static enum act_parse_ret parse_http_return(const char **args, int *orig_arg, struct proxy *px,
2246 struct act_rule *rule, char **err)
2247{
Christopher Faulet47e791e2020-05-13 14:36:55 +02002248 /* Prepare parsing of log-format strings */
2249 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
2250 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, 200, err);
2251 if (!rule->arg.http_reply)
2252 return ACT_RET_PRS_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002253
Christopher Fauletba946bf2020-05-13 08:50:07 +02002254 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet5ff0c642020-05-12 18:33:37 +02002255 rule->action = ACT_CUSTOM;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002256 rule->check_ptr = check_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002257 rule->action_ptr = http_action_return;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002258 rule->release_ptr = release_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002259 return ACT_RET_PRS_OK;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002260}
2261
Christopher Faulet021a8e42021-03-29 10:46:38 +02002262
2263
2264/* This function executes a wait-for-body action. It waits for the message
2265 * payload for a max configured time (.arg.p[0]) and eventually for only first
2266 * <arg.p[1]> bytes (0 means no limit). It relies on http_wait_for_msg_body()
2267 * function. it returns ACT_RET_CONT when conditions are met to stop to wait.
2268 * Otherwise ACT_RET_YIELD is returned to wait for more data. ACT_RET_INV is
2269 * returned if a parsing error is raised by lower level and ACT_RET_ERR if an
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05002270 * internal error occurred. Finally ACT_RET_ABRT is returned when a timeout
2271 * occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02002272 */
2273static enum act_return http_action_wait_for_body(struct act_rule *rule, struct proxy *px,
2274 struct session *sess, struct stream *s, int flags)
2275{
2276 struct channel *chn = ((rule->from == ACT_F_HTTP_REQ) ? &s->req : &s->res);
2277 unsigned int time = (uintptr_t)rule->arg.act.p[0];
2278 unsigned int bytes = (uintptr_t)rule->arg.act.p[1];
2279
2280 switch (http_wait_for_msg_body(s, chn, time, bytes)) {
2281 case HTTP_RULE_RES_CONT:
2282 return ACT_RET_CONT;
2283 case HTTP_RULE_RES_YIELD:
2284 return ACT_RET_YIELD;
2285 case HTTP_RULE_RES_BADREQ:
2286 return ACT_RET_INV;
2287 case HTTP_RULE_RES_ERROR:
2288 return ACT_RET_ERR;
2289 case HTTP_RULE_RES_ABRT:
2290 return ACT_RET_ABRT;
2291 default:
2292 return ACT_RET_ERR;
2293 }
2294}
2295
2296/* Parse a "wait-for-body" action. It returns ACT_RET_PRS_OK on success,
2297 * ACT_RET_PRS_ERR on error.
2298 */
2299static enum act_parse_ret parse_http_wait_for_body(const char **args, int *orig_arg, struct proxy *px,
2300 struct act_rule *rule, char **err)
2301{
2302 int cur_arg;
2303 unsigned int time, bytes;
2304 const char *res;
2305
2306 cur_arg = *orig_arg;
2307 if (!*args[cur_arg]) {
2308 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2309 return ACT_RET_PRS_ERR;
2310 }
2311
2312 time = UINT_MAX; /* To be sure it is set */
2313 bytes = 0; /* Default value, wait all the body */
2314 while (*(args[cur_arg])) {
2315 if (strcmp(args[cur_arg], "time") == 0) {
2316 if (!*args[cur_arg + 1]) {
2317 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2318 return ACT_RET_PRS_ERR;
2319 }
2320 res = parse_time_err(args[cur_arg+1], &time, TIME_UNIT_MS);
2321 if (res == PARSE_TIME_OVER) {
2322 memprintf(err, "time overflow (maximum value is 2147483647 ms or ~24.8 days)");
2323 return ACT_RET_PRS_ERR;
2324 }
2325 if (res == PARSE_TIME_UNDER) {
2326 memprintf(err, "time underflow (minimum non-null value is 1 ms)");
2327 return ACT_RET_PRS_ERR;
2328 }
2329 if (res) {
2330 memprintf(err, "unexpected character '%c'", *res);
2331 return ACT_RET_PRS_ERR;
2332 }
2333 cur_arg++;
2334 }
2335 else if (strcmp(args[cur_arg], "at-least") == 0) {
2336 if (!*args[cur_arg + 1]) {
2337 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2338 return ACT_RET_PRS_ERR;
2339 }
2340 res = parse_size_err(args[cur_arg+1], &bytes);
2341 if (res) {
2342 memprintf(err, "unexpected character '%c'", *res);
2343 return ACT_RET_PRS_ERR;
2344 }
2345 cur_arg++;
2346 }
2347 else
2348 break;
2349 cur_arg++;
2350 }
2351
2352 if (time == UINT_MAX) {
2353 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2354 return ACT_RET_PRS_ERR;
2355 }
2356
2357 rule->arg.act.p[0] = (void *)(uintptr_t)time;
2358 rule->arg.act.p[1] = (void *)(uintptr_t)bytes;
2359
2360 *orig_arg = cur_arg;
2361
2362 rule->action = ACT_CUSTOM;
2363 rule->action_ptr = http_action_wait_for_body;
2364 return ACT_RET_PRS_OK;
2365}
2366
Willy Tarreau79e57332018-10-02 16:01:16 +02002367/************************************************************************/
2368/* All supported http-request action keywords must be declared here. */
2369/************************************************************************/
2370
2371static struct action_kw_list http_req_actions = {
2372 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002373 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002374 { "add-header", parse_http_set_header, 0 },
2375 { "allow", parse_http_allow, 0 },
2376 { "auth", parse_http_auth, 0 },
2377 { "capture", parse_http_req_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002378 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002379 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002380 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002381 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002382 { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 },
2383 { "early-hint", parse_http_set_header, 0 },
Amaury Denoyellea9e639a2021-05-06 15:50:12 +02002384 { "normalize-uri", parse_http_normalize_uri, KWF_EXPERIMENTAL },
Christopher Faulet81e20172019-12-12 16:40:30 +01002385 { "redirect", parse_http_redirect, 0 },
2386 { "reject", parse_http_action_reject, 0 },
2387 { "replace-header", parse_http_replace_header, 0 },
2388 { "replace-path", parse_replace_uri, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002389 { "replace-pathq", parse_replace_uri, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002390 { "replace-uri", parse_replace_uri, 0 },
2391 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002392 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002393 { "set-header", parse_http_set_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002394 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002395 { "set-method", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002396 { "set-path", parse_set_req_line, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002397 { "set-pathq", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002398 { "set-query", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002399 { "set-uri", parse_set_req_line, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002400 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002401 { "tarpit", parse_http_deny, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002402 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002403 { "set-timeout", parse_http_set_timeout, 0 },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002404 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002405 { NULL, NULL }
2406 }
2407};
2408
Willy Tarreau0108d902018-11-25 19:14:37 +01002409INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2410
Willy Tarreau79e57332018-10-02 16:01:16 +02002411static struct action_kw_list http_res_actions = {
2412 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002413 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002414 { "add-header", parse_http_set_header, 0 },
2415 { "allow", parse_http_allow, 0 },
2416 { "capture", parse_http_res_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002417 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002418 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002419 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002420 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002421 { "redirect", parse_http_redirect, 0 },
2422 { "replace-header", parse_http_replace_header, 0 },
2423 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002424 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002425 { "set-header", parse_http_set_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002426 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002427 { "set-status", parse_http_set_status, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002428 { "strict-mode", parse_http_strict_mode, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002429 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002430 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002431 { NULL, NULL }
2432 }
2433};
2434
Willy Tarreau0108d902018-11-25 19:14:37 +01002435INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +02002436
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002437static struct action_kw_list http_after_res_actions = {
2438 .kw = {
2439 { "add-header", parse_http_set_header, 0 },
2440 { "allow", parse_http_allow, 0 },
2441 { "del-header", parse_http_del_header, 0 },
2442 { "replace-header", parse_http_replace_header, 0 },
2443 { "replace-value", parse_http_replace_header, 0 },
2444 { "set-header", parse_http_set_header, 0 },
2445 { "set-status", parse_http_set_status, 0 },
2446 { "strict-mode", parse_http_strict_mode, 0 },
2447 { NULL, NULL }
2448 }
2449};
2450
2451INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_actions);
2452
Willy Tarreau79e57332018-10-02 16:01:16 +02002453/*
2454 * Local variables:
2455 * c-indent-level: 8
2456 * c-basic-offset: 8
2457 * End:
2458 */