blob: 74a894ff7fae2d724de6e2d42553296d08058b70 [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>
Christopher Faulet9a521232022-03-30 14:42:50 +020026#include <haproxy/conn_stream.h>
27#include <haproxy/cs_utils.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020028#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020029#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020030#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020031#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020032#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020033#include <haproxy/log.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020034#include <haproxy/pattern.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020035#include <haproxy/pool.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020036#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020037#include <haproxy/sample.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020038#include <haproxy/stream_interface.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020039#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020040#include <haproxy/uri_auth-t.h>
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +020041#include <haproxy/uri_normalizer.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020042#include <haproxy/version.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020043
Willy Tarreau79e57332018-10-02 16:01:16 +020044
Christopher Faulet2eb53962020-01-14 14:47:34 +010045/* Release memory allocated by most of HTTP actions. Concretly, it releases
46 * <arg.http>.
47 */
48static void release_http_action(struct act_rule *rule)
49{
50 struct logformat_node *lf, *lfb;
51
Tim Duesterhused526372020-03-05 17:56:33 +010052 istfree(&rule->arg.http.str);
Christopher Faulet2eb53962020-01-14 14:47:34 +010053 if (rule->arg.http.re)
54 regex_free(rule->arg.http.re);
55 list_for_each_entry_safe(lf, lfb, &rule->arg.http.fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +020056 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +010057 release_sample_expr(lf->expr);
58 free(lf->arg);
59 free(lf);
60 }
61}
62
Christopher Faulet5cb513a2020-05-13 17:56:56 +020063/* Release memory allocated by HTTP actions relying on an http reply. Concretly,
64 * it releases <.arg.http_reply>
65 */
66static void release_act_http_reply(struct act_rule *rule)
67{
68 release_http_reply(rule->arg.http_reply);
69 rule->arg.http_reply = NULL;
70}
71
72
73/* Check function for HTTP actions relying on an http reply. The function
74 * returns 1 in success case, otherwise, it returns 0 and err is filled.
75 */
76static int check_act_http_reply(struct act_rule *rule, struct proxy *px, char **err)
77{
78 struct http_reply *reply = rule->arg.http_reply;
79
80 if (!http_check_http_reply(reply, px, err)) {
81 release_act_http_reply(rule);
82 return 0;
83 }
84 return 1;
85}
86
Willy Tarreau79e57332018-10-02 16:01:16 +020087
88/* This function executes one of the set-{method,path,query,uri} actions. It
89 * builds a string in the trash from the specified format string. It finds
Christopher Faulet2c22a692019-12-18 15:39:56 +010090 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +050091 * parse_set_req_line(). The replacement action is executed by the function
Christopher Faulete00d06c2019-12-16 17:18:42 +010092 * http_action_set_req_line(). On success, it returns ACT_RET_CONT. If an error
93 * occurs while soft rewrites are enabled, the action is canceled, but the rule
94 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau79e57332018-10-02 16:01:16 +020095 */
96static enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
97 struct session *sess, struct stream *s, int flags)
98{
99 struct buffer *replace;
Christopher Faulet13403762019-12-13 09:01:57 +0100100 enum act_return ret = ACT_RET_CONT;
Willy Tarreau79e57332018-10-02 16:01:16 +0200101
102 replace = alloc_trash_chunk();
103 if (!replace)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100104 goto fail_alloc;
Willy Tarreau79e57332018-10-02 16:01:16 +0200105
106 /* If we have to create a query string, prepare a '?'. */
Christopher Faulet2c22a692019-12-18 15:39:56 +0100107 if (rule->action == 2) // set-query
Willy Tarreau79e57332018-10-02 16:01:16 +0200108 replace->area[replace->data++] = '?';
109 replace->data += build_logline(s, replace->area + replace->data,
110 replace->size - replace->data,
Christopher Faulet96bff762019-12-17 13:46:18 +0100111 &rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200112
Christopher Faulet2c22a692019-12-18 15:39:56 +0100113 if (http_req_replace_stline(rule->action, replace->area, replace->data, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100114 goto fail_rewrite;
Willy Tarreau79e57332018-10-02 16:01:16 +0200115
Christopher Faulete00d06c2019-12-16 17:18:42 +0100116 leave:
Willy Tarreau79e57332018-10-02 16:01:16 +0200117 free_trash_chunk(replace);
118 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100119
120 fail_alloc:
121 if (!(s->flags & SF_ERR_MASK))
122 s->flags |= SF_ERR_RESOURCE;
123 ret = ACT_RET_ERR;
124 goto leave;
125
126 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +0200127 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100128 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200129 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100130 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200131 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100132 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200133 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100134
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100135 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100136 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100137 if (!(s->flags & SF_ERR_MASK))
138 s->flags |= SF_ERR_PRXCOND;
139 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100140 goto leave;
Willy Tarreau79e57332018-10-02 16:01:16 +0200141}
142
143/* parse an http-request action among :
144 * set-method
145 * set-path
Christopher Faulet312294f2020-09-02 17:17:44 +0200146 * set-pathq
Willy Tarreau79e57332018-10-02 16:01:16 +0200147 * set-query
148 * set-uri
149 *
150 * All of them accept a single argument of type string representing a log-format.
Christopher Faulet96bff762019-12-17 13:46:18 +0100151 * The resulting rule makes use of <http.fmt> to store the log-format list head,
Christopher Faulet2c22a692019-12-18 15:39:56 +0100152 * and <.action> to store the action type as an int (0=method, 1=path, 2=query,
153 * 3=uri). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreau79e57332018-10-02 16:01:16 +0200154 */
155static enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
156 struct act_rule *rule, char **err)
157{
158 int cur_arg = *orig_arg;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +0200159 int cap = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200160
Willy Tarreau79e57332018-10-02 16:01:16 +0200161 switch (args[0][4]) {
162 case 'm' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100163 rule->action = 0; // set-method
Willy Tarreau79e57332018-10-02 16:01:16 +0200164 break;
165 case 'p' :
Christopher Faulet312294f2020-09-02 17:17:44 +0200166 if (args[0][8] == 'q')
167 rule->action = 4; // set-pathq
168 else
169 rule->action = 1; // set-path
Willy Tarreau79e57332018-10-02 16:01:16 +0200170 break;
171 case 'q' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100172 rule->action = 2; // set-query
Willy Tarreau79e57332018-10-02 16:01:16 +0200173 break;
174 case 'u' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100175 rule->action = 3; // set-uri
Willy Tarreau79e57332018-10-02 16:01:16 +0200176 break;
177 default:
178 memprintf(err, "internal error: unhandled action '%s'", args[0]);
179 return ACT_RET_PRS_ERR;
180 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100181 rule->action_ptr = http_action_set_req_line;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100182 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200183
184 if (!*args[cur_arg] ||
185 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
186 memprintf(err, "expects exactly 1 argument <format>");
187 return ACT_RET_PRS_ERR;
188 }
189
Christopher Faulet96bff762019-12-17 13:46:18 +0100190 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200191 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +0200192 if (px->cap & PR_CAP_FE)
193 cap |= SMP_VAL_FE_HRQ_HDR;
194 if (px->cap & PR_CAP_BE)
195 cap |= SMP_VAL_BE_HRQ_HDR;
196 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200197 return ACT_RET_PRS_ERR;
198 }
199
200 (*orig_arg)++;
201 return ACT_RET_PRS_OK;
202}
203
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200204/* This function executes the http-request normalize-uri action.
205 * `rule->action` is expected to be a value from `enum act_normalize_uri`.
206 *
207 * On success, it returns ACT_RET_CONT. If an error
208 * occurs while soft rewrites are enabled, the action is canceled, but the rule
209 * processing continue. Otherwsize ACT_RET_ERR is returned.
210 */
211static enum act_return http_action_normalize_uri(struct act_rule *rule, struct proxy *px,
212 struct session *sess, struct stream *s, int flags)
213{
214 enum act_return ret = ACT_RET_CONT;
215 struct htx *htx = htxbuf(&s->req.buf);
216 const struct ist uri = htx_sl_req_uri(http_get_stline(htx));
217 struct buffer *replace = alloc_trash_chunk();
218 enum uri_normalizer_err err = URI_NORMALIZER_ERR_INTERNAL_ERROR;
219
220 if (!replace)
221 goto fail_alloc;
222
223 switch ((enum act_normalize_uri) rule->action) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200224 case ACT_NORMALIZE_URI_PATH_MERGE_SLASHES: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200225 struct http_uri_parser parser = http_uri_parser_init(uri);
226 const struct ist path = http_parse_path(&parser);
Tim Duesterhusd371e992021-04-15 21:45:58 +0200227 struct ist newpath = ist2(replace->area, replace->size);
228
229 if (!isttest(path))
230 goto leave;
231
232 err = uri_normalizer_path_merge_slashes(iststop(path, '?'), &newpath);
233
234 if (err != URI_NORMALIZER_ERR_NONE)
235 break;
236
237 if (!http_replace_req_path(htx, newpath, 0))
238 goto fail_rewrite;
239
240 break;
241 }
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200242 case ACT_NORMALIZE_URI_PATH_STRIP_DOT: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200243 struct http_uri_parser parser = http_uri_parser_init(uri);
244 const struct ist path = http_parse_path(&parser);
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200245 struct ist newpath = ist2(replace->area, replace->size);
246
247 if (!isttest(path))
248 goto leave;
249
250 err = uri_normalizer_path_dot(iststop(path, '?'), &newpath);
251
252 if (err != URI_NORMALIZER_ERR_NONE)
253 break;
254
255 if (!http_replace_req_path(htx, newpath, 0))
256 goto fail_rewrite;
257
258 break;
259 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200260 case ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT:
261 case ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200262 struct http_uri_parser parser = http_uri_parser_init(uri);
263 const struct ist path = http_parse_path(&parser);
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200264 struct ist newpath = ist2(replace->area, replace->size);
265
266 if (!isttest(path))
267 goto leave;
268
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200269 err = uri_normalizer_path_dotdot(iststop(path, '?'), rule->action == ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL, &newpath);
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200270
271 if (err != URI_NORMALIZER_ERR_NONE)
272 break;
273
274 if (!http_replace_req_path(htx, newpath, 0))
275 goto fail_rewrite;
276
277 break;
278 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200279 case ACT_NORMALIZE_URI_QUERY_SORT_BY_NAME: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200280 struct http_uri_parser parser = http_uri_parser_init(uri);
281 const struct ist path = http_parse_path(&parser);
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200282 struct ist newquery = ist2(replace->area, replace->size);
283
284 if (!isttest(path))
285 goto leave;
286
287 err = uri_normalizer_query_sort(istfind(path, '?'), '&', &newquery);
288
289 if (err != URI_NORMALIZER_ERR_NONE)
290 break;
291
292 if (!http_replace_req_query(htx, newquery))
293 goto fail_rewrite;
294
295 break;
296 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200297 case ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE:
298 case ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200299 struct http_uri_parser parser = http_uri_parser_init(uri);
300 const struct ist path = http_parse_path(&parser);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200301 struct ist newpath = ist2(replace->area, replace->size);
302
303 if (!isttest(path))
304 goto leave;
305
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200306 err = uri_normalizer_percent_upper(path, rule->action == ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT, &newpath);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200307
308 if (err != URI_NORMALIZER_ERR_NONE)
309 break;
310
311 if (!http_replace_req_path(htx, newpath, 1))
312 goto fail_rewrite;
313
314 break;
315 }
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +0200316 case ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED:
317 case ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200318 struct http_uri_parser parser = http_uri_parser_init(uri);
319 const struct ist path = http_parse_path(&parser);
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +0200320 struct ist newpath = ist2(replace->area, replace->size);
321
322 if (!isttest(path))
323 goto leave;
324
325 err = uri_normalizer_percent_decode_unreserved(path, rule->action == ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT, &newpath);
326
327 if (err != URI_NORMALIZER_ERR_NONE)
328 break;
329
330 if (!http_replace_req_path(htx, newpath, 1))
331 goto fail_rewrite;
332
333 break;
334 }
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200335 case ACT_NORMALIZE_URI_FRAGMENT_STRIP: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200336 struct http_uri_parser parser = http_uri_parser_init(uri);
337 const struct ist path = http_parse_path(&parser);
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200338 struct ist newpath = ist2(replace->area, replace->size);
339
340 if (!isttest(path))
341 goto leave;
342
343 err = uri_normalizer_fragment_strip(path, &newpath);
344
345 if (err != URI_NORMALIZER_ERR_NONE)
346 break;
347
348 if (!http_replace_req_path(htx, newpath, 1))
349 goto fail_rewrite;
350
351 break;
352 }
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200353 case ACT_NORMALIZE_URI_FRAGMENT_ENCODE: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200354 struct http_uri_parser parser = http_uri_parser_init(uri);
355 const struct ist path = http_parse_path(&parser);
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200356 struct ist newpath = ist2(replace->area, replace->size);
357
358 if (!isttest(path))
359 goto leave;
360
361 err = uri_normalizer_fragment_encode(path, &newpath);
362
363 if (err != URI_NORMALIZER_ERR_NONE)
364 break;
365
366 if (!http_replace_req_path(htx, newpath, 1))
367 goto fail_rewrite;
368
369 break;
370 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200371 }
372
373 switch (err) {
374 case URI_NORMALIZER_ERR_NONE:
375 break;
376 case URI_NORMALIZER_ERR_INTERNAL_ERROR:
377 ret = ACT_RET_ERR;
378 break;
379 case URI_NORMALIZER_ERR_INVALID_INPUT:
380 ret = ACT_RET_INV;
381 break;
382 case URI_NORMALIZER_ERR_ALLOC:
383 goto fail_alloc;
384 }
385
386 leave:
387 free_trash_chunk(replace);
388 return ret;
389
390 fail_alloc:
391 if (!(s->flags & SF_ERR_MASK))
392 s->flags |= SF_ERR_RESOURCE;
393 ret = ACT_RET_ERR;
394 goto leave;
395
396 fail_rewrite:
397 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
398 if (s->flags & SF_BE_ASSIGNED)
399 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
400 if (sess->listener && sess->listener->counters)
401 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
402 if (objt_server(s->target))
403 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
404
405 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
406 ret = ACT_RET_ERR;
407 if (!(s->flags & SF_ERR_MASK))
408 s->flags |= SF_ERR_PRXCOND;
409 }
410 goto leave;
411}
412
413/* Parses the http-request normalize-uri action. It expects a single <normalizer>
414 * argument, corresponding too a value in `enum act_normalize_uri`.
415 *
416 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
417 */
418static enum act_parse_ret parse_http_normalize_uri(const char **args, int *orig_arg, struct proxy *px,
419 struct act_rule *rule, char **err)
420{
421 int cur_arg = *orig_arg;
422
423 rule->action_ptr = http_action_normalize_uri;
424 rule->release_ptr = NULL;
425
426 if (!*args[cur_arg]) {
427 memprintf(err, "missing argument <normalizer>");
428 return ACT_RET_PRS_ERR;
429 }
430
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200431 if (strcmp(args[cur_arg], "path-merge-slashes") == 0) {
Tim Duesterhusd371e992021-04-15 21:45:58 +0200432 cur_arg++;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200433
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200434 rule->action = ACT_NORMALIZE_URI_PATH_MERGE_SLASHES;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200435 }
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200436 else if (strcmp(args[cur_arg], "path-strip-dot") == 0) {
437 cur_arg++;
438
439 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOT;
440 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200441 else if (strcmp(args[cur_arg], "path-strip-dotdot") == 0) {
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200442 cur_arg++;
443
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200444 if (strcmp(args[cur_arg], "full") == 0) {
445 cur_arg++;
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200446 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL;
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200447 }
448 else if (!*args[cur_arg]) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200449 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT;
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200450 }
451 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
Tim Duesterhus2f413132021-05-10 23:21:20 +0200452 memprintf(err, "unknown argument '%s' for 'path-strip-dotdot' normalizer", args[cur_arg]);
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200453 return ACT_RET_PRS_ERR;
454 }
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200455 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200456 else if (strcmp(args[cur_arg], "query-sort-by-name") == 0) {
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200457 cur_arg++;
458
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200459 rule->action = ACT_NORMALIZE_URI_QUERY_SORT_BY_NAME;
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200460 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200461 else if (strcmp(args[cur_arg], "percent-to-uppercase") == 0) {
Tim Duesterhusa4071932021-04-15 21:46:02 +0200462 cur_arg++;
463
464 if (strcmp(args[cur_arg], "strict") == 0) {
465 cur_arg++;
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200466 rule->action = ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT;
Tim Duesterhusa4071932021-04-15 21:46:02 +0200467 }
468 else if (!*args[cur_arg]) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200469 rule->action = ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE;
Tim Duesterhusa4071932021-04-15 21:46:02 +0200470 }
471 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
Tim Duesterhus2f413132021-05-10 23:21:20 +0200472 memprintf(err, "unknown argument '%s' for 'percent-to-uppercase' normalizer", args[cur_arg]);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200473 return ACT_RET_PRS_ERR;
474 }
475 }
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +0200476 else if (strcmp(args[cur_arg], "percent-decode-unreserved") == 0) {
477 cur_arg++;
478
479 if (strcmp(args[cur_arg], "strict") == 0) {
480 cur_arg++;
481 rule->action = ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT;
482 }
483 else if (!*args[cur_arg]) {
484 rule->action = ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED;
485 }
486 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
487 memprintf(err, "unknown argument '%s' for 'percent-decode-unreserved' normalizer", args[cur_arg]);
488 return ACT_RET_PRS_ERR;
489 }
490 }
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200491 else if (strcmp(args[cur_arg], "fragment-strip") == 0) {
492 cur_arg++;
493
494 rule->action = ACT_NORMALIZE_URI_FRAGMENT_STRIP;
495 }
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200496 else if (strcmp(args[cur_arg], "fragment-encode") == 0) {
497 cur_arg++;
498
499 rule->action = ACT_NORMALIZE_URI_FRAGMENT_ENCODE;
500 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200501 else {
502 memprintf(err, "unknown normalizer '%s'", args[cur_arg]);
503 return ACT_RET_PRS_ERR;
504 }
505
506 *orig_arg = cur_arg;
507 return ACT_RET_PRS_OK;
508}
509
Willy Tarreau33810222019-06-12 17:44:02 +0200510/* This function executes a replace-uri action. It finds its arguments in
Christopher Faulet96bff762019-12-17 13:46:18 +0100511 * <rule>.arg.http. It builds a string in the trash from the format string
Willy Tarreau33810222019-06-12 17:44:02 +0200512 * previously filled by function parse_replace_uri() and will execute the regex
Christopher Faulet96bff762019-12-17 13:46:18 +0100513 * in <http.re> to replace the URI. It uses the format string present in
Christopher Faulet2c22a692019-12-18 15:39:56 +0100514 * <http.fmt>. The component to act on (path/uri) is taken from <.action> which
Christopher Faulet96bff762019-12-17 13:46:18 +0100515 * contains 1 for the path or 3 for the URI (values used by
516 * http_req_replace_stline()). On success, it returns ACT_RET_CONT. If an error
517 * occurs while soft rewrites are enabled, the action is canceled, but the rule
518 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau33810222019-06-12 17:44:02 +0200519 */
520static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px,
521 struct session *sess, struct stream *s, int flags)
522{
Christopher Faulet13403762019-12-13 09:01:57 +0100523 enum act_return ret = ACT_RET_CONT;
Willy Tarreau33810222019-06-12 17:44:02 +0200524 struct buffer *replace, *output;
525 struct ist uri;
526 int len;
527
528 replace = alloc_trash_chunk();
529 output = alloc_trash_chunk();
530 if (!replace || !output)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100531 goto fail_alloc;
Christopher Faulet12c28b62019-07-15 16:30:24 +0200532 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Willy Tarreau262c3f12019-12-17 06:52:51 +0100533
Amaury Denoyellec453f952021-07-06 11:40:12 +0200534 if (rule->action == 1) { // replace-path
535 struct http_uri_parser parser = http_uri_parser_init(uri);
536 uri = iststop(http_parse_path(&parser), '?');
537 }
538 else if (rule->action == 4) { // replace-pathq
539 struct http_uri_parser parser = http_uri_parser_init(uri);
540 uri = http_parse_path(&parser);
541 }
Willy Tarreau262c3f12019-12-17 06:52:51 +0100542
Christopher Faulet114e7592022-04-08 10:44:21 +0200543 if (!istlen(uri))
544 goto leave;
545
Christopher Faulet96bff762019-12-17 13:46:18 +0100546 if (!regex_exec_match2(rule->arg.http.re, uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
Willy Tarreau33810222019-06-12 17:44:02 +0200547 goto leave;
548
Christopher Faulet96bff762019-12-17 13:46:18 +0100549 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200550
551 /* note: uri.ptr doesn't need to be zero-terminated because it will
552 * only be used to pick pmatch references.
553 */
554 len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch);
555 if (len == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100556 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200557
Christopher Faulet2c22a692019-12-18 15:39:56 +0100558 if (http_req_replace_stline(rule->action, output->area, len, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100559 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200560
Christopher Faulete00d06c2019-12-16 17:18:42 +0100561 leave:
Willy Tarreau33810222019-06-12 17:44:02 +0200562 free_trash_chunk(output);
563 free_trash_chunk(replace);
564 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100565
566 fail_alloc:
567 if (!(s->flags & SF_ERR_MASK))
568 s->flags |= SF_ERR_RESOURCE;
569 ret = ACT_RET_ERR;
570 goto leave;
571
572 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +0200573 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100574 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200575 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100576 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200577 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100578 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200579 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100580
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100581 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100582 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100583 if (!(s->flags & SF_ERR_MASK))
584 s->flags |= SF_ERR_PRXCOND;
585 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100586 goto leave;
Willy Tarreau33810222019-06-12 17:44:02 +0200587}
588
Christopher Faulet312294f2020-09-02 17:17:44 +0200589/* parse a "replace-uri", "replace-path" or "replace-pathq"
590 * http-request action.
Willy Tarreau33810222019-06-12 17:44:02 +0200591 * This action takes 2 arguments (a regex and a replacement format string).
Christopher Faulet2c22a692019-12-18 15:39:56 +0100592 * The resulting rule makes use of <.action> to store the action (1/3 for now),
Christopher Faulet96bff762019-12-17 13:46:18 +0100593 * <http.re> to store the compiled regex, and <http.fmt> to store the log-format
Willy Tarreau33810222019-06-12 17:44:02 +0200594 * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
595 */
596static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px,
597 struct act_rule *rule, char **err)
598{
599 int cur_arg = *orig_arg;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +0200600 int cap = 0;
Willy Tarreau33810222019-06-12 17:44:02 +0200601 char *error = NULL;
602
Christopher Faulet312294f2020-09-02 17:17:44 +0200603 switch (args[0][8]) {
604 case 'p':
605 if (args[0][12] == 'q')
606 rule->action = 4; // replace-pathq, same as set-pathq
607 else
608 rule->action = 1; // replace-path, same as set-path
609 break;
610 case 'u':
Christopher Faulet2c22a692019-12-18 15:39:56 +0100611 rule->action = 3; // replace-uri, same as set-uri
Christopher Faulet312294f2020-09-02 17:17:44 +0200612 break;
613 default:
614 memprintf(err, "internal error: unhandled action '%s'", args[0]);
615 return ACT_RET_PRS_ERR;
616 }
Willy Tarreau262c3f12019-12-17 06:52:51 +0100617
Willy Tarreau33810222019-06-12 17:44:02 +0200618 rule->action_ptr = http_action_replace_uri;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100619 rule->release_ptr = release_http_action;
Willy Tarreau33810222019-06-12 17:44:02 +0200620
621 if (!*args[cur_arg] || !*args[cur_arg+1] ||
622 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
623 memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>");
624 return ACT_RET_PRS_ERR;
625 }
626
Christopher Faulet96bff762019-12-17 13:46:18 +0100627 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, &error))) {
Willy Tarreau33810222019-06-12 17:44:02 +0200628 memprintf(err, "failed to parse the regex : %s", error);
629 free(error);
630 return ACT_RET_PRS_ERR;
631 }
632
Christopher Faulet96bff762019-12-17 13:46:18 +0100633 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200634 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +0200635 if (px->cap & PR_CAP_FE)
636 cap |= SMP_VAL_FE_HRQ_HDR;
637 if (px->cap & PR_CAP_BE)
638 cap |= SMP_VAL_BE_HRQ_HDR;
639 if (!parse_logformat_string(args[cur_arg + 1], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Christopher Faulet1337b322020-01-14 14:50:55 +0100640 regex_free(rule->arg.http.re);
Willy Tarreau33810222019-06-12 17:44:02 +0200641 return ACT_RET_PRS_ERR;
642 }
643
644 (*orig_arg) += 2;
645 return ACT_RET_PRS_OK;
646}
647
Willy Tarreau79e57332018-10-02 16:01:16 +0200648/* This function is just a compliant action wrapper for "set-status". */
649static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
650 struct session *sess, struct stream *s, int flags)
651{
Christopher Faulet96bff762019-12-17 13:46:18 +0100652 if (http_res_set_status(rule->arg.http.i, rule->arg.http.str, s) == -1) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200653 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100654 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200655 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100656 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200657 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100658 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200659 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100660
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100661 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100662 if (!(s->flags & SF_ERR_MASK))
663 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet692a6c22020-02-07 10:22:31 +0100664 return ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100665 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100666 }
667
Willy Tarreau79e57332018-10-02 16:01:16 +0200668 return ACT_RET_CONT;
669}
670
671/* parse set-status action:
672 * This action accepts a single argument of type int representing
673 * an http status code. It returns ACT_RET_PRS_OK on success,
674 * ACT_RET_PRS_ERR on error.
675 */
676static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
677 struct act_rule *rule, char **err)
678{
679 char *error;
680
681 rule->action = ACT_CUSTOM;
682 rule->action_ptr = action_http_set_status;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100683 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200684
685 /* Check if an argument is available */
686 if (!*args[*orig_arg]) {
687 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
688 return ACT_RET_PRS_ERR;
689 }
690
691 /* convert status code as integer */
Christopher Faulet96bff762019-12-17 13:46:18 +0100692 rule->arg.http.i = strtol(args[*orig_arg], &error, 10);
693 if (*error != '\0' || rule->arg.http.i < 100 || rule->arg.http.i > 999) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200694 memprintf(err, "expects an integer status code between 100 and 999");
695 return ACT_RET_PRS_ERR;
696 }
697
698 (*orig_arg)++;
699
700 /* set custom reason string */
Christopher Faulet96bff762019-12-17 13:46:18 +0100701 rule->arg.http.str = ist(NULL); // If null, we use the default reason for the status code.
Willy Tarreau79e57332018-10-02 16:01:16 +0200702 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
703 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
704 (*orig_arg)++;
Tim Duesterhusf4f6c0f2022-03-15 13:11:08 +0100705 rule->arg.http.str = ist(strdup(args[*orig_arg]));
Willy Tarreau79e57332018-10-02 16:01:16 +0200706 (*orig_arg)++;
707 }
708
Christopher Fauletc20b3712020-01-27 15:51:56 +0100709 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200710 return ACT_RET_PRS_OK;
711}
712
713/* This function executes the "reject" HTTP action. It clears the request and
714 * response buffer without sending any response. It can be useful as an HTTP
715 * alternative to the silent-drop action to defend against DoS attacks, and may
716 * also be used with HTTP/2 to close a connection instead of just a stream.
717 * The txn status is unchanged, indicating no response was sent. The termination
Christopher Faulet90d22a82020-03-06 11:18:39 +0100718 * flags will indicate "PR". It always returns ACT_RET_ABRT.
Willy Tarreau79e57332018-10-02 16:01:16 +0200719 */
720static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
721 struct session *sess, struct stream *s, int flags)
722{
Christopher Faulet9a521232022-03-30 14:42:50 +0200723 cs_must_kill_conn(chn_prod(&s->req));
Willy Tarreau79e57332018-10-02 16:01:16 +0200724 channel_abort(&s->req);
725 channel_abort(&s->res);
Christopher Fauletd4a824e2020-03-06 15:07:09 +0100726 s->req.analysers &= AN_REQ_FLT_END;
727 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau79e57332018-10-02 16:01:16 +0200728
Willy Tarreau4781b152021-04-06 13:53:36 +0200729 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
730 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200731 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200732 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200733
734 if (!(s->flags & SF_ERR_MASK))
735 s->flags |= SF_ERR_PRXCOND;
736 if (!(s->flags & SF_FINST_MASK))
737 s->flags |= SF_FINST_R;
738
Christopher Faulet90d22a82020-03-06 11:18:39 +0100739 return ACT_RET_ABRT;
Willy Tarreau79e57332018-10-02 16:01:16 +0200740}
741
742/* parse the "reject" action:
743 * This action takes no argument and returns ACT_RET_PRS_OK on success,
744 * ACT_RET_PRS_ERR on error.
745 */
746static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
747 struct act_rule *rule, char **err)
748{
749 rule->action = ACT_CUSTOM;
750 rule->action_ptr = http_action_reject;
751 return ACT_RET_PRS_OK;
752}
753
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200754/* This function executes the "disable-l7-retry" HTTP action.
755 * It disables L7 retries (all retry except for a connection failure). This
756 * can be useful for example to avoid retrying on POST requests.
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200757 * It just removes the L7 retry flag on the HTTP transaction, and always
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200758 * return ACT_RET_CONT;
759 */
760static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px,
761 struct session *sess, struct stream *s, int flags)
762{
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200763 /* In theory, the TX_L7_RETRY flags isn't set at this point, but
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200764 * let's be future-proof and remove it anyway.
765 */
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200766 s->txn->flags &= ~TX_L7_RETRY;
767 s->txn->flags |= TX_D_L7_RETRY;
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200768 return ACT_RET_CONT;
769}
770
771/* parse the "disable-l7-retry" action:
772 * This action takes no argument and returns ACT_RET_PRS_OK on success,
773 * ACT_RET_PRS_ERR on error.
774 */
775static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args,
776 int *orig_args, struct proxy *px,
777 struct act_rule *rule, char **err)
778{
779 rule->action = ACT_CUSTOM;
780 rule->action_ptr = http_req_disable_l7_retry;
781 return ACT_RET_PRS_OK;
782}
783
Willy Tarreau79e57332018-10-02 16:01:16 +0200784/* This function executes the "capture" action. It executes a fetch expression,
785 * turns the result into a string and puts it in a capture slot. It always
786 * returns 1. If an error occurs the action is cancelled, but the rule
787 * processing continues.
788 */
789static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
790 struct session *sess, struct stream *s, int flags)
791{
792 struct sample *key;
793 struct cap_hdr *h = rule->arg.cap.hdr;
794 char **cap = s->req_cap;
795 int len;
796
797 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
798 if (!key)
799 return ACT_RET_CONT;
800
801 if (cap[h->index] == NULL)
802 cap[h->index] = pool_alloc(h->pool);
803
804 if (cap[h->index] == NULL) /* no more capture memory */
805 return ACT_RET_CONT;
806
807 len = key->data.u.str.data;
808 if (len > h->len)
809 len = h->len;
810
811 memcpy(cap[h->index], key->data.u.str.area, len);
812 cap[h->index][len] = 0;
813 return ACT_RET_CONT;
814}
815
816/* This function executes the "capture" action and store the result in a
817 * capture slot if exists. It executes a fetch expression, turns the result
818 * into a string and puts it in a capture slot. It always returns 1. If an
819 * error occurs the action is cancelled, but the rule processing continues.
820 */
821static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
822 struct session *sess, struct stream *s, int flags)
823{
824 struct sample *key;
825 struct cap_hdr *h;
826 char **cap = s->req_cap;
827 struct proxy *fe = strm_fe(s);
828 int len;
829 int i;
830
831 /* Look for the original configuration. */
832 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
833 h != NULL && i != rule->arg.capid.idx ;
834 i--, h = h->next);
835 if (!h)
836 return ACT_RET_CONT;
837
838 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
839 if (!key)
840 return ACT_RET_CONT;
841
842 if (cap[h->index] == NULL)
843 cap[h->index] = pool_alloc(h->pool);
844
845 if (cap[h->index] == NULL) /* no more capture memory */
846 return ACT_RET_CONT;
847
848 len = key->data.u.str.data;
849 if (len > h->len)
850 len = h->len;
851
852 memcpy(cap[h->index], key->data.u.str.area, len);
853 cap[h->index][len] = 0;
854 return ACT_RET_CONT;
855}
856
857/* Check an "http-request capture" action.
858 *
859 * The function returns 1 in success case, otherwise, it returns 0 and err is
860 * filled.
861 */
862static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
863{
864 if (rule->action_ptr != http_action_req_capture_by_id)
865 return 1;
866
Baptiste Assmann19a69b32020-01-16 14:34:22 +0100867 /* capture slots can only be declared in frontends, so we can't check their
868 * existence in backends at configuration parsing step
869 */
870 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_req_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200871 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
872 rule->arg.capid.idx);
873 return 0;
874 }
875
876 return 1;
877}
878
Christopher Faulet2eb53962020-01-14 14:47:34 +0100879/* Release memory allocate by an http capture action */
880static void release_http_capture(struct act_rule *rule)
881{
882 if (rule->action_ptr == http_action_req_capture)
883 release_sample_expr(rule->arg.cap.expr);
884 else
885 release_sample_expr(rule->arg.capid.expr);
886}
887
Willy Tarreau79e57332018-10-02 16:01:16 +0200888/* parse an "http-request capture" action. It takes a single argument which is
889 * a sample fetch expression. It stores the expression into arg->act.p[0] and
890 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
891 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
892 */
893static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
894 struct act_rule *rule, char **err)
895{
896 struct sample_expr *expr;
897 struct cap_hdr *hdr;
898 int cur_arg;
899 int len = 0;
900
901 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
902 if (strcmp(args[cur_arg], "if") == 0 ||
903 strcmp(args[cur_arg], "unless") == 0)
904 break;
905
906 if (cur_arg < *orig_arg + 3) {
907 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
908 return ACT_RET_PRS_ERR;
909 }
910
911 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100912 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 +0200913 if (!expr)
914 return ACT_RET_PRS_ERR;
915
916 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
917 memprintf(err,
918 "fetch method '%s' extracts information from '%s', none of which is available here",
919 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100920 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200921 return ACT_RET_PRS_ERR;
922 }
923
924 if (!args[cur_arg] || !*args[cur_arg]) {
925 memprintf(err, "expects 'len or 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100926 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200927 return ACT_RET_PRS_ERR;
928 }
929
930 if (strcmp(args[cur_arg], "len") == 0) {
931 cur_arg++;
932
933 if (!(px->cap & PR_CAP_FE)) {
934 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Christopher Faulet1337b322020-01-14 14:50:55 +0100935 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200936 return ACT_RET_PRS_ERR;
937 }
938
939 px->conf.args.ctx = ARGC_CAP;
940
941 if (!args[cur_arg]) {
942 memprintf(err, "missing length value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100943 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200944 return ACT_RET_PRS_ERR;
945 }
946 /* we copy the table name for now, it will be resolved later */
947 len = atoi(args[cur_arg]);
948 if (len <= 0) {
949 memprintf(err, "length must be > 0");
Christopher Faulet1337b322020-01-14 14:50:55 +0100950 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200951 return ACT_RET_PRS_ERR;
952 }
953 cur_arg++;
954
Willy Tarreau79e57332018-10-02 16:01:16 +0200955 hdr = calloc(1, sizeof(*hdr));
Remi Tricot-Le Bretona4bf8a02021-05-12 17:54:17 +0200956 if (!hdr) {
957 memprintf(err, "out of memory");
958 release_sample_expr(expr);
959 return ACT_RET_PRS_ERR;
960 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200961 hdr->next = px->req_cap;
962 hdr->name = NULL; /* not a header capture */
963 hdr->namelen = 0;
964 hdr->len = len;
965 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
966 hdr->index = px->nb_req_cap++;
967
968 px->req_cap = hdr;
969 px->to_log |= LW_REQHDR;
970
971 rule->action = ACT_CUSTOM;
972 rule->action_ptr = http_action_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100973 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200974 rule->arg.cap.expr = expr;
975 rule->arg.cap.hdr = hdr;
976 }
977
978 else if (strcmp(args[cur_arg], "id") == 0) {
979 int id;
980 char *error;
981
982 cur_arg++;
983
984 if (!args[cur_arg]) {
985 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100986 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200987 return ACT_RET_PRS_ERR;
988 }
989
990 id = strtol(args[cur_arg], &error, 10);
991 if (*error != '\0') {
992 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100993 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200994 return ACT_RET_PRS_ERR;
995 }
996 cur_arg++;
997
998 px->conf.args.ctx = ARGC_CAP;
999
1000 rule->action = ACT_CUSTOM;
1001 rule->action_ptr = http_action_req_capture_by_id;
1002 rule->check_ptr = check_http_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001003 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +02001004 rule->arg.capid.expr = expr;
1005 rule->arg.capid.idx = id;
1006 }
1007
1008 else {
1009 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001010 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001011 return ACT_RET_PRS_ERR;
1012 }
1013
1014 *orig_arg = cur_arg;
1015 return ACT_RET_PRS_OK;
1016}
1017
1018/* This function executes the "capture" action and store the result in a
1019 * capture slot if exists. It executes a fetch expression, turns the result
1020 * into a string and puts it in a capture slot. It always returns 1. If an
1021 * error occurs the action is cancelled, but the rule processing continues.
1022 */
1023static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
1024 struct session *sess, struct stream *s, int flags)
1025{
1026 struct sample *key;
1027 struct cap_hdr *h;
1028 char **cap = s->res_cap;
1029 struct proxy *fe = strm_fe(s);
1030 int len;
1031 int i;
1032
1033 /* Look for the original configuration. */
1034 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
1035 h != NULL && i != rule->arg.capid.idx ;
1036 i--, h = h->next);
1037 if (!h)
1038 return ACT_RET_CONT;
1039
1040 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
1041 if (!key)
1042 return ACT_RET_CONT;
1043
1044 if (cap[h->index] == NULL)
1045 cap[h->index] = pool_alloc(h->pool);
1046
1047 if (cap[h->index] == NULL) /* no more capture memory */
1048 return ACT_RET_CONT;
1049
1050 len = key->data.u.str.data;
1051 if (len > h->len)
1052 len = h->len;
1053
1054 memcpy(cap[h->index], key->data.u.str.area, len);
1055 cap[h->index][len] = 0;
1056 return ACT_RET_CONT;
1057}
1058
1059/* Check an "http-response capture" action.
1060 *
1061 * The function returns 1 in success case, otherwise, it returns 0 and err is
1062 * filled.
1063 */
1064static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
1065{
1066 if (rule->action_ptr != http_action_res_capture_by_id)
1067 return 1;
1068
Tim Duesterhusf3f4aa02020-07-03 13:43:42 +02001069 /* capture slots can only be declared in frontends, so we can't check their
1070 * existence in backends at configuration parsing step
1071 */
1072 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_rsp_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001073 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
1074 rule->arg.capid.idx);
1075 return 0;
1076 }
1077
1078 return 1;
1079}
1080
1081/* parse an "http-response capture" action. It takes a single argument which is
1082 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07001083 * the allocated hdr_cap struct of the preallocated id into arg->act.p[1].
Willy Tarreau79e57332018-10-02 16:01:16 +02001084 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1085 */
1086static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
1087 struct act_rule *rule, char **err)
1088{
1089 struct sample_expr *expr;
1090 int cur_arg;
1091 int id;
1092 char *error;
1093
1094 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
1095 if (strcmp(args[cur_arg], "if") == 0 ||
1096 strcmp(args[cur_arg], "unless") == 0)
1097 break;
1098
1099 if (cur_arg < *orig_arg + 3) {
1100 memprintf(err, "expects <expression> id <idx>");
1101 return ACT_RET_PRS_ERR;
1102 }
1103
1104 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001105 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 +02001106 if (!expr)
1107 return ACT_RET_PRS_ERR;
1108
1109 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
1110 memprintf(err,
1111 "fetch method '%s' extracts information from '%s', none of which is available here",
1112 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01001113 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001114 return ACT_RET_PRS_ERR;
1115 }
1116
1117 if (!args[cur_arg] || !*args[cur_arg]) {
1118 memprintf(err, "expects 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +01001119 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001120 return ACT_RET_PRS_ERR;
1121 }
1122
1123 if (strcmp(args[cur_arg], "id") != 0) {
1124 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001125 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001126 return ACT_RET_PRS_ERR;
1127 }
1128
1129 cur_arg++;
1130
1131 if (!args[cur_arg]) {
1132 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +01001133 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001134 return ACT_RET_PRS_ERR;
1135 }
1136
1137 id = strtol(args[cur_arg], &error, 10);
1138 if (*error != '\0') {
1139 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001140 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001141 return ACT_RET_PRS_ERR;
1142 }
1143 cur_arg++;
1144
1145 px->conf.args.ctx = ARGC_CAP;
1146
1147 rule->action = ACT_CUSTOM;
1148 rule->action_ptr = http_action_res_capture_by_id;
1149 rule->check_ptr = check_http_res_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001150 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +02001151 rule->arg.capid.expr = expr;
1152 rule->arg.capid.idx = id;
1153
1154 *orig_arg = cur_arg;
1155 return ACT_RET_PRS_OK;
1156}
1157
Christopher Faulet81e20172019-12-12 16:40:30 +01001158/* Parse a "allow" action for a request or a response rule. It takes no argument. It
1159 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1160 */
1161static enum act_parse_ret parse_http_allow(const char **args, int *orig_arg, struct proxy *px,
1162 struct act_rule *rule, char **err)
1163{
1164 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +01001165 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +01001166 return ACT_RET_PRS_OK;
1167}
1168
Christopher Faulete0fca292020-01-13 21:49:03 +01001169/* Parse "deny" or "tarpit" actions for a request rule or "deny" action for a
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001170 * response rule. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on
1171 * error. It relies on http_parse_http_reply() to set
1172 * <.arg.http_reply>.
Christopher Faulet81e20172019-12-12 16:40:30 +01001173 */
Christopher Faulete0fca292020-01-13 21:49:03 +01001174static enum act_parse_ret parse_http_deny(const char **args, int *orig_arg, struct proxy *px,
1175 struct act_rule *rule, char **err)
Christopher Faulet81e20172019-12-12 16:40:30 +01001176{
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001177 int default_status;
1178 int cur_arg, arg = 0;
Christopher Faulet81e20172019-12-12 16:40:30 +01001179
1180 cur_arg = *orig_arg;
Christopher Faulete0fca292020-01-13 21:49:03 +01001181 if (rule->from == ACT_F_HTTP_REQ) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001182 if (strcmp(args[cur_arg - 1], "tarpit") == 0) {
Christopher Faulete0fca292020-01-13 21:49:03 +01001183 rule->action = ACT_HTTP_REQ_TARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001184 default_status = 500;
Christopher Faulet81e20172019-12-12 16:40:30 +01001185 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001186 else {
1187 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001188 default_status = 403;
Christopher Faulet81e20172019-12-12 16:40:30 +01001189 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001190 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001191 else {
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001192 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001193 default_status = 502;
Christopher Faulete0fca292020-01-13 21:49:03 +01001194 }
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001195
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001196 /* If no args or only a deny_status specified, fallback on the legacy
1197 * mode and use default error files despite the fact that
1198 * default-errorfiles is not used. Otherwise, parse an http reply.
1199 */
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001200
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001201 /* Prepare parsing of log-format strings */
1202 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001203
Christopher Faulet9467f182020-06-30 09:32:01 +02001204 if (!*(args[cur_arg]) || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001205 rule->arg.http_reply = http_parse_http_reply((const char *[]){"default-errorfiles", ""}, &arg, px, default_status, err);
1206 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001207 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001208
1209 if (strcmp(args[cur_arg], "deny_status") == 0) {
Christopher Faulet9467f182020-06-30 09:32:01 +02001210 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 +02001211 rule->arg.http_reply = http_parse_http_reply((const char *[]){"status", args[cur_arg+1], "default-errorfiles", ""},
1212 &arg, px, default_status, err);
1213 *orig_arg += 2;
1214 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001215 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001216 args[cur_arg] += 5; /* skip "deny_" for the parsing */
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001217 }
1218
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001219 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, default_status, err);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001220
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001221 end:
1222 if (!rule->arg.http_reply)
1223 return ACT_RET_PRS_ERR;
1224
1225 rule->flags |= ACT_FLAG_FINAL;
1226 rule->check_ptr = check_act_http_reply;
1227 rule->release_ptr = release_act_http_reply;
Christopher Faulet81e20172019-12-12 16:40:30 +01001228 return ACT_RET_PRS_OK;
1229}
1230
Christopher Fauletb3048832020-05-27 15:26:43 +02001231
1232/* This function executes a auth action. It builds an 401/407 HTX message using
1233 * the corresponding proxy's error message. On success, it returns
1234 * ACT_RET_ABRT. If an error occurs ACT_RET_ERR is returned.
1235 */
1236static enum act_return http_action_auth(struct act_rule *rule, struct proxy *px,
1237 struct session *sess, struct stream *s, int flags)
1238{
1239 struct channel *req = &s->req;
1240 struct channel *res = &s->res;
1241 struct htx *htx = htx_from_buf(&res->buf);
1242 struct http_reply *reply;
1243 const char *auth_realm;
1244 struct http_hdr_ctx ctx;
1245 struct ist hdr;
1246
1247 /* Auth might be performed on regular http-req rules as well as on stats */
1248 auth_realm = rule->arg.http.str.ptr;
1249 if (!auth_realm) {
1250 if (px->uri_auth && s->current_rule_list == &px->uri_auth->http_req_rules)
1251 auth_realm = STATS_DEFAULT_REALM;
1252 else
1253 auth_realm = px->id;
1254 }
1255
1256 if (!(s->txn->flags & TX_USE_PX_CONN)) {
1257 s->txn->status = 401;
1258 hdr = ist("WWW-Authenticate");
1259 }
1260 else {
1261 s->txn->status = 407;
1262 hdr = ist("Proxy-Authenticate");
1263 }
1264 reply = http_error_message(s);
1265 channel_htx_truncate(res, htx);
1266
1267 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
1268 goto fail;
1269
1270 /* Write the generic 40x message */
1271 if (http_reply_to_htx(s, htx, reply) == -1)
1272 goto fail;
1273
1274 /* Remove all existing occurrences of the XXX-Authenticate header */
1275 ctx.blk = NULL;
1276 while (http_find_header(htx, hdr, &ctx, 1))
1277 http_remove_header(htx, &ctx);
1278
1279 /* Now a the right XXX-Authenticate header */
1280 if (!http_add_header(htx, hdr, ist2(b_orig(&trash), b_data(&trash))))
1281 goto fail;
1282
1283 /* Finally forward the reply */
1284 htx_to_buf(htx, &res->buf);
1285 if (!http_forward_proxy_resp(s, 1))
1286 goto fail;
1287
1288 /* Note: Only eval on the request */
1289 s->logs.tv_request = now;
1290 req->analysers &= AN_REQ_FLT_END;
1291
1292 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02001293 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Fauletb3048832020-05-27 15:26:43 +02001294
1295 if (!(s->flags & SF_ERR_MASK))
1296 s->flags |= SF_ERR_LOCAL;
1297 if (!(s->flags & SF_FINST_MASK))
1298 s->flags |= SF_FINST_R;
1299
1300 stream_inc_http_err_ctr(s);
1301 return ACT_RET_ABRT;
1302
1303 fail:
1304 /* If an error occurred, remove the incomplete HTTP response from the
1305 * buffer */
1306 channel_htx_truncate(res, htx);
1307 return ACT_RET_ERR;
1308}
1309
Christopher Faulet81e20172019-12-12 16:40:30 +01001310/* Parse a "auth" action. It may take 2 optional arguments to define a "realm"
1311 * parameter. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1312 */
1313static enum act_parse_ret parse_http_auth(const char **args, int *orig_arg, struct proxy *px,
1314 struct act_rule *rule, char **err)
1315{
1316 int cur_arg;
1317
Christopher Fauletb3048832020-05-27 15:26:43 +02001318 rule->action = ACT_CUSTOM;
Christopher Faulet245cf792019-12-18 14:58:12 +01001319 rule->flags |= ACT_FLAG_FINAL;
Christopher Fauletb3048832020-05-27 15:26:43 +02001320 rule->action_ptr = http_action_auth;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001321 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001322
1323 cur_arg = *orig_arg;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001324 if (strcmp(args[cur_arg], "realm") == 0) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001325 cur_arg++;
1326 if (!*args[cur_arg]) {
1327 memprintf(err, "missing realm value.\n");
1328 return ACT_RET_PRS_ERR;
1329 }
Tim Duesterhusf4f6c0f2022-03-15 13:11:08 +01001330 rule->arg.http.str = ist(strdup(args[cur_arg]));
Christopher Faulet81e20172019-12-12 16:40:30 +01001331 cur_arg++;
1332 }
1333
Christopher Fauletc20b3712020-01-27 15:51:56 +01001334 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001335 *orig_arg = cur_arg;
1336 return ACT_RET_PRS_OK;
1337}
1338
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001339/* This function executes a early-hint action. It adds an HTTP Early Hint HTTP
1340 * 103 response header with <.arg.http.str> name and with a value built
1341 * according to <.arg.http.fmt> log line format. If it is the first early-hint
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001342 * rule of series, the 103 response start-line is added first. At the end, if
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001343 * the next rule is not an early-hint rule or if it is the last rule, the EOH
1344 * block is added to terminate the response. On success, it returns
1345 * ACT_RET_CONT. If an error occurs while soft rewrites are enabled, the action
1346 * is canceled, but the rule processing continue. Otherwsize ACT_RET_ERR is
1347 * returned.
1348 */
1349static enum act_return http_action_early_hint(struct act_rule *rule, struct proxy *px,
1350 struct session *sess, struct stream *s, int flags)
1351{
1352 struct act_rule *prev_rule, *next_rule;
1353 struct channel *res = &s->res;
1354 struct htx *htx = htx_from_buf(&res->buf);
1355 struct buffer *value = alloc_trash_chunk();
1356 enum act_return ret = ACT_RET_CONT;
1357
1358 if (!(s->txn->req.flags & HTTP_MSGF_VER_11))
1359 goto leave;
1360
1361 if (!value) {
1362 if (!(s->flags & SF_ERR_MASK))
1363 s->flags |= SF_ERR_RESOURCE;
1364 goto error;
1365 }
1366
1367 /* get previous and next rules */
1368 prev_rule = LIST_PREV(&rule->list, typeof(rule), list);
1369 next_rule = LIST_NEXT(&rule->list, typeof(rule), list);
1370
1371 /* if no previous rule or previous rule is not early-hint, start a new response. Otherwise,
1372 * continue to add link to a previously started response */
1373 if (&prev_rule->list == s->current_rule_list || prev_rule->action_ptr != http_action_early_hint) {
1374 struct htx_sl *sl;
1375 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
1376 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
1377
1378 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
1379 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
1380 if (!sl)
1381 goto error;
1382 sl->info.res.status = 103;
1383 }
1384
1385 /* Add the HTTP Early Hint HTTP 103 response heade */
1386 value->data = build_logline(s, b_tail(value), b_room(value), &rule->arg.http.fmt);
1387 if (!htx_add_header(htx, rule->arg.http.str, ist2(b_head(value), b_data(value))))
1388 goto error;
1389
1390 /* if it is the last rule or the next one is not an early-hint, terminate the current
1391 * response. */
1392 if (&next_rule->list == s->current_rule_list || next_rule->action_ptr != http_action_early_hint) {
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001393 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
1394 /* If an error occurred during an Early-hint rule,
1395 * remove the incomplete HTTP 103 response from the
1396 * buffer */
1397 goto error;
1398 }
1399
Christopher Fauleta72a7e42020-01-28 09:28:11 +01001400 if (!http_forward_proxy_resp(s, 0))
1401 goto error;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001402 }
1403
1404 leave:
1405 free_trash_chunk(value);
1406 return ret;
1407
1408 error:
1409 /* If an error occurred during an Early-hint rule, remove the incomplete
1410 * HTTP 103 response from the buffer */
1411 channel_htx_truncate(res, htx);
1412 ret = ACT_RET_ERR;
1413 goto leave;
1414}
1415
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001416/* This function executes a set-header or add-header actions. It builds a string
1417 * in the trash from the specified format string. It finds the action to be
1418 * performed in <.action>, previously filled by function parse_set_header(). The
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001419 * replacement action is executed by the function http_action_set_header(). On
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001420 * success, it returns ACT_RET_CONT. If an error occurs while soft rewrites are
1421 * enabled, the action is canceled, but the rule processing continue. Otherwsize
1422 * ACT_RET_ERR is returned.
1423 */
1424static enum act_return http_action_set_header(struct act_rule *rule, struct proxy *px,
1425 struct session *sess, struct stream *s, int flags)
1426{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001427 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1428 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001429 enum act_return ret = ACT_RET_CONT;
1430 struct buffer *replace;
1431 struct http_hdr_ctx ctx;
1432 struct ist n, v;
1433
1434 replace = alloc_trash_chunk();
1435 if (!replace)
1436 goto fail_alloc;
1437
1438 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1439 n = rule->arg.http.str;
1440 v = ist2(replace->area, replace->data);
1441
1442 if (rule->action == 0) { // set-header
1443 /* remove all occurrences of the header */
1444 ctx.blk = NULL;
1445 while (http_find_header(htx, n, &ctx, 1))
1446 http_remove_header(htx, &ctx);
1447 }
1448
1449 /* Now add header */
1450 if (!http_add_header(htx, n, v))
1451 goto fail_rewrite;
1452
1453 leave:
1454 free_trash_chunk(replace);
1455 return ret;
1456
1457 fail_alloc:
1458 if (!(s->flags & SF_ERR_MASK))
1459 s->flags |= SF_ERR_RESOURCE;
1460 ret = ACT_RET_ERR;
1461 goto leave;
1462
1463 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001464 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001465 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001466 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001467 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001468 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001469 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001470 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001471
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001472 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001473 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001474 if (!(s->flags & SF_ERR_MASK))
1475 s->flags |= SF_ERR_PRXCOND;
1476 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001477 goto leave;
1478}
1479
Christopher Faulet81e20172019-12-12 16:40:30 +01001480/* Parse a "set-header", "add-header" or "early-hint" actions. It takes an
1481 * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK
1482 * on success, ACT_RET_PRS_ERR on error.
1483 *
1484 * Note: same function is used for the request and the response. However
1485 * "early-hint" rules are only supported for request rules.
1486 */
1487static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px,
1488 struct act_rule *rule, char **err)
1489{
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001490 int cap = 0, cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +01001491
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001492 if (args[*orig_arg-1][0] == 'e') {
1493 rule->action = ACT_CUSTOM;
1494 rule->action_ptr = http_action_early_hint;
1495 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001496 else {
1497 if (args[*orig_arg-1][0] == 's')
1498 rule->action = 0; // set-header
1499 else
1500 rule->action = 1; // add-header
1501 rule->action_ptr = http_action_set_header;
1502 }
Christopher Faulet2eb53962020-01-14 14:47:34 +01001503 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001504
1505 cur_arg = *orig_arg;
1506 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1507 memprintf(err, "expects exactly 2 arguments");
1508 return ACT_RET_PRS_ERR;
1509 }
1510
Christopher Faulet81e20172019-12-12 16:40:30 +01001511
Tim Duesterhusf4f6c0f2022-03-15 13:11:08 +01001512 rule->arg.http.str = ist(strdup(args[cur_arg]));
Christopher Faulet96bff762019-12-17 13:46:18 +01001513 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001514
1515 if (rule->from == ACT_F_HTTP_REQ) {
1516 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001517 if (px->cap & PR_CAP_FE)
1518 cap |= SMP_VAL_FE_HRQ_HDR;
1519 if (px->cap & PR_CAP_BE)
1520 cap |= SMP_VAL_BE_HRQ_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001521 }
1522 else{
1523 px->conf.args.ctx = ARGC_HRS;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001524 if (px->cap & PR_CAP_FE)
1525 cap |= SMP_VAL_FE_HRS_HDR;
1526 if (px->cap & PR_CAP_BE)
1527 cap |= SMP_VAL_BE_HRS_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001528 }
1529
1530 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001531 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001532 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001533 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001534 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001535
1536 free(px->conf.lfs_file);
1537 px->conf.lfs_file = strdup(px->conf.args.file);
1538 px->conf.lfs_line = px->conf.args.line;
1539
1540 *orig_arg = cur_arg + 1;
1541 return ACT_RET_PRS_OK;
1542}
1543
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001544/* This function executes a replace-header or replace-value actions. It
1545 * builds a string in the trash from the specified format string. It finds
1546 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001547 * parse_replace_header(). The replacement action is executed by the function
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001548 * http_action_replace_header(). On success, it returns ACT_RET_CONT. If an error
1549 * occurs while soft rewrites are enabled, the action is canceled, but the rule
1550 * processing continue. Otherwsize ACT_RET_ERR is returned.
1551 */
1552static enum act_return http_action_replace_header(struct act_rule *rule, struct proxy *px,
1553 struct session *sess, struct stream *s, int flags)
1554{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001555 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1556 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001557 enum act_return ret = ACT_RET_CONT;
1558 struct buffer *replace;
1559 int r;
1560
1561 replace = alloc_trash_chunk();
1562 if (!replace)
1563 goto fail_alloc;
1564
1565 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1566
1567 r = http_replace_hdrs(s, htx, rule->arg.http.str, replace->area, rule->arg.http.re, (rule->action == 0));
1568 if (r == -1)
1569 goto fail_rewrite;
1570
1571 leave:
1572 free_trash_chunk(replace);
1573 return ret;
1574
1575 fail_alloc:
1576 if (!(s->flags & SF_ERR_MASK))
1577 s->flags |= SF_ERR_RESOURCE;
1578 ret = ACT_RET_ERR;
1579 goto leave;
1580
1581 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001582 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001583 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001584 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001585 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001586 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001587 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001588 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001589
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001590 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001591 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001592 if (!(s->flags & SF_ERR_MASK))
1593 s->flags |= SF_ERR_PRXCOND;
1594 }
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001595 goto leave;
1596}
1597
Christopher Faulet81e20172019-12-12 16:40:30 +01001598/* Parse a "replace-header" or "replace-value" actions. It takes an header name,
1599 * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on
1600 * success, ACT_RET_PRS_ERR on error.
1601 */
1602static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px,
1603 struct act_rule *rule, char **err)
1604{
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001605 int cap = 0, cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +01001606
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001607 if (args[*orig_arg-1][8] == 'h')
1608 rule->action = 0; // replace-header
1609 else
1610 rule->action = 1; // replace-value
1611 rule->action_ptr = http_action_replace_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001612 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001613
1614 cur_arg = *orig_arg;
1615 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2]) {
1616 memprintf(err, "expects exactly 3 arguments");
1617 return ACT_RET_PRS_ERR;
1618 }
1619
Tim Duesterhusf4f6c0f2022-03-15 13:11:08 +01001620 rule->arg.http.str = ist(strdup(args[cur_arg]));
Christopher Faulet96bff762019-12-17 13:46:18 +01001621 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001622
1623 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001624 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, err))) {
Tim Duesterhused526372020-03-05 17:56:33 +01001625 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001626 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001627 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001628
1629 if (rule->from == ACT_F_HTTP_REQ) {
1630 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001631 if (px->cap & PR_CAP_FE)
1632 cap |= SMP_VAL_FE_HRQ_HDR;
1633 if (px->cap & PR_CAP_BE)
1634 cap |= SMP_VAL_BE_HRQ_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001635 }
1636 else{
1637 px->conf.args.ctx = ARGC_HRS;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001638 if (px->cap & PR_CAP_FE)
1639 cap |= SMP_VAL_FE_HRS_HDR;
1640 if (px->cap & PR_CAP_BE)
1641 cap |= SMP_VAL_BE_HRS_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001642 }
1643
1644 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001645 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001646 istfree(&rule->arg.http.str);
Christopher Faulet1337b322020-01-14 14:50:55 +01001647 regex_free(rule->arg.http.re);
Christopher Faulet81e20172019-12-12 16:40:30 +01001648 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001649 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001650
1651 free(px->conf.lfs_file);
1652 px->conf.lfs_file = strdup(px->conf.args.file);
1653 px->conf.lfs_line = px->conf.args.line;
1654
1655 *orig_arg = cur_arg + 1;
1656 return ACT_RET_PRS_OK;
1657}
1658
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001659/* This function executes a del-header action with selected matching mode for
1660 * header name. It finds the matching method to be performed in <.action>, previously
1661 * filled by function parse_http_del_header(). On success, it returns ACT_RET_CONT.
1662 * Otherwise ACT_RET_ERR is returned.
1663 */
1664static enum act_return http_action_del_header(struct act_rule *rule, struct proxy *px,
1665 struct session *sess, struct stream *s, int flags)
1666{
1667 struct http_hdr_ctx ctx;
1668 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1669 struct htx *htx = htxbuf(&msg->chn->buf);
1670 enum act_return ret = ACT_RET_CONT;
1671
1672 /* remove all occurrences of the header */
1673 ctx.blk = NULL;
1674 switch (rule->action) {
1675 case PAT_MATCH_STR:
1676 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
1677 http_remove_header(htx, &ctx);
1678 break;
1679 case PAT_MATCH_BEG:
1680 while (http_find_pfx_header(htx, rule->arg.http.str, &ctx, 1))
1681 http_remove_header(htx, &ctx);
1682 break;
1683 case PAT_MATCH_END:
1684 while (http_find_sfx_header(htx, rule->arg.http.str, &ctx, 1))
1685 http_remove_header(htx, &ctx);
1686 break;
1687 case PAT_MATCH_SUB:
1688 while (http_find_sub_header(htx, rule->arg.http.str, &ctx, 1))
1689 http_remove_header(htx, &ctx);
1690 break;
1691 case PAT_MATCH_REG:
1692 while (http_match_header(htx, rule->arg.http.re, &ctx, 1))
1693 http_remove_header(htx, &ctx);
1694 break;
1695 default:
1696 return ACT_RET_ERR;
1697 }
1698 return ret;
1699}
1700
1701/* Parse a "del-header" action. It takes string as a required argument,
1702 * optional flag (currently only -m) and optional matching method of input string
1703 * with header name to be deleted. Default matching method is exact match (-m str).
1704 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001705 */
1706static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px,
1707 struct act_rule *rule, char **err)
1708{
1709 int cur_arg;
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001710 int pat_idx;
Christopher Faulet81e20172019-12-12 16:40:30 +01001711
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001712 /* set exact matching (-m str) as default */
1713 rule->action = PAT_MATCH_STR;
1714 rule->action_ptr = http_action_del_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001715 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001716
1717 cur_arg = *orig_arg;
1718 if (!*args[cur_arg]) {
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001719 memprintf(err, "expects at least 1 argument");
Christopher Faulet81e20172019-12-12 16:40:30 +01001720 return ACT_RET_PRS_ERR;
1721 }
1722
Tim Duesterhusf4f6c0f2022-03-15 13:11:08 +01001723 rule->arg.http.str = ist(strdup(args[cur_arg]));
Christopher Faulet81e20172019-12-12 16:40:30 +01001724 px->conf.args.ctx = (rule->from == ACT_F_HTTP_REQ ? ARGC_HRQ : ARGC_HRS);
1725
Maciej Zdeb6dee9962020-11-23 16:03:09 +00001726 LIST_INIT(&rule->arg.http.fmt);
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001727 if (strcmp(args[cur_arg+1], "-m") == 0) {
1728 cur_arg++;
1729 if (!*args[cur_arg+1]) {
1730 memprintf(err, "-m flag expects exactly 1 argument");
1731 return ACT_RET_PRS_ERR;
1732 }
1733
1734 cur_arg++;
1735 pat_idx = pat_find_match_name(args[cur_arg]);
1736 switch (pat_idx) {
1737 case PAT_MATCH_REG:
1738 if (!(rule->arg.http.re = regex_comp(rule->arg.http.str.ptr, 1, 1, err)))
1739 return ACT_RET_PRS_ERR;
1740 /* fall through */
1741 case PAT_MATCH_STR:
1742 case PAT_MATCH_BEG:
1743 case PAT_MATCH_END:
1744 case PAT_MATCH_SUB:
1745 rule->action = pat_idx;
1746 break;
1747 default:
1748 memprintf(err, "-m with unsupported matching method '%s'", args[cur_arg]);
1749 return ACT_RET_PRS_ERR;
1750 }
1751 }
1752
Christopher Faulet81e20172019-12-12 16:40:30 +01001753 *orig_arg = cur_arg + 1;
1754 return ACT_RET_PRS_OK;
1755}
1756
Christopher Faulet2eb53962020-01-14 14:47:34 +01001757/* Release memory allocated by an http redirect action. */
1758static void release_http_redir(struct act_rule *rule)
1759{
1760 struct logformat_node *lf, *lfb;
1761 struct redirect_rule *redir;
1762
1763 redir = rule->arg.redir;
Willy Tarreau2b718102021-04-21 07:32:39 +02001764 LIST_DELETE(&redir->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001765 if (redir->cond) {
1766 prune_acl_cond(redir->cond);
1767 free(redir->cond);
1768 }
1769 free(redir->rdr_str);
1770 free(redir->cookie_str);
1771 list_for_each_entry_safe(lf, lfb, &redir->rdr_fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001772 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001773 free(lf);
1774 }
1775 free(redir);
1776}
1777
Christopher Faulet81e20172019-12-12 16:40:30 +01001778/* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success,
1779 * ACT_RET_PRS_ERR on error.
1780 */
1781static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px,
1782 struct act_rule *rule, char **err)
1783{
1784 struct redirect_rule *redir;
1785 int dir, cur_arg;
1786
1787 rule->action = ACT_HTTP_REDIR;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001788 rule->release_ptr = release_http_redir;
Christopher Faulet81e20172019-12-12 16:40:30 +01001789
1790 cur_arg = *orig_arg;
1791
1792 dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1);
1793 if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL)
1794 return ACT_RET_PRS_ERR;
1795
Willy Tarreaubc1223b2021-09-02 16:54:33 +02001796 if (!(redir->flags & REDIRECT_FLAG_IGNORE_EMPTY))
1797 rule->flags |= ACT_FLAG_FINAL;
1798
Christopher Faulet81e20172019-12-12 16:40:30 +01001799 rule->arg.redir = redir;
1800 rule->cond = redir->cond;
1801 redir->cond = NULL;
1802
1803 /* skip all arguments */
1804 while (*args[cur_arg])
1805 cur_arg++;
1806
1807 *orig_arg = cur_arg;
1808 return ACT_RET_PRS_OK;
1809}
1810
Christopher Faulet046cf442019-12-17 15:45:23 +01001811/* This function executes a add-acl, del-acl, set-map or del-map actions. On
1812 * success, it returns ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1813 */
1814static enum act_return http_action_set_map(struct act_rule *rule, struct proxy *px,
1815 struct session *sess, struct stream *s, int flags)
1816{
1817 struct pat_ref *ref;
1818 struct buffer *key = NULL, *value = NULL;
1819 enum act_return ret = ACT_RET_CONT;
1820
1821 /* collect reference */
1822 ref = pat_ref_lookup(rule->arg.map.ref);
1823 if (!ref)
1824 goto leave;
1825
1826 /* allocate key */
1827 key = alloc_trash_chunk();
1828 if (!key)
1829 goto fail_alloc;
1830
1831 /* collect key */
1832 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
1833 key->area[key->data] = '\0';
1834
1835 switch (rule->action) {
1836 case 0: // add-acl
1837 /* add entry only if it does not already exist */
1838 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1839 if (pat_ref_find_elt(ref, key->area) == NULL)
1840 pat_ref_add(ref, key->area, NULL, NULL);
1841 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1842 break;
1843
1844 case 1: // set-map
1845 /* allocate value */
1846 value = alloc_trash_chunk();
1847 if (!value)
1848 goto fail_alloc;
1849
1850 /* collect value */
1851 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
1852 value->area[value->data] = '\0';
1853
1854 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1855 if (pat_ref_find_elt(ref, key->area) != NULL) {
1856 /* update entry if it exists */
1857 pat_ref_set(ref, key->area, value->area, NULL);
1858 }
1859 else {
1860 /* insert a new entry */
1861 pat_ref_add(ref, key->area, value->area, NULL);
1862 }
1863 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1864 break;
1865
1866 case 2: // del-acl
1867 case 3: // del-map
1868 /* returned code: 1=ok, 0=ko */
1869 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1870 pat_ref_delete(ref, key->area);
1871 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1872 break;
1873
1874 default:
1875 ret = ACT_RET_ERR;
1876 }
1877
1878
1879 leave:
1880 free_trash_chunk(key);
1881 free_trash_chunk(value);
1882 return ret;
1883
1884 fail_alloc:
1885 if (!(s->flags & SF_ERR_MASK))
1886 s->flags |= SF_ERR_RESOURCE;
1887 ret = ACT_RET_ERR;
1888 goto leave;
1889}
1890
Christopher Faulet2eb53962020-01-14 14:47:34 +01001891/* Release memory allocated by an http map/acl action. */
1892static void release_http_map(struct act_rule *rule)
1893{
1894 struct logformat_node *lf, *lfb;
1895
1896 free(rule->arg.map.ref);
1897 list_for_each_entry_safe(lf, lfb, &rule->arg.map.key, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001898 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001899 release_sample_expr(lf->expr);
1900 free(lf->arg);
1901 free(lf);
1902 }
1903 if (rule->action == 1) {
1904 list_for_each_entry_safe(lf, lfb, &rule->arg.map.value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001905 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001906 release_sample_expr(lf->expr);
1907 free(lf->arg);
1908 free(lf);
1909 }
1910 }
1911}
1912
Christopher Faulet81e20172019-12-12 16:40:30 +01001913/* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or
Christopher Faulet046cf442019-12-17 15:45:23 +01001914 * two log-format string as argument depending on the action. The action is
1915 * stored in <.action> as an int (0=add-acl, 1=set-map, 2=del-acl,
1916 * 3=del-map). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001917 */
1918static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px,
1919 struct act_rule *rule, char **err)
1920{
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001921 int cap = 0, cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +01001922
Christopher Faulet046cf442019-12-17 15:45:23 +01001923 if (args[*orig_arg-1][0] == 'a') // add-acl
1924 rule->action = 0;
1925 else if (args[*orig_arg-1][0] == 's') // set-map
1926 rule->action = 1;
1927 else if (args[*orig_arg-1][4] == 'a') // del-acl
1928 rule->action = 2;
1929 else if (args[*orig_arg-1][4] == 'm') // del-map
1930 rule->action = 3;
1931 else {
1932 memprintf(err, "internal error: unhandled action '%s'", args[0]);
1933 return ACT_RET_PRS_ERR;
1934 }
1935 rule->action_ptr = http_action_set_map;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001936 rule->release_ptr = release_http_map;
Christopher Faulet81e20172019-12-12 16:40:30 +01001937
1938 cur_arg = *orig_arg;
Christopher Faulet046cf442019-12-17 15:45:23 +01001939 if (rule->action == 1 && (!*args[cur_arg] || !*args[cur_arg+1])) {
1940 /* 2 args for set-map */
Christopher Faulet81e20172019-12-12 16:40:30 +01001941 memprintf(err, "expects exactly 2 arguments");
1942 return ACT_RET_PRS_ERR;
1943 }
1944 else if (!*args[cur_arg]) {
Christopher Faulet046cf442019-12-17 15:45:23 +01001945 /* only one arg for other actions */
Christopher Faulet81e20172019-12-12 16:40:30 +01001946 memprintf(err, "expects exactly 1 arguments");
1947 return ACT_RET_PRS_ERR;
1948 }
1949
1950 /*
1951 * '+ 8' for 'set-map(' (same for del-map)
1952 * '- 9' for 'set-map(' + trailing ')' (same for del-map)
1953 */
1954 rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9);
1955
1956 if (rule->from == ACT_F_HTTP_REQ) {
1957 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001958 if (px->cap & PR_CAP_FE)
1959 cap |= SMP_VAL_FE_HRQ_HDR;
1960 if (px->cap & PR_CAP_BE)
1961 cap |= SMP_VAL_BE_HRQ_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001962 }
1963 else{
1964 px->conf.args.ctx = ARGC_HRS;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001965 if (px->cap & PR_CAP_FE)
1966 cap |= SMP_VAL_FE_HRS_HDR;
1967 if (px->cap & PR_CAP_BE)
1968 cap |= SMP_VAL_BE_HRS_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001969 }
1970
1971 /* key pattern */
1972 LIST_INIT(&rule->arg.map.key);
Christopher Faulet1337b322020-01-14 14:50:55 +01001973 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err)) {
1974 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001975 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001976 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001977
Christopher Faulet046cf442019-12-17 15:45:23 +01001978 if (rule->action == 1) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001979 /* value pattern for set-map only */
1980 cur_arg++;
1981 LIST_INIT(&rule->arg.map.value);
Christopher Faulet1337b322020-01-14 14:50:55 +01001982 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err)) {
1983 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001984 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001985 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001986 }
1987
1988 free(px->conf.lfs_file);
1989 px->conf.lfs_file = strdup(px->conf.args.file);
1990 px->conf.lfs_line = px->conf.args.line;
1991
1992 *orig_arg = cur_arg + 1;
1993 return ACT_RET_PRS_OK;
1994}
1995
Christopher Fauletac98d812019-12-18 09:20:16 +01001996/* This function executes a track-sc* actions. On success, it returns
1997 * ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1998 */
1999static enum act_return http_action_track_sc(struct act_rule *rule, struct proxy *px,
2000 struct session *sess, struct stream *s, int flags)
2001{
2002 struct stktable *t;
2003 struct stksess *ts;
2004 struct stktable_key *key;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002005 void *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *ptr6;
Christopher Fauletac98d812019-12-18 09:20:16 +01002006 int opt;
2007
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002008 ptr1 = ptr2 = ptr3 = ptr4 = ptr5 = ptr6 = NULL;
Christopher Fauletac98d812019-12-18 09:20:16 +01002009 opt = ((rule->from == ACT_F_HTTP_REQ) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES) | SMP_OPT_FINAL;
2010
2011 t = rule->arg.trk_ctr.table.t;
Emeric Brun362d25e2021-03-10 16:58:03 +01002012
2013 if (stkctr_entry(&s->stkctr[rule->action]))
2014 goto end;
2015
Christopher Fauletac98d812019-12-18 09:20:16 +01002016 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, NULL);
2017
2018 if (!key)
2019 goto end;
2020 ts = stktable_get_entry(t, key);
2021 if (!ts)
2022 goto end;
2023
2024 stream_track_stkctr(&s->stkctr[rule->action], t, ts);
2025
2026 /* let's count a new HTTP request as it's the first time we do it */
2027 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2028 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2029
2030 /* When the client triggers a 4xx from the server, it's most often due
2031 * to a missing object or permission. These events should be tracked
2032 * because if they happen often, it may indicate a brute force or a
2033 * vulnerability scan. Normally this is done when receiving the response
2034 * but here we're tracking after this ought to have been done so we have
2035 * to do it on purpose.
2036 */
2037 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 400) < 100) {
2038 ptr3 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2039 ptr4 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2040 }
2041
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002042 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 500) < 100 &&
2043 s->txn->status != 501 && s->txn->status != 505) {
2044 ptr5 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
2045 ptr6 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
2046 }
2047
2048 if (ptr1 || ptr2 || ptr3 || ptr4 || ptr5 || ptr6) {
Christopher Fauletac98d812019-12-18 09:20:16 +01002049 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2050
2051 if (ptr1)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002052 stktable_data_cast(ptr1, std_t_uint)++;
Christopher Fauletac98d812019-12-18 09:20:16 +01002053 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002054 update_freq_ctr_period(&stktable_data_cast(ptr2, std_t_frqp),
Christopher Fauletac98d812019-12-18 09:20:16 +01002055 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2056 if (ptr3)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002057 stktable_data_cast(ptr3, std_t_uint)++;
Christopher Fauletac98d812019-12-18 09:20:16 +01002058 if (ptr4)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002059 update_freq_ctr_period(&stktable_data_cast(ptr4, std_t_frqp),
Christopher Fauletac98d812019-12-18 09:20:16 +01002060 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002061 if (ptr5)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002062 stktable_data_cast(ptr5, std_t_uint)++;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002063 if (ptr6)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002064 update_freq_ctr_period(&stktable_data_cast(ptr6, std_t_frqp),
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002065 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u, 1);
Christopher Fauletac98d812019-12-18 09:20:16 +01002066
2067 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2068
2069 /* If data was modified, we need to touch to re-schedule sync */
2070 stktable_touch_local(t, ts, 0);
2071 }
2072
2073 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT);
2074 if (sess->fe != s->be)
2075 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND);
2076
2077 end:
2078 return ACT_RET_CONT;
2079}
Christopher Faulet81e20172019-12-12 16:40:30 +01002080
Christopher Faulet2eb53962020-01-14 14:47:34 +01002081static void release_http_track_sc(struct act_rule *rule)
2082{
2083 release_sample_expr(rule->arg.trk_ctr.expr);
2084}
2085
Christopher Faulet81e20172019-12-12 16:40:30 +01002086/* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success,
2087 * ACT_RET_PRS_ERR on error.
2088 */
2089static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px,
2090 struct act_rule *rule, char **err)
2091{
2092 struct sample_expr *expr;
2093 unsigned int where;
2094 unsigned int tsc_num;
2095 const char *tsc_num_str;
2096 int cur_arg;
2097
2098 tsc_num_str = &args[*orig_arg-1][8];
2099 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1)
2100 return ACT_RET_PRS_ERR;
2101
2102 cur_arg = *orig_arg;
2103 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002104 err, &px->conf.args, NULL);
Christopher Faulet81e20172019-12-12 16:40:30 +01002105 if (!expr)
2106 return ACT_RET_PRS_ERR;
2107
2108 where = 0;
2109 if (px->cap & PR_CAP_FE)
2110 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
2111 if (px->cap & PR_CAP_BE)
2112 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
2113
2114 if (!(expr->fetch->val & where)) {
2115 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here",
2116 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01002117 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002118 return ACT_RET_PRS_ERR;
2119 }
2120
2121 if (strcmp(args[cur_arg], "table") == 0) {
2122 cur_arg++;
2123 if (!*args[cur_arg]) {
2124 memprintf(err, "missing table name");
Christopher Faulet1337b322020-01-14 14:50:55 +01002125 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002126 return ACT_RET_PRS_ERR;
2127 }
2128
2129 /* we copy the table name for now, it will be resolved later */
2130 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
2131 cur_arg++;
2132 }
2133
Christopher Fauletac98d812019-12-18 09:20:16 +01002134 rule->action = tsc_num;
Christopher Faulet81e20172019-12-12 16:40:30 +01002135 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +01002136 rule->action_ptr = http_action_track_sc;
Christopher Faulet2eb53962020-01-14 14:47:34 +01002137 rule->release_ptr = release_http_track_sc;
Christopher Faulet81e20172019-12-12 16:40:30 +01002138 rule->check_ptr = check_trk_action;
2139
2140 *orig_arg = cur_arg;
2141 return ACT_RET_PRS_OK;
2142}
2143
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002144static enum act_return action_timeout_set_stream_timeout(struct act_rule *rule,
2145 struct proxy *px,
2146 struct session *sess,
2147 struct stream *s,
2148 int flags)
2149{
2150 struct sample *key;
2151
2152 if (rule->arg.timeout.expr) {
2153 key = sample_fetch_as_type(px, sess, s, SMP_OPT_FINAL, rule->arg.timeout.expr, SMP_T_SINT);
2154 if (!key)
2155 return ACT_RET_CONT;
2156
2157 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(key->data.u.sint));
2158 }
2159 else {
2160 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(rule->arg.timeout.value));
2161 }
2162
2163 return ACT_RET_CONT;
2164}
2165
2166/* Parse a "set-timeout" action. Returns ACT_RET_PRS_ERR if parsing error.
2167 */
2168static enum act_parse_ret parse_http_set_timeout(const char **args,
2169 int *orig_arg,
2170 struct proxy *px,
2171 struct act_rule *rule, char **err)
2172{
2173 int cur_arg;
2174
2175 rule->action = ACT_CUSTOM;
2176 rule->action_ptr = action_timeout_set_stream_timeout;
2177 rule->release_ptr = release_timeout_action;
2178
2179 cur_arg = *orig_arg;
2180 if (!*args[cur_arg] || !*args[cur_arg + 1]) {
2181 memprintf(err, "expects exactly 2 arguments");
2182 return ACT_RET_PRS_ERR;
2183 }
2184
2185 if (!(px->cap & PR_CAP_BE)) {
2186 memprintf(err, "proxy '%s' has no backend capability", px->id);
2187 return ACT_RET_PRS_ERR;
2188 }
2189
2190 if (cfg_parse_rule_set_timeout(args, cur_arg,
2191 &rule->arg.timeout.value,
2192 &rule->arg.timeout.type,
2193 &rule->arg.timeout.expr,
2194 err,
2195 px->conf.args.file,
2196 px->conf.args.line, &px->conf.args) == -1) {
2197 return ACT_RET_PRS_ERR;
2198 }
2199
2200 *orig_arg = cur_arg + 2;
2201
2202 return ACT_RET_PRS_OK;
2203}
2204
Christopher Faulet46f95542019-12-20 10:07:22 +01002205/* This function executes a strict-mode actions. On success, it always returns
2206 * ACT_RET_CONT
2207 */
2208static enum act_return http_action_strict_mode(struct act_rule *rule, struct proxy *px,
2209 struct session *sess, struct stream *s, int flags)
2210{
2211 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
2212
2213 if (rule->action == 0) // strict-mode on
2214 msg->flags &= ~HTTP_MSGF_SOFT_RW;
2215 else // strict-mode off
2216 msg->flags |= HTTP_MSGF_SOFT_RW;
2217 return ACT_RET_CONT;
2218}
2219
2220/* Parse a "strict-mode" action. It returns ACT_RET_PRS_OK on success,
2221 * ACT_RET_PRS_ERR on error.
2222 */
2223static enum act_parse_ret parse_http_strict_mode(const char **args, int *orig_arg, struct proxy *px,
2224 struct act_rule *rule, char **err)
2225{
2226 int cur_arg;
2227
Christopher Faulet46f95542019-12-20 10:07:22 +01002228 cur_arg = *orig_arg;
2229 if (!*args[cur_arg]) {
2230 memprintf(err, "expects exactly 1 arguments");
2231 return ACT_RET_PRS_ERR;
2232 }
2233
2234 if (strcasecmp(args[cur_arg], "on") == 0)
2235 rule->action = 0; // strict-mode on
2236 else if (strcasecmp(args[cur_arg], "off") == 0)
2237 rule->action = 1; // strict-mode off
2238 else {
2239 memprintf(err, "Unexpected value '%s'. Only 'on' and 'off' are supported", args[cur_arg]);
2240 return ACT_RET_PRS_ERR;
2241 }
2242 rule->action_ptr = http_action_strict_mode;
2243
2244 *orig_arg = cur_arg + 1;
2245 return ACT_RET_PRS_OK;
2246}
2247
Christopher Faulet24231ab2020-01-24 17:44:23 +01002248/* This function executes a return action. It builds an HTX message from an
2249 * errorfile, an raw file or a log-format string, depending on <.action>
2250 * value. On success, it returns ACT_RET_ABRT. If an error occurs ACT_RET_ERR is
2251 * returned.
2252 */
2253static enum act_return http_action_return(struct act_rule *rule, struct proxy *px,
2254 struct session *sess, struct stream *s, int flags)
2255{
2256 struct channel *req = &s->req;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002257
Christopher Faulet2d36df22021-02-19 11:41:01 +01002258 s->txn->status = rule->arg.http_reply->status;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002259 if (http_reply_message(s, rule->arg.http_reply) == -1)
2260 return ACT_RET_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002261
Christopher Faulet24231ab2020-01-24 17:44:23 +01002262 if (rule->from == ACT_F_HTTP_REQ) {
2263 /* let's log the request time */
2264 s->logs.tv_request = now;
2265 req->analysers &= AN_REQ_FLT_END;
2266
2267 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002268 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002269 }
2270
2271 if (!(s->flags & SF_ERR_MASK))
2272 s->flags |= SF_ERR_LOCAL;
2273 if (!(s->flags & SF_FINST_MASK))
2274 s->flags |= ((rule->from == ACT_F_HTTP_REQ) ? SF_FINST_R : SF_FINST_H);
2275
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002276 return ACT_RET_ABRT;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002277}
2278
Christopher Faulet24231ab2020-01-24 17:44:23 +01002279/* Parse a "return" action. It returns ACT_RET_PRS_OK on success,
Christopher Faulet47e791e2020-05-13 14:36:55 +02002280 * ACT_RET_PRS_ERR on error. It relies on http_parse_http_reply() to set
2281 * <.arg.http_reply>.
Christopher Faulet24231ab2020-01-24 17:44:23 +01002282 */
2283static enum act_parse_ret parse_http_return(const char **args, int *orig_arg, struct proxy *px,
2284 struct act_rule *rule, char **err)
2285{
Christopher Faulet47e791e2020-05-13 14:36:55 +02002286 /* Prepare parsing of log-format strings */
2287 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
2288 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, 200, err);
2289 if (!rule->arg.http_reply)
2290 return ACT_RET_PRS_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002291
Christopher Fauletba946bf2020-05-13 08:50:07 +02002292 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet5ff0c642020-05-12 18:33:37 +02002293 rule->action = ACT_CUSTOM;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002294 rule->check_ptr = check_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002295 rule->action_ptr = http_action_return;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002296 rule->release_ptr = release_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002297 return ACT_RET_PRS_OK;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002298}
2299
Christopher Faulet021a8e42021-03-29 10:46:38 +02002300
2301
2302/* This function executes a wait-for-body action. It waits for the message
2303 * payload for a max configured time (.arg.p[0]) and eventually for only first
2304 * <arg.p[1]> bytes (0 means no limit). It relies on http_wait_for_msg_body()
2305 * function. it returns ACT_RET_CONT when conditions are met to stop to wait.
2306 * Otherwise ACT_RET_YIELD is returned to wait for more data. ACT_RET_INV is
2307 * returned if a parsing error is raised by lower level and ACT_RET_ERR if an
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05002308 * internal error occurred. Finally ACT_RET_ABRT is returned when a timeout
2309 * occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02002310 */
2311static enum act_return http_action_wait_for_body(struct act_rule *rule, struct proxy *px,
2312 struct session *sess, struct stream *s, int flags)
2313{
2314 struct channel *chn = ((rule->from == ACT_F_HTTP_REQ) ? &s->req : &s->res);
2315 unsigned int time = (uintptr_t)rule->arg.act.p[0];
2316 unsigned int bytes = (uintptr_t)rule->arg.act.p[1];
2317
2318 switch (http_wait_for_msg_body(s, chn, time, bytes)) {
2319 case HTTP_RULE_RES_CONT:
2320 return ACT_RET_CONT;
2321 case HTTP_RULE_RES_YIELD:
2322 return ACT_RET_YIELD;
2323 case HTTP_RULE_RES_BADREQ:
2324 return ACT_RET_INV;
2325 case HTTP_RULE_RES_ERROR:
2326 return ACT_RET_ERR;
2327 case HTTP_RULE_RES_ABRT:
2328 return ACT_RET_ABRT;
2329 default:
2330 return ACT_RET_ERR;
2331 }
2332}
2333
2334/* Parse a "wait-for-body" action. It returns ACT_RET_PRS_OK on success,
2335 * ACT_RET_PRS_ERR on error.
2336 */
2337static enum act_parse_ret parse_http_wait_for_body(const char **args, int *orig_arg, struct proxy *px,
2338 struct act_rule *rule, char **err)
2339{
2340 int cur_arg;
2341 unsigned int time, bytes;
2342 const char *res;
2343
2344 cur_arg = *orig_arg;
2345 if (!*args[cur_arg]) {
2346 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2347 return ACT_RET_PRS_ERR;
2348 }
2349
2350 time = UINT_MAX; /* To be sure it is set */
2351 bytes = 0; /* Default value, wait all the body */
2352 while (*(args[cur_arg])) {
2353 if (strcmp(args[cur_arg], "time") == 0) {
2354 if (!*args[cur_arg + 1]) {
2355 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2356 return ACT_RET_PRS_ERR;
2357 }
2358 res = parse_time_err(args[cur_arg+1], &time, TIME_UNIT_MS);
2359 if (res == PARSE_TIME_OVER) {
2360 memprintf(err, "time overflow (maximum value is 2147483647 ms or ~24.8 days)");
2361 return ACT_RET_PRS_ERR;
2362 }
2363 if (res == PARSE_TIME_UNDER) {
2364 memprintf(err, "time underflow (minimum non-null value is 1 ms)");
2365 return ACT_RET_PRS_ERR;
2366 }
2367 if (res) {
2368 memprintf(err, "unexpected character '%c'", *res);
2369 return ACT_RET_PRS_ERR;
2370 }
2371 cur_arg++;
2372 }
2373 else if (strcmp(args[cur_arg], "at-least") == 0) {
2374 if (!*args[cur_arg + 1]) {
2375 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2376 return ACT_RET_PRS_ERR;
2377 }
2378 res = parse_size_err(args[cur_arg+1], &bytes);
2379 if (res) {
2380 memprintf(err, "unexpected character '%c'", *res);
2381 return ACT_RET_PRS_ERR;
2382 }
2383 cur_arg++;
2384 }
2385 else
2386 break;
2387 cur_arg++;
2388 }
2389
2390 if (time == UINT_MAX) {
2391 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2392 return ACT_RET_PRS_ERR;
2393 }
2394
2395 rule->arg.act.p[0] = (void *)(uintptr_t)time;
2396 rule->arg.act.p[1] = (void *)(uintptr_t)bytes;
2397
2398 *orig_arg = cur_arg;
2399
2400 rule->action = ACT_CUSTOM;
2401 rule->action_ptr = http_action_wait_for_body;
2402 return ACT_RET_PRS_OK;
2403}
2404
Willy Tarreau79e57332018-10-02 16:01:16 +02002405/************************************************************************/
2406/* All supported http-request action keywords must be declared here. */
2407/************************************************************************/
2408
2409static struct action_kw_list http_req_actions = {
2410 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002411 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002412 { "add-header", parse_http_set_header, 0 },
2413 { "allow", parse_http_allow, 0 },
2414 { "auth", parse_http_auth, 0 },
2415 { "capture", parse_http_req_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002416 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002417 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002418 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002419 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002420 { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 },
2421 { "early-hint", parse_http_set_header, 0 },
Amaury Denoyellea9e639a2021-05-06 15:50:12 +02002422 { "normalize-uri", parse_http_normalize_uri, KWF_EXPERIMENTAL },
Christopher Faulet81e20172019-12-12 16:40:30 +01002423 { "redirect", parse_http_redirect, 0 },
2424 { "reject", parse_http_action_reject, 0 },
2425 { "replace-header", parse_http_replace_header, 0 },
2426 { "replace-path", parse_replace_uri, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002427 { "replace-pathq", parse_replace_uri, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002428 { "replace-uri", parse_replace_uri, 0 },
2429 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002430 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002431 { "set-header", parse_http_set_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002432 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002433 { "set-method", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002434 { "set-path", parse_set_req_line, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002435 { "set-pathq", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002436 { "set-query", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002437 { "set-uri", parse_set_req_line, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002438 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002439 { "tarpit", parse_http_deny, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002440 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002441 { "set-timeout", parse_http_set_timeout, 0 },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002442 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002443 { NULL, NULL }
2444 }
2445};
2446
Willy Tarreau0108d902018-11-25 19:14:37 +01002447INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2448
Willy Tarreau79e57332018-10-02 16:01:16 +02002449static struct action_kw_list http_res_actions = {
2450 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002451 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002452 { "add-header", parse_http_set_header, 0 },
2453 { "allow", parse_http_allow, 0 },
2454 { "capture", parse_http_res_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002455 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002456 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002457 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002458 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002459 { "redirect", parse_http_redirect, 0 },
2460 { "replace-header", parse_http_replace_header, 0 },
2461 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002462 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002463 { "set-header", parse_http_set_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002464 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002465 { "set-status", parse_http_set_status, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002466 { "strict-mode", parse_http_strict_mode, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002467 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002468 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002469 { NULL, NULL }
2470 }
2471};
2472
Willy Tarreau0108d902018-11-25 19:14:37 +01002473INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +02002474
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002475static struct action_kw_list http_after_res_actions = {
2476 .kw = {
2477 { "add-header", parse_http_set_header, 0 },
2478 { "allow", parse_http_allow, 0 },
Christopher Fauletba8f0632021-12-06 08:43:22 +01002479 { "capture", parse_http_res_capture, 0 },
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002480 { "del-header", parse_http_del_header, 0 },
2481 { "replace-header", parse_http_replace_header, 0 },
2482 { "replace-value", parse_http_replace_header, 0 },
2483 { "set-header", parse_http_set_header, 0 },
2484 { "set-status", parse_http_set_status, 0 },
2485 { "strict-mode", parse_http_strict_mode, 0 },
2486 { NULL, NULL }
2487 }
2488};
2489
2490INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_actions);
2491
Willy Tarreau79e57332018-10-02 16:01:16 +02002492/*
2493 * Local variables:
2494 * c-indent-level: 8
2495 * c-basic-offset: 8
2496 * End:
2497 */