blob: fb77f801b210e204b7a0343c7b06eda293afceba [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
2 * HTTP actions
3 *
4 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/types.h>
14
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
18
Willy Tarreaudcc048a2020-06-04 19:11:43 +020019#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020020#include <haproxy/action.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020021#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/arg.h>
23#include <haproxy/capture-t.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020024#include <haproxy/cfgparse.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020025#include <haproxy/chunk.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020026#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020027#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020028#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020029#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020030#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020031#include <haproxy/log.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020032#include <haproxy/pattern.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020033#include <haproxy/pool.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020034#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020035#include <haproxy/sample.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020036#include <haproxy/sc_strm.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020037#include <haproxy/stconn.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020038#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020039#include <haproxy/uri_auth-t.h>
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +020040#include <haproxy/uri_normalizer.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020041#include <haproxy/version.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020042
Willy Tarreau79e57332018-10-02 16:01:16 +020043
Christopher Faulet2eb53962020-01-14 14:47:34 +010044/* Release memory allocated by most of HTTP actions. Concretly, it releases
45 * <arg.http>.
46 */
47static void release_http_action(struct act_rule *rule)
48{
49 struct logformat_node *lf, *lfb;
50
Tim Duesterhused526372020-03-05 17:56:33 +010051 istfree(&rule->arg.http.str);
Christopher Faulet2eb53962020-01-14 14:47:34 +010052 if (rule->arg.http.re)
53 regex_free(rule->arg.http.re);
54 list_for_each_entry_safe(lf, lfb, &rule->arg.http.fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +020055 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +010056 release_sample_expr(lf->expr);
57 free(lf->arg);
58 free(lf);
59 }
60}
61
Christopher Faulet5cb513a2020-05-13 17:56:56 +020062/* Release memory allocated by HTTP actions relying on an http reply. Concretly,
63 * it releases <.arg.http_reply>
64 */
65static void release_act_http_reply(struct act_rule *rule)
66{
67 release_http_reply(rule->arg.http_reply);
68 rule->arg.http_reply = NULL;
69}
70
71
72/* Check function for HTTP actions relying on an http reply. The function
73 * returns 1 in success case, otherwise, it returns 0 and err is filled.
74 */
75static int check_act_http_reply(struct act_rule *rule, struct proxy *px, char **err)
76{
77 struct http_reply *reply = rule->arg.http_reply;
78
79 if (!http_check_http_reply(reply, px, err)) {
80 release_act_http_reply(rule);
81 return 0;
82 }
83 return 1;
84}
85
Willy Tarreau79e57332018-10-02 16:01:16 +020086
87/* This function executes one of the set-{method,path,query,uri} actions. It
88 * builds a string in the trash from the specified format string. It finds
Christopher Faulet2c22a692019-12-18 15:39:56 +010089 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +050090 * parse_set_req_line(). The replacement action is executed by the function
Christopher Faulete00d06c2019-12-16 17:18:42 +010091 * http_action_set_req_line(). On success, it returns ACT_RET_CONT. If an error
92 * occurs while soft rewrites are enabled, the action is canceled, but the rule
93 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau79e57332018-10-02 16:01:16 +020094 */
95static enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
96 struct session *sess, struct stream *s, int flags)
97{
98 struct buffer *replace;
Christopher Faulet13403762019-12-13 09:01:57 +010099 enum act_return ret = ACT_RET_CONT;
Willy Tarreau79e57332018-10-02 16:01:16 +0200100
101 replace = alloc_trash_chunk();
102 if (!replace)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100103 goto fail_alloc;
Willy Tarreau79e57332018-10-02 16:01:16 +0200104
105 /* If we have to create a query string, prepare a '?'. */
Christopher Faulet2c22a692019-12-18 15:39:56 +0100106 if (rule->action == 2) // set-query
Willy Tarreau79e57332018-10-02 16:01:16 +0200107 replace->area[replace->data++] = '?';
108 replace->data += build_logline(s, replace->area + replace->data,
109 replace->size - replace->data,
Christopher Faulet96bff762019-12-17 13:46:18 +0100110 &rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200111
Christopher Faulet2c22a692019-12-18 15:39:56 +0100112 if (http_req_replace_stline(rule->action, replace->area, replace->data, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100113 goto fail_rewrite;
Willy Tarreau79e57332018-10-02 16:01:16 +0200114
Christopher Faulete00d06c2019-12-16 17:18:42 +0100115 leave:
Willy Tarreau79e57332018-10-02 16:01:16 +0200116 free_trash_chunk(replace);
117 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100118
119 fail_alloc:
120 if (!(s->flags & SF_ERR_MASK))
121 s->flags |= SF_ERR_RESOURCE;
122 ret = ACT_RET_ERR;
123 goto leave;
124
125 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +0200126 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100127 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200128 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100129 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200130 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100131 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200132 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100133
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100134 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100135 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100136 if (!(s->flags & SF_ERR_MASK))
137 s->flags |= SF_ERR_PRXCOND;
138 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100139 goto leave;
Willy Tarreau79e57332018-10-02 16:01:16 +0200140}
141
142/* parse an http-request action among :
143 * set-method
144 * set-path
Christopher Faulet312294f2020-09-02 17:17:44 +0200145 * set-pathq
Willy Tarreau79e57332018-10-02 16:01:16 +0200146 * set-query
147 * set-uri
148 *
149 * All of them accept a single argument of type string representing a log-format.
Christopher Faulet96bff762019-12-17 13:46:18 +0100150 * The resulting rule makes use of <http.fmt> to store the log-format list head,
Christopher Faulet2c22a692019-12-18 15:39:56 +0100151 * and <.action> to store the action type as an int (0=method, 1=path, 2=query,
152 * 3=uri). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreau79e57332018-10-02 16:01:16 +0200153 */
154static enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
155 struct act_rule *rule, char **err)
156{
157 int cur_arg = *orig_arg;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +0200158 int cap = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200159
Willy Tarreau79e57332018-10-02 16:01:16 +0200160 switch (args[0][4]) {
161 case 'm' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100162 rule->action = 0; // set-method
Willy Tarreau79e57332018-10-02 16:01:16 +0200163 break;
164 case 'p' :
Christopher Faulet312294f2020-09-02 17:17:44 +0200165 if (args[0][8] == 'q')
166 rule->action = 4; // set-pathq
167 else
168 rule->action = 1; // set-path
Willy Tarreau79e57332018-10-02 16:01:16 +0200169 break;
170 case 'q' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100171 rule->action = 2; // set-query
Willy Tarreau79e57332018-10-02 16:01:16 +0200172 break;
173 case 'u' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100174 rule->action = 3; // set-uri
Willy Tarreau79e57332018-10-02 16:01:16 +0200175 break;
176 default:
177 memprintf(err, "internal error: unhandled action '%s'", args[0]);
178 return ACT_RET_PRS_ERR;
179 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100180 rule->action_ptr = http_action_set_req_line;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100181 rule->release_ptr = release_http_action;
Willy Tarreau6a03a0d2022-09-02 19:19:01 +0200182 LIST_INIT(&rule->arg.http.fmt);
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
Willy Tarreau79e57332018-10-02 16:01:16 +0200190 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +0200191 if (px->cap & PR_CAP_FE)
192 cap |= SMP_VAL_FE_HRQ_HDR;
193 if (px->cap & PR_CAP_BE)
194 cap |= SMP_VAL_BE_HRQ_HDR;
195 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200196 return ACT_RET_PRS_ERR;
197 }
198
199 (*orig_arg)++;
200 return ACT_RET_PRS_OK;
201}
202
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200203/* This function executes the http-request normalize-uri action.
204 * `rule->action` is expected to be a value from `enum act_normalize_uri`.
205 *
206 * On success, it returns ACT_RET_CONT. If an error
207 * occurs while soft rewrites are enabled, the action is canceled, but the rule
208 * processing continue. Otherwsize ACT_RET_ERR is returned.
209 */
210static enum act_return http_action_normalize_uri(struct act_rule *rule, struct proxy *px,
211 struct session *sess, struct stream *s, int flags)
212{
213 enum act_return ret = ACT_RET_CONT;
214 struct htx *htx = htxbuf(&s->req.buf);
215 const struct ist uri = htx_sl_req_uri(http_get_stline(htx));
216 struct buffer *replace = alloc_trash_chunk();
217 enum uri_normalizer_err err = URI_NORMALIZER_ERR_INTERNAL_ERROR;
218
219 if (!replace)
220 goto fail_alloc;
221
222 switch ((enum act_normalize_uri) rule->action) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200223 case ACT_NORMALIZE_URI_PATH_MERGE_SLASHES: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200224 struct http_uri_parser parser = http_uri_parser_init(uri);
225 const struct ist path = http_parse_path(&parser);
Tim Duesterhusd371e992021-04-15 21:45:58 +0200226 struct ist newpath = ist2(replace->area, replace->size);
227
228 if (!isttest(path))
229 goto leave;
230
231 err = uri_normalizer_path_merge_slashes(iststop(path, '?'), &newpath);
232
233 if (err != URI_NORMALIZER_ERR_NONE)
234 break;
235
236 if (!http_replace_req_path(htx, newpath, 0))
237 goto fail_rewrite;
238
239 break;
240 }
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200241 case ACT_NORMALIZE_URI_PATH_STRIP_DOT: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200242 struct http_uri_parser parser = http_uri_parser_init(uri);
243 const struct ist path = http_parse_path(&parser);
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200244 struct ist newpath = ist2(replace->area, replace->size);
245
246 if (!isttest(path))
247 goto leave;
248
249 err = uri_normalizer_path_dot(iststop(path, '?'), &newpath);
250
251 if (err != URI_NORMALIZER_ERR_NONE)
252 break;
253
254 if (!http_replace_req_path(htx, newpath, 0))
255 goto fail_rewrite;
256
257 break;
258 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200259 case ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT:
260 case ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200261 struct http_uri_parser parser = http_uri_parser_init(uri);
262 const struct ist path = http_parse_path(&parser);
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200263 struct ist newpath = ist2(replace->area, replace->size);
264
265 if (!isttest(path))
266 goto leave;
267
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200268 err = uri_normalizer_path_dotdot(iststop(path, '?'), rule->action == ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL, &newpath);
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200269
270 if (err != URI_NORMALIZER_ERR_NONE)
271 break;
272
273 if (!http_replace_req_path(htx, newpath, 0))
274 goto fail_rewrite;
275
276 break;
277 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200278 case ACT_NORMALIZE_URI_QUERY_SORT_BY_NAME: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200279 struct http_uri_parser parser = http_uri_parser_init(uri);
280 const struct ist path = http_parse_path(&parser);
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200281 struct ist newquery = ist2(replace->area, replace->size);
282
283 if (!isttest(path))
284 goto leave;
285
286 err = uri_normalizer_query_sort(istfind(path, '?'), '&', &newquery);
287
288 if (err != URI_NORMALIZER_ERR_NONE)
289 break;
290
291 if (!http_replace_req_query(htx, newquery))
292 goto fail_rewrite;
293
294 break;
295 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200296 case ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE:
297 case ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200298 struct http_uri_parser parser = http_uri_parser_init(uri);
299 const struct ist path = http_parse_path(&parser);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200300 struct ist newpath = ist2(replace->area, replace->size);
301
302 if (!isttest(path))
303 goto leave;
304
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200305 err = uri_normalizer_percent_upper(path, rule->action == ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT, &newpath);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200306
307 if (err != URI_NORMALIZER_ERR_NONE)
308 break;
309
310 if (!http_replace_req_path(htx, newpath, 1))
311 goto fail_rewrite;
312
313 break;
314 }
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +0200315 case ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED:
316 case ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200317 struct http_uri_parser parser = http_uri_parser_init(uri);
318 const struct ist path = http_parse_path(&parser);
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +0200319 struct ist newpath = ist2(replace->area, replace->size);
320
321 if (!isttest(path))
322 goto leave;
323
324 err = uri_normalizer_percent_decode_unreserved(path, rule->action == ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT, &newpath);
325
326 if (err != URI_NORMALIZER_ERR_NONE)
327 break;
328
329 if (!http_replace_req_path(htx, newpath, 1))
330 goto fail_rewrite;
331
332 break;
333 }
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200334 case ACT_NORMALIZE_URI_FRAGMENT_STRIP: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200335 struct http_uri_parser parser = http_uri_parser_init(uri);
336 const struct ist path = http_parse_path(&parser);
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200337 struct ist newpath = ist2(replace->area, replace->size);
338
339 if (!isttest(path))
340 goto leave;
341
342 err = uri_normalizer_fragment_strip(path, &newpath);
343
344 if (err != URI_NORMALIZER_ERR_NONE)
345 break;
346
347 if (!http_replace_req_path(htx, newpath, 1))
348 goto fail_rewrite;
349
350 break;
351 }
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200352 case ACT_NORMALIZE_URI_FRAGMENT_ENCODE: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200353 struct http_uri_parser parser = http_uri_parser_init(uri);
354 const struct ist path = http_parse_path(&parser);
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200355 struct ist newpath = ist2(replace->area, replace->size);
356
357 if (!isttest(path))
358 goto leave;
359
360 err = uri_normalizer_fragment_encode(path, &newpath);
361
362 if (err != URI_NORMALIZER_ERR_NONE)
363 break;
364
365 if (!http_replace_req_path(htx, newpath, 1))
366 goto fail_rewrite;
367
368 break;
369 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200370 }
371
372 switch (err) {
373 case URI_NORMALIZER_ERR_NONE:
374 break;
375 case URI_NORMALIZER_ERR_INTERNAL_ERROR:
376 ret = ACT_RET_ERR;
377 break;
378 case URI_NORMALIZER_ERR_INVALID_INPUT:
379 ret = ACT_RET_INV;
380 break;
381 case URI_NORMALIZER_ERR_ALLOC:
382 goto fail_alloc;
383 }
384
385 leave:
386 free_trash_chunk(replace);
387 return ret;
388
389 fail_alloc:
390 if (!(s->flags & SF_ERR_MASK))
391 s->flags |= SF_ERR_RESOURCE;
392 ret = ACT_RET_ERR;
393 goto leave;
394
395 fail_rewrite:
396 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
397 if (s->flags & SF_BE_ASSIGNED)
398 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
399 if (sess->listener && sess->listener->counters)
400 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
401 if (objt_server(s->target))
402 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
403
404 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
405 ret = ACT_RET_ERR;
406 if (!(s->flags & SF_ERR_MASK))
407 s->flags |= SF_ERR_PRXCOND;
408 }
409 goto leave;
410}
411
412/* Parses the http-request normalize-uri action. It expects a single <normalizer>
413 * argument, corresponding too a value in `enum act_normalize_uri`.
414 *
415 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
416 */
417static enum act_parse_ret parse_http_normalize_uri(const char **args, int *orig_arg, struct proxy *px,
418 struct act_rule *rule, char **err)
419{
420 int cur_arg = *orig_arg;
421
422 rule->action_ptr = http_action_normalize_uri;
423 rule->release_ptr = NULL;
424
425 if (!*args[cur_arg]) {
426 memprintf(err, "missing argument <normalizer>");
427 return ACT_RET_PRS_ERR;
428 }
429
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200430 if (strcmp(args[cur_arg], "path-merge-slashes") == 0) {
Tim Duesterhusd371e992021-04-15 21:45:58 +0200431 cur_arg++;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200432
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200433 rule->action = ACT_NORMALIZE_URI_PATH_MERGE_SLASHES;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200434 }
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200435 else if (strcmp(args[cur_arg], "path-strip-dot") == 0) {
436 cur_arg++;
437
438 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOT;
439 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200440 else if (strcmp(args[cur_arg], "path-strip-dotdot") == 0) {
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200441 cur_arg++;
442
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200443 if (strcmp(args[cur_arg], "full") == 0) {
444 cur_arg++;
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200445 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL;
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200446 }
447 else if (!*args[cur_arg]) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200448 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT;
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200449 }
450 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
Tim Duesterhus2f413132021-05-10 23:21:20 +0200451 memprintf(err, "unknown argument '%s' for 'path-strip-dotdot' normalizer", args[cur_arg]);
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200452 return ACT_RET_PRS_ERR;
453 }
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200454 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200455 else if (strcmp(args[cur_arg], "query-sort-by-name") == 0) {
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200456 cur_arg++;
457
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200458 rule->action = ACT_NORMALIZE_URI_QUERY_SORT_BY_NAME;
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200459 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200460 else if (strcmp(args[cur_arg], "percent-to-uppercase") == 0) {
Tim Duesterhusa4071932021-04-15 21:46:02 +0200461 cur_arg++;
462
463 if (strcmp(args[cur_arg], "strict") == 0) {
464 cur_arg++;
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200465 rule->action = ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT;
Tim Duesterhusa4071932021-04-15 21:46:02 +0200466 }
467 else if (!*args[cur_arg]) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200468 rule->action = ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE;
Tim Duesterhusa4071932021-04-15 21:46:02 +0200469 }
470 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
Tim Duesterhus2f413132021-05-10 23:21:20 +0200471 memprintf(err, "unknown argument '%s' for 'percent-to-uppercase' normalizer", args[cur_arg]);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200472 return ACT_RET_PRS_ERR;
473 }
474 }
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +0200475 else if (strcmp(args[cur_arg], "percent-decode-unreserved") == 0) {
476 cur_arg++;
477
478 if (strcmp(args[cur_arg], "strict") == 0) {
479 cur_arg++;
480 rule->action = ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT;
481 }
482 else if (!*args[cur_arg]) {
483 rule->action = ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED;
484 }
485 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
486 memprintf(err, "unknown argument '%s' for 'percent-decode-unreserved' normalizer", args[cur_arg]);
487 return ACT_RET_PRS_ERR;
488 }
489 }
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200490 else if (strcmp(args[cur_arg], "fragment-strip") == 0) {
491 cur_arg++;
492
493 rule->action = ACT_NORMALIZE_URI_FRAGMENT_STRIP;
494 }
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200495 else if (strcmp(args[cur_arg], "fragment-encode") == 0) {
496 cur_arg++;
497
498 rule->action = ACT_NORMALIZE_URI_FRAGMENT_ENCODE;
499 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200500 else {
501 memprintf(err, "unknown normalizer '%s'", args[cur_arg]);
502 return ACT_RET_PRS_ERR;
503 }
504
505 *orig_arg = cur_arg;
506 return ACT_RET_PRS_OK;
507}
508
Willy Tarreau33810222019-06-12 17:44:02 +0200509/* This function executes a replace-uri action. It finds its arguments in
Christopher Faulet96bff762019-12-17 13:46:18 +0100510 * <rule>.arg.http. It builds a string in the trash from the format string
Willy Tarreau33810222019-06-12 17:44:02 +0200511 * previously filled by function parse_replace_uri() and will execute the regex
Christopher Faulet96bff762019-12-17 13:46:18 +0100512 * in <http.re> to replace the URI. It uses the format string present in
Christopher Faulet2c22a692019-12-18 15:39:56 +0100513 * <http.fmt>. The component to act on (path/uri) is taken from <.action> which
Christopher Faulet96bff762019-12-17 13:46:18 +0100514 * contains 1 for the path or 3 for the URI (values used by
515 * http_req_replace_stline()). On success, it returns ACT_RET_CONT. If an error
516 * occurs while soft rewrites are enabled, the action is canceled, but the rule
517 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau33810222019-06-12 17:44:02 +0200518 */
519static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px,
520 struct session *sess, struct stream *s, int flags)
521{
Christopher Faulet13403762019-12-13 09:01:57 +0100522 enum act_return ret = ACT_RET_CONT;
Willy Tarreau33810222019-06-12 17:44:02 +0200523 struct buffer *replace, *output;
524 struct ist uri;
525 int len;
526
527 replace = alloc_trash_chunk();
528 output = alloc_trash_chunk();
529 if (!replace || !output)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100530 goto fail_alloc;
Christopher Faulet12c28b62019-07-15 16:30:24 +0200531 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Willy Tarreau262c3f12019-12-17 06:52:51 +0100532
Amaury Denoyellec453f952021-07-06 11:40:12 +0200533 if (rule->action == 1) { // replace-path
534 struct http_uri_parser parser = http_uri_parser_init(uri);
535 uri = iststop(http_parse_path(&parser), '?');
536 }
537 else if (rule->action == 4) { // replace-pathq
538 struct http_uri_parser parser = http_uri_parser_init(uri);
539 uri = http_parse_path(&parser);
540 }
Willy Tarreau262c3f12019-12-17 06:52:51 +0100541
Christopher Faulet114e7592022-04-08 10:44:21 +0200542 if (!istlen(uri))
543 goto leave;
544
Christopher Faulet96bff762019-12-17 13:46:18 +0100545 if (!regex_exec_match2(rule->arg.http.re, uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
Willy Tarreau33810222019-06-12 17:44:02 +0200546 goto leave;
547
Christopher Faulet96bff762019-12-17 13:46:18 +0100548 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200549
550 /* note: uri.ptr doesn't need to be zero-terminated because it will
551 * only be used to pick pmatch references.
552 */
553 len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch);
554 if (len == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100555 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200556
Christopher Faulet2c22a692019-12-18 15:39:56 +0100557 if (http_req_replace_stline(rule->action, output->area, len, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100558 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200559
Christopher Faulete00d06c2019-12-16 17:18:42 +0100560 leave:
Willy Tarreau33810222019-06-12 17:44:02 +0200561 free_trash_chunk(output);
562 free_trash_chunk(replace);
563 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100564
565 fail_alloc:
566 if (!(s->flags & SF_ERR_MASK))
567 s->flags |= SF_ERR_RESOURCE;
568 ret = ACT_RET_ERR;
569 goto leave;
570
571 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +0200572 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100573 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200574 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100575 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200576 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100577 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200578 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100579
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100580 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100581 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100582 if (!(s->flags & SF_ERR_MASK))
583 s->flags |= SF_ERR_PRXCOND;
584 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100585 goto leave;
Willy Tarreau33810222019-06-12 17:44:02 +0200586}
587
Christopher Faulet312294f2020-09-02 17:17:44 +0200588/* parse a "replace-uri", "replace-path" or "replace-pathq"
589 * http-request action.
Willy Tarreau33810222019-06-12 17:44:02 +0200590 * This action takes 2 arguments (a regex and a replacement format string).
Christopher Faulet2c22a692019-12-18 15:39:56 +0100591 * The resulting rule makes use of <.action> to store the action (1/3 for now),
Christopher Faulet96bff762019-12-17 13:46:18 +0100592 * <http.re> to store the compiled regex, and <http.fmt> to store the log-format
Willy Tarreau33810222019-06-12 17:44:02 +0200593 * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
594 */
595static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px,
596 struct act_rule *rule, char **err)
597{
598 int cur_arg = *orig_arg;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +0200599 int cap = 0;
Willy Tarreau33810222019-06-12 17:44:02 +0200600 char *error = NULL;
601
Christopher Faulet312294f2020-09-02 17:17:44 +0200602 switch (args[0][8]) {
603 case 'p':
604 if (args[0][12] == 'q')
605 rule->action = 4; // replace-pathq, same as set-pathq
606 else
607 rule->action = 1; // replace-path, same as set-path
608 break;
609 case 'u':
Christopher Faulet2c22a692019-12-18 15:39:56 +0100610 rule->action = 3; // replace-uri, same as set-uri
Christopher Faulet312294f2020-09-02 17:17:44 +0200611 break;
612 default:
613 memprintf(err, "internal error: unhandled action '%s'", args[0]);
614 return ACT_RET_PRS_ERR;
615 }
Willy Tarreau262c3f12019-12-17 06:52:51 +0100616
Willy Tarreau33810222019-06-12 17:44:02 +0200617 rule->action_ptr = http_action_replace_uri;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100618 rule->release_ptr = release_http_action;
Willy Tarreau6a03a0d2022-09-02 19:19:01 +0200619 LIST_INIT(&rule->arg.http.fmt);
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
Willy Tarreau33810222019-06-12 17:44:02 +0200633 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +0200634 if (px->cap & PR_CAP_FE)
635 cap |= SMP_VAL_FE_HRQ_HDR;
636 if (px->cap & PR_CAP_BE)
637 cap |= SMP_VAL_BE_HRQ_HDR;
638 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 +0100639 regex_free(rule->arg.http.re);
Willy Tarreau33810222019-06-12 17:44:02 +0200640 return ACT_RET_PRS_ERR;
641 }
642
643 (*orig_arg) += 2;
644 return ACT_RET_PRS_OK;
645}
646
Willy Tarreau79e57332018-10-02 16:01:16 +0200647/* This function is just a compliant action wrapper for "set-status". */
648static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
649 struct session *sess, struct stream *s, int flags)
650{
Christopher Faulet96bff762019-12-17 13:46:18 +0100651 if (http_res_set_status(rule->arg.http.i, rule->arg.http.str, s) == -1) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200652 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100653 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200654 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100655 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200656 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100657 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200658 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100659
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100660 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100661 if (!(s->flags & SF_ERR_MASK))
662 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet692a6c22020-02-07 10:22:31 +0100663 return ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100664 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100665 }
666
Willy Tarreau79e57332018-10-02 16:01:16 +0200667 return ACT_RET_CONT;
668}
669
670/* parse set-status action:
671 * This action accepts a single argument of type int representing
672 * an http status code. It returns ACT_RET_PRS_OK on success,
673 * ACT_RET_PRS_ERR on error.
674 */
675static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
676 struct act_rule *rule, char **err)
677{
678 char *error;
679
680 rule->action = ACT_CUSTOM;
681 rule->action_ptr = action_http_set_status;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100682 rule->release_ptr = release_http_action;
Willy Tarreau6a03a0d2022-09-02 19:19:01 +0200683 LIST_INIT(&rule->arg.http.fmt);
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
709 return ACT_RET_PRS_OK;
710}
711
712/* This function executes the "reject" HTTP action. It clears the request and
713 * response buffer without sending any response. It can be useful as an HTTP
714 * alternative to the silent-drop action to defend against DoS attacks, and may
715 * also be used with HTTP/2 to close a connection instead of just a stream.
716 * The txn status is unchanged, indicating no response was sent. The termination
Christopher Faulet90d22a82020-03-06 11:18:39 +0100717 * flags will indicate "PR". It always returns ACT_RET_ABRT.
Willy Tarreau79e57332018-10-02 16:01:16 +0200718 */
719static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
720 struct session *sess, struct stream *s, int flags)
721{
Willy Tarreauf61dd192022-05-27 09:00:19 +0200722 sc_must_kill_conn(chn_prod(&s->req));
Willy Tarreau79e57332018-10-02 16:01:16 +0200723 channel_abort(&s->req);
724 channel_abort(&s->res);
Christopher Fauletd4a824e2020-03-06 15:07:09 +0100725 s->req.analysers &= AN_REQ_FLT_END;
726 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau79e57332018-10-02 16:01:16 +0200727
Willy Tarreau4781b152021-04-06 13:53:36 +0200728 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
729 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200730 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200731 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200732
733 if (!(s->flags & SF_ERR_MASK))
734 s->flags |= SF_ERR_PRXCOND;
735 if (!(s->flags & SF_FINST_MASK))
736 s->flags |= SF_FINST_R;
737
Christopher Faulet90d22a82020-03-06 11:18:39 +0100738 return ACT_RET_ABRT;
Willy Tarreau79e57332018-10-02 16:01:16 +0200739}
740
741/* parse the "reject" action:
742 * This action takes no argument and returns ACT_RET_PRS_OK on success,
743 * ACT_RET_PRS_ERR on error.
744 */
745static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
746 struct act_rule *rule, char **err)
747{
748 rule->action = ACT_CUSTOM;
749 rule->action_ptr = http_action_reject;
750 return ACT_RET_PRS_OK;
751}
752
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200753/* This function executes the "disable-l7-retry" HTTP action.
754 * It disables L7 retries (all retry except for a connection failure). This
755 * can be useful for example to avoid retrying on POST requests.
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200756 * It just removes the L7 retry flag on the HTTP transaction, and always
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200757 * return ACT_RET_CONT;
758 */
759static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px,
760 struct session *sess, struct stream *s, int flags)
761{
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200762 /* In theory, the TX_L7_RETRY flags isn't set at this point, but
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200763 * let's be future-proof and remove it anyway.
764 */
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200765 s->txn->flags &= ~TX_L7_RETRY;
766 s->txn->flags |= TX_D_L7_RETRY;
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200767 return ACT_RET_CONT;
768}
769
770/* parse the "disable-l7-retry" action:
771 * This action takes no argument and returns ACT_RET_PRS_OK on success,
772 * ACT_RET_PRS_ERR on error.
773 */
774static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args,
775 int *orig_args, struct proxy *px,
776 struct act_rule *rule, char **err)
777{
778 rule->action = ACT_CUSTOM;
779 rule->action_ptr = http_req_disable_l7_retry;
780 return ACT_RET_PRS_OK;
781}
782
Willy Tarreau79e57332018-10-02 16:01:16 +0200783/* This function executes the "capture" action. It executes a fetch expression,
784 * turns the result into a string and puts it in a capture slot. It always
785 * returns 1. If an error occurs the action is cancelled, but the rule
786 * processing continues.
787 */
788static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
789 struct session *sess, struct stream *s, int flags)
790{
791 struct sample *key;
792 struct cap_hdr *h = rule->arg.cap.hdr;
793 char **cap = s->req_cap;
794 int len;
795
796 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
797 if (!key)
798 return ACT_RET_CONT;
799
800 if (cap[h->index] == NULL)
801 cap[h->index] = pool_alloc(h->pool);
802
803 if (cap[h->index] == NULL) /* no more capture memory */
804 return ACT_RET_CONT;
805
806 len = key->data.u.str.data;
807 if (len > h->len)
808 len = h->len;
809
810 memcpy(cap[h->index], key->data.u.str.area, len);
811 cap[h->index][len] = 0;
812 return ACT_RET_CONT;
813}
814
815/* This function executes the "capture" action and store the result in a
816 * capture slot if exists. It executes a fetch expression, turns the result
817 * into a string and puts it in a capture slot. It always returns 1. If an
818 * error occurs the action is cancelled, but the rule processing continues.
819 */
820static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
821 struct session *sess, struct stream *s, int flags)
822{
823 struct sample *key;
824 struct cap_hdr *h;
825 char **cap = s->req_cap;
826 struct proxy *fe = strm_fe(s);
827 int len;
828 int i;
829
830 /* Look for the original configuration. */
831 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
832 h != NULL && i != rule->arg.capid.idx ;
833 i--, h = h->next);
834 if (!h)
835 return ACT_RET_CONT;
836
837 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
838 if (!key)
839 return ACT_RET_CONT;
840
841 if (cap[h->index] == NULL)
842 cap[h->index] = pool_alloc(h->pool);
843
844 if (cap[h->index] == NULL) /* no more capture memory */
845 return ACT_RET_CONT;
846
847 len = key->data.u.str.data;
848 if (len > h->len)
849 len = h->len;
850
851 memcpy(cap[h->index], key->data.u.str.area, len);
852 cap[h->index][len] = 0;
853 return ACT_RET_CONT;
854}
855
856/* Check an "http-request capture" action.
857 *
858 * The function returns 1 in success case, otherwise, it returns 0 and err is
859 * filled.
860 */
861static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
862{
863 if (rule->action_ptr != http_action_req_capture_by_id)
864 return 1;
865
Baptiste Assmann19a69b32020-01-16 14:34:22 +0100866 /* capture slots can only be declared in frontends, so we can't check their
867 * existence in backends at configuration parsing step
868 */
869 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_req_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200870 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
871 rule->arg.capid.idx);
872 return 0;
873 }
874
875 return 1;
876}
877
Christopher Faulet2eb53962020-01-14 14:47:34 +0100878/* Release memory allocate by an http capture action */
879static void release_http_capture(struct act_rule *rule)
880{
881 if (rule->action_ptr == http_action_req_capture)
882 release_sample_expr(rule->arg.cap.expr);
883 else
884 release_sample_expr(rule->arg.capid.expr);
885}
886
Willy Tarreau79e57332018-10-02 16:01:16 +0200887/* parse an "http-request capture" action. It takes a single argument which is
888 * a sample fetch expression. It stores the expression into arg->act.p[0] and
889 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
890 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
891 */
892static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
893 struct act_rule *rule, char **err)
894{
895 struct sample_expr *expr;
896 struct cap_hdr *hdr;
897 int cur_arg;
898 int len = 0;
899
900 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
901 if (strcmp(args[cur_arg], "if") == 0 ||
902 strcmp(args[cur_arg], "unless") == 0)
903 break;
904
905 if (cur_arg < *orig_arg + 3) {
906 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
907 return ACT_RET_PRS_ERR;
908 }
909
910 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100911 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 +0200912 if (!expr)
913 return ACT_RET_PRS_ERR;
914
915 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
916 memprintf(err,
917 "fetch method '%s' extracts information from '%s', none of which is available here",
918 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100919 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200920 return ACT_RET_PRS_ERR;
921 }
922
923 if (!args[cur_arg] || !*args[cur_arg]) {
924 memprintf(err, "expects 'len or 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100925 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200926 return ACT_RET_PRS_ERR;
927 }
928
929 if (strcmp(args[cur_arg], "len") == 0) {
930 cur_arg++;
931
932 if (!(px->cap & PR_CAP_FE)) {
933 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Christopher Faulet1337b322020-01-14 14:50:55 +0100934 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200935 return ACT_RET_PRS_ERR;
936 }
937
938 px->conf.args.ctx = ARGC_CAP;
939
940 if (!args[cur_arg]) {
941 memprintf(err, "missing length value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100942 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200943 return ACT_RET_PRS_ERR;
944 }
945 /* we copy the table name for now, it will be resolved later */
946 len = atoi(args[cur_arg]);
947 if (len <= 0) {
948 memprintf(err, "length must be > 0");
Christopher Faulet1337b322020-01-14 14:50:55 +0100949 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200950 return ACT_RET_PRS_ERR;
951 }
952 cur_arg++;
953
Willy Tarreau79e57332018-10-02 16:01:16 +0200954 hdr = calloc(1, sizeof(*hdr));
Remi Tricot-Le Bretona4bf8a02021-05-12 17:54:17 +0200955 if (!hdr) {
956 memprintf(err, "out of memory");
957 release_sample_expr(expr);
958 return ACT_RET_PRS_ERR;
959 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200960 hdr->next = px->req_cap;
961 hdr->name = NULL; /* not a header capture */
962 hdr->namelen = 0;
963 hdr->len = len;
964 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
965 hdr->index = px->nb_req_cap++;
966
967 px->req_cap = hdr;
968 px->to_log |= LW_REQHDR;
969
970 rule->action = ACT_CUSTOM;
971 rule->action_ptr = http_action_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100972 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200973 rule->arg.cap.expr = expr;
974 rule->arg.cap.hdr = hdr;
975 }
976
977 else if (strcmp(args[cur_arg], "id") == 0) {
978 int id;
979 char *error;
980
981 cur_arg++;
982
983 if (!args[cur_arg]) {
984 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100985 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200986 return ACT_RET_PRS_ERR;
987 }
988
989 id = strtol(args[cur_arg], &error, 10);
990 if (*error != '\0') {
991 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100992 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200993 return ACT_RET_PRS_ERR;
994 }
995 cur_arg++;
996
997 px->conf.args.ctx = ARGC_CAP;
998
999 rule->action = ACT_CUSTOM;
1000 rule->action_ptr = http_action_req_capture_by_id;
1001 rule->check_ptr = check_http_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001002 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +02001003 rule->arg.capid.expr = expr;
1004 rule->arg.capid.idx = id;
1005 }
1006
1007 else {
1008 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001009 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001010 return ACT_RET_PRS_ERR;
1011 }
1012
1013 *orig_arg = cur_arg;
1014 return ACT_RET_PRS_OK;
1015}
1016
1017/* This function executes the "capture" action and store the result in a
1018 * capture slot if exists. It executes a fetch expression, turns the result
1019 * into a string and puts it in a capture slot. It always returns 1. If an
1020 * error occurs the action is cancelled, but the rule processing continues.
1021 */
1022static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
1023 struct session *sess, struct stream *s, int flags)
1024{
1025 struct sample *key;
1026 struct cap_hdr *h;
1027 char **cap = s->res_cap;
1028 struct proxy *fe = strm_fe(s);
1029 int len;
1030 int i;
1031
1032 /* Look for the original configuration. */
1033 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
1034 h != NULL && i != rule->arg.capid.idx ;
1035 i--, h = h->next);
1036 if (!h)
1037 return ACT_RET_CONT;
1038
1039 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
1040 if (!key)
1041 return ACT_RET_CONT;
1042
1043 if (cap[h->index] == NULL)
1044 cap[h->index] = pool_alloc(h->pool);
1045
1046 if (cap[h->index] == NULL) /* no more capture memory */
1047 return ACT_RET_CONT;
1048
1049 len = key->data.u.str.data;
1050 if (len > h->len)
1051 len = h->len;
1052
1053 memcpy(cap[h->index], key->data.u.str.area, len);
1054 cap[h->index][len] = 0;
1055 return ACT_RET_CONT;
1056}
1057
1058/* Check an "http-response capture" action.
1059 *
1060 * The function returns 1 in success case, otherwise, it returns 0 and err is
1061 * filled.
1062 */
1063static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
1064{
1065 if (rule->action_ptr != http_action_res_capture_by_id)
1066 return 1;
1067
Tim Duesterhusf3f4aa02020-07-03 13:43:42 +02001068 /* capture slots can only be declared in frontends, so we can't check their
1069 * existence in backends at configuration parsing step
1070 */
1071 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_rsp_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001072 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
1073 rule->arg.capid.idx);
1074 return 0;
1075 }
1076
1077 return 1;
1078}
1079
1080/* parse an "http-response capture" action. It takes a single argument which is
1081 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07001082 * the allocated hdr_cap struct of the preallocated id into arg->act.p[1].
Willy Tarreau79e57332018-10-02 16:01:16 +02001083 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1084 */
1085static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
1086 struct act_rule *rule, char **err)
1087{
1088 struct sample_expr *expr;
1089 int cur_arg;
1090 int id;
1091 char *error;
1092
1093 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
1094 if (strcmp(args[cur_arg], "if") == 0 ||
1095 strcmp(args[cur_arg], "unless") == 0)
1096 break;
1097
1098 if (cur_arg < *orig_arg + 3) {
1099 memprintf(err, "expects <expression> id <idx>");
1100 return ACT_RET_PRS_ERR;
1101 }
1102
1103 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001104 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 +02001105 if (!expr)
1106 return ACT_RET_PRS_ERR;
1107
1108 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
1109 memprintf(err,
1110 "fetch method '%s' extracts information from '%s', none of which is available here",
1111 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01001112 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001113 return ACT_RET_PRS_ERR;
1114 }
1115
1116 if (!args[cur_arg] || !*args[cur_arg]) {
1117 memprintf(err, "expects 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +01001118 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001119 return ACT_RET_PRS_ERR;
1120 }
1121
1122 if (strcmp(args[cur_arg], "id") != 0) {
1123 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001124 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001125 return ACT_RET_PRS_ERR;
1126 }
1127
1128 cur_arg++;
1129
1130 if (!args[cur_arg]) {
1131 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +01001132 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001133 return ACT_RET_PRS_ERR;
1134 }
1135
1136 id = strtol(args[cur_arg], &error, 10);
1137 if (*error != '\0') {
1138 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001139 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001140 return ACT_RET_PRS_ERR;
1141 }
1142 cur_arg++;
1143
1144 px->conf.args.ctx = ARGC_CAP;
1145
1146 rule->action = ACT_CUSTOM;
1147 rule->action_ptr = http_action_res_capture_by_id;
1148 rule->check_ptr = check_http_res_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001149 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +02001150 rule->arg.capid.expr = expr;
1151 rule->arg.capid.idx = id;
1152
1153 *orig_arg = cur_arg;
1154 return ACT_RET_PRS_OK;
1155}
1156
Christopher Faulet81e20172019-12-12 16:40:30 +01001157/* Parse a "allow" action for a request or a response rule. It takes no argument. It
1158 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1159 */
1160static enum act_parse_ret parse_http_allow(const char **args, int *orig_arg, struct proxy *px,
1161 struct act_rule *rule, char **err)
1162{
1163 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +01001164 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +01001165 return ACT_RET_PRS_OK;
1166}
1167
Christopher Faulete0fca292020-01-13 21:49:03 +01001168/* Parse "deny" or "tarpit" actions for a request rule or "deny" action for a
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001169 * response rule. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on
1170 * error. It relies on http_parse_http_reply() to set
1171 * <.arg.http_reply>.
Christopher Faulet81e20172019-12-12 16:40:30 +01001172 */
Christopher Faulete0fca292020-01-13 21:49:03 +01001173static enum act_parse_ret parse_http_deny(const char **args, int *orig_arg, struct proxy *px,
1174 struct act_rule *rule, char **err)
Christopher Faulet81e20172019-12-12 16:40:30 +01001175{
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001176 int default_status;
1177 int cur_arg, arg = 0;
Christopher Faulet81e20172019-12-12 16:40:30 +01001178
1179 cur_arg = *orig_arg;
Christopher Faulete0fca292020-01-13 21:49:03 +01001180 if (rule->from == ACT_F_HTTP_REQ) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001181 if (strcmp(args[cur_arg - 1], "tarpit") == 0) {
Christopher Faulete0fca292020-01-13 21:49:03 +01001182 rule->action = ACT_HTTP_REQ_TARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001183 default_status = 500;
Christopher Faulet81e20172019-12-12 16:40:30 +01001184 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001185 else {
1186 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001187 default_status = 403;
Christopher Faulet81e20172019-12-12 16:40:30 +01001188 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001189 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001190 else {
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001191 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001192 default_status = 502;
Christopher Faulete0fca292020-01-13 21:49:03 +01001193 }
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001194
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001195 /* If no args or only a deny_status specified, fallback on the legacy
1196 * mode and use default error files despite the fact that
1197 * default-errorfiles is not used. Otherwise, parse an http reply.
1198 */
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001199
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001200 /* Prepare parsing of log-format strings */
1201 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001202
Christopher Faulet9467f182020-06-30 09:32:01 +02001203 if (!*(args[cur_arg]) || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001204 rule->arg.http_reply = http_parse_http_reply((const char *[]){"default-errorfiles", ""}, &arg, px, default_status, err);
1205 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001206 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001207
1208 if (strcmp(args[cur_arg], "deny_status") == 0) {
Christopher Faulet9467f182020-06-30 09:32:01 +02001209 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 +02001210 rule->arg.http_reply = http_parse_http_reply((const char *[]){"status", args[cur_arg+1], "default-errorfiles", ""},
1211 &arg, px, default_status, err);
1212 *orig_arg += 2;
1213 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001214 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001215 args[cur_arg] += 5; /* skip "deny_" for the parsing */
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001216 }
1217
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001218 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, default_status, err);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001219
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001220 end:
1221 if (!rule->arg.http_reply)
1222 return ACT_RET_PRS_ERR;
1223
1224 rule->flags |= ACT_FLAG_FINAL;
1225 rule->check_ptr = check_act_http_reply;
1226 rule->release_ptr = release_act_http_reply;
Christopher Faulet81e20172019-12-12 16:40:30 +01001227 return ACT_RET_PRS_OK;
1228}
1229
Christopher Fauletb3048832020-05-27 15:26:43 +02001230
1231/* This function executes a auth action. It builds an 401/407 HTX message using
1232 * the corresponding proxy's error message. On success, it returns
1233 * ACT_RET_ABRT. If an error occurs ACT_RET_ERR is returned.
1234 */
1235static enum act_return http_action_auth(struct act_rule *rule, struct proxy *px,
1236 struct session *sess, struct stream *s, int flags)
1237{
1238 struct channel *req = &s->req;
1239 struct channel *res = &s->res;
1240 struct htx *htx = htx_from_buf(&res->buf);
1241 struct http_reply *reply;
1242 const char *auth_realm;
1243 struct http_hdr_ctx ctx;
1244 struct ist hdr;
1245
1246 /* Auth might be performed on regular http-req rules as well as on stats */
1247 auth_realm = rule->arg.http.str.ptr;
1248 if (!auth_realm) {
1249 if (px->uri_auth && s->current_rule_list == &px->uri_auth->http_req_rules)
1250 auth_realm = STATS_DEFAULT_REALM;
1251 else
1252 auth_realm = px->id;
1253 }
1254
1255 if (!(s->txn->flags & TX_USE_PX_CONN)) {
1256 s->txn->status = 401;
1257 hdr = ist("WWW-Authenticate");
1258 }
1259 else {
1260 s->txn->status = 407;
1261 hdr = ist("Proxy-Authenticate");
1262 }
1263 reply = http_error_message(s);
1264 channel_htx_truncate(res, htx);
1265
1266 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
1267 goto fail;
1268
1269 /* Write the generic 40x message */
1270 if (http_reply_to_htx(s, htx, reply) == -1)
1271 goto fail;
1272
1273 /* Remove all existing occurrences of the XXX-Authenticate header */
1274 ctx.blk = NULL;
1275 while (http_find_header(htx, hdr, &ctx, 1))
1276 http_remove_header(htx, &ctx);
1277
1278 /* Now a the right XXX-Authenticate header */
1279 if (!http_add_header(htx, hdr, ist2(b_orig(&trash), b_data(&trash))))
1280 goto fail;
1281
1282 /* Finally forward the reply */
1283 htx_to_buf(htx, &res->buf);
1284 if (!http_forward_proxy_resp(s, 1))
1285 goto fail;
1286
1287 /* Note: Only eval on the request */
1288 s->logs.tv_request = now;
1289 req->analysers &= AN_REQ_FLT_END;
1290
1291 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02001292 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Fauletb3048832020-05-27 15:26:43 +02001293
1294 if (!(s->flags & SF_ERR_MASK))
1295 s->flags |= SF_ERR_LOCAL;
1296 if (!(s->flags & SF_FINST_MASK))
1297 s->flags |= SF_FINST_R;
1298
1299 stream_inc_http_err_ctr(s);
1300 return ACT_RET_ABRT;
1301
1302 fail:
1303 /* If an error occurred, remove the incomplete HTTP response from the
1304 * buffer */
1305 channel_htx_truncate(res, htx);
1306 return ACT_RET_ERR;
1307}
1308
Christopher Faulet81e20172019-12-12 16:40:30 +01001309/* Parse a "auth" action. It may take 2 optional arguments to define a "realm"
1310 * parameter. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1311 */
1312static enum act_parse_ret parse_http_auth(const char **args, int *orig_arg, struct proxy *px,
1313 struct act_rule *rule, char **err)
1314{
1315 int cur_arg;
1316
Christopher Fauletb3048832020-05-27 15:26:43 +02001317 rule->action = ACT_CUSTOM;
Christopher Faulet245cf792019-12-18 14:58:12 +01001318 rule->flags |= ACT_FLAG_FINAL;
Christopher Fauletb3048832020-05-27 15:26:43 +02001319 rule->action_ptr = http_action_auth;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001320 rule->release_ptr = release_http_action;
Willy Tarreau6a03a0d2022-09-02 19:19:01 +02001321 LIST_INIT(&rule->arg.http.fmt);
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
1334 *orig_arg = cur_arg;
1335 return ACT_RET_PRS_OK;
1336}
1337
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001338/* This function executes a early-hint action. It adds an HTTP Early Hint HTTP
1339 * 103 response header with <.arg.http.str> name and with a value built
1340 * according to <.arg.http.fmt> log line format. If it is the first early-hint
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001341 * rule of series, the 103 response start-line is added first. At the end, if
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001342 * the next rule is not an early-hint rule or if it is the last rule, the EOH
1343 * block is added to terminate the response. On success, it returns
1344 * ACT_RET_CONT. If an error occurs while soft rewrites are enabled, the action
1345 * is canceled, but the rule processing continue. Otherwsize ACT_RET_ERR is
1346 * returned.
1347 */
1348static enum act_return http_action_early_hint(struct act_rule *rule, struct proxy *px,
1349 struct session *sess, struct stream *s, int flags)
1350{
Christopher Faulet4c3d3d22022-07-05 16:24:15 +02001351 struct act_rule *next_rule;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001352 struct channel *res = &s->res;
1353 struct htx *htx = htx_from_buf(&res->buf);
1354 struct buffer *value = alloc_trash_chunk();
1355 enum act_return ret = ACT_RET_CONT;
1356
1357 if (!(s->txn->req.flags & HTTP_MSGF_VER_11))
1358 goto leave;
1359
1360 if (!value) {
1361 if (!(s->flags & SF_ERR_MASK))
1362 s->flags |= SF_ERR_RESOURCE;
1363 goto error;
1364 }
1365
Christopher Faulet4c3d3d22022-07-05 16:24:15 +02001366 /* if there is no pending 103 response, start a new response. Otherwise,
1367 * continue to add link to a previously started response
1368 */
1369 if (s->txn->status != 103) {
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001370 struct htx_sl *sl;
1371 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
1372 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
1373
1374 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
1375 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
1376 if (!sl)
1377 goto error;
1378 sl->info.res.status = 103;
Christopher Faulet4c3d3d22022-07-05 16:24:15 +02001379 s->txn->status = 103;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001380 }
1381
1382 /* Add the HTTP Early Hint HTTP 103 response heade */
1383 value->data = build_logline(s, b_tail(value), b_room(value), &rule->arg.http.fmt);
1384 if (!htx_add_header(htx, rule->arg.http.str, ist2(b_head(value), b_data(value))))
1385 goto error;
1386
Christopher Faulet4c3d3d22022-07-05 16:24:15 +02001387 /* if it is the last rule or the next one is not an early-hint or an
1388 * conditional early-hint, terminate the current response.
1389 */
1390 next_rule = LIST_NEXT(&rule->list, typeof(rule), list);
1391 if (&next_rule->list == s->current_rule_list || next_rule->action_ptr != http_action_early_hint || next_rule->cond) {
1392 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001393 goto error;
Christopher Fauleta72a7e42020-01-28 09:28:11 +01001394 if (!http_forward_proxy_resp(s, 0))
1395 goto error;
Christopher Faulet4c3d3d22022-07-05 16:24:15 +02001396 s->txn->status = 0;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001397 }
1398
1399 leave:
1400 free_trash_chunk(value);
1401 return ret;
1402
1403 error:
1404 /* If an error occurred during an Early-hint rule, remove the incomplete
1405 * HTTP 103 response from the buffer */
1406 channel_htx_truncate(res, htx);
1407 ret = ACT_RET_ERR;
Christopher Faulet4c3d3d22022-07-05 16:24:15 +02001408 s->txn->status = 0;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001409 goto leave;
1410}
1411
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001412/* This function executes a set-header or add-header actions. It builds a string
1413 * in the trash from the specified format string. It finds the action to be
1414 * performed in <.action>, previously filled by function parse_set_header(). The
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001415 * replacement action is executed by the function http_action_set_header(). On
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001416 * success, it returns ACT_RET_CONT. If an error occurs while soft rewrites are
1417 * enabled, the action is canceled, but the rule processing continue. Otherwsize
1418 * ACT_RET_ERR is returned.
1419 */
1420static enum act_return http_action_set_header(struct act_rule *rule, struct proxy *px,
1421 struct session *sess, struct stream *s, int flags)
1422{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001423 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1424 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001425 enum act_return ret = ACT_RET_CONT;
1426 struct buffer *replace;
1427 struct http_hdr_ctx ctx;
1428 struct ist n, v;
1429
1430 replace = alloc_trash_chunk();
1431 if (!replace)
1432 goto fail_alloc;
1433
1434 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1435 n = rule->arg.http.str;
1436 v = ist2(replace->area, replace->data);
1437
1438 if (rule->action == 0) { // set-header
1439 /* remove all occurrences of the header */
1440 ctx.blk = NULL;
1441 while (http_find_header(htx, n, &ctx, 1))
1442 http_remove_header(htx, &ctx);
1443 }
1444
1445 /* Now add header */
1446 if (!http_add_header(htx, n, v))
1447 goto fail_rewrite;
1448
1449 leave:
1450 free_trash_chunk(replace);
1451 return ret;
1452
1453 fail_alloc:
1454 if (!(s->flags & SF_ERR_MASK))
1455 s->flags |= SF_ERR_RESOURCE;
1456 ret = ACT_RET_ERR;
1457 goto leave;
1458
1459 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001460 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001461 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001462 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001463 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001464 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001465 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001466 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001467
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001468 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001469 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001470 if (!(s->flags & SF_ERR_MASK))
1471 s->flags |= SF_ERR_PRXCOND;
1472 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001473 goto leave;
1474}
1475
Christopher Faulet81e20172019-12-12 16:40:30 +01001476/* Parse a "set-header", "add-header" or "early-hint" actions. It takes an
1477 * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK
1478 * on success, ACT_RET_PRS_ERR on error.
1479 *
1480 * Note: same function is used for the request and the response. However
1481 * "early-hint" rules are only supported for request rules.
1482 */
1483static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px,
1484 struct act_rule *rule, char **err)
1485{
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001486 int cap = 0, cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +01001487
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001488 if (args[*orig_arg-1][0] == 'e') {
1489 rule->action = ACT_CUSTOM;
1490 rule->action_ptr = http_action_early_hint;
1491 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001492 else {
1493 if (args[*orig_arg-1][0] == 's')
1494 rule->action = 0; // set-header
1495 else
1496 rule->action = 1; // add-header
1497 rule->action_ptr = http_action_set_header;
1498 }
Christopher Faulet2eb53962020-01-14 14:47:34 +01001499 rule->release_ptr = release_http_action;
Willy Tarreau6a03a0d2022-09-02 19:19:01 +02001500 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001501
1502 cur_arg = *orig_arg;
1503 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1504 memprintf(err, "expects exactly 2 arguments");
1505 return ACT_RET_PRS_ERR;
1506 }
1507
Christopher Faulet81e20172019-12-12 16:40:30 +01001508
Tim Duesterhusf4f6c0f2022-03-15 13:11:08 +01001509 rule->arg.http.str = ist(strdup(args[cur_arg]));
Christopher Faulet81e20172019-12-12 16:40:30 +01001510
1511 if (rule->from == ACT_F_HTTP_REQ) {
1512 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001513 if (px->cap & PR_CAP_FE)
1514 cap |= SMP_VAL_FE_HRQ_HDR;
1515 if (px->cap & PR_CAP_BE)
1516 cap |= SMP_VAL_BE_HRQ_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001517 }
1518 else{
1519 px->conf.args.ctx = ARGC_HRS;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001520 if (px->cap & PR_CAP_FE)
1521 cap |= SMP_VAL_FE_HRS_HDR;
1522 if (px->cap & PR_CAP_BE)
1523 cap |= SMP_VAL_BE_HRS_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001524 }
1525
1526 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001527 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001528 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001529 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001530 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001531
1532 free(px->conf.lfs_file);
1533 px->conf.lfs_file = strdup(px->conf.args.file);
1534 px->conf.lfs_line = px->conf.args.line;
1535
1536 *orig_arg = cur_arg + 1;
1537 return ACT_RET_PRS_OK;
1538}
1539
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001540/* This function executes a replace-header or replace-value actions. It
1541 * builds a string in the trash from the specified format string. It finds
1542 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001543 * parse_replace_header(). The replacement action is executed by the function
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001544 * http_action_replace_header(). On success, it returns ACT_RET_CONT. If an error
1545 * occurs while soft rewrites are enabled, the action is canceled, but the rule
1546 * processing continue. Otherwsize ACT_RET_ERR is returned.
1547 */
1548static enum act_return http_action_replace_header(struct act_rule *rule, struct proxy *px,
1549 struct session *sess, struct stream *s, int flags)
1550{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001551 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1552 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001553 enum act_return ret = ACT_RET_CONT;
1554 struct buffer *replace;
1555 int r;
1556
1557 replace = alloc_trash_chunk();
1558 if (!replace)
1559 goto fail_alloc;
1560
1561 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1562
1563 r = http_replace_hdrs(s, htx, rule->arg.http.str, replace->area, rule->arg.http.re, (rule->action == 0));
1564 if (r == -1)
1565 goto fail_rewrite;
1566
1567 leave:
1568 free_trash_chunk(replace);
1569 return ret;
1570
1571 fail_alloc:
1572 if (!(s->flags & SF_ERR_MASK))
1573 s->flags |= SF_ERR_RESOURCE;
1574 ret = ACT_RET_ERR;
1575 goto leave;
1576
1577 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001578 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001579 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001580 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001581 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001582 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001583 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001584 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001585
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001586 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001587 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001588 if (!(s->flags & SF_ERR_MASK))
1589 s->flags |= SF_ERR_PRXCOND;
1590 }
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001591 goto leave;
1592}
1593
Christopher Faulet81e20172019-12-12 16:40:30 +01001594/* Parse a "replace-header" or "replace-value" actions. It takes an header name,
1595 * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on
1596 * success, ACT_RET_PRS_ERR on error.
1597 */
1598static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px,
1599 struct act_rule *rule, char **err)
1600{
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001601 int cap = 0, cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +01001602
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001603 if (args[*orig_arg-1][8] == 'h')
1604 rule->action = 0; // replace-header
1605 else
1606 rule->action = 1; // replace-value
1607 rule->action_ptr = http_action_replace_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001608 rule->release_ptr = release_http_action;
Willy Tarreau6a03a0d2022-09-02 19:19:01 +02001609 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001610
1611 cur_arg = *orig_arg;
1612 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2]) {
1613 memprintf(err, "expects exactly 3 arguments");
1614 return ACT_RET_PRS_ERR;
1615 }
1616
Tim Duesterhusf4f6c0f2022-03-15 13:11:08 +01001617 rule->arg.http.str = ist(strdup(args[cur_arg]));
Christopher Faulet81e20172019-12-12 16:40:30 +01001618
1619 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001620 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, err))) {
Tim Duesterhused526372020-03-05 17:56:33 +01001621 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001622 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001623 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001624
1625 if (rule->from == ACT_F_HTTP_REQ) {
1626 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001627 if (px->cap & PR_CAP_FE)
1628 cap |= SMP_VAL_FE_HRQ_HDR;
1629 if (px->cap & PR_CAP_BE)
1630 cap |= SMP_VAL_BE_HRQ_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001631 }
1632 else{
1633 px->conf.args.ctx = ARGC_HRS;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001634 if (px->cap & PR_CAP_FE)
1635 cap |= SMP_VAL_FE_HRS_HDR;
1636 if (px->cap & PR_CAP_BE)
1637 cap |= SMP_VAL_BE_HRS_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001638 }
1639
1640 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001641 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001642 istfree(&rule->arg.http.str);
Christopher Faulet1337b322020-01-14 14:50:55 +01001643 regex_free(rule->arg.http.re);
Christopher Faulet81e20172019-12-12 16:40:30 +01001644 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001645 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001646
1647 free(px->conf.lfs_file);
1648 px->conf.lfs_file = strdup(px->conf.args.file);
1649 px->conf.lfs_line = px->conf.args.line;
1650
1651 *orig_arg = cur_arg + 1;
1652 return ACT_RET_PRS_OK;
1653}
1654
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001655/* This function executes a del-header action with selected matching mode for
1656 * header name. It finds the matching method to be performed in <.action>, previously
1657 * filled by function parse_http_del_header(). On success, it returns ACT_RET_CONT.
1658 * Otherwise ACT_RET_ERR is returned.
1659 */
1660static enum act_return http_action_del_header(struct act_rule *rule, struct proxy *px,
1661 struct session *sess, struct stream *s, int flags)
1662{
1663 struct http_hdr_ctx ctx;
1664 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1665 struct htx *htx = htxbuf(&msg->chn->buf);
1666 enum act_return ret = ACT_RET_CONT;
1667
1668 /* remove all occurrences of the header */
1669 ctx.blk = NULL;
1670 switch (rule->action) {
1671 case PAT_MATCH_STR:
1672 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
1673 http_remove_header(htx, &ctx);
1674 break;
1675 case PAT_MATCH_BEG:
1676 while (http_find_pfx_header(htx, rule->arg.http.str, &ctx, 1))
1677 http_remove_header(htx, &ctx);
1678 break;
1679 case PAT_MATCH_END:
1680 while (http_find_sfx_header(htx, rule->arg.http.str, &ctx, 1))
1681 http_remove_header(htx, &ctx);
1682 break;
1683 case PAT_MATCH_SUB:
1684 while (http_find_sub_header(htx, rule->arg.http.str, &ctx, 1))
1685 http_remove_header(htx, &ctx);
1686 break;
1687 case PAT_MATCH_REG:
1688 while (http_match_header(htx, rule->arg.http.re, &ctx, 1))
1689 http_remove_header(htx, &ctx);
1690 break;
1691 default:
1692 return ACT_RET_ERR;
1693 }
1694 return ret;
1695}
1696
1697/* Parse a "del-header" action. It takes string as a required argument,
1698 * optional flag (currently only -m) and optional matching method of input string
1699 * with header name to be deleted. Default matching method is exact match (-m str).
1700 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001701 */
1702static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px,
1703 struct act_rule *rule, char **err)
1704{
1705 int cur_arg;
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001706 int pat_idx;
Christopher Faulet81e20172019-12-12 16:40:30 +01001707
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001708 /* set exact matching (-m str) as default */
1709 rule->action = PAT_MATCH_STR;
1710 rule->action_ptr = http_action_del_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001711 rule->release_ptr = release_http_action;
Willy Tarreau6a03a0d2022-09-02 19:19:01 +02001712 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001713
1714 cur_arg = *orig_arg;
1715 if (!*args[cur_arg]) {
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001716 memprintf(err, "expects at least 1 argument");
Christopher Faulet81e20172019-12-12 16:40:30 +01001717 return ACT_RET_PRS_ERR;
1718 }
1719
Tim Duesterhusf4f6c0f2022-03-15 13:11:08 +01001720 rule->arg.http.str = ist(strdup(args[cur_arg]));
Christopher Faulet81e20172019-12-12 16:40:30 +01001721 px->conf.args.ctx = (rule->from == ACT_F_HTTP_REQ ? ARGC_HRQ : ARGC_HRS);
1722
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001723 if (strcmp(args[cur_arg+1], "-m") == 0) {
1724 cur_arg++;
1725 if (!*args[cur_arg+1]) {
1726 memprintf(err, "-m flag expects exactly 1 argument");
1727 return ACT_RET_PRS_ERR;
1728 }
1729
1730 cur_arg++;
1731 pat_idx = pat_find_match_name(args[cur_arg]);
1732 switch (pat_idx) {
1733 case PAT_MATCH_REG:
1734 if (!(rule->arg.http.re = regex_comp(rule->arg.http.str.ptr, 1, 1, err)))
1735 return ACT_RET_PRS_ERR;
Willy Tarreauc5bc4ad2022-11-14 07:32:04 +01001736 __fallthrough;
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001737 case PAT_MATCH_STR:
1738 case PAT_MATCH_BEG:
1739 case PAT_MATCH_END:
1740 case PAT_MATCH_SUB:
1741 rule->action = pat_idx;
1742 break;
1743 default:
1744 memprintf(err, "-m with unsupported matching method '%s'", args[cur_arg]);
1745 return ACT_RET_PRS_ERR;
1746 }
1747 }
1748
Christopher Faulet81e20172019-12-12 16:40:30 +01001749 *orig_arg = cur_arg + 1;
1750 return ACT_RET_PRS_OK;
1751}
1752
Christopher Faulet2eb53962020-01-14 14:47:34 +01001753/* Release memory allocated by an http redirect action. */
1754static void release_http_redir(struct act_rule *rule)
1755{
1756 struct logformat_node *lf, *lfb;
1757 struct redirect_rule *redir;
1758
1759 redir = rule->arg.redir;
Willy Tarreau16daaf32022-04-25 10:25:15 +02001760 if (!redir)
1761 return;
1762
Willy Tarreau2b718102021-04-21 07:32:39 +02001763 LIST_DELETE(&redir->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001764 if (redir->cond) {
1765 prune_acl_cond(redir->cond);
1766 free(redir->cond);
1767 }
1768 free(redir->rdr_str);
1769 free(redir->cookie_str);
1770 list_for_each_entry_safe(lf, lfb, &redir->rdr_fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001771 LIST_DELETE(&lf->list);
Remi Tricot-Le Breton31202842023-01-06 16:31:06 +01001772 release_sample_expr(lf->expr);
1773 free(lf->arg);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001774 free(lf);
1775 }
1776 free(redir);
1777}
1778
Christopher Faulet81e20172019-12-12 16:40:30 +01001779/* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success,
1780 * ACT_RET_PRS_ERR on error.
1781 */
1782static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px,
1783 struct act_rule *rule, char **err)
1784{
1785 struct redirect_rule *redir;
1786 int dir, cur_arg;
1787
1788 rule->action = ACT_HTTP_REDIR;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001789 rule->release_ptr = release_http_redir;
Christopher Faulet81e20172019-12-12 16:40:30 +01001790
1791 cur_arg = *orig_arg;
1792
1793 dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1);
1794 if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL)
1795 return ACT_RET_PRS_ERR;
1796
Willy Tarreaubc1223b2021-09-02 16:54:33 +02001797 if (!(redir->flags & REDIRECT_FLAG_IGNORE_EMPTY))
1798 rule->flags |= ACT_FLAG_FINAL;
1799
Christopher Faulet81e20172019-12-12 16:40:30 +01001800 rule->arg.redir = redir;
1801 rule->cond = redir->cond;
1802 redir->cond = NULL;
1803
1804 /* skip all arguments */
1805 while (*args[cur_arg])
1806 cur_arg++;
1807
1808 *orig_arg = cur_arg;
1809 return ACT_RET_PRS_OK;
1810}
1811
Christopher Faulet046cf442019-12-17 15:45:23 +01001812/* This function executes a add-acl, del-acl, set-map or del-map actions. On
1813 * success, it returns ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1814 */
1815static enum act_return http_action_set_map(struct act_rule *rule, struct proxy *px,
1816 struct session *sess, struct stream *s, int flags)
1817{
1818 struct pat_ref *ref;
1819 struct buffer *key = NULL, *value = NULL;
1820 enum act_return ret = ACT_RET_CONT;
1821
1822 /* collect reference */
1823 ref = pat_ref_lookup(rule->arg.map.ref);
1824 if (!ref)
1825 goto leave;
1826
1827 /* allocate key */
1828 key = alloc_trash_chunk();
1829 if (!key)
1830 goto fail_alloc;
1831
1832 /* collect key */
1833 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
1834 key->area[key->data] = '\0';
1835
1836 switch (rule->action) {
1837 case 0: // add-acl
1838 /* add entry only if it does not already exist */
1839 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1840 if (pat_ref_find_elt(ref, key->area) == NULL)
1841 pat_ref_add(ref, key->area, NULL, NULL);
1842 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1843 break;
1844
1845 case 1: // set-map
1846 /* allocate value */
1847 value = alloc_trash_chunk();
1848 if (!value)
1849 goto fail_alloc;
1850
1851 /* collect value */
1852 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
1853 value->area[value->data] = '\0';
1854
1855 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1856 if (pat_ref_find_elt(ref, key->area) != NULL) {
1857 /* update entry if it exists */
1858 pat_ref_set(ref, key->area, value->area, NULL);
1859 }
1860 else {
1861 /* insert a new entry */
1862 pat_ref_add(ref, key->area, value->area, NULL);
1863 }
1864 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1865 break;
1866
1867 case 2: // del-acl
1868 case 3: // del-map
1869 /* returned code: 1=ok, 0=ko */
1870 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1871 pat_ref_delete(ref, key->area);
1872 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1873 break;
1874
1875 default:
1876 ret = ACT_RET_ERR;
1877 }
1878
1879
1880 leave:
1881 free_trash_chunk(key);
1882 free_trash_chunk(value);
1883 return ret;
1884
1885 fail_alloc:
1886 if (!(s->flags & SF_ERR_MASK))
1887 s->flags |= SF_ERR_RESOURCE;
1888 ret = ACT_RET_ERR;
1889 goto leave;
1890}
1891
Christopher Faulet2eb53962020-01-14 14:47:34 +01001892/* Release memory allocated by an http map/acl action. */
1893static void release_http_map(struct act_rule *rule)
1894{
1895 struct logformat_node *lf, *lfb;
1896
1897 free(rule->arg.map.ref);
1898 list_for_each_entry_safe(lf, lfb, &rule->arg.map.key, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001899 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001900 release_sample_expr(lf->expr);
1901 free(lf->arg);
1902 free(lf);
1903 }
1904 if (rule->action == 1) {
1905 list_for_each_entry_safe(lf, lfb, &rule->arg.map.value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001906 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001907 release_sample_expr(lf->expr);
1908 free(lf->arg);
1909 free(lf);
1910 }
1911 }
1912}
1913
Christopher Faulet81e20172019-12-12 16:40:30 +01001914/* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or
Christopher Faulet046cf442019-12-17 15:45:23 +01001915 * two log-format string as argument depending on the action. The action is
1916 * stored in <.action> as an int (0=add-acl, 1=set-map, 2=del-acl,
1917 * 3=del-map). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001918 */
1919static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px,
1920 struct act_rule *rule, char **err)
1921{
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001922 int cap = 0, cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +01001923
Christopher Faulet046cf442019-12-17 15:45:23 +01001924 if (args[*orig_arg-1][0] == 'a') // add-acl
1925 rule->action = 0;
1926 else if (args[*orig_arg-1][0] == 's') // set-map
1927 rule->action = 1;
1928 else if (args[*orig_arg-1][4] == 'a') // del-acl
1929 rule->action = 2;
1930 else if (args[*orig_arg-1][4] == 'm') // del-map
1931 rule->action = 3;
1932 else {
1933 memprintf(err, "internal error: unhandled action '%s'", args[0]);
1934 return ACT_RET_PRS_ERR;
1935 }
1936 rule->action_ptr = http_action_set_map;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001937 rule->release_ptr = release_http_map;
Christopher Faulet81e20172019-12-12 16:40:30 +01001938
1939 cur_arg = *orig_arg;
Christopher Faulet046cf442019-12-17 15:45:23 +01001940 if (rule->action == 1 && (!*args[cur_arg] || !*args[cur_arg+1])) {
1941 /* 2 args for set-map */
Christopher Faulet81e20172019-12-12 16:40:30 +01001942 memprintf(err, "expects exactly 2 arguments");
1943 return ACT_RET_PRS_ERR;
1944 }
1945 else if (!*args[cur_arg]) {
Christopher Faulet046cf442019-12-17 15:45:23 +01001946 /* only one arg for other actions */
Christopher Faulet81e20172019-12-12 16:40:30 +01001947 memprintf(err, "expects exactly 1 arguments");
1948 return ACT_RET_PRS_ERR;
1949 }
1950
1951 /*
1952 * '+ 8' for 'set-map(' (same for del-map)
1953 * '- 9' for 'set-map(' + trailing ')' (same for del-map)
1954 */
1955 rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9);
1956
1957 if (rule->from == ACT_F_HTTP_REQ) {
1958 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001959 if (px->cap & PR_CAP_FE)
1960 cap |= SMP_VAL_FE_HRQ_HDR;
1961 if (px->cap & PR_CAP_BE)
1962 cap |= SMP_VAL_BE_HRQ_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001963 }
1964 else{
1965 px->conf.args.ctx = ARGC_HRS;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001966 if (px->cap & PR_CAP_FE)
1967 cap |= SMP_VAL_FE_HRS_HDR;
1968 if (px->cap & PR_CAP_BE)
1969 cap |= SMP_VAL_BE_HRS_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001970 }
1971
1972 /* key pattern */
1973 LIST_INIT(&rule->arg.map.key);
Christopher Faulet1337b322020-01-14 14:50:55 +01001974 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err)) {
1975 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001976 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001977 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001978
Christopher Faulet046cf442019-12-17 15:45:23 +01001979 if (rule->action == 1) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001980 /* value pattern for set-map only */
1981 cur_arg++;
1982 LIST_INIT(&rule->arg.map.value);
Christopher Faulet1337b322020-01-14 14:50:55 +01001983 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err)) {
1984 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001985 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001986 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001987 }
1988
1989 free(px->conf.lfs_file);
1990 px->conf.lfs_file = strdup(px->conf.args.file);
1991 px->conf.lfs_line = px->conf.args.line;
1992
1993 *orig_arg = cur_arg + 1;
1994 return ACT_RET_PRS_OK;
1995}
1996
Christopher Fauletac98d812019-12-18 09:20:16 +01001997/* This function executes a track-sc* actions. On success, it returns
1998 * ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1999 */
2000static enum act_return http_action_track_sc(struct act_rule *rule, struct proxy *px,
2001 struct session *sess, struct stream *s, int flags)
2002{
2003 struct stktable *t;
2004 struct stksess *ts;
2005 struct stktable_key *key;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002006 void *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *ptr6;
Christopher Fauletac98d812019-12-18 09:20:16 +01002007 int opt;
2008
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002009 ptr1 = ptr2 = ptr3 = ptr4 = ptr5 = ptr6 = NULL;
Christopher Fauletac98d812019-12-18 09:20:16 +01002010 opt = ((rule->from == ACT_F_HTTP_REQ) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES) | SMP_OPT_FINAL;
2011
2012 t = rule->arg.trk_ctr.table.t;
Emeric Brun362d25e2021-03-10 16:58:03 +01002013
2014 if (stkctr_entry(&s->stkctr[rule->action]))
2015 goto end;
2016
Christopher Fauletac98d812019-12-18 09:20:16 +01002017 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, NULL);
2018
2019 if (!key)
2020 goto end;
2021 ts = stktable_get_entry(t, key);
2022 if (!ts)
2023 goto end;
2024
2025 stream_track_stkctr(&s->stkctr[rule->action], t, ts);
2026
2027 /* let's count a new HTTP request as it's the first time we do it */
2028 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2029 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2030
2031 /* When the client triggers a 4xx from the server, it's most often due
2032 * to a missing object or permission. These events should be tracked
2033 * because if they happen often, it may indicate a brute force or a
2034 * vulnerability scan. Normally this is done when receiving the response
2035 * but here we're tracking after this ought to have been done so we have
2036 * to do it on purpose.
2037 */
2038 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 400) < 100) {
2039 ptr3 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2040 ptr4 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2041 }
2042
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002043 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 500) < 100 &&
2044 s->txn->status != 501 && s->txn->status != 505) {
2045 ptr5 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
2046 ptr6 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
2047 }
2048
2049 if (ptr1 || ptr2 || ptr3 || ptr4 || ptr5 || ptr6) {
Christopher Fauletac98d812019-12-18 09:20:16 +01002050 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2051
2052 if (ptr1)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002053 stktable_data_cast(ptr1, std_t_uint)++;
Christopher Fauletac98d812019-12-18 09:20:16 +01002054 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002055 update_freq_ctr_period(&stktable_data_cast(ptr2, std_t_frqp),
Christopher Fauletac98d812019-12-18 09:20:16 +01002056 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2057 if (ptr3)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002058 stktable_data_cast(ptr3, std_t_uint)++;
Christopher Fauletac98d812019-12-18 09:20:16 +01002059 if (ptr4)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002060 update_freq_ctr_period(&stktable_data_cast(ptr4, std_t_frqp),
Christopher Fauletac98d812019-12-18 09:20:16 +01002061 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002062 if (ptr5)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002063 stktable_data_cast(ptr5, std_t_uint)++;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002064 if (ptr6)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002065 update_freq_ctr_period(&stktable_data_cast(ptr6, std_t_frqp),
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002066 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u, 1);
Christopher Fauletac98d812019-12-18 09:20:16 +01002067
2068 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2069
2070 /* If data was modified, we need to touch to re-schedule sync */
2071 stktable_touch_local(t, ts, 0);
2072 }
2073
2074 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT);
2075 if (sess->fe != s->be)
2076 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND);
2077
2078 end:
2079 return ACT_RET_CONT;
2080}
Christopher Faulet81e20172019-12-12 16:40:30 +01002081
Christopher Faulet2eb53962020-01-14 14:47:34 +01002082static void release_http_track_sc(struct act_rule *rule)
2083{
2084 release_sample_expr(rule->arg.trk_ctr.expr);
2085}
2086
Christopher Faulet81e20172019-12-12 16:40:30 +01002087/* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success,
2088 * ACT_RET_PRS_ERR on error.
2089 */
2090static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px,
2091 struct act_rule *rule, char **err)
2092{
2093 struct sample_expr *expr;
2094 unsigned int where;
2095 unsigned int tsc_num;
2096 const char *tsc_num_str;
2097 int cur_arg;
2098
2099 tsc_num_str = &args[*orig_arg-1][8];
2100 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1)
2101 return ACT_RET_PRS_ERR;
2102
2103 cur_arg = *orig_arg;
2104 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002105 err, &px->conf.args, NULL);
Christopher Faulet81e20172019-12-12 16:40:30 +01002106 if (!expr)
2107 return ACT_RET_PRS_ERR;
2108
2109 where = 0;
2110 if (px->cap & PR_CAP_FE)
2111 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
2112 if (px->cap & PR_CAP_BE)
2113 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
2114
2115 if (!(expr->fetch->val & where)) {
2116 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here",
2117 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01002118 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002119 return ACT_RET_PRS_ERR;
2120 }
2121
2122 if (strcmp(args[cur_arg], "table") == 0) {
2123 cur_arg++;
2124 if (!*args[cur_arg]) {
2125 memprintf(err, "missing table name");
Christopher Faulet1337b322020-01-14 14:50:55 +01002126 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002127 return ACT_RET_PRS_ERR;
2128 }
2129
2130 /* we copy the table name for now, it will be resolved later */
2131 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
2132 cur_arg++;
2133 }
2134
Christopher Fauletac98d812019-12-18 09:20:16 +01002135 rule->action = tsc_num;
Christopher Faulet81e20172019-12-12 16:40:30 +01002136 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +01002137 rule->action_ptr = http_action_track_sc;
Christopher Faulet2eb53962020-01-14 14:47:34 +01002138 rule->release_ptr = release_http_track_sc;
Christopher Faulet81e20172019-12-12 16:40:30 +01002139 rule->check_ptr = check_trk_action;
2140
2141 *orig_arg = cur_arg;
2142 return ACT_RET_PRS_OK;
2143}
2144
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002145static enum act_return action_timeout_set_stream_timeout(struct act_rule *rule,
2146 struct proxy *px,
2147 struct session *sess,
2148 struct stream *s,
2149 int flags)
2150{
2151 struct sample *key;
2152
2153 if (rule->arg.timeout.expr) {
2154 key = sample_fetch_as_type(px, sess, s, SMP_OPT_FINAL, rule->arg.timeout.expr, SMP_T_SINT);
2155 if (!key)
2156 return ACT_RET_CONT;
2157
2158 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(key->data.u.sint));
2159 }
2160 else {
2161 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(rule->arg.timeout.value));
2162 }
2163
2164 return ACT_RET_CONT;
2165}
2166
2167/* Parse a "set-timeout" action. Returns ACT_RET_PRS_ERR if parsing error.
2168 */
2169static enum act_parse_ret parse_http_set_timeout(const char **args,
2170 int *orig_arg,
2171 struct proxy *px,
2172 struct act_rule *rule, char **err)
2173{
2174 int cur_arg;
2175
2176 rule->action = ACT_CUSTOM;
2177 rule->action_ptr = action_timeout_set_stream_timeout;
2178 rule->release_ptr = release_timeout_action;
2179
2180 cur_arg = *orig_arg;
2181 if (!*args[cur_arg] || !*args[cur_arg + 1]) {
2182 memprintf(err, "expects exactly 2 arguments");
2183 return ACT_RET_PRS_ERR;
2184 }
2185
2186 if (!(px->cap & PR_CAP_BE)) {
2187 memprintf(err, "proxy '%s' has no backend capability", px->id);
2188 return ACT_RET_PRS_ERR;
2189 }
2190
2191 if (cfg_parse_rule_set_timeout(args, cur_arg,
2192 &rule->arg.timeout.value,
2193 &rule->arg.timeout.type,
2194 &rule->arg.timeout.expr,
2195 err,
2196 px->conf.args.file,
2197 px->conf.args.line, &px->conf.args) == -1) {
2198 return ACT_RET_PRS_ERR;
2199 }
2200
2201 *orig_arg = cur_arg + 2;
2202
2203 return ACT_RET_PRS_OK;
2204}
2205
Christopher Faulet46f95542019-12-20 10:07:22 +01002206/* This function executes a strict-mode actions. On success, it always returns
2207 * ACT_RET_CONT
2208 */
2209static enum act_return http_action_strict_mode(struct act_rule *rule, struct proxy *px,
2210 struct session *sess, struct stream *s, int flags)
2211{
2212 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
2213
2214 if (rule->action == 0) // strict-mode on
2215 msg->flags &= ~HTTP_MSGF_SOFT_RW;
2216 else // strict-mode off
2217 msg->flags |= HTTP_MSGF_SOFT_RW;
2218 return ACT_RET_CONT;
2219}
2220
2221/* Parse a "strict-mode" action. It returns ACT_RET_PRS_OK on success,
2222 * ACT_RET_PRS_ERR on error.
2223 */
2224static enum act_parse_ret parse_http_strict_mode(const char **args, int *orig_arg, struct proxy *px,
2225 struct act_rule *rule, char **err)
2226{
2227 int cur_arg;
2228
Christopher Faulet46f95542019-12-20 10:07:22 +01002229 cur_arg = *orig_arg;
2230 if (!*args[cur_arg]) {
2231 memprintf(err, "expects exactly 1 arguments");
2232 return ACT_RET_PRS_ERR;
2233 }
2234
2235 if (strcasecmp(args[cur_arg], "on") == 0)
2236 rule->action = 0; // strict-mode on
2237 else if (strcasecmp(args[cur_arg], "off") == 0)
2238 rule->action = 1; // strict-mode off
2239 else {
2240 memprintf(err, "Unexpected value '%s'. Only 'on' and 'off' are supported", args[cur_arg]);
2241 return ACT_RET_PRS_ERR;
2242 }
2243 rule->action_ptr = http_action_strict_mode;
2244
2245 *orig_arg = cur_arg + 1;
2246 return ACT_RET_PRS_OK;
2247}
2248
Christopher Faulet24231ab2020-01-24 17:44:23 +01002249/* This function executes a return action. It builds an HTX message from an
2250 * errorfile, an raw file or a log-format string, depending on <.action>
2251 * value. On success, it returns ACT_RET_ABRT. If an error occurs ACT_RET_ERR is
2252 * returned.
2253 */
2254static enum act_return http_action_return(struct act_rule *rule, struct proxy *px,
2255 struct session *sess, struct stream *s, int flags)
2256{
2257 struct channel *req = &s->req;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002258
Christopher Faulet2d36df22021-02-19 11:41:01 +01002259 s->txn->status = rule->arg.http_reply->status;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002260 if (http_reply_message(s, rule->arg.http_reply) == -1)
2261 return ACT_RET_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002262
Christopher Faulet24231ab2020-01-24 17:44:23 +01002263 if (rule->from == ACT_F_HTTP_REQ) {
2264 /* let's log the request time */
2265 s->logs.tv_request = now;
2266 req->analysers &= AN_REQ_FLT_END;
2267
2268 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002269 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002270 }
2271
2272 if (!(s->flags & SF_ERR_MASK))
2273 s->flags |= SF_ERR_LOCAL;
2274 if (!(s->flags & SF_FINST_MASK))
2275 s->flags |= ((rule->from == ACT_F_HTTP_REQ) ? SF_FINST_R : SF_FINST_H);
2276
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002277 return ACT_RET_ABRT;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002278}
2279
Christopher Faulet24231ab2020-01-24 17:44:23 +01002280/* Parse a "return" action. It returns ACT_RET_PRS_OK on success,
Christopher Faulet47e791e2020-05-13 14:36:55 +02002281 * ACT_RET_PRS_ERR on error. It relies on http_parse_http_reply() to set
2282 * <.arg.http_reply>.
Christopher Faulet24231ab2020-01-24 17:44:23 +01002283 */
2284static enum act_parse_ret parse_http_return(const char **args, int *orig_arg, struct proxy *px,
2285 struct act_rule *rule, char **err)
2286{
Christopher Faulet47e791e2020-05-13 14:36:55 +02002287 /* Prepare parsing of log-format strings */
2288 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
2289 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, 200, err);
2290 if (!rule->arg.http_reply)
2291 return ACT_RET_PRS_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002292
Christopher Fauletba946bf2020-05-13 08:50:07 +02002293 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet5ff0c642020-05-12 18:33:37 +02002294 rule->action = ACT_CUSTOM;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002295 rule->check_ptr = check_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002296 rule->action_ptr = http_action_return;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002297 rule->release_ptr = release_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002298 return ACT_RET_PRS_OK;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002299}
2300
Christopher Faulet021a8e42021-03-29 10:46:38 +02002301
2302
2303/* This function executes a wait-for-body action. It waits for the message
2304 * payload for a max configured time (.arg.p[0]) and eventually for only first
2305 * <arg.p[1]> bytes (0 means no limit). It relies on http_wait_for_msg_body()
2306 * function. it returns ACT_RET_CONT when conditions are met to stop to wait.
2307 * Otherwise ACT_RET_YIELD is returned to wait for more data. ACT_RET_INV is
2308 * returned if a parsing error is raised by lower level and ACT_RET_ERR if an
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05002309 * internal error occurred. Finally ACT_RET_ABRT is returned when a timeout
2310 * occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02002311 */
2312static enum act_return http_action_wait_for_body(struct act_rule *rule, struct proxy *px,
2313 struct session *sess, struct stream *s, int flags)
2314{
2315 struct channel *chn = ((rule->from == ACT_F_HTTP_REQ) ? &s->req : &s->res);
2316 unsigned int time = (uintptr_t)rule->arg.act.p[0];
2317 unsigned int bytes = (uintptr_t)rule->arg.act.p[1];
2318
2319 switch (http_wait_for_msg_body(s, chn, time, bytes)) {
2320 case HTTP_RULE_RES_CONT:
2321 return ACT_RET_CONT;
2322 case HTTP_RULE_RES_YIELD:
2323 return ACT_RET_YIELD;
2324 case HTTP_RULE_RES_BADREQ:
2325 return ACT_RET_INV;
2326 case HTTP_RULE_RES_ERROR:
2327 return ACT_RET_ERR;
2328 case HTTP_RULE_RES_ABRT:
2329 return ACT_RET_ABRT;
2330 default:
2331 return ACT_RET_ERR;
2332 }
2333}
2334
2335/* Parse a "wait-for-body" action. It returns ACT_RET_PRS_OK on success,
2336 * ACT_RET_PRS_ERR on error.
2337 */
2338static enum act_parse_ret parse_http_wait_for_body(const char **args, int *orig_arg, struct proxy *px,
2339 struct act_rule *rule, char **err)
2340{
2341 int cur_arg;
2342 unsigned int time, bytes;
2343 const char *res;
2344
2345 cur_arg = *orig_arg;
2346 if (!*args[cur_arg]) {
2347 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2348 return ACT_RET_PRS_ERR;
2349 }
2350
2351 time = UINT_MAX; /* To be sure it is set */
2352 bytes = 0; /* Default value, wait all the body */
2353 while (*(args[cur_arg])) {
2354 if (strcmp(args[cur_arg], "time") == 0) {
2355 if (!*args[cur_arg + 1]) {
2356 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2357 return ACT_RET_PRS_ERR;
2358 }
2359 res = parse_time_err(args[cur_arg+1], &time, TIME_UNIT_MS);
2360 if (res == PARSE_TIME_OVER) {
2361 memprintf(err, "time overflow (maximum value is 2147483647 ms or ~24.8 days)");
2362 return ACT_RET_PRS_ERR;
2363 }
2364 if (res == PARSE_TIME_UNDER) {
2365 memprintf(err, "time underflow (minimum non-null value is 1 ms)");
2366 return ACT_RET_PRS_ERR;
2367 }
2368 if (res) {
2369 memprintf(err, "unexpected character '%c'", *res);
2370 return ACT_RET_PRS_ERR;
2371 }
2372 cur_arg++;
2373 }
2374 else if (strcmp(args[cur_arg], "at-least") == 0) {
2375 if (!*args[cur_arg + 1]) {
2376 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2377 return ACT_RET_PRS_ERR;
2378 }
2379 res = parse_size_err(args[cur_arg+1], &bytes);
2380 if (res) {
2381 memprintf(err, "unexpected character '%c'", *res);
2382 return ACT_RET_PRS_ERR;
2383 }
2384 cur_arg++;
2385 }
2386 else
2387 break;
2388 cur_arg++;
2389 }
2390
2391 if (time == UINT_MAX) {
2392 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2393 return ACT_RET_PRS_ERR;
2394 }
2395
2396 rule->arg.act.p[0] = (void *)(uintptr_t)time;
2397 rule->arg.act.p[1] = (void *)(uintptr_t)bytes;
2398
2399 *orig_arg = cur_arg;
2400
2401 rule->action = ACT_CUSTOM;
2402 rule->action_ptr = http_action_wait_for_body;
2403 return ACT_RET_PRS_OK;
2404}
2405
Willy Tarreau79e57332018-10-02 16:01:16 +02002406/************************************************************************/
2407/* All supported http-request action keywords must be declared here. */
2408/************************************************************************/
2409
2410static struct action_kw_list http_req_actions = {
2411 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002412 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002413 { "add-header", parse_http_set_header, 0 },
2414 { "allow", parse_http_allow, 0 },
2415 { "auth", parse_http_auth, 0 },
2416 { "capture", parse_http_req_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002417 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002418 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002419 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002420 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002421 { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 },
2422 { "early-hint", parse_http_set_header, 0 },
Amaury Denoyellea9e639a2021-05-06 15:50:12 +02002423 { "normalize-uri", parse_http_normalize_uri, KWF_EXPERIMENTAL },
Christopher Faulet81e20172019-12-12 16:40:30 +01002424 { "redirect", parse_http_redirect, 0 },
2425 { "reject", parse_http_action_reject, 0 },
2426 { "replace-header", parse_http_replace_header, 0 },
2427 { "replace-path", parse_replace_uri, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002428 { "replace-pathq", parse_replace_uri, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002429 { "replace-uri", parse_replace_uri, 0 },
2430 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002431 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002432 { "set-header", parse_http_set_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002433 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002434 { "set-method", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002435 { "set-path", parse_set_req_line, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002436 { "set-pathq", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002437 { "set-query", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002438 { "set-uri", parse_set_req_line, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002439 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002440 { "tarpit", parse_http_deny, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002441 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002442 { "set-timeout", parse_http_set_timeout, 0 },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002443 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002444 { NULL, NULL }
2445 }
2446};
2447
Willy Tarreau0108d902018-11-25 19:14:37 +01002448INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2449
Willy Tarreau79e57332018-10-02 16:01:16 +02002450static struct action_kw_list http_res_actions = {
2451 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002452 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002453 { "add-header", parse_http_set_header, 0 },
2454 { "allow", parse_http_allow, 0 },
2455 { "capture", parse_http_res_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002456 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002457 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002458 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002459 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002460 { "redirect", parse_http_redirect, 0 },
2461 { "replace-header", parse_http_replace_header, 0 },
2462 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002463 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002464 { "set-header", parse_http_set_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002465 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002466 { "set-status", parse_http_set_status, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002467 { "strict-mode", parse_http_strict_mode, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002468 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002469 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002470 { NULL, NULL }
2471 }
2472};
2473
Willy Tarreau0108d902018-11-25 19:14:37 +01002474INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +02002475
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002476static struct action_kw_list http_after_res_actions = {
2477 .kw = {
2478 { "add-header", parse_http_set_header, 0 },
2479 { "allow", parse_http_allow, 0 },
Christopher Fauletba8f0632021-12-06 08:43:22 +01002480 { "capture", parse_http_res_capture, 0 },
Christopher Fauleta9248042023-01-05 11:17:38 +01002481 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002482 { "del-header", parse_http_del_header, 0 },
Christopher Fauleta9248042023-01-05 11:17:38 +01002483 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002484 { "replace-header", parse_http_replace_header, 0 },
2485 { "replace-value", parse_http_replace_header, 0 },
2486 { "set-header", parse_http_set_header, 0 },
Christopher Fauleta9248042023-01-05 11:17:38 +01002487 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002488 { "set-status", parse_http_set_status, 0 },
2489 { "strict-mode", parse_http_strict_mode, 0 },
2490 { NULL, NULL }
2491 }
2492};
2493
2494INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_actions);
2495
Willy Tarreau79e57332018-10-02 16:01:16 +02002496/*
2497 * Local variables:
2498 * c-indent-level: 8
2499 * c-basic-offset: 8
2500 * End:
2501 */