blob: 133a30c6aa4d994f377acbd1f5230794b5afd618 [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
2 * HTTP actions
3 *
4 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/types.h>
14
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
18
Willy Tarreaudcc048a2020-06-04 19:11:43 +020019#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020020#include <haproxy/action.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020021#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/arg.h>
23#include <haproxy/capture-t.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020024#include <haproxy/cfgparse.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020025#include <haproxy/chunk.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020026#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020027#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020028#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020029#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020030#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020031#include <haproxy/log.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020032#include <haproxy/pattern.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020033#include <haproxy/pool.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020034#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020035#include <haproxy/sample.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020036#include <haproxy/stream_interface.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020037#include <haproxy/tools.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020038#include <haproxy/uri_auth-t.h>
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +020039#include <haproxy/uri_normalizer.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020040#include <haproxy/version.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020041
Willy Tarreau79e57332018-10-02 16:01:16 +020042
Christopher Faulet2eb53962020-01-14 14:47:34 +010043/* Release memory allocated by most of HTTP actions. Concretly, it releases
44 * <arg.http>.
45 */
46static void release_http_action(struct act_rule *rule)
47{
48 struct logformat_node *lf, *lfb;
49
Tim Duesterhused526372020-03-05 17:56:33 +010050 istfree(&rule->arg.http.str);
Christopher Faulet2eb53962020-01-14 14:47:34 +010051 if (rule->arg.http.re)
52 regex_free(rule->arg.http.re);
53 list_for_each_entry_safe(lf, lfb, &rule->arg.http.fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +020054 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +010055 release_sample_expr(lf->expr);
56 free(lf->arg);
57 free(lf);
58 }
59}
60
Christopher Faulet5cb513a2020-05-13 17:56:56 +020061/* Release memory allocated by HTTP actions relying on an http reply. Concretly,
62 * it releases <.arg.http_reply>
63 */
64static void release_act_http_reply(struct act_rule *rule)
65{
66 release_http_reply(rule->arg.http_reply);
67 rule->arg.http_reply = NULL;
68}
69
70
71/* Check function for HTTP actions relying on an http reply. The function
72 * returns 1 in success case, otherwise, it returns 0 and err is filled.
73 */
74static int check_act_http_reply(struct act_rule *rule, struct proxy *px, char **err)
75{
76 struct http_reply *reply = rule->arg.http_reply;
77
78 if (!http_check_http_reply(reply, px, err)) {
79 release_act_http_reply(rule);
80 return 0;
81 }
82 return 1;
83}
84
Willy Tarreau79e57332018-10-02 16:01:16 +020085
86/* This function executes one of the set-{method,path,query,uri} actions. It
87 * builds a string in the trash from the specified format string. It finds
Christopher Faulet2c22a692019-12-18 15:39:56 +010088 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +050089 * parse_set_req_line(). The replacement action is executed by the function
Christopher Faulete00d06c2019-12-16 17:18:42 +010090 * http_action_set_req_line(). On success, it returns ACT_RET_CONT. If an error
91 * occurs while soft rewrites are enabled, the action is canceled, but the rule
92 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau79e57332018-10-02 16:01:16 +020093 */
94static enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
95 struct session *sess, struct stream *s, int flags)
96{
97 struct buffer *replace;
Christopher Faulet13403762019-12-13 09:01:57 +010098 enum act_return ret = ACT_RET_CONT;
Willy Tarreau79e57332018-10-02 16:01:16 +020099
100 replace = alloc_trash_chunk();
101 if (!replace)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100102 goto fail_alloc;
Willy Tarreau79e57332018-10-02 16:01:16 +0200103
104 /* If we have to create a query string, prepare a '?'. */
Christopher Faulet2c22a692019-12-18 15:39:56 +0100105 if (rule->action == 2) // set-query
Willy Tarreau79e57332018-10-02 16:01:16 +0200106 replace->area[replace->data++] = '?';
107 replace->data += build_logline(s, replace->area + replace->data,
108 replace->size - replace->data,
Christopher Faulet96bff762019-12-17 13:46:18 +0100109 &rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200110
Christopher Faulet2c22a692019-12-18 15:39:56 +0100111 if (http_req_replace_stline(rule->action, replace->area, replace->data, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100112 goto fail_rewrite;
Willy Tarreau79e57332018-10-02 16:01:16 +0200113
Christopher Faulete00d06c2019-12-16 17:18:42 +0100114 leave:
Willy Tarreau79e57332018-10-02 16:01:16 +0200115 free_trash_chunk(replace);
116 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100117
118 fail_alloc:
119 if (!(s->flags & SF_ERR_MASK))
120 s->flags |= SF_ERR_RESOURCE;
121 ret = ACT_RET_ERR;
122 goto leave;
123
124 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +0200125 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100126 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200127 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100128 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200129 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100130 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200131 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100132
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100133 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100134 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100135 if (!(s->flags & SF_ERR_MASK))
136 s->flags |= SF_ERR_PRXCOND;
137 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100138 goto leave;
Willy Tarreau79e57332018-10-02 16:01:16 +0200139}
140
141/* parse an http-request action among :
142 * set-method
143 * set-path
Christopher Faulet312294f2020-09-02 17:17:44 +0200144 * set-pathq
Willy Tarreau79e57332018-10-02 16:01:16 +0200145 * set-query
146 * set-uri
147 *
148 * All of them accept a single argument of type string representing a log-format.
Christopher Faulet96bff762019-12-17 13:46:18 +0100149 * The resulting rule makes use of <http.fmt> to store the log-format list head,
Christopher Faulet2c22a692019-12-18 15:39:56 +0100150 * and <.action> to store the action type as an int (0=method, 1=path, 2=query,
151 * 3=uri). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreau79e57332018-10-02 16:01:16 +0200152 */
153static enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
154 struct act_rule *rule, char **err)
155{
156 int cur_arg = *orig_arg;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +0200157 int cap = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200158
Willy Tarreau79e57332018-10-02 16:01:16 +0200159 switch (args[0][4]) {
160 case 'm' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100161 rule->action = 0; // set-method
Willy Tarreau79e57332018-10-02 16:01:16 +0200162 break;
163 case 'p' :
Christopher Faulet312294f2020-09-02 17:17:44 +0200164 if (args[0][8] == 'q')
165 rule->action = 4; // set-pathq
166 else
167 rule->action = 1; // set-path
Willy Tarreau79e57332018-10-02 16:01:16 +0200168 break;
169 case 'q' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100170 rule->action = 2; // set-query
Willy Tarreau79e57332018-10-02 16:01:16 +0200171 break;
172 case 'u' :
Christopher Faulet2c22a692019-12-18 15:39:56 +0100173 rule->action = 3; // set-uri
Willy Tarreau79e57332018-10-02 16:01:16 +0200174 break;
175 default:
176 memprintf(err, "internal error: unhandled action '%s'", args[0]);
177 return ACT_RET_PRS_ERR;
178 }
Christopher Faulet96bff762019-12-17 13:46:18 +0100179 rule->action_ptr = http_action_set_req_line;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100180 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200181
182 if (!*args[cur_arg] ||
183 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
184 memprintf(err, "expects exactly 1 argument <format>");
185 return ACT_RET_PRS_ERR;
186 }
187
Christopher Faulet96bff762019-12-17 13:46:18 +0100188 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200189 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +0200190 if (px->cap & PR_CAP_FE)
191 cap |= SMP_VAL_FE_HRQ_HDR;
192 if (px->cap & PR_CAP_BE)
193 cap |= SMP_VAL_BE_HRQ_HDR;
194 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200195 return ACT_RET_PRS_ERR;
196 }
197
198 (*orig_arg)++;
199 return ACT_RET_PRS_OK;
200}
201
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200202/* This function executes the http-request normalize-uri action.
203 * `rule->action` is expected to be a value from `enum act_normalize_uri`.
204 *
205 * On success, it returns ACT_RET_CONT. If an error
206 * occurs while soft rewrites are enabled, the action is canceled, but the rule
207 * processing continue. Otherwsize ACT_RET_ERR is returned.
208 */
209static enum act_return http_action_normalize_uri(struct act_rule *rule, struct proxy *px,
210 struct session *sess, struct stream *s, int flags)
211{
212 enum act_return ret = ACT_RET_CONT;
213 struct htx *htx = htxbuf(&s->req.buf);
214 const struct ist uri = htx_sl_req_uri(http_get_stline(htx));
215 struct buffer *replace = alloc_trash_chunk();
216 enum uri_normalizer_err err = URI_NORMALIZER_ERR_INTERNAL_ERROR;
217
218 if (!replace)
219 goto fail_alloc;
220
221 switch ((enum act_normalize_uri) rule->action) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200222 case ACT_NORMALIZE_URI_PATH_MERGE_SLASHES: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200223 struct http_uri_parser parser = http_uri_parser_init(uri);
224 const struct ist path = http_parse_path(&parser);
Tim Duesterhusd371e992021-04-15 21:45:58 +0200225 struct ist newpath = ist2(replace->area, replace->size);
226
227 if (!isttest(path))
228 goto leave;
229
230 err = uri_normalizer_path_merge_slashes(iststop(path, '?'), &newpath);
231
232 if (err != URI_NORMALIZER_ERR_NONE)
233 break;
234
235 if (!http_replace_req_path(htx, newpath, 0))
236 goto fail_rewrite;
237
238 break;
239 }
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200240 case ACT_NORMALIZE_URI_PATH_STRIP_DOT: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200241 struct http_uri_parser parser = http_uri_parser_init(uri);
242 const struct ist path = http_parse_path(&parser);
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200243 struct ist newpath = ist2(replace->area, replace->size);
244
245 if (!isttest(path))
246 goto leave;
247
248 err = uri_normalizer_path_dot(iststop(path, '?'), &newpath);
249
250 if (err != URI_NORMALIZER_ERR_NONE)
251 break;
252
253 if (!http_replace_req_path(htx, newpath, 0))
254 goto fail_rewrite;
255
256 break;
257 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200258 case ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT:
259 case ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200260 struct http_uri_parser parser = http_uri_parser_init(uri);
261 const struct ist path = http_parse_path(&parser);
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200262 struct ist newpath = ist2(replace->area, replace->size);
263
264 if (!isttest(path))
265 goto leave;
266
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200267 err = uri_normalizer_path_dotdot(iststop(path, '?'), rule->action == ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL, &newpath);
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200268
269 if (err != URI_NORMALIZER_ERR_NONE)
270 break;
271
272 if (!http_replace_req_path(htx, newpath, 0))
273 goto fail_rewrite;
274
275 break;
276 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200277 case ACT_NORMALIZE_URI_QUERY_SORT_BY_NAME: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200278 struct http_uri_parser parser = http_uri_parser_init(uri);
279 const struct ist path = http_parse_path(&parser);
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200280 struct ist newquery = ist2(replace->area, replace->size);
281
282 if (!isttest(path))
283 goto leave;
284
285 err = uri_normalizer_query_sort(istfind(path, '?'), '&', &newquery);
286
287 if (err != URI_NORMALIZER_ERR_NONE)
288 break;
289
290 if (!http_replace_req_query(htx, newquery))
291 goto fail_rewrite;
292
293 break;
294 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200295 case ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE:
296 case ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200297 struct http_uri_parser parser = http_uri_parser_init(uri);
298 const struct ist path = http_parse_path(&parser);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200299 struct ist newpath = ist2(replace->area, replace->size);
300
301 if (!isttest(path))
302 goto leave;
303
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200304 err = uri_normalizer_percent_upper(path, rule->action == ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT, &newpath);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200305
306 if (err != URI_NORMALIZER_ERR_NONE)
307 break;
308
309 if (!http_replace_req_path(htx, newpath, 1))
310 goto fail_rewrite;
311
312 break;
313 }
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +0200314 case ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED:
315 case ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200316 struct http_uri_parser parser = http_uri_parser_init(uri);
317 const struct ist path = http_parse_path(&parser);
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +0200318 struct ist newpath = ist2(replace->area, replace->size);
319
320 if (!isttest(path))
321 goto leave;
322
323 err = uri_normalizer_percent_decode_unreserved(path, rule->action == ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT, &newpath);
324
325 if (err != URI_NORMALIZER_ERR_NONE)
326 break;
327
328 if (!http_replace_req_path(htx, newpath, 1))
329 goto fail_rewrite;
330
331 break;
332 }
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200333 case ACT_NORMALIZE_URI_FRAGMENT_STRIP: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200334 struct http_uri_parser parser = http_uri_parser_init(uri);
335 const struct ist path = http_parse_path(&parser);
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200336 struct ist newpath = ist2(replace->area, replace->size);
337
338 if (!isttest(path))
339 goto leave;
340
341 err = uri_normalizer_fragment_strip(path, &newpath);
342
343 if (err != URI_NORMALIZER_ERR_NONE)
344 break;
345
346 if (!http_replace_req_path(htx, newpath, 1))
347 goto fail_rewrite;
348
349 break;
350 }
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200351 case ACT_NORMALIZE_URI_FRAGMENT_ENCODE: {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200352 struct http_uri_parser parser = http_uri_parser_init(uri);
353 const struct ist path = http_parse_path(&parser);
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200354 struct ist newpath = ist2(replace->area, replace->size);
355
356 if (!isttest(path))
357 goto leave;
358
359 err = uri_normalizer_fragment_encode(path, &newpath);
360
361 if (err != URI_NORMALIZER_ERR_NONE)
362 break;
363
364 if (!http_replace_req_path(htx, newpath, 1))
365 goto fail_rewrite;
366
367 break;
368 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200369 }
370
371 switch (err) {
372 case URI_NORMALIZER_ERR_NONE:
373 break;
374 case URI_NORMALIZER_ERR_INTERNAL_ERROR:
375 ret = ACT_RET_ERR;
376 break;
377 case URI_NORMALIZER_ERR_INVALID_INPUT:
378 ret = ACT_RET_INV;
379 break;
380 case URI_NORMALIZER_ERR_ALLOC:
381 goto fail_alloc;
382 }
383
384 leave:
385 free_trash_chunk(replace);
386 return ret;
387
388 fail_alloc:
389 if (!(s->flags & SF_ERR_MASK))
390 s->flags |= SF_ERR_RESOURCE;
391 ret = ACT_RET_ERR;
392 goto leave;
393
394 fail_rewrite:
395 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
396 if (s->flags & SF_BE_ASSIGNED)
397 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
398 if (sess->listener && sess->listener->counters)
399 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
400 if (objt_server(s->target))
401 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
402
403 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
404 ret = ACT_RET_ERR;
405 if (!(s->flags & SF_ERR_MASK))
406 s->flags |= SF_ERR_PRXCOND;
407 }
408 goto leave;
409}
410
411/* Parses the http-request normalize-uri action. It expects a single <normalizer>
412 * argument, corresponding too a value in `enum act_normalize_uri`.
413 *
414 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
415 */
416static enum act_parse_ret parse_http_normalize_uri(const char **args, int *orig_arg, struct proxy *px,
417 struct act_rule *rule, char **err)
418{
419 int cur_arg = *orig_arg;
420
421 rule->action_ptr = http_action_normalize_uri;
422 rule->release_ptr = NULL;
423
424 if (!*args[cur_arg]) {
425 memprintf(err, "missing argument <normalizer>");
426 return ACT_RET_PRS_ERR;
427 }
428
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200429 if (strcmp(args[cur_arg], "path-merge-slashes") == 0) {
Tim Duesterhusd371e992021-04-15 21:45:58 +0200430 cur_arg++;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200431
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200432 rule->action = ACT_NORMALIZE_URI_PATH_MERGE_SLASHES;
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200433 }
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200434 else if (strcmp(args[cur_arg], "path-strip-dot") == 0) {
435 cur_arg++;
436
437 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOT;
438 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200439 else if (strcmp(args[cur_arg], "path-strip-dotdot") == 0) {
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200440 cur_arg++;
441
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200442 if (strcmp(args[cur_arg], "full") == 0) {
443 cur_arg++;
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200444 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT_FULL;
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200445 }
446 else if (!*args[cur_arg]) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200447 rule->action = ACT_NORMALIZE_URI_PATH_STRIP_DOTDOT;
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200448 }
449 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
Tim Duesterhus2f413132021-05-10 23:21:20 +0200450 memprintf(err, "unknown argument '%s' for 'path-strip-dotdot' normalizer", args[cur_arg]);
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200451 return ACT_RET_PRS_ERR;
452 }
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200453 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200454 else if (strcmp(args[cur_arg], "query-sort-by-name") == 0) {
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200455 cur_arg++;
456
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200457 rule->action = ACT_NORMALIZE_URI_QUERY_SORT_BY_NAME;
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200458 }
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200459 else if (strcmp(args[cur_arg], "percent-to-uppercase") == 0) {
Tim Duesterhusa4071932021-04-15 21:46:02 +0200460 cur_arg++;
461
462 if (strcmp(args[cur_arg], "strict") == 0) {
463 cur_arg++;
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200464 rule->action = ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE_STRICT;
Tim Duesterhusa4071932021-04-15 21:46:02 +0200465 }
466 else if (!*args[cur_arg]) {
Tim Duesterhus5be6ab22021-04-17 11:21:10 +0200467 rule->action = ACT_NORMALIZE_URI_PERCENT_TO_UPPERCASE;
Tim Duesterhusa4071932021-04-15 21:46:02 +0200468 }
469 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
Tim Duesterhus2f413132021-05-10 23:21:20 +0200470 memprintf(err, "unknown argument '%s' for 'percent-to-uppercase' normalizer", args[cur_arg]);
Tim Duesterhusa4071932021-04-15 21:46:02 +0200471 return ACT_RET_PRS_ERR;
472 }
473 }
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +0200474 else if (strcmp(args[cur_arg], "percent-decode-unreserved") == 0) {
475 cur_arg++;
476
477 if (strcmp(args[cur_arg], "strict") == 0) {
478 cur_arg++;
479 rule->action = ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED_STRICT;
480 }
481 else if (!*args[cur_arg]) {
482 rule->action = ACT_NORMALIZE_URI_PERCENT_DECODE_UNRESERVED;
483 }
484 else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
485 memprintf(err, "unknown argument '%s' for 'percent-decode-unreserved' normalizer", args[cur_arg]);
486 return ACT_RET_PRS_ERR;
487 }
488 }
Tim Duesterhusc9e05ab2021-05-10 17:28:25 +0200489 else if (strcmp(args[cur_arg], "fragment-strip") == 0) {
490 cur_arg++;
491
492 rule->action = ACT_NORMALIZE_URI_FRAGMENT_STRIP;
493 }
Tim Duesterhusdec1c362021-05-10 17:28:26 +0200494 else if (strcmp(args[cur_arg], "fragment-encode") == 0) {
495 cur_arg++;
496
497 rule->action = ACT_NORMALIZE_URI_FRAGMENT_ENCODE;
498 }
Tim Duesterhusd2bedcc2021-04-15 21:45:57 +0200499 else {
500 memprintf(err, "unknown normalizer '%s'", args[cur_arg]);
501 return ACT_RET_PRS_ERR;
502 }
503
504 *orig_arg = cur_arg;
505 return ACT_RET_PRS_OK;
506}
507
Willy Tarreau33810222019-06-12 17:44:02 +0200508/* This function executes a replace-uri action. It finds its arguments in
Christopher Faulet96bff762019-12-17 13:46:18 +0100509 * <rule>.arg.http. It builds a string in the trash from the format string
Willy Tarreau33810222019-06-12 17:44:02 +0200510 * previously filled by function parse_replace_uri() and will execute the regex
Christopher Faulet96bff762019-12-17 13:46:18 +0100511 * in <http.re> to replace the URI. It uses the format string present in
Christopher Faulet2c22a692019-12-18 15:39:56 +0100512 * <http.fmt>. The component to act on (path/uri) is taken from <.action> which
Christopher Faulet96bff762019-12-17 13:46:18 +0100513 * contains 1 for the path or 3 for the URI (values used by
514 * http_req_replace_stline()). On success, it returns ACT_RET_CONT. If an error
515 * occurs while soft rewrites are enabled, the action is canceled, but the rule
516 * processing continue. Otherwsize ACT_RET_ERR is returned.
Willy Tarreau33810222019-06-12 17:44:02 +0200517 */
518static enum act_return http_action_replace_uri(struct act_rule *rule, struct proxy *px,
519 struct session *sess, struct stream *s, int flags)
520{
Christopher Faulet13403762019-12-13 09:01:57 +0100521 enum act_return ret = ACT_RET_CONT;
Willy Tarreau33810222019-06-12 17:44:02 +0200522 struct buffer *replace, *output;
523 struct ist uri;
524 int len;
525
526 replace = alloc_trash_chunk();
527 output = alloc_trash_chunk();
528 if (!replace || !output)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100529 goto fail_alloc;
Christopher Faulet12c28b62019-07-15 16:30:24 +0200530 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Willy Tarreau262c3f12019-12-17 06:52:51 +0100531
Amaury Denoyellec453f952021-07-06 11:40:12 +0200532 if (rule->action == 1) { // replace-path
533 struct http_uri_parser parser = http_uri_parser_init(uri);
534 uri = iststop(http_parse_path(&parser), '?');
535 }
536 else if (rule->action == 4) { // replace-pathq
537 struct http_uri_parser parser = http_uri_parser_init(uri);
538 uri = http_parse_path(&parser);
539 }
Willy Tarreau262c3f12019-12-17 06:52:51 +0100540
Christopher Faulet96bff762019-12-17 13:46:18 +0100541 if (!regex_exec_match2(rule->arg.http.re, uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
Willy Tarreau33810222019-06-12 17:44:02 +0200542 goto leave;
543
Christopher Faulet96bff762019-12-17 13:46:18 +0100544 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200545
546 /* note: uri.ptr doesn't need to be zero-terminated because it will
547 * only be used to pick pmatch references.
548 */
549 len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch);
550 if (len == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100551 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200552
Christopher Faulet2c22a692019-12-18 15:39:56 +0100553 if (http_req_replace_stline(rule->action, output->area, len, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100554 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200555
Christopher Faulete00d06c2019-12-16 17:18:42 +0100556 leave:
Willy Tarreau33810222019-06-12 17:44:02 +0200557 free_trash_chunk(output);
558 free_trash_chunk(replace);
559 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100560
561 fail_alloc:
562 if (!(s->flags & SF_ERR_MASK))
563 s->flags |= SF_ERR_RESOURCE;
564 ret = ACT_RET_ERR;
565 goto leave;
566
567 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +0200568 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100569 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200570 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100571 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200572 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100573 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200574 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100575
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100576 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100577 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100578 if (!(s->flags & SF_ERR_MASK))
579 s->flags |= SF_ERR_PRXCOND;
580 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100581 goto leave;
Willy Tarreau33810222019-06-12 17:44:02 +0200582}
583
Christopher Faulet312294f2020-09-02 17:17:44 +0200584/* parse a "replace-uri", "replace-path" or "replace-pathq"
585 * http-request action.
Willy Tarreau33810222019-06-12 17:44:02 +0200586 * This action takes 2 arguments (a regex and a replacement format string).
Christopher Faulet2c22a692019-12-18 15:39:56 +0100587 * The resulting rule makes use of <.action> to store the action (1/3 for now),
Christopher Faulet96bff762019-12-17 13:46:18 +0100588 * <http.re> to store the compiled regex, and <http.fmt> to store the log-format
Willy Tarreau33810222019-06-12 17:44:02 +0200589 * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
590 */
591static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px,
592 struct act_rule *rule, char **err)
593{
594 int cur_arg = *orig_arg;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +0200595 int cap = 0;
Willy Tarreau33810222019-06-12 17:44:02 +0200596 char *error = NULL;
597
Christopher Faulet312294f2020-09-02 17:17:44 +0200598 switch (args[0][8]) {
599 case 'p':
600 if (args[0][12] == 'q')
601 rule->action = 4; // replace-pathq, same as set-pathq
602 else
603 rule->action = 1; // replace-path, same as set-path
604 break;
605 case 'u':
Christopher Faulet2c22a692019-12-18 15:39:56 +0100606 rule->action = 3; // replace-uri, same as set-uri
Christopher Faulet312294f2020-09-02 17:17:44 +0200607 break;
608 default:
609 memprintf(err, "internal error: unhandled action '%s'", args[0]);
610 return ACT_RET_PRS_ERR;
611 }
Willy Tarreau262c3f12019-12-17 06:52:51 +0100612
Willy Tarreau33810222019-06-12 17:44:02 +0200613 rule->action_ptr = http_action_replace_uri;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100614 rule->release_ptr = release_http_action;
Willy Tarreau33810222019-06-12 17:44:02 +0200615
616 if (!*args[cur_arg] || !*args[cur_arg+1] ||
617 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
618 memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>");
619 return ACT_RET_PRS_ERR;
620 }
621
Christopher Faulet96bff762019-12-17 13:46:18 +0100622 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, &error))) {
Willy Tarreau33810222019-06-12 17:44:02 +0200623 memprintf(err, "failed to parse the regex : %s", error);
624 free(error);
625 return ACT_RET_PRS_ERR;
626 }
627
Christopher Faulet96bff762019-12-17 13:46:18 +0100628 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200629 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +0200630 if (px->cap & PR_CAP_FE)
631 cap |= SMP_VAL_FE_HRQ_HDR;
632 if (px->cap & PR_CAP_BE)
633 cap |= SMP_VAL_BE_HRQ_HDR;
634 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 +0100635 regex_free(rule->arg.http.re);
Willy Tarreau33810222019-06-12 17:44:02 +0200636 return ACT_RET_PRS_ERR;
637 }
638
639 (*orig_arg) += 2;
640 return ACT_RET_PRS_OK;
641}
642
Willy Tarreau79e57332018-10-02 16:01:16 +0200643/* This function is just a compliant action wrapper for "set-status". */
644static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
645 struct session *sess, struct stream *s, int flags)
646{
Christopher Faulet96bff762019-12-17 13:46:18 +0100647 if (http_res_set_status(rule->arg.http.i, rule->arg.http.str, s) == -1) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200648 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100649 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200650 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100651 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200652 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100653 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200654 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100655
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100656 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100657 if (!(s->flags & SF_ERR_MASK))
658 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet692a6c22020-02-07 10:22:31 +0100659 return ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100660 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100661 }
662
Willy Tarreau79e57332018-10-02 16:01:16 +0200663 return ACT_RET_CONT;
664}
665
666/* parse set-status action:
667 * This action accepts a single argument of type int representing
668 * an http status code. It returns ACT_RET_PRS_OK on success,
669 * ACT_RET_PRS_ERR on error.
670 */
671static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
672 struct act_rule *rule, char **err)
673{
674 char *error;
675
676 rule->action = ACT_CUSTOM;
677 rule->action_ptr = action_http_set_status;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100678 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200679
680 /* Check if an argument is available */
681 if (!*args[*orig_arg]) {
682 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
683 return ACT_RET_PRS_ERR;
684 }
685
686 /* convert status code as integer */
Christopher Faulet96bff762019-12-17 13:46:18 +0100687 rule->arg.http.i = strtol(args[*orig_arg], &error, 10);
688 if (*error != '\0' || rule->arg.http.i < 100 || rule->arg.http.i > 999) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200689 memprintf(err, "expects an integer status code between 100 and 999");
690 return ACT_RET_PRS_ERR;
691 }
692
693 (*orig_arg)++;
694
695 /* set custom reason string */
Christopher Faulet96bff762019-12-17 13:46:18 +0100696 rule->arg.http.str = ist(NULL); // If null, we use the default reason for the status code.
Willy Tarreau79e57332018-10-02 16:01:16 +0200697 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
698 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
699 (*orig_arg)++;
Tim Duesterhusf4f6c0f2022-03-15 13:11:08 +0100700 rule->arg.http.str = ist(strdup(args[*orig_arg]));
Willy Tarreau79e57332018-10-02 16:01:16 +0200701 (*orig_arg)++;
702 }
703
Christopher Fauletc20b3712020-01-27 15:51:56 +0100704 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200705 return ACT_RET_PRS_OK;
706}
707
708/* This function executes the "reject" HTTP action. It clears the request and
709 * response buffer without sending any response. It can be useful as an HTTP
710 * alternative to the silent-drop action to defend against DoS attacks, and may
711 * also be used with HTTP/2 to close a connection instead of just a stream.
712 * The txn status is unchanged, indicating no response was sent. The termination
Christopher Faulet90d22a82020-03-06 11:18:39 +0100713 * flags will indicate "PR". It always returns ACT_RET_ABRT.
Willy Tarreau79e57332018-10-02 16:01:16 +0200714 */
715static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
716 struct session *sess, struct stream *s, int flags)
717{
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100718 si_must_kill_conn(chn_prod(&s->req));
Willy Tarreau79e57332018-10-02 16:01:16 +0200719 channel_abort(&s->req);
720 channel_abort(&s->res);
Christopher Fauletd4a824e2020-03-06 15:07:09 +0100721 s->req.analysers &= AN_REQ_FLT_END;
722 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau79e57332018-10-02 16:01:16 +0200723
Willy Tarreau4781b152021-04-06 13:53:36 +0200724 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
725 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200726 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200727 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200728
729 if (!(s->flags & SF_ERR_MASK))
730 s->flags |= SF_ERR_PRXCOND;
731 if (!(s->flags & SF_FINST_MASK))
732 s->flags |= SF_FINST_R;
733
Christopher Faulet90d22a82020-03-06 11:18:39 +0100734 return ACT_RET_ABRT;
Willy Tarreau79e57332018-10-02 16:01:16 +0200735}
736
737/* parse the "reject" action:
738 * This action takes no argument and returns ACT_RET_PRS_OK on success,
739 * ACT_RET_PRS_ERR on error.
740 */
741static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
742 struct act_rule *rule, char **err)
743{
744 rule->action = ACT_CUSTOM;
745 rule->action_ptr = http_action_reject;
746 return ACT_RET_PRS_OK;
747}
748
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200749/* This function executes the "disable-l7-retry" HTTP action.
750 * It disables L7 retries (all retry except for a connection failure). This
751 * can be useful for example to avoid retrying on POST requests.
752 * It just removes the L7 retry flag on the stream_interface, and always
753 * return ACT_RET_CONT;
754 */
755static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px,
756 struct session *sess, struct stream *s, int flags)
757{
Christopher Faulete1ede302021-12-23 13:57:00 +0100758 struct stream_interface *si = cs_si(s->csb);
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200759
760 /* In theory, the SI_FL_L7_RETRY flags isn't set at this point, but
761 * let's be future-proof and remove it anyway.
762 */
763 si->flags &= ~SI_FL_L7_RETRY;
764 si->flags |= SI_FL_D_L7_RETRY;
765 return ACT_RET_CONT;
766}
767
768/* parse the "disable-l7-retry" action:
769 * This action takes no argument and returns ACT_RET_PRS_OK on success,
770 * ACT_RET_PRS_ERR on error.
771 */
772static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args,
773 int *orig_args, struct proxy *px,
774 struct act_rule *rule, char **err)
775{
776 rule->action = ACT_CUSTOM;
777 rule->action_ptr = http_req_disable_l7_retry;
778 return ACT_RET_PRS_OK;
779}
780
Willy Tarreau79e57332018-10-02 16:01:16 +0200781/* This function executes the "capture" action. It executes a fetch expression,
782 * turns the result into a string and puts it in a capture slot. It always
783 * returns 1. If an error occurs the action is cancelled, but the rule
784 * processing continues.
785 */
786static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
787 struct session *sess, struct stream *s, int flags)
788{
789 struct sample *key;
790 struct cap_hdr *h = rule->arg.cap.hdr;
791 char **cap = s->req_cap;
792 int len;
793
794 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
795 if (!key)
796 return ACT_RET_CONT;
797
798 if (cap[h->index] == NULL)
799 cap[h->index] = pool_alloc(h->pool);
800
801 if (cap[h->index] == NULL) /* no more capture memory */
802 return ACT_RET_CONT;
803
804 len = key->data.u.str.data;
805 if (len > h->len)
806 len = h->len;
807
808 memcpy(cap[h->index], key->data.u.str.area, len);
809 cap[h->index][len] = 0;
810 return ACT_RET_CONT;
811}
812
813/* This function executes the "capture" action and store the result in a
814 * capture slot if exists. It executes a fetch expression, turns the result
815 * into a string and puts it in a capture slot. It always returns 1. If an
816 * error occurs the action is cancelled, but the rule processing continues.
817 */
818static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
819 struct session *sess, struct stream *s, int flags)
820{
821 struct sample *key;
822 struct cap_hdr *h;
823 char **cap = s->req_cap;
824 struct proxy *fe = strm_fe(s);
825 int len;
826 int i;
827
828 /* Look for the original configuration. */
829 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
830 h != NULL && i != rule->arg.capid.idx ;
831 i--, h = h->next);
832 if (!h)
833 return ACT_RET_CONT;
834
835 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
836 if (!key)
837 return ACT_RET_CONT;
838
839 if (cap[h->index] == NULL)
840 cap[h->index] = pool_alloc(h->pool);
841
842 if (cap[h->index] == NULL) /* no more capture memory */
843 return ACT_RET_CONT;
844
845 len = key->data.u.str.data;
846 if (len > h->len)
847 len = h->len;
848
849 memcpy(cap[h->index], key->data.u.str.area, len);
850 cap[h->index][len] = 0;
851 return ACT_RET_CONT;
852}
853
854/* Check an "http-request capture" action.
855 *
856 * The function returns 1 in success case, otherwise, it returns 0 and err is
857 * filled.
858 */
859static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
860{
861 if (rule->action_ptr != http_action_req_capture_by_id)
862 return 1;
863
Baptiste Assmann19a69b32020-01-16 14:34:22 +0100864 /* capture slots can only be declared in frontends, so we can't check their
865 * existence in backends at configuration parsing step
866 */
867 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_req_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200868 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
869 rule->arg.capid.idx);
870 return 0;
871 }
872
873 return 1;
874}
875
Christopher Faulet2eb53962020-01-14 14:47:34 +0100876/* Release memory allocate by an http capture action */
877static void release_http_capture(struct act_rule *rule)
878{
879 if (rule->action_ptr == http_action_req_capture)
880 release_sample_expr(rule->arg.cap.expr);
881 else
882 release_sample_expr(rule->arg.capid.expr);
883}
884
Willy Tarreau79e57332018-10-02 16:01:16 +0200885/* parse an "http-request capture" action. It takes a single argument which is
886 * a sample fetch expression. It stores the expression into arg->act.p[0] and
887 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
888 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
889 */
890static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
891 struct act_rule *rule, char **err)
892{
893 struct sample_expr *expr;
894 struct cap_hdr *hdr;
895 int cur_arg;
896 int len = 0;
897
898 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
899 if (strcmp(args[cur_arg], "if") == 0 ||
900 strcmp(args[cur_arg], "unless") == 0)
901 break;
902
903 if (cur_arg < *orig_arg + 3) {
904 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
905 return ACT_RET_PRS_ERR;
906 }
907
908 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100909 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 +0200910 if (!expr)
911 return ACT_RET_PRS_ERR;
912
913 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
914 memprintf(err,
915 "fetch method '%s' extracts information from '%s', none of which is available here",
916 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100917 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200918 return ACT_RET_PRS_ERR;
919 }
920
921 if (!args[cur_arg] || !*args[cur_arg]) {
922 memprintf(err, "expects 'len or 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100923 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200924 return ACT_RET_PRS_ERR;
925 }
926
927 if (strcmp(args[cur_arg], "len") == 0) {
928 cur_arg++;
929
930 if (!(px->cap & PR_CAP_FE)) {
931 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Christopher Faulet1337b322020-01-14 14:50:55 +0100932 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200933 return ACT_RET_PRS_ERR;
934 }
935
936 px->conf.args.ctx = ARGC_CAP;
937
938 if (!args[cur_arg]) {
939 memprintf(err, "missing length value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100940 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200941 return ACT_RET_PRS_ERR;
942 }
943 /* we copy the table name for now, it will be resolved later */
944 len = atoi(args[cur_arg]);
945 if (len <= 0) {
946 memprintf(err, "length must be > 0");
Christopher Faulet1337b322020-01-14 14:50:55 +0100947 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200948 return ACT_RET_PRS_ERR;
949 }
950 cur_arg++;
951
Willy Tarreau79e57332018-10-02 16:01:16 +0200952 hdr = calloc(1, sizeof(*hdr));
Remi Tricot-Le Bretona4bf8a02021-05-12 17:54:17 +0200953 if (!hdr) {
954 memprintf(err, "out of memory");
955 release_sample_expr(expr);
956 return ACT_RET_PRS_ERR;
957 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200958 hdr->next = px->req_cap;
959 hdr->name = NULL; /* not a header capture */
960 hdr->namelen = 0;
961 hdr->len = len;
962 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
963 hdr->index = px->nb_req_cap++;
964
965 px->req_cap = hdr;
966 px->to_log |= LW_REQHDR;
967
968 rule->action = ACT_CUSTOM;
969 rule->action_ptr = http_action_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100970 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200971 rule->arg.cap.expr = expr;
972 rule->arg.cap.hdr = hdr;
973 }
974
975 else if (strcmp(args[cur_arg], "id") == 0) {
976 int id;
977 char *error;
978
979 cur_arg++;
980
981 if (!args[cur_arg]) {
982 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100983 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200984 return ACT_RET_PRS_ERR;
985 }
986
987 id = strtol(args[cur_arg], &error, 10);
988 if (*error != '\0') {
989 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100990 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200991 return ACT_RET_PRS_ERR;
992 }
993 cur_arg++;
994
995 px->conf.args.ctx = ARGC_CAP;
996
997 rule->action = ACT_CUSTOM;
998 rule->action_ptr = http_action_req_capture_by_id;
999 rule->check_ptr = check_http_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001000 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +02001001 rule->arg.capid.expr = expr;
1002 rule->arg.capid.idx = id;
1003 }
1004
1005 else {
1006 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001007 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001008 return ACT_RET_PRS_ERR;
1009 }
1010
1011 *orig_arg = cur_arg;
1012 return ACT_RET_PRS_OK;
1013}
1014
1015/* This function executes the "capture" action and store the result in a
1016 * capture slot if exists. It executes a fetch expression, turns the result
1017 * into a string and puts it in a capture slot. It always returns 1. If an
1018 * error occurs the action is cancelled, but the rule processing continues.
1019 */
1020static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
1021 struct session *sess, struct stream *s, int flags)
1022{
1023 struct sample *key;
1024 struct cap_hdr *h;
1025 char **cap = s->res_cap;
1026 struct proxy *fe = strm_fe(s);
1027 int len;
1028 int i;
1029
1030 /* Look for the original configuration. */
1031 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
1032 h != NULL && i != rule->arg.capid.idx ;
1033 i--, h = h->next);
1034 if (!h)
1035 return ACT_RET_CONT;
1036
1037 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
1038 if (!key)
1039 return ACT_RET_CONT;
1040
1041 if (cap[h->index] == NULL)
1042 cap[h->index] = pool_alloc(h->pool);
1043
1044 if (cap[h->index] == NULL) /* no more capture memory */
1045 return ACT_RET_CONT;
1046
1047 len = key->data.u.str.data;
1048 if (len > h->len)
1049 len = h->len;
1050
1051 memcpy(cap[h->index], key->data.u.str.area, len);
1052 cap[h->index][len] = 0;
1053 return ACT_RET_CONT;
1054}
1055
1056/* Check an "http-response capture" action.
1057 *
1058 * The function returns 1 in success case, otherwise, it returns 0 and err is
1059 * filled.
1060 */
1061static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
1062{
1063 if (rule->action_ptr != http_action_res_capture_by_id)
1064 return 1;
1065
Tim Duesterhusf3f4aa02020-07-03 13:43:42 +02001066 /* capture slots can only be declared in frontends, so we can't check their
1067 * existence in backends at configuration parsing step
1068 */
1069 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_rsp_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001070 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
1071 rule->arg.capid.idx);
1072 return 0;
1073 }
1074
1075 return 1;
1076}
1077
1078/* parse an "http-response capture" action. It takes a single argument which is
1079 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07001080 * the allocated hdr_cap struct of the preallocated id into arg->act.p[1].
Willy Tarreau79e57332018-10-02 16:01:16 +02001081 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1082 */
1083static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
1084 struct act_rule *rule, char **err)
1085{
1086 struct sample_expr *expr;
1087 int cur_arg;
1088 int id;
1089 char *error;
1090
1091 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
1092 if (strcmp(args[cur_arg], "if") == 0 ||
1093 strcmp(args[cur_arg], "unless") == 0)
1094 break;
1095
1096 if (cur_arg < *orig_arg + 3) {
1097 memprintf(err, "expects <expression> id <idx>");
1098 return ACT_RET_PRS_ERR;
1099 }
1100
1101 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001102 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 +02001103 if (!expr)
1104 return ACT_RET_PRS_ERR;
1105
1106 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
1107 memprintf(err,
1108 "fetch method '%s' extracts information from '%s', none of which is available here",
1109 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01001110 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001111 return ACT_RET_PRS_ERR;
1112 }
1113
1114 if (!args[cur_arg] || !*args[cur_arg]) {
1115 memprintf(err, "expects 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +01001116 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001117 return ACT_RET_PRS_ERR;
1118 }
1119
1120 if (strcmp(args[cur_arg], "id") != 0) {
1121 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001122 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001123 return ACT_RET_PRS_ERR;
1124 }
1125
1126 cur_arg++;
1127
1128 if (!args[cur_arg]) {
1129 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +01001130 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001131 return ACT_RET_PRS_ERR;
1132 }
1133
1134 id = strtol(args[cur_arg], &error, 10);
1135 if (*error != '\0') {
1136 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001137 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001138 return ACT_RET_PRS_ERR;
1139 }
1140 cur_arg++;
1141
1142 px->conf.args.ctx = ARGC_CAP;
1143
1144 rule->action = ACT_CUSTOM;
1145 rule->action_ptr = http_action_res_capture_by_id;
1146 rule->check_ptr = check_http_res_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001147 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +02001148 rule->arg.capid.expr = expr;
1149 rule->arg.capid.idx = id;
1150
1151 *orig_arg = cur_arg;
1152 return ACT_RET_PRS_OK;
1153}
1154
Christopher Faulet81e20172019-12-12 16:40:30 +01001155/* Parse a "allow" action for a request or a response rule. It takes no argument. It
1156 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1157 */
1158static enum act_parse_ret parse_http_allow(const char **args, int *orig_arg, struct proxy *px,
1159 struct act_rule *rule, char **err)
1160{
1161 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +01001162 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +01001163 return ACT_RET_PRS_OK;
1164}
1165
Christopher Faulete0fca292020-01-13 21:49:03 +01001166/* Parse "deny" or "tarpit" actions for a request rule or "deny" action for a
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001167 * response rule. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on
1168 * error. It relies on http_parse_http_reply() to set
1169 * <.arg.http_reply>.
Christopher Faulet81e20172019-12-12 16:40:30 +01001170 */
Christopher Faulete0fca292020-01-13 21:49:03 +01001171static enum act_parse_ret parse_http_deny(const char **args, int *orig_arg, struct proxy *px,
1172 struct act_rule *rule, char **err)
Christopher Faulet81e20172019-12-12 16:40:30 +01001173{
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001174 int default_status;
1175 int cur_arg, arg = 0;
Christopher Faulet81e20172019-12-12 16:40:30 +01001176
1177 cur_arg = *orig_arg;
Christopher Faulete0fca292020-01-13 21:49:03 +01001178 if (rule->from == ACT_F_HTTP_REQ) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001179 if (strcmp(args[cur_arg - 1], "tarpit") == 0) {
Christopher Faulete0fca292020-01-13 21:49:03 +01001180 rule->action = ACT_HTTP_REQ_TARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001181 default_status = 500;
Christopher Faulet81e20172019-12-12 16:40:30 +01001182 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001183 else {
1184 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001185 default_status = 403;
Christopher Faulet81e20172019-12-12 16:40:30 +01001186 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001187 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001188 else {
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001189 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001190 default_status = 502;
Christopher Faulete0fca292020-01-13 21:49:03 +01001191 }
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001192
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001193 /* If no args or only a deny_status specified, fallback on the legacy
1194 * mode and use default error files despite the fact that
1195 * default-errorfiles is not used. Otherwise, parse an http reply.
1196 */
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001197
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001198 /* Prepare parsing of log-format strings */
1199 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001200
Christopher Faulet9467f182020-06-30 09:32:01 +02001201 if (!*(args[cur_arg]) || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001202 rule->arg.http_reply = http_parse_http_reply((const char *[]){"default-errorfiles", ""}, &arg, px, default_status, err);
1203 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001204 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001205
1206 if (strcmp(args[cur_arg], "deny_status") == 0) {
Christopher Faulet9467f182020-06-30 09:32:01 +02001207 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 +02001208 rule->arg.http_reply = http_parse_http_reply((const char *[]){"status", args[cur_arg+1], "default-errorfiles", ""},
1209 &arg, px, default_status, err);
1210 *orig_arg += 2;
1211 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001212 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001213 args[cur_arg] += 5; /* skip "deny_" for the parsing */
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001214 }
1215
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001216 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, default_status, err);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001217
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001218 end:
1219 if (!rule->arg.http_reply)
1220 return ACT_RET_PRS_ERR;
1221
1222 rule->flags |= ACT_FLAG_FINAL;
1223 rule->check_ptr = check_act_http_reply;
1224 rule->release_ptr = release_act_http_reply;
Christopher Faulet81e20172019-12-12 16:40:30 +01001225 return ACT_RET_PRS_OK;
1226}
1227
Christopher Fauletb3048832020-05-27 15:26:43 +02001228
1229/* This function executes a auth action. It builds an 401/407 HTX message using
1230 * the corresponding proxy's error message. On success, it returns
1231 * ACT_RET_ABRT. If an error occurs ACT_RET_ERR is returned.
1232 */
1233static enum act_return http_action_auth(struct act_rule *rule, struct proxy *px,
1234 struct session *sess, struct stream *s, int flags)
1235{
1236 struct channel *req = &s->req;
1237 struct channel *res = &s->res;
1238 struct htx *htx = htx_from_buf(&res->buf);
1239 struct http_reply *reply;
1240 const char *auth_realm;
1241 struct http_hdr_ctx ctx;
1242 struct ist hdr;
1243
1244 /* Auth might be performed on regular http-req rules as well as on stats */
1245 auth_realm = rule->arg.http.str.ptr;
1246 if (!auth_realm) {
1247 if (px->uri_auth && s->current_rule_list == &px->uri_auth->http_req_rules)
1248 auth_realm = STATS_DEFAULT_REALM;
1249 else
1250 auth_realm = px->id;
1251 }
1252
1253 if (!(s->txn->flags & TX_USE_PX_CONN)) {
1254 s->txn->status = 401;
1255 hdr = ist("WWW-Authenticate");
1256 }
1257 else {
1258 s->txn->status = 407;
1259 hdr = ist("Proxy-Authenticate");
1260 }
1261 reply = http_error_message(s);
1262 channel_htx_truncate(res, htx);
1263
1264 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
1265 goto fail;
1266
1267 /* Write the generic 40x message */
1268 if (http_reply_to_htx(s, htx, reply) == -1)
1269 goto fail;
1270
1271 /* Remove all existing occurrences of the XXX-Authenticate header */
1272 ctx.blk = NULL;
1273 while (http_find_header(htx, hdr, &ctx, 1))
1274 http_remove_header(htx, &ctx);
1275
1276 /* Now a the right XXX-Authenticate header */
1277 if (!http_add_header(htx, hdr, ist2(b_orig(&trash), b_data(&trash))))
1278 goto fail;
1279
1280 /* Finally forward the reply */
1281 htx_to_buf(htx, &res->buf);
1282 if (!http_forward_proxy_resp(s, 1))
1283 goto fail;
1284
1285 /* Note: Only eval on the request */
1286 s->logs.tv_request = now;
1287 req->analysers &= AN_REQ_FLT_END;
1288
1289 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02001290 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Fauletb3048832020-05-27 15:26:43 +02001291
1292 if (!(s->flags & SF_ERR_MASK))
1293 s->flags |= SF_ERR_LOCAL;
1294 if (!(s->flags & SF_FINST_MASK))
1295 s->flags |= SF_FINST_R;
1296
1297 stream_inc_http_err_ctr(s);
1298 return ACT_RET_ABRT;
1299
1300 fail:
1301 /* If an error occurred, remove the incomplete HTTP response from the
1302 * buffer */
1303 channel_htx_truncate(res, htx);
1304 return ACT_RET_ERR;
1305}
1306
Christopher Faulet81e20172019-12-12 16:40:30 +01001307/* Parse a "auth" action. It may take 2 optional arguments to define a "realm"
1308 * parameter. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1309 */
1310static enum act_parse_ret parse_http_auth(const char **args, int *orig_arg, struct proxy *px,
1311 struct act_rule *rule, char **err)
1312{
1313 int cur_arg;
1314
Christopher Fauletb3048832020-05-27 15:26:43 +02001315 rule->action = ACT_CUSTOM;
Christopher Faulet245cf792019-12-18 14:58:12 +01001316 rule->flags |= ACT_FLAG_FINAL;
Christopher Fauletb3048832020-05-27 15:26:43 +02001317 rule->action_ptr = http_action_auth;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001318 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001319
1320 cur_arg = *orig_arg;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001321 if (strcmp(args[cur_arg], "realm") == 0) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001322 cur_arg++;
1323 if (!*args[cur_arg]) {
1324 memprintf(err, "missing realm value.\n");
1325 return ACT_RET_PRS_ERR;
1326 }
Tim Duesterhusf4f6c0f2022-03-15 13:11:08 +01001327 rule->arg.http.str = ist(strdup(args[cur_arg]));
Christopher Faulet81e20172019-12-12 16:40:30 +01001328 cur_arg++;
1329 }
1330
Christopher Fauletc20b3712020-01-27 15:51:56 +01001331 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001332 *orig_arg = cur_arg;
1333 return ACT_RET_PRS_OK;
1334}
1335
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001336/* This function executes a early-hint action. It adds an HTTP Early Hint HTTP
1337 * 103 response header with <.arg.http.str> name and with a value built
1338 * according to <.arg.http.fmt> log line format. If it is the first early-hint
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001339 * rule of series, the 103 response start-line is added first. At the end, if
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001340 * the next rule is not an early-hint rule or if it is the last rule, the EOH
1341 * block is added to terminate the response. On success, it returns
1342 * ACT_RET_CONT. If an error occurs while soft rewrites are enabled, the action
1343 * is canceled, but the rule processing continue. Otherwsize ACT_RET_ERR is
1344 * returned.
1345 */
1346static enum act_return http_action_early_hint(struct act_rule *rule, struct proxy *px,
1347 struct session *sess, struct stream *s, int flags)
1348{
1349 struct act_rule *prev_rule, *next_rule;
1350 struct channel *res = &s->res;
1351 struct htx *htx = htx_from_buf(&res->buf);
1352 struct buffer *value = alloc_trash_chunk();
1353 enum act_return ret = ACT_RET_CONT;
1354
1355 if (!(s->txn->req.flags & HTTP_MSGF_VER_11))
1356 goto leave;
1357
1358 if (!value) {
1359 if (!(s->flags & SF_ERR_MASK))
1360 s->flags |= SF_ERR_RESOURCE;
1361 goto error;
1362 }
1363
1364 /* get previous and next rules */
1365 prev_rule = LIST_PREV(&rule->list, typeof(rule), list);
1366 next_rule = LIST_NEXT(&rule->list, typeof(rule), list);
1367
1368 /* if no previous rule or previous rule is not early-hint, start a new response. Otherwise,
1369 * continue to add link to a previously started response */
1370 if (&prev_rule->list == s->current_rule_list || prev_rule->action_ptr != http_action_early_hint) {
1371 struct htx_sl *sl;
1372 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
1373 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
1374
1375 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
1376 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
1377 if (!sl)
1378 goto error;
1379 sl->info.res.status = 103;
1380 }
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
1387 /* if it is the last rule or the next one is not an early-hint, terminate the current
1388 * response. */
1389 if (&next_rule->list == s->current_rule_list || next_rule->action_ptr != http_action_early_hint) {
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001390 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
1391 /* If an error occurred during an Early-hint rule,
1392 * remove the incomplete HTTP 103 response from the
1393 * buffer */
1394 goto error;
1395 }
1396
Christopher Fauleta72a7e42020-01-28 09:28:11 +01001397 if (!http_forward_proxy_resp(s, 0))
1398 goto error;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001399 }
1400
1401 leave:
1402 free_trash_chunk(value);
1403 return ret;
1404
1405 error:
1406 /* If an error occurred during an Early-hint rule, remove the incomplete
1407 * HTTP 103 response from the buffer */
1408 channel_htx_truncate(res, htx);
1409 ret = ACT_RET_ERR;
1410 goto leave;
1411}
1412
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001413/* This function executes a set-header or add-header actions. It builds a string
1414 * in the trash from the specified format string. It finds the action to be
1415 * performed in <.action>, previously filled by function parse_set_header(). The
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001416 * replacement action is executed by the function http_action_set_header(). On
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001417 * success, it returns ACT_RET_CONT. If an error occurs while soft rewrites are
1418 * enabled, the action is canceled, but the rule processing continue. Otherwsize
1419 * ACT_RET_ERR is returned.
1420 */
1421static enum act_return http_action_set_header(struct act_rule *rule, struct proxy *px,
1422 struct session *sess, struct stream *s, int flags)
1423{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001424 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1425 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001426 enum act_return ret = ACT_RET_CONT;
1427 struct buffer *replace;
1428 struct http_hdr_ctx ctx;
1429 struct ist n, v;
1430
1431 replace = alloc_trash_chunk();
1432 if (!replace)
1433 goto fail_alloc;
1434
1435 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1436 n = rule->arg.http.str;
1437 v = ist2(replace->area, replace->data);
1438
1439 if (rule->action == 0) { // set-header
1440 /* remove all occurrences of the header */
1441 ctx.blk = NULL;
1442 while (http_find_header(htx, n, &ctx, 1))
1443 http_remove_header(htx, &ctx);
1444 }
1445
1446 /* Now add header */
1447 if (!http_add_header(htx, n, v))
1448 goto fail_rewrite;
1449
1450 leave:
1451 free_trash_chunk(replace);
1452 return ret;
1453
1454 fail_alloc:
1455 if (!(s->flags & SF_ERR_MASK))
1456 s->flags |= SF_ERR_RESOURCE;
1457 ret = ACT_RET_ERR;
1458 goto leave;
1459
1460 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001461 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001462 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001463 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001464 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001465 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001466 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001467 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001468
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001469 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001470 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001471 if (!(s->flags & SF_ERR_MASK))
1472 s->flags |= SF_ERR_PRXCOND;
1473 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001474 goto leave;
1475}
1476
Christopher Faulet81e20172019-12-12 16:40:30 +01001477/* Parse a "set-header", "add-header" or "early-hint" actions. It takes an
1478 * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK
1479 * on success, ACT_RET_PRS_ERR on error.
1480 *
1481 * Note: same function is used for the request and the response. However
1482 * "early-hint" rules are only supported for request rules.
1483 */
1484static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px,
1485 struct act_rule *rule, char **err)
1486{
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001487 int cap = 0, cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +01001488
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001489 if (args[*orig_arg-1][0] == 'e') {
1490 rule->action = ACT_CUSTOM;
1491 rule->action_ptr = http_action_early_hint;
1492 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001493 else {
1494 if (args[*orig_arg-1][0] == 's')
1495 rule->action = 0; // set-header
1496 else
1497 rule->action = 1; // add-header
1498 rule->action_ptr = http_action_set_header;
1499 }
Christopher Faulet2eb53962020-01-14 14:47:34 +01001500 rule->release_ptr = release_http_action;
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 Faulet96bff762019-12-17 13:46:18 +01001510 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001511
1512 if (rule->from == ACT_F_HTTP_REQ) {
1513 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001514 if (px->cap & PR_CAP_FE)
1515 cap |= SMP_VAL_FE_HRQ_HDR;
1516 if (px->cap & PR_CAP_BE)
1517 cap |= SMP_VAL_BE_HRQ_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001518 }
1519 else{
1520 px->conf.args.ctx = ARGC_HRS;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001521 if (px->cap & PR_CAP_FE)
1522 cap |= SMP_VAL_FE_HRS_HDR;
1523 if (px->cap & PR_CAP_BE)
1524 cap |= SMP_VAL_BE_HRS_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001525 }
1526
1527 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001528 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001529 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001530 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001531 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001532
1533 free(px->conf.lfs_file);
1534 px->conf.lfs_file = strdup(px->conf.args.file);
1535 px->conf.lfs_line = px->conf.args.line;
1536
1537 *orig_arg = cur_arg + 1;
1538 return ACT_RET_PRS_OK;
1539}
1540
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001541/* This function executes a replace-header or replace-value actions. It
1542 * builds a string in the trash from the specified format string. It finds
1543 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001544 * parse_replace_header(). The replacement action is executed by the function
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001545 * http_action_replace_header(). On success, it returns ACT_RET_CONT. If an error
1546 * occurs while soft rewrites are enabled, the action is canceled, but the rule
1547 * processing continue. Otherwsize ACT_RET_ERR is returned.
1548 */
1549static enum act_return http_action_replace_header(struct act_rule *rule, struct proxy *px,
1550 struct session *sess, struct stream *s, int flags)
1551{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001552 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1553 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001554 enum act_return ret = ACT_RET_CONT;
1555 struct buffer *replace;
1556 int r;
1557
1558 replace = alloc_trash_chunk();
1559 if (!replace)
1560 goto fail_alloc;
1561
1562 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1563
1564 r = http_replace_hdrs(s, htx, rule->arg.http.str, replace->area, rule->arg.http.re, (rule->action == 0));
1565 if (r == -1)
1566 goto fail_rewrite;
1567
1568 leave:
1569 free_trash_chunk(replace);
1570 return ret;
1571
1572 fail_alloc:
1573 if (!(s->flags & SF_ERR_MASK))
1574 s->flags |= SF_ERR_RESOURCE;
1575 ret = ACT_RET_ERR;
1576 goto leave;
1577
1578 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001579 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001580 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001581 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001582 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001583 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001584 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001585 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001586
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001587 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001588 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001589 if (!(s->flags & SF_ERR_MASK))
1590 s->flags |= SF_ERR_PRXCOND;
1591 }
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001592 goto leave;
1593}
1594
Christopher Faulet81e20172019-12-12 16:40:30 +01001595/* Parse a "replace-header" or "replace-value" actions. It takes an header name,
1596 * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on
1597 * success, ACT_RET_PRS_ERR on error.
1598 */
1599static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px,
1600 struct act_rule *rule, char **err)
1601{
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001602 int cap = 0, cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +01001603
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001604 if (args[*orig_arg-1][8] == 'h')
1605 rule->action = 0; // replace-header
1606 else
1607 rule->action = 1; // replace-value
1608 rule->action_ptr = http_action_replace_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001609 rule->release_ptr = release_http_action;
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 Faulet96bff762019-12-17 13:46:18 +01001618 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001619
1620 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001621 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, err))) {
Tim Duesterhused526372020-03-05 17:56:33 +01001622 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001623 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001624 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001625
1626 if (rule->from == ACT_F_HTTP_REQ) {
1627 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001628 if (px->cap & PR_CAP_FE)
1629 cap |= SMP_VAL_FE_HRQ_HDR;
1630 if (px->cap & PR_CAP_BE)
1631 cap |= SMP_VAL_BE_HRQ_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001632 }
1633 else{
1634 px->conf.args.ctx = ARGC_HRS;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001635 if (px->cap & PR_CAP_FE)
1636 cap |= SMP_VAL_FE_HRS_HDR;
1637 if (px->cap & PR_CAP_BE)
1638 cap |= SMP_VAL_BE_HRS_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001639 }
1640
1641 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001642 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001643 istfree(&rule->arg.http.str);
Christopher Faulet1337b322020-01-14 14:50:55 +01001644 regex_free(rule->arg.http.re);
Christopher Faulet81e20172019-12-12 16:40:30 +01001645 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001646 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001647
1648 free(px->conf.lfs_file);
1649 px->conf.lfs_file = strdup(px->conf.args.file);
1650 px->conf.lfs_line = px->conf.args.line;
1651
1652 *orig_arg = cur_arg + 1;
1653 return ACT_RET_PRS_OK;
1654}
1655
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001656/* This function executes a del-header action with selected matching mode for
1657 * header name. It finds the matching method to be performed in <.action>, previously
1658 * filled by function parse_http_del_header(). On success, it returns ACT_RET_CONT.
1659 * Otherwise ACT_RET_ERR is returned.
1660 */
1661static enum act_return http_action_del_header(struct act_rule *rule, struct proxy *px,
1662 struct session *sess, struct stream *s, int flags)
1663{
1664 struct http_hdr_ctx ctx;
1665 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1666 struct htx *htx = htxbuf(&msg->chn->buf);
1667 enum act_return ret = ACT_RET_CONT;
1668
1669 /* remove all occurrences of the header */
1670 ctx.blk = NULL;
1671 switch (rule->action) {
1672 case PAT_MATCH_STR:
1673 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
1674 http_remove_header(htx, &ctx);
1675 break;
1676 case PAT_MATCH_BEG:
1677 while (http_find_pfx_header(htx, rule->arg.http.str, &ctx, 1))
1678 http_remove_header(htx, &ctx);
1679 break;
1680 case PAT_MATCH_END:
1681 while (http_find_sfx_header(htx, rule->arg.http.str, &ctx, 1))
1682 http_remove_header(htx, &ctx);
1683 break;
1684 case PAT_MATCH_SUB:
1685 while (http_find_sub_header(htx, rule->arg.http.str, &ctx, 1))
1686 http_remove_header(htx, &ctx);
1687 break;
1688 case PAT_MATCH_REG:
1689 while (http_match_header(htx, rule->arg.http.re, &ctx, 1))
1690 http_remove_header(htx, &ctx);
1691 break;
1692 default:
1693 return ACT_RET_ERR;
1694 }
1695 return ret;
1696}
1697
1698/* Parse a "del-header" action. It takes string as a required argument,
1699 * optional flag (currently only -m) and optional matching method of input string
1700 * with header name to be deleted. Default matching method is exact match (-m str).
1701 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001702 */
1703static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px,
1704 struct act_rule *rule, char **err)
1705{
1706 int cur_arg;
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001707 int pat_idx;
Christopher Faulet81e20172019-12-12 16:40:30 +01001708
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001709 /* set exact matching (-m str) as default */
1710 rule->action = PAT_MATCH_STR;
1711 rule->action_ptr = http_action_del_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001712 rule->release_ptr = release_http_action;
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 Zdeb6dee9962020-11-23 16:03:09 +00001723 LIST_INIT(&rule->arg.http.fmt);
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001724 if (strcmp(args[cur_arg+1], "-m") == 0) {
1725 cur_arg++;
1726 if (!*args[cur_arg+1]) {
1727 memprintf(err, "-m flag expects exactly 1 argument");
1728 return ACT_RET_PRS_ERR;
1729 }
1730
1731 cur_arg++;
1732 pat_idx = pat_find_match_name(args[cur_arg]);
1733 switch (pat_idx) {
1734 case PAT_MATCH_REG:
1735 if (!(rule->arg.http.re = regex_comp(rule->arg.http.str.ptr, 1, 1, err)))
1736 return ACT_RET_PRS_ERR;
1737 /* fall through */
1738 case PAT_MATCH_STR:
1739 case PAT_MATCH_BEG:
1740 case PAT_MATCH_END:
1741 case PAT_MATCH_SUB:
1742 rule->action = pat_idx;
1743 break;
1744 default:
1745 memprintf(err, "-m with unsupported matching method '%s'", args[cur_arg]);
1746 return ACT_RET_PRS_ERR;
1747 }
1748 }
1749
Christopher Faulet81e20172019-12-12 16:40:30 +01001750 *orig_arg = cur_arg + 1;
1751 return ACT_RET_PRS_OK;
1752}
1753
Christopher Faulet2eb53962020-01-14 14:47:34 +01001754/* Release memory allocated by an http redirect action. */
1755static void release_http_redir(struct act_rule *rule)
1756{
1757 struct logformat_node *lf, *lfb;
1758 struct redirect_rule *redir;
1759
1760 redir = rule->arg.redir;
Willy Tarreau2b718102021-04-21 07:32:39 +02001761 LIST_DELETE(&redir->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001762 if (redir->cond) {
1763 prune_acl_cond(redir->cond);
1764 free(redir->cond);
1765 }
1766 free(redir->rdr_str);
1767 free(redir->cookie_str);
1768 list_for_each_entry_safe(lf, lfb, &redir->rdr_fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001769 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001770 free(lf);
1771 }
1772 free(redir);
1773}
1774
Christopher Faulet81e20172019-12-12 16:40:30 +01001775/* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success,
1776 * ACT_RET_PRS_ERR on error.
1777 */
1778static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px,
1779 struct act_rule *rule, char **err)
1780{
1781 struct redirect_rule *redir;
1782 int dir, cur_arg;
1783
1784 rule->action = ACT_HTTP_REDIR;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001785 rule->release_ptr = release_http_redir;
Christopher Faulet81e20172019-12-12 16:40:30 +01001786
1787 cur_arg = *orig_arg;
1788
1789 dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1);
1790 if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL)
1791 return ACT_RET_PRS_ERR;
1792
Willy Tarreaubc1223b2021-09-02 16:54:33 +02001793 if (!(redir->flags & REDIRECT_FLAG_IGNORE_EMPTY))
1794 rule->flags |= ACT_FLAG_FINAL;
1795
Christopher Faulet81e20172019-12-12 16:40:30 +01001796 rule->arg.redir = redir;
1797 rule->cond = redir->cond;
1798 redir->cond = NULL;
1799
1800 /* skip all arguments */
1801 while (*args[cur_arg])
1802 cur_arg++;
1803
1804 *orig_arg = cur_arg;
1805 return ACT_RET_PRS_OK;
1806}
1807
Christopher Faulet046cf442019-12-17 15:45:23 +01001808/* This function executes a add-acl, del-acl, set-map or del-map actions. On
1809 * success, it returns ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1810 */
1811static enum act_return http_action_set_map(struct act_rule *rule, struct proxy *px,
1812 struct session *sess, struct stream *s, int flags)
1813{
1814 struct pat_ref *ref;
1815 struct buffer *key = NULL, *value = NULL;
1816 enum act_return ret = ACT_RET_CONT;
1817
1818 /* collect reference */
1819 ref = pat_ref_lookup(rule->arg.map.ref);
1820 if (!ref)
1821 goto leave;
1822
1823 /* allocate key */
1824 key = alloc_trash_chunk();
1825 if (!key)
1826 goto fail_alloc;
1827
1828 /* collect key */
1829 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
1830 key->area[key->data] = '\0';
1831
1832 switch (rule->action) {
1833 case 0: // add-acl
1834 /* add entry only if it does not already exist */
1835 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1836 if (pat_ref_find_elt(ref, key->area) == NULL)
1837 pat_ref_add(ref, key->area, NULL, NULL);
1838 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1839 break;
1840
1841 case 1: // set-map
1842 /* allocate value */
1843 value = alloc_trash_chunk();
1844 if (!value)
1845 goto fail_alloc;
1846
1847 /* collect value */
1848 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
1849 value->area[value->data] = '\0';
1850
1851 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1852 if (pat_ref_find_elt(ref, key->area) != NULL) {
1853 /* update entry if it exists */
1854 pat_ref_set(ref, key->area, value->area, NULL);
1855 }
1856 else {
1857 /* insert a new entry */
1858 pat_ref_add(ref, key->area, value->area, NULL);
1859 }
1860 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1861 break;
1862
1863 case 2: // del-acl
1864 case 3: // del-map
1865 /* returned code: 1=ok, 0=ko */
1866 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1867 pat_ref_delete(ref, key->area);
1868 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1869 break;
1870
1871 default:
1872 ret = ACT_RET_ERR;
1873 }
1874
1875
1876 leave:
1877 free_trash_chunk(key);
1878 free_trash_chunk(value);
1879 return ret;
1880
1881 fail_alloc:
1882 if (!(s->flags & SF_ERR_MASK))
1883 s->flags |= SF_ERR_RESOURCE;
1884 ret = ACT_RET_ERR;
1885 goto leave;
1886}
1887
Christopher Faulet2eb53962020-01-14 14:47:34 +01001888/* Release memory allocated by an http map/acl action. */
1889static void release_http_map(struct act_rule *rule)
1890{
1891 struct logformat_node *lf, *lfb;
1892
1893 free(rule->arg.map.ref);
1894 list_for_each_entry_safe(lf, lfb, &rule->arg.map.key, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001895 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001896 release_sample_expr(lf->expr);
1897 free(lf->arg);
1898 free(lf);
1899 }
1900 if (rule->action == 1) {
1901 list_for_each_entry_safe(lf, lfb, &rule->arg.map.value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001902 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001903 release_sample_expr(lf->expr);
1904 free(lf->arg);
1905 free(lf);
1906 }
1907 }
1908}
1909
Christopher Faulet81e20172019-12-12 16:40:30 +01001910/* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or
Christopher Faulet046cf442019-12-17 15:45:23 +01001911 * two log-format string as argument depending on the action. The action is
1912 * stored in <.action> as an int (0=add-acl, 1=set-map, 2=del-acl,
1913 * 3=del-map). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001914 */
1915static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px,
1916 struct act_rule *rule, char **err)
1917{
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001918 int cap = 0, cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +01001919
Christopher Faulet046cf442019-12-17 15:45:23 +01001920 if (args[*orig_arg-1][0] == 'a') // add-acl
1921 rule->action = 0;
1922 else if (args[*orig_arg-1][0] == 's') // set-map
1923 rule->action = 1;
1924 else if (args[*orig_arg-1][4] == 'a') // del-acl
1925 rule->action = 2;
1926 else if (args[*orig_arg-1][4] == 'm') // del-map
1927 rule->action = 3;
1928 else {
1929 memprintf(err, "internal error: unhandled action '%s'", args[0]);
1930 return ACT_RET_PRS_ERR;
1931 }
1932 rule->action_ptr = http_action_set_map;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001933 rule->release_ptr = release_http_map;
Christopher Faulet81e20172019-12-12 16:40:30 +01001934
1935 cur_arg = *orig_arg;
Christopher Faulet046cf442019-12-17 15:45:23 +01001936 if (rule->action == 1 && (!*args[cur_arg] || !*args[cur_arg+1])) {
1937 /* 2 args for set-map */
Christopher Faulet81e20172019-12-12 16:40:30 +01001938 memprintf(err, "expects exactly 2 arguments");
1939 return ACT_RET_PRS_ERR;
1940 }
1941 else if (!*args[cur_arg]) {
Christopher Faulet046cf442019-12-17 15:45:23 +01001942 /* only one arg for other actions */
Christopher Faulet81e20172019-12-12 16:40:30 +01001943 memprintf(err, "expects exactly 1 arguments");
1944 return ACT_RET_PRS_ERR;
1945 }
1946
1947 /*
1948 * '+ 8' for 'set-map(' (same for del-map)
1949 * '- 9' for 'set-map(' + trailing ')' (same for del-map)
1950 */
1951 rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9);
1952
1953 if (rule->from == ACT_F_HTTP_REQ) {
1954 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001955 if (px->cap & PR_CAP_FE)
1956 cap |= SMP_VAL_FE_HRQ_HDR;
1957 if (px->cap & PR_CAP_BE)
1958 cap |= SMP_VAL_BE_HRQ_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001959 }
1960 else{
1961 px->conf.args.ctx = ARGC_HRS;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001962 if (px->cap & PR_CAP_FE)
1963 cap |= SMP_VAL_FE_HRS_HDR;
1964 if (px->cap & PR_CAP_BE)
1965 cap |= SMP_VAL_BE_HRS_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001966 }
1967
1968 /* key pattern */
1969 LIST_INIT(&rule->arg.map.key);
Christopher Faulet1337b322020-01-14 14:50:55 +01001970 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err)) {
1971 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001972 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001973 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001974
Christopher Faulet046cf442019-12-17 15:45:23 +01001975 if (rule->action == 1) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001976 /* value pattern for set-map only */
1977 cur_arg++;
1978 LIST_INIT(&rule->arg.map.value);
Christopher Faulet1337b322020-01-14 14:50:55 +01001979 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err)) {
1980 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001981 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001982 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001983 }
1984
1985 free(px->conf.lfs_file);
1986 px->conf.lfs_file = strdup(px->conf.args.file);
1987 px->conf.lfs_line = px->conf.args.line;
1988
1989 *orig_arg = cur_arg + 1;
1990 return ACT_RET_PRS_OK;
1991}
1992
Christopher Fauletac98d812019-12-18 09:20:16 +01001993/* This function executes a track-sc* actions. On success, it returns
1994 * ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1995 */
1996static enum act_return http_action_track_sc(struct act_rule *rule, struct proxy *px,
1997 struct session *sess, struct stream *s, int flags)
1998{
1999 struct stktable *t;
2000 struct stksess *ts;
2001 struct stktable_key *key;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002002 void *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *ptr6;
Christopher Fauletac98d812019-12-18 09:20:16 +01002003 int opt;
2004
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002005 ptr1 = ptr2 = ptr3 = ptr4 = ptr5 = ptr6 = NULL;
Christopher Fauletac98d812019-12-18 09:20:16 +01002006 opt = ((rule->from == ACT_F_HTTP_REQ) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES) | SMP_OPT_FINAL;
2007
2008 t = rule->arg.trk_ctr.table.t;
Emeric Brun362d25e2021-03-10 16:58:03 +01002009
2010 if (stkctr_entry(&s->stkctr[rule->action]))
2011 goto end;
2012
Christopher Fauletac98d812019-12-18 09:20:16 +01002013 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, NULL);
2014
2015 if (!key)
2016 goto end;
2017 ts = stktable_get_entry(t, key);
2018 if (!ts)
2019 goto end;
2020
2021 stream_track_stkctr(&s->stkctr[rule->action], t, ts);
2022
2023 /* let's count a new HTTP request as it's the first time we do it */
2024 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2025 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2026
2027 /* When the client triggers a 4xx from the server, it's most often due
2028 * to a missing object or permission. These events should be tracked
2029 * because if they happen often, it may indicate a brute force or a
2030 * vulnerability scan. Normally this is done when receiving the response
2031 * but here we're tracking after this ought to have been done so we have
2032 * to do it on purpose.
2033 */
2034 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 400) < 100) {
2035 ptr3 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2036 ptr4 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2037 }
2038
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002039 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 500) < 100 &&
2040 s->txn->status != 501 && s->txn->status != 505) {
2041 ptr5 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
2042 ptr6 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
2043 }
2044
2045 if (ptr1 || ptr2 || ptr3 || ptr4 || ptr5 || ptr6) {
Christopher Fauletac98d812019-12-18 09:20:16 +01002046 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2047
2048 if (ptr1)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002049 stktable_data_cast(ptr1, std_t_uint)++;
Christopher Fauletac98d812019-12-18 09:20:16 +01002050 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002051 update_freq_ctr_period(&stktable_data_cast(ptr2, std_t_frqp),
Christopher Fauletac98d812019-12-18 09:20:16 +01002052 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2053 if (ptr3)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002054 stktable_data_cast(ptr3, std_t_uint)++;
Christopher Fauletac98d812019-12-18 09:20:16 +01002055 if (ptr4)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002056 update_freq_ctr_period(&stktable_data_cast(ptr4, std_t_frqp),
Christopher Fauletac98d812019-12-18 09:20:16 +01002057 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002058 if (ptr5)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002059 stktable_data_cast(ptr5, std_t_uint)++;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002060 if (ptr6)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002061 update_freq_ctr_period(&stktable_data_cast(ptr6, std_t_frqp),
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002062 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u, 1);
Christopher Fauletac98d812019-12-18 09:20:16 +01002063
2064 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2065
2066 /* If data was modified, we need to touch to re-schedule sync */
2067 stktable_touch_local(t, ts, 0);
2068 }
2069
2070 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT);
2071 if (sess->fe != s->be)
2072 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND);
2073
2074 end:
2075 return ACT_RET_CONT;
2076}
Christopher Faulet81e20172019-12-12 16:40:30 +01002077
Christopher Faulet2eb53962020-01-14 14:47:34 +01002078static void release_http_track_sc(struct act_rule *rule)
2079{
2080 release_sample_expr(rule->arg.trk_ctr.expr);
2081}
2082
Christopher Faulet81e20172019-12-12 16:40:30 +01002083/* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success,
2084 * ACT_RET_PRS_ERR on error.
2085 */
2086static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px,
2087 struct act_rule *rule, char **err)
2088{
2089 struct sample_expr *expr;
2090 unsigned int where;
2091 unsigned int tsc_num;
2092 const char *tsc_num_str;
2093 int cur_arg;
2094
2095 tsc_num_str = &args[*orig_arg-1][8];
2096 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1)
2097 return ACT_RET_PRS_ERR;
2098
2099 cur_arg = *orig_arg;
2100 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002101 err, &px->conf.args, NULL);
Christopher Faulet81e20172019-12-12 16:40:30 +01002102 if (!expr)
2103 return ACT_RET_PRS_ERR;
2104
2105 where = 0;
2106 if (px->cap & PR_CAP_FE)
2107 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
2108 if (px->cap & PR_CAP_BE)
2109 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
2110
2111 if (!(expr->fetch->val & where)) {
2112 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here",
2113 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01002114 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002115 return ACT_RET_PRS_ERR;
2116 }
2117
2118 if (strcmp(args[cur_arg], "table") == 0) {
2119 cur_arg++;
2120 if (!*args[cur_arg]) {
2121 memprintf(err, "missing table name");
Christopher Faulet1337b322020-01-14 14:50:55 +01002122 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002123 return ACT_RET_PRS_ERR;
2124 }
2125
2126 /* we copy the table name for now, it will be resolved later */
2127 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
2128 cur_arg++;
2129 }
2130
Christopher Fauletac98d812019-12-18 09:20:16 +01002131 rule->action = tsc_num;
Christopher Faulet81e20172019-12-12 16:40:30 +01002132 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +01002133 rule->action_ptr = http_action_track_sc;
Christopher Faulet2eb53962020-01-14 14:47:34 +01002134 rule->release_ptr = release_http_track_sc;
Christopher Faulet81e20172019-12-12 16:40:30 +01002135 rule->check_ptr = check_trk_action;
2136
2137 *orig_arg = cur_arg;
2138 return ACT_RET_PRS_OK;
2139}
2140
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002141static enum act_return action_timeout_set_stream_timeout(struct act_rule *rule,
2142 struct proxy *px,
2143 struct session *sess,
2144 struct stream *s,
2145 int flags)
2146{
2147 struct sample *key;
2148
2149 if (rule->arg.timeout.expr) {
2150 key = sample_fetch_as_type(px, sess, s, SMP_OPT_FINAL, rule->arg.timeout.expr, SMP_T_SINT);
2151 if (!key)
2152 return ACT_RET_CONT;
2153
2154 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(key->data.u.sint));
2155 }
2156 else {
2157 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(rule->arg.timeout.value));
2158 }
2159
2160 return ACT_RET_CONT;
2161}
2162
2163/* Parse a "set-timeout" action. Returns ACT_RET_PRS_ERR if parsing error.
2164 */
2165static enum act_parse_ret parse_http_set_timeout(const char **args,
2166 int *orig_arg,
2167 struct proxy *px,
2168 struct act_rule *rule, char **err)
2169{
2170 int cur_arg;
2171
2172 rule->action = ACT_CUSTOM;
2173 rule->action_ptr = action_timeout_set_stream_timeout;
2174 rule->release_ptr = release_timeout_action;
2175
2176 cur_arg = *orig_arg;
2177 if (!*args[cur_arg] || !*args[cur_arg + 1]) {
2178 memprintf(err, "expects exactly 2 arguments");
2179 return ACT_RET_PRS_ERR;
2180 }
2181
2182 if (!(px->cap & PR_CAP_BE)) {
2183 memprintf(err, "proxy '%s' has no backend capability", px->id);
2184 return ACT_RET_PRS_ERR;
2185 }
2186
2187 if (cfg_parse_rule_set_timeout(args, cur_arg,
2188 &rule->arg.timeout.value,
2189 &rule->arg.timeout.type,
2190 &rule->arg.timeout.expr,
2191 err,
2192 px->conf.args.file,
2193 px->conf.args.line, &px->conf.args) == -1) {
2194 return ACT_RET_PRS_ERR;
2195 }
2196
2197 *orig_arg = cur_arg + 2;
2198
2199 return ACT_RET_PRS_OK;
2200}
2201
Christopher Faulet46f95542019-12-20 10:07:22 +01002202/* This function executes a strict-mode actions. On success, it always returns
2203 * ACT_RET_CONT
2204 */
2205static enum act_return http_action_strict_mode(struct act_rule *rule, struct proxy *px,
2206 struct session *sess, struct stream *s, int flags)
2207{
2208 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
2209
2210 if (rule->action == 0) // strict-mode on
2211 msg->flags &= ~HTTP_MSGF_SOFT_RW;
2212 else // strict-mode off
2213 msg->flags |= HTTP_MSGF_SOFT_RW;
2214 return ACT_RET_CONT;
2215}
2216
2217/* Parse a "strict-mode" action. It returns ACT_RET_PRS_OK on success,
2218 * ACT_RET_PRS_ERR on error.
2219 */
2220static enum act_parse_ret parse_http_strict_mode(const char **args, int *orig_arg, struct proxy *px,
2221 struct act_rule *rule, char **err)
2222{
2223 int cur_arg;
2224
Christopher Faulet46f95542019-12-20 10:07:22 +01002225 cur_arg = *orig_arg;
2226 if (!*args[cur_arg]) {
2227 memprintf(err, "expects exactly 1 arguments");
2228 return ACT_RET_PRS_ERR;
2229 }
2230
2231 if (strcasecmp(args[cur_arg], "on") == 0)
2232 rule->action = 0; // strict-mode on
2233 else if (strcasecmp(args[cur_arg], "off") == 0)
2234 rule->action = 1; // strict-mode off
2235 else {
2236 memprintf(err, "Unexpected value '%s'. Only 'on' and 'off' are supported", args[cur_arg]);
2237 return ACT_RET_PRS_ERR;
2238 }
2239 rule->action_ptr = http_action_strict_mode;
2240
2241 *orig_arg = cur_arg + 1;
2242 return ACT_RET_PRS_OK;
2243}
2244
Christopher Faulet24231ab2020-01-24 17:44:23 +01002245/* This function executes a return action. It builds an HTX message from an
2246 * errorfile, an raw file or a log-format string, depending on <.action>
2247 * value. On success, it returns ACT_RET_ABRT. If an error occurs ACT_RET_ERR is
2248 * returned.
2249 */
2250static enum act_return http_action_return(struct act_rule *rule, struct proxy *px,
2251 struct session *sess, struct stream *s, int flags)
2252{
2253 struct channel *req = &s->req;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002254
Christopher Faulet2d36df22021-02-19 11:41:01 +01002255 s->txn->status = rule->arg.http_reply->status;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002256 if (http_reply_message(s, rule->arg.http_reply) == -1)
2257 return ACT_RET_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002258
Christopher Faulet24231ab2020-01-24 17:44:23 +01002259 if (rule->from == ACT_F_HTTP_REQ) {
2260 /* let's log the request time */
2261 s->logs.tv_request = now;
2262 req->analysers &= AN_REQ_FLT_END;
2263
2264 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002265 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002266 }
2267
2268 if (!(s->flags & SF_ERR_MASK))
2269 s->flags |= SF_ERR_LOCAL;
2270 if (!(s->flags & SF_FINST_MASK))
2271 s->flags |= ((rule->from == ACT_F_HTTP_REQ) ? SF_FINST_R : SF_FINST_H);
2272
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002273 return ACT_RET_ABRT;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002274}
2275
Christopher Faulet24231ab2020-01-24 17:44:23 +01002276/* Parse a "return" action. It returns ACT_RET_PRS_OK on success,
Christopher Faulet47e791e2020-05-13 14:36:55 +02002277 * ACT_RET_PRS_ERR on error. It relies on http_parse_http_reply() to set
2278 * <.arg.http_reply>.
Christopher Faulet24231ab2020-01-24 17:44:23 +01002279 */
2280static enum act_parse_ret parse_http_return(const char **args, int *orig_arg, struct proxy *px,
2281 struct act_rule *rule, char **err)
2282{
Christopher Faulet47e791e2020-05-13 14:36:55 +02002283 /* Prepare parsing of log-format strings */
2284 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
2285 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, 200, err);
2286 if (!rule->arg.http_reply)
2287 return ACT_RET_PRS_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002288
Christopher Fauletba946bf2020-05-13 08:50:07 +02002289 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet5ff0c642020-05-12 18:33:37 +02002290 rule->action = ACT_CUSTOM;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002291 rule->check_ptr = check_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002292 rule->action_ptr = http_action_return;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002293 rule->release_ptr = release_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002294 return ACT_RET_PRS_OK;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002295}
2296
Christopher Faulet021a8e42021-03-29 10:46:38 +02002297
2298
2299/* This function executes a wait-for-body action. It waits for the message
2300 * payload for a max configured time (.arg.p[0]) and eventually for only first
2301 * <arg.p[1]> bytes (0 means no limit). It relies on http_wait_for_msg_body()
2302 * function. it returns ACT_RET_CONT when conditions are met to stop to wait.
2303 * Otherwise ACT_RET_YIELD is returned to wait for more data. ACT_RET_INV is
2304 * returned if a parsing error is raised by lower level and ACT_RET_ERR if an
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05002305 * internal error occurred. Finally ACT_RET_ABRT is returned when a timeout
2306 * occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02002307 */
2308static enum act_return http_action_wait_for_body(struct act_rule *rule, struct proxy *px,
2309 struct session *sess, struct stream *s, int flags)
2310{
2311 struct channel *chn = ((rule->from == ACT_F_HTTP_REQ) ? &s->req : &s->res);
2312 unsigned int time = (uintptr_t)rule->arg.act.p[0];
2313 unsigned int bytes = (uintptr_t)rule->arg.act.p[1];
2314
2315 switch (http_wait_for_msg_body(s, chn, time, bytes)) {
2316 case HTTP_RULE_RES_CONT:
2317 return ACT_RET_CONT;
2318 case HTTP_RULE_RES_YIELD:
2319 return ACT_RET_YIELD;
2320 case HTTP_RULE_RES_BADREQ:
2321 return ACT_RET_INV;
2322 case HTTP_RULE_RES_ERROR:
2323 return ACT_RET_ERR;
2324 case HTTP_RULE_RES_ABRT:
2325 return ACT_RET_ABRT;
2326 default:
2327 return ACT_RET_ERR;
2328 }
2329}
2330
2331/* Parse a "wait-for-body" action. It returns ACT_RET_PRS_OK on success,
2332 * ACT_RET_PRS_ERR on error.
2333 */
2334static enum act_parse_ret parse_http_wait_for_body(const char **args, int *orig_arg, struct proxy *px,
2335 struct act_rule *rule, char **err)
2336{
2337 int cur_arg;
2338 unsigned int time, bytes;
2339 const char *res;
2340
2341 cur_arg = *orig_arg;
2342 if (!*args[cur_arg]) {
2343 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2344 return ACT_RET_PRS_ERR;
2345 }
2346
2347 time = UINT_MAX; /* To be sure it is set */
2348 bytes = 0; /* Default value, wait all the body */
2349 while (*(args[cur_arg])) {
2350 if (strcmp(args[cur_arg], "time") == 0) {
2351 if (!*args[cur_arg + 1]) {
2352 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2353 return ACT_RET_PRS_ERR;
2354 }
2355 res = parse_time_err(args[cur_arg+1], &time, TIME_UNIT_MS);
2356 if (res == PARSE_TIME_OVER) {
2357 memprintf(err, "time overflow (maximum value is 2147483647 ms or ~24.8 days)");
2358 return ACT_RET_PRS_ERR;
2359 }
2360 if (res == PARSE_TIME_UNDER) {
2361 memprintf(err, "time underflow (minimum non-null value is 1 ms)");
2362 return ACT_RET_PRS_ERR;
2363 }
2364 if (res) {
2365 memprintf(err, "unexpected character '%c'", *res);
2366 return ACT_RET_PRS_ERR;
2367 }
2368 cur_arg++;
2369 }
2370 else if (strcmp(args[cur_arg], "at-least") == 0) {
2371 if (!*args[cur_arg + 1]) {
2372 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2373 return ACT_RET_PRS_ERR;
2374 }
2375 res = parse_size_err(args[cur_arg+1], &bytes);
2376 if (res) {
2377 memprintf(err, "unexpected character '%c'", *res);
2378 return ACT_RET_PRS_ERR;
2379 }
2380 cur_arg++;
2381 }
2382 else
2383 break;
2384 cur_arg++;
2385 }
2386
2387 if (time == UINT_MAX) {
2388 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2389 return ACT_RET_PRS_ERR;
2390 }
2391
2392 rule->arg.act.p[0] = (void *)(uintptr_t)time;
2393 rule->arg.act.p[1] = (void *)(uintptr_t)bytes;
2394
2395 *orig_arg = cur_arg;
2396
2397 rule->action = ACT_CUSTOM;
2398 rule->action_ptr = http_action_wait_for_body;
2399 return ACT_RET_PRS_OK;
2400}
2401
Willy Tarreau79e57332018-10-02 16:01:16 +02002402/************************************************************************/
2403/* All supported http-request action keywords must be declared here. */
2404/************************************************************************/
2405
2406static struct action_kw_list http_req_actions = {
2407 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002408 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002409 { "add-header", parse_http_set_header, 0 },
2410 { "allow", parse_http_allow, 0 },
2411 { "auth", parse_http_auth, 0 },
2412 { "capture", parse_http_req_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002413 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002414 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002415 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002416 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002417 { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 },
2418 { "early-hint", parse_http_set_header, 0 },
Amaury Denoyellea9e639a2021-05-06 15:50:12 +02002419 { "normalize-uri", parse_http_normalize_uri, KWF_EXPERIMENTAL },
Christopher Faulet81e20172019-12-12 16:40:30 +01002420 { "redirect", parse_http_redirect, 0 },
2421 { "reject", parse_http_action_reject, 0 },
2422 { "replace-header", parse_http_replace_header, 0 },
2423 { "replace-path", parse_replace_uri, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002424 { "replace-pathq", parse_replace_uri, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002425 { "replace-uri", parse_replace_uri, 0 },
2426 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002427 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002428 { "set-header", parse_http_set_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002429 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002430 { "set-method", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002431 { "set-path", parse_set_req_line, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002432 { "set-pathq", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002433 { "set-query", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002434 { "set-uri", parse_set_req_line, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002435 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002436 { "tarpit", parse_http_deny, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002437 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002438 { "set-timeout", parse_http_set_timeout, 0 },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002439 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002440 { NULL, NULL }
2441 }
2442};
2443
Willy Tarreau0108d902018-11-25 19:14:37 +01002444INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2445
Willy Tarreau79e57332018-10-02 16:01:16 +02002446static struct action_kw_list http_res_actions = {
2447 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002448 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002449 { "add-header", parse_http_set_header, 0 },
2450 { "allow", parse_http_allow, 0 },
2451 { "capture", parse_http_res_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002452 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002453 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002454 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002455 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002456 { "redirect", parse_http_redirect, 0 },
2457 { "replace-header", parse_http_replace_header, 0 },
2458 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002459 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002460 { "set-header", parse_http_set_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002461 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002462 { "set-status", parse_http_set_status, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002463 { "strict-mode", parse_http_strict_mode, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002464 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002465 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002466 { NULL, NULL }
2467 }
2468};
2469
Willy Tarreau0108d902018-11-25 19:14:37 +01002470INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +02002471
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002472static struct action_kw_list http_after_res_actions = {
2473 .kw = {
2474 { "add-header", parse_http_set_header, 0 },
2475 { "allow", parse_http_allow, 0 },
Christopher Fauletba8f0632021-12-06 08:43:22 +01002476 { "capture", parse_http_res_capture, 0 },
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002477 { "del-header", parse_http_del_header, 0 },
2478 { "replace-header", parse_http_replace_header, 0 },
2479 { "replace-value", parse_http_replace_header, 0 },
2480 { "set-header", parse_http_set_header, 0 },
2481 { "set-status", parse_http_set_status, 0 },
2482 { "strict-mode", parse_http_strict_mode, 0 },
2483 { NULL, NULL }
2484 }
2485};
2486
2487INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_actions);
2488
Willy Tarreau79e57332018-10-02 16:01:16 +02002489/*
2490 * Local variables:
2491 * c-indent-level: 8
2492 * c-basic-offset: 8
2493 * End:
2494 */