blob: f35fadfb56b83ee1694924adfc5694017500a5ac [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 Faulet114e7592022-04-08 10:44:21 +0200541 if (!istlen(uri))
542 goto leave;
543
Christopher Faulet96bff762019-12-17 13:46:18 +0100544 if (!regex_exec_match2(rule->arg.http.re, uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
Willy Tarreau33810222019-06-12 17:44:02 +0200545 goto leave;
546
Christopher Faulet96bff762019-12-17 13:46:18 +0100547 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200548
549 /* note: uri.ptr doesn't need to be zero-terminated because it will
550 * only be used to pick pmatch references.
551 */
552 len = exp_replace(output->area, output->size, uri.ptr, replace->area, pmatch);
553 if (len == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100554 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200555
Christopher Faulet2c22a692019-12-18 15:39:56 +0100556 if (http_req_replace_stline(rule->action, output->area, len, px, s) == -1)
Christopher Faulete00d06c2019-12-16 17:18:42 +0100557 goto fail_rewrite;
Willy Tarreau33810222019-06-12 17:44:02 +0200558
Christopher Faulete00d06c2019-12-16 17:18:42 +0100559 leave:
Willy Tarreau33810222019-06-12 17:44:02 +0200560 free_trash_chunk(output);
561 free_trash_chunk(replace);
562 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +0100563
564 fail_alloc:
565 if (!(s->flags & SF_ERR_MASK))
566 s->flags |= SF_ERR_RESOURCE;
567 ret = ACT_RET_ERR;
568 goto leave;
569
570 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +0200571 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100572 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200573 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100574 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200575 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100576 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200577 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100578
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100579 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulete00d06c2019-12-16 17:18:42 +0100580 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100581 if (!(s->flags & SF_ERR_MASK))
582 s->flags |= SF_ERR_PRXCOND;
583 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100584 goto leave;
Willy Tarreau33810222019-06-12 17:44:02 +0200585}
586
Christopher Faulet312294f2020-09-02 17:17:44 +0200587/* parse a "replace-uri", "replace-path" or "replace-pathq"
588 * http-request action.
Willy Tarreau33810222019-06-12 17:44:02 +0200589 * This action takes 2 arguments (a regex and a replacement format string).
Christopher Faulet2c22a692019-12-18 15:39:56 +0100590 * The resulting rule makes use of <.action> to store the action (1/3 for now),
Christopher Faulet96bff762019-12-17 13:46:18 +0100591 * <http.re> to store the compiled regex, and <http.fmt> to store the log-format
Willy Tarreau33810222019-06-12 17:44:02 +0200592 * list head. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
593 */
594static enum act_parse_ret parse_replace_uri(const char **args, int *orig_arg, struct proxy *px,
595 struct act_rule *rule, char **err)
596{
597 int cur_arg = *orig_arg;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +0200598 int cap = 0;
Willy Tarreau33810222019-06-12 17:44:02 +0200599 char *error = NULL;
600
Christopher Faulet312294f2020-09-02 17:17:44 +0200601 switch (args[0][8]) {
602 case 'p':
603 if (args[0][12] == 'q')
604 rule->action = 4; // replace-pathq, same as set-pathq
605 else
606 rule->action = 1; // replace-path, same as set-path
607 break;
608 case 'u':
Christopher Faulet2c22a692019-12-18 15:39:56 +0100609 rule->action = 3; // replace-uri, same as set-uri
Christopher Faulet312294f2020-09-02 17:17:44 +0200610 break;
611 default:
612 memprintf(err, "internal error: unhandled action '%s'", args[0]);
613 return ACT_RET_PRS_ERR;
614 }
Willy Tarreau262c3f12019-12-17 06:52:51 +0100615
Willy Tarreau33810222019-06-12 17:44:02 +0200616 rule->action_ptr = http_action_replace_uri;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100617 rule->release_ptr = release_http_action;
Willy Tarreau33810222019-06-12 17:44:02 +0200618
619 if (!*args[cur_arg] || !*args[cur_arg+1] ||
620 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
621 memprintf(err, "expects exactly 2 arguments <match-regex> and <replace-format>");
622 return ACT_RET_PRS_ERR;
623 }
624
Christopher Faulet96bff762019-12-17 13:46:18 +0100625 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, &error))) {
Willy Tarreau33810222019-06-12 17:44:02 +0200626 memprintf(err, "failed to parse the regex : %s", error);
627 free(error);
628 return ACT_RET_PRS_ERR;
629 }
630
Christopher Faulet96bff762019-12-17 13:46:18 +0100631 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau33810222019-06-12 17:44:02 +0200632 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +0200633 if (px->cap & PR_CAP_FE)
634 cap |= SMP_VAL_FE_HRQ_HDR;
635 if (px->cap & PR_CAP_BE)
636 cap |= SMP_VAL_BE_HRQ_HDR;
637 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 +0100638 regex_free(rule->arg.http.re);
Willy Tarreau33810222019-06-12 17:44:02 +0200639 return ACT_RET_PRS_ERR;
640 }
641
642 (*orig_arg) += 2;
643 return ACT_RET_PRS_OK;
644}
645
Willy Tarreau79e57332018-10-02 16:01:16 +0200646/* This function is just a compliant action wrapper for "set-status". */
647static enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
648 struct session *sess, struct stream *s, int flags)
649{
Christopher Faulet96bff762019-12-17 13:46:18 +0100650 if (http_res_set_status(rule->arg.http.i, rule->arg.http.str, s) == -1) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200651 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100652 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200653 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +0100654 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200655 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100656 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200657 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulete00d06c2019-12-16 17:18:42 +0100658
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100659 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100660 if (!(s->flags & SF_ERR_MASK))
661 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet692a6c22020-02-07 10:22:31 +0100662 return ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +0100663 }
Christopher Faulete00d06c2019-12-16 17:18:42 +0100664 }
665
Willy Tarreau79e57332018-10-02 16:01:16 +0200666 return ACT_RET_CONT;
667}
668
669/* parse set-status action:
670 * This action accepts a single argument of type int representing
671 * an http status code. It returns ACT_RET_PRS_OK on success,
672 * ACT_RET_PRS_ERR on error.
673 */
674static enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
675 struct act_rule *rule, char **err)
676{
677 char *error;
678
679 rule->action = ACT_CUSTOM;
680 rule->action_ptr = action_http_set_status;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100681 rule->release_ptr = release_http_action;
Willy Tarreau79e57332018-10-02 16:01:16 +0200682
683 /* Check if an argument is available */
684 if (!*args[*orig_arg]) {
685 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
686 return ACT_RET_PRS_ERR;
687 }
688
689 /* convert status code as integer */
Christopher Faulet96bff762019-12-17 13:46:18 +0100690 rule->arg.http.i = strtol(args[*orig_arg], &error, 10);
691 if (*error != '\0' || rule->arg.http.i < 100 || rule->arg.http.i > 999) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200692 memprintf(err, "expects an integer status code between 100 and 999");
693 return ACT_RET_PRS_ERR;
694 }
695
696 (*orig_arg)++;
697
698 /* set custom reason string */
Christopher Faulet96bff762019-12-17 13:46:18 +0100699 rule->arg.http.str = ist(NULL); // If null, we use the default reason for the status code.
Willy Tarreau79e57332018-10-02 16:01:16 +0200700 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
701 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
702 (*orig_arg)++;
Tim Duesterhusf4f6c0f2022-03-15 13:11:08 +0100703 rule->arg.http.str = ist(strdup(args[*orig_arg]));
Willy Tarreau79e57332018-10-02 16:01:16 +0200704 (*orig_arg)++;
705 }
706
Christopher Fauletc20b3712020-01-27 15:51:56 +0100707 LIST_INIT(&rule->arg.http.fmt);
Willy Tarreau79e57332018-10-02 16:01:16 +0200708 return ACT_RET_PRS_OK;
709}
710
711/* This function executes the "reject" HTTP action. It clears the request and
712 * response buffer without sending any response. It can be useful as an HTTP
713 * alternative to the silent-drop action to defend against DoS attacks, and may
714 * also be used with HTTP/2 to close a connection instead of just a stream.
715 * The txn status is unchanged, indicating no response was sent. The termination
Christopher Faulet90d22a82020-03-06 11:18:39 +0100716 * flags will indicate "PR". It always returns ACT_RET_ABRT.
Willy Tarreau79e57332018-10-02 16:01:16 +0200717 */
718static enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
719 struct session *sess, struct stream *s, int flags)
720{
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100721 si_must_kill_conn(chn_prod(&s->req));
Willy Tarreau79e57332018-10-02 16:01:16 +0200722 channel_abort(&s->req);
723 channel_abort(&s->res);
Christopher Fauletd4a824e2020-03-06 15:07:09 +0100724 s->req.analysers &= AN_REQ_FLT_END;
725 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau79e57332018-10-02 16:01:16 +0200726
Willy Tarreau4781b152021-04-06 13:53:36 +0200727 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
728 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200729 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200730 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Willy Tarreau79e57332018-10-02 16:01:16 +0200731
732 if (!(s->flags & SF_ERR_MASK))
733 s->flags |= SF_ERR_PRXCOND;
734 if (!(s->flags & SF_FINST_MASK))
735 s->flags |= SF_FINST_R;
736
Christopher Faulet90d22a82020-03-06 11:18:39 +0100737 return ACT_RET_ABRT;
Willy Tarreau79e57332018-10-02 16:01:16 +0200738}
739
740/* parse the "reject" action:
741 * This action takes no argument and returns ACT_RET_PRS_OK on success,
742 * ACT_RET_PRS_ERR on error.
743 */
744static enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
745 struct act_rule *rule, char **err)
746{
747 rule->action = ACT_CUSTOM;
748 rule->action_ptr = http_action_reject;
749 return ACT_RET_PRS_OK;
750}
751
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200752/* This function executes the "disable-l7-retry" HTTP action.
753 * It disables L7 retries (all retry except for a connection failure). This
754 * can be useful for example to avoid retrying on POST requests.
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200755 * It just removes the L7 retry flag on the HTTP transaction, and always
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200756 * return ACT_RET_CONT;
757 */
758static enum act_return http_req_disable_l7_retry(struct act_rule *rule, struct proxy *px,
759 struct session *sess, struct stream *s, int flags)
760{
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200761 /* In theory, the TX_L7_RETRY flags isn't set at this point, but
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200762 * let's be future-proof and remove it anyway.
763 */
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200764 s->txn->flags &= ~TX_L7_RETRY;
765 s->txn->flags |= TX_D_L7_RETRY;
Olivier Houchard602bf7d2019-05-10 13:59:15 +0200766 return ACT_RET_CONT;
767}
768
769/* parse the "disable-l7-retry" action:
770 * This action takes no argument and returns ACT_RET_PRS_OK on success,
771 * ACT_RET_PRS_ERR on error.
772 */
773static enum act_parse_ret parse_http_req_disable_l7_retry(const char **args,
774 int *orig_args, struct proxy *px,
775 struct act_rule *rule, char **err)
776{
777 rule->action = ACT_CUSTOM;
778 rule->action_ptr = http_req_disable_l7_retry;
779 return ACT_RET_PRS_OK;
780}
781
Willy Tarreau79e57332018-10-02 16:01:16 +0200782/* This function executes the "capture" action. It executes a fetch expression,
783 * turns the result into a string and puts it in a capture slot. It always
784 * returns 1. If an error occurs the action is cancelled, but the rule
785 * processing continues.
786 */
787static enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
788 struct session *sess, struct stream *s, int flags)
789{
790 struct sample *key;
791 struct cap_hdr *h = rule->arg.cap.hdr;
792 char **cap = s->req_cap;
793 int len;
794
795 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
796 if (!key)
797 return ACT_RET_CONT;
798
799 if (cap[h->index] == NULL)
800 cap[h->index] = pool_alloc(h->pool);
801
802 if (cap[h->index] == NULL) /* no more capture memory */
803 return ACT_RET_CONT;
804
805 len = key->data.u.str.data;
806 if (len > h->len)
807 len = h->len;
808
809 memcpy(cap[h->index], key->data.u.str.area, len);
810 cap[h->index][len] = 0;
811 return ACT_RET_CONT;
812}
813
814/* This function executes the "capture" action and store the result in a
815 * capture slot if exists. It executes a fetch expression, turns the result
816 * into a string and puts it in a capture slot. It always returns 1. If an
817 * error occurs the action is cancelled, but the rule processing continues.
818 */
819static enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
820 struct session *sess, struct stream *s, int flags)
821{
822 struct sample *key;
823 struct cap_hdr *h;
824 char **cap = s->req_cap;
825 struct proxy *fe = strm_fe(s);
826 int len;
827 int i;
828
829 /* Look for the original configuration. */
830 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
831 h != NULL && i != rule->arg.capid.idx ;
832 i--, h = h->next);
833 if (!h)
834 return ACT_RET_CONT;
835
836 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
837 if (!key)
838 return ACT_RET_CONT;
839
840 if (cap[h->index] == NULL)
841 cap[h->index] = pool_alloc(h->pool);
842
843 if (cap[h->index] == NULL) /* no more capture memory */
844 return ACT_RET_CONT;
845
846 len = key->data.u.str.data;
847 if (len > h->len)
848 len = h->len;
849
850 memcpy(cap[h->index], key->data.u.str.area, len);
851 cap[h->index][len] = 0;
852 return ACT_RET_CONT;
853}
854
855/* Check an "http-request capture" action.
856 *
857 * The function returns 1 in success case, otherwise, it returns 0 and err is
858 * filled.
859 */
860static int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
861{
862 if (rule->action_ptr != http_action_req_capture_by_id)
863 return 1;
864
Baptiste Assmann19a69b32020-01-16 14:34:22 +0100865 /* capture slots can only be declared in frontends, so we can't check their
866 * existence in backends at configuration parsing step
867 */
868 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_req_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200869 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
870 rule->arg.capid.idx);
871 return 0;
872 }
873
874 return 1;
875}
876
Christopher Faulet2eb53962020-01-14 14:47:34 +0100877/* Release memory allocate by an http capture action */
878static void release_http_capture(struct act_rule *rule)
879{
880 if (rule->action_ptr == http_action_req_capture)
881 release_sample_expr(rule->arg.cap.expr);
882 else
883 release_sample_expr(rule->arg.capid.expr);
884}
885
Willy Tarreau79e57332018-10-02 16:01:16 +0200886/* parse an "http-request capture" action. It takes a single argument which is
887 * a sample fetch expression. It stores the expression into arg->act.p[0] and
888 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
889 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
890 */
891static enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
892 struct act_rule *rule, char **err)
893{
894 struct sample_expr *expr;
895 struct cap_hdr *hdr;
896 int cur_arg;
897 int len = 0;
898
899 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
900 if (strcmp(args[cur_arg], "if") == 0 ||
901 strcmp(args[cur_arg], "unless") == 0)
902 break;
903
904 if (cur_arg < *orig_arg + 3) {
905 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
906 return ACT_RET_PRS_ERR;
907 }
908
909 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100910 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 +0200911 if (!expr)
912 return ACT_RET_PRS_ERR;
913
914 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
915 memprintf(err,
916 "fetch method '%s' extracts information from '%s', none of which is available here",
917 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +0100918 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200919 return ACT_RET_PRS_ERR;
920 }
921
922 if (!args[cur_arg] || !*args[cur_arg]) {
923 memprintf(err, "expects 'len or 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +0100924 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200925 return ACT_RET_PRS_ERR;
926 }
927
928 if (strcmp(args[cur_arg], "len") == 0) {
929 cur_arg++;
930
931 if (!(px->cap & PR_CAP_FE)) {
932 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Christopher Faulet1337b322020-01-14 14:50:55 +0100933 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200934 return ACT_RET_PRS_ERR;
935 }
936
937 px->conf.args.ctx = ARGC_CAP;
938
939 if (!args[cur_arg]) {
940 memprintf(err, "missing length value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100941 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200942 return ACT_RET_PRS_ERR;
943 }
944 /* we copy the table name for now, it will be resolved later */
945 len = atoi(args[cur_arg]);
946 if (len <= 0) {
947 memprintf(err, "length must be > 0");
Christopher Faulet1337b322020-01-14 14:50:55 +0100948 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200949 return ACT_RET_PRS_ERR;
950 }
951 cur_arg++;
952
Willy Tarreau79e57332018-10-02 16:01:16 +0200953 hdr = calloc(1, sizeof(*hdr));
Remi Tricot-Le Bretona4bf8a02021-05-12 17:54:17 +0200954 if (!hdr) {
955 memprintf(err, "out of memory");
956 release_sample_expr(expr);
957 return ACT_RET_PRS_ERR;
958 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200959 hdr->next = px->req_cap;
960 hdr->name = NULL; /* not a header capture */
961 hdr->namelen = 0;
962 hdr->len = len;
963 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
964 hdr->index = px->nb_req_cap++;
965
966 px->req_cap = hdr;
967 px->to_log |= LW_REQHDR;
968
969 rule->action = ACT_CUSTOM;
970 rule->action_ptr = http_action_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +0100971 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +0200972 rule->arg.cap.expr = expr;
973 rule->arg.cap.hdr = hdr;
974 }
975
976 else if (strcmp(args[cur_arg], "id") == 0) {
977 int id;
978 char *error;
979
980 cur_arg++;
981
982 if (!args[cur_arg]) {
983 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +0100984 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200985 return ACT_RET_PRS_ERR;
986 }
987
988 id = strtol(args[cur_arg], &error, 10);
989 if (*error != '\0') {
990 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +0100991 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200992 return ACT_RET_PRS_ERR;
993 }
994 cur_arg++;
995
996 px->conf.args.ctx = ARGC_CAP;
997
998 rule->action = ACT_CUSTOM;
999 rule->action_ptr = http_action_req_capture_by_id;
1000 rule->check_ptr = check_http_req_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001001 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +02001002 rule->arg.capid.expr = expr;
1003 rule->arg.capid.idx = id;
1004 }
1005
1006 else {
1007 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001008 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001009 return ACT_RET_PRS_ERR;
1010 }
1011
1012 *orig_arg = cur_arg;
1013 return ACT_RET_PRS_OK;
1014}
1015
1016/* This function executes the "capture" action and store the result in a
1017 * capture slot if exists. It executes a fetch expression, turns the result
1018 * into a string and puts it in a capture slot. It always returns 1. If an
1019 * error occurs the action is cancelled, but the rule processing continues.
1020 */
1021static enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
1022 struct session *sess, struct stream *s, int flags)
1023{
1024 struct sample *key;
1025 struct cap_hdr *h;
1026 char **cap = s->res_cap;
1027 struct proxy *fe = strm_fe(s);
1028 int len;
1029 int i;
1030
1031 /* Look for the original configuration. */
1032 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
1033 h != NULL && i != rule->arg.capid.idx ;
1034 i--, h = h->next);
1035 if (!h)
1036 return ACT_RET_CONT;
1037
1038 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
1039 if (!key)
1040 return ACT_RET_CONT;
1041
1042 if (cap[h->index] == NULL)
1043 cap[h->index] = pool_alloc(h->pool);
1044
1045 if (cap[h->index] == NULL) /* no more capture memory */
1046 return ACT_RET_CONT;
1047
1048 len = key->data.u.str.data;
1049 if (len > h->len)
1050 len = h->len;
1051
1052 memcpy(cap[h->index], key->data.u.str.area, len);
1053 cap[h->index][len] = 0;
1054 return ACT_RET_CONT;
1055}
1056
1057/* Check an "http-response capture" action.
1058 *
1059 * The function returns 1 in success case, otherwise, it returns 0 and err is
1060 * filled.
1061 */
1062static int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
1063{
1064 if (rule->action_ptr != http_action_res_capture_by_id)
1065 return 1;
1066
Tim Duesterhusf3f4aa02020-07-03 13:43:42 +02001067 /* capture slots can only be declared in frontends, so we can't check their
1068 * existence in backends at configuration parsing step
1069 */
1070 if (px->cap & PR_CAP_FE && rule->arg.capid.idx >= px->nb_rsp_cap) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001071 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
1072 rule->arg.capid.idx);
1073 return 0;
1074 }
1075
1076 return 1;
1077}
1078
1079/* parse an "http-response capture" action. It takes a single argument which is
1080 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07001081 * the allocated hdr_cap struct of the preallocated id into arg->act.p[1].
Willy Tarreau79e57332018-10-02 16:01:16 +02001082 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1083 */
1084static enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
1085 struct act_rule *rule, char **err)
1086{
1087 struct sample_expr *expr;
1088 int cur_arg;
1089 int id;
1090 char *error;
1091
1092 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
1093 if (strcmp(args[cur_arg], "if") == 0 ||
1094 strcmp(args[cur_arg], "unless") == 0)
1095 break;
1096
1097 if (cur_arg < *orig_arg + 3) {
1098 memprintf(err, "expects <expression> id <idx>");
1099 return ACT_RET_PRS_ERR;
1100 }
1101
1102 cur_arg = *orig_arg;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001103 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 +02001104 if (!expr)
1105 return ACT_RET_PRS_ERR;
1106
1107 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
1108 memprintf(err,
1109 "fetch method '%s' extracts information from '%s', none of which is available here",
1110 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01001111 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001112 return ACT_RET_PRS_ERR;
1113 }
1114
1115 if (!args[cur_arg] || !*args[cur_arg]) {
1116 memprintf(err, "expects 'id'");
Christopher Faulet1337b322020-01-14 14:50:55 +01001117 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001118 return ACT_RET_PRS_ERR;
1119 }
1120
1121 if (strcmp(args[cur_arg], "id") != 0) {
1122 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001123 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001124 return ACT_RET_PRS_ERR;
1125 }
1126
1127 cur_arg++;
1128
1129 if (!args[cur_arg]) {
1130 memprintf(err, "missing id value");
Christopher Faulet1337b322020-01-14 14:50:55 +01001131 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001132 return ACT_RET_PRS_ERR;
1133 }
1134
1135 id = strtol(args[cur_arg], &error, 10);
1136 if (*error != '\0') {
1137 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
Christopher Faulet1337b322020-01-14 14:50:55 +01001138 release_sample_expr(expr);
Willy Tarreau79e57332018-10-02 16:01:16 +02001139 return ACT_RET_PRS_ERR;
1140 }
1141 cur_arg++;
1142
1143 px->conf.args.ctx = ARGC_CAP;
1144
1145 rule->action = ACT_CUSTOM;
1146 rule->action_ptr = http_action_res_capture_by_id;
1147 rule->check_ptr = check_http_res_capture;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001148 rule->release_ptr = release_http_capture;
Willy Tarreau79e57332018-10-02 16:01:16 +02001149 rule->arg.capid.expr = expr;
1150 rule->arg.capid.idx = id;
1151
1152 *orig_arg = cur_arg;
1153 return ACT_RET_PRS_OK;
1154}
1155
Christopher Faulet81e20172019-12-12 16:40:30 +01001156/* Parse a "allow" action for a request or a response rule. It takes no argument. It
1157 * returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1158 */
1159static enum act_parse_ret parse_http_allow(const char **args, int *orig_arg, struct proxy *px,
1160 struct act_rule *rule, char **err)
1161{
1162 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +01001163 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet81e20172019-12-12 16:40:30 +01001164 return ACT_RET_PRS_OK;
1165}
1166
Christopher Faulete0fca292020-01-13 21:49:03 +01001167/* Parse "deny" or "tarpit" actions for a request rule or "deny" action for a
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001168 * response rule. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on
1169 * error. It relies on http_parse_http_reply() to set
1170 * <.arg.http_reply>.
Christopher Faulet81e20172019-12-12 16:40:30 +01001171 */
Christopher Faulete0fca292020-01-13 21:49:03 +01001172static enum act_parse_ret parse_http_deny(const char **args, int *orig_arg, struct proxy *px,
1173 struct act_rule *rule, char **err)
Christopher Faulet81e20172019-12-12 16:40:30 +01001174{
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001175 int default_status;
1176 int cur_arg, arg = 0;
Christopher Faulet81e20172019-12-12 16:40:30 +01001177
1178 cur_arg = *orig_arg;
Christopher Faulete0fca292020-01-13 21:49:03 +01001179 if (rule->from == ACT_F_HTTP_REQ) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001180 if (strcmp(args[cur_arg - 1], "tarpit") == 0) {
Christopher Faulete0fca292020-01-13 21:49:03 +01001181 rule->action = ACT_HTTP_REQ_TARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001182 default_status = 500;
Christopher Faulet81e20172019-12-12 16:40:30 +01001183 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001184 else {
1185 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001186 default_status = 403;
Christopher Faulet81e20172019-12-12 16:40:30 +01001187 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001188 }
Christopher Faulete0fca292020-01-13 21:49:03 +01001189 else {
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001190 rule->action = ACT_ACTION_DENY;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001191 default_status = 502;
Christopher Faulete0fca292020-01-13 21:49:03 +01001192 }
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001193
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001194 /* If no args or only a deny_status specified, fallback on the legacy
1195 * mode and use default error files despite the fact that
1196 * default-errorfiles is not used. Otherwise, parse an http reply.
1197 */
Christopher Faulet040c8cd2020-01-13 16:43:45 +01001198
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001199 /* Prepare parsing of log-format strings */
1200 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001201
Christopher Faulet9467f182020-06-30 09:32:01 +02001202 if (!*(args[cur_arg]) || strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001203 rule->arg.http_reply = http_parse_http_reply((const char *[]){"default-errorfiles", ""}, &arg, px, default_status, err);
1204 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001205 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001206
1207 if (strcmp(args[cur_arg], "deny_status") == 0) {
Christopher Faulet9467f182020-06-30 09:32:01 +02001208 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 +02001209 rule->arg.http_reply = http_parse_http_reply((const char *[]){"status", args[cur_arg+1], "default-errorfiles", ""},
1210 &arg, px, default_status, err);
1211 *orig_arg += 2;
1212 goto end;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001213 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001214 args[cur_arg] += 5; /* skip "deny_" for the parsing */
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001215 }
1216
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001217 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, default_status, err);
Christopher Faulet554c0eb2020-01-14 12:00:28 +01001218
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001219 end:
1220 if (!rule->arg.http_reply)
1221 return ACT_RET_PRS_ERR;
1222
1223 rule->flags |= ACT_FLAG_FINAL;
1224 rule->check_ptr = check_act_http_reply;
1225 rule->release_ptr = release_act_http_reply;
Christopher Faulet81e20172019-12-12 16:40:30 +01001226 return ACT_RET_PRS_OK;
1227}
1228
Christopher Fauletb3048832020-05-27 15:26:43 +02001229
1230/* This function executes a auth action. It builds an 401/407 HTX message using
1231 * the corresponding proxy's error message. On success, it returns
1232 * ACT_RET_ABRT. If an error occurs ACT_RET_ERR is returned.
1233 */
1234static enum act_return http_action_auth(struct act_rule *rule, struct proxy *px,
1235 struct session *sess, struct stream *s, int flags)
1236{
1237 struct channel *req = &s->req;
1238 struct channel *res = &s->res;
1239 struct htx *htx = htx_from_buf(&res->buf);
1240 struct http_reply *reply;
1241 const char *auth_realm;
1242 struct http_hdr_ctx ctx;
1243 struct ist hdr;
1244
1245 /* Auth might be performed on regular http-req rules as well as on stats */
1246 auth_realm = rule->arg.http.str.ptr;
1247 if (!auth_realm) {
1248 if (px->uri_auth && s->current_rule_list == &px->uri_auth->http_req_rules)
1249 auth_realm = STATS_DEFAULT_REALM;
1250 else
1251 auth_realm = px->id;
1252 }
1253
1254 if (!(s->txn->flags & TX_USE_PX_CONN)) {
1255 s->txn->status = 401;
1256 hdr = ist("WWW-Authenticate");
1257 }
1258 else {
1259 s->txn->status = 407;
1260 hdr = ist("Proxy-Authenticate");
1261 }
1262 reply = http_error_message(s);
1263 channel_htx_truncate(res, htx);
1264
1265 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
1266 goto fail;
1267
1268 /* Write the generic 40x message */
1269 if (http_reply_to_htx(s, htx, reply) == -1)
1270 goto fail;
1271
1272 /* Remove all existing occurrences of the XXX-Authenticate header */
1273 ctx.blk = NULL;
1274 while (http_find_header(htx, hdr, &ctx, 1))
1275 http_remove_header(htx, &ctx);
1276
1277 /* Now a the right XXX-Authenticate header */
1278 if (!http_add_header(htx, hdr, ist2(b_orig(&trash), b_data(&trash))))
1279 goto fail;
1280
1281 /* Finally forward the reply */
1282 htx_to_buf(htx, &res->buf);
1283 if (!http_forward_proxy_resp(s, 1))
1284 goto fail;
1285
1286 /* Note: Only eval on the request */
1287 s->logs.tv_request = now;
1288 req->analysers &= AN_REQ_FLT_END;
1289
1290 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02001291 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Fauletb3048832020-05-27 15:26:43 +02001292
1293 if (!(s->flags & SF_ERR_MASK))
1294 s->flags |= SF_ERR_LOCAL;
1295 if (!(s->flags & SF_FINST_MASK))
1296 s->flags |= SF_FINST_R;
1297
1298 stream_inc_http_err_ctr(s);
1299 return ACT_RET_ABRT;
1300
1301 fail:
1302 /* If an error occurred, remove the incomplete HTTP response from the
1303 * buffer */
1304 channel_htx_truncate(res, htx);
1305 return ACT_RET_ERR;
1306}
1307
Christopher Faulet81e20172019-12-12 16:40:30 +01001308/* Parse a "auth" action. It may take 2 optional arguments to define a "realm"
1309 * parameter. It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
1310 */
1311static enum act_parse_ret parse_http_auth(const char **args, int *orig_arg, struct proxy *px,
1312 struct act_rule *rule, char **err)
1313{
1314 int cur_arg;
1315
Christopher Fauletb3048832020-05-27 15:26:43 +02001316 rule->action = ACT_CUSTOM;
Christopher Faulet245cf792019-12-18 14:58:12 +01001317 rule->flags |= ACT_FLAG_FINAL;
Christopher Fauletb3048832020-05-27 15:26:43 +02001318 rule->action_ptr = http_action_auth;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001319 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001320
1321 cur_arg = *orig_arg;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001322 if (strcmp(args[cur_arg], "realm") == 0) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001323 cur_arg++;
1324 if (!*args[cur_arg]) {
1325 memprintf(err, "missing realm value.\n");
1326 return ACT_RET_PRS_ERR;
1327 }
Tim Duesterhusf4f6c0f2022-03-15 13:11:08 +01001328 rule->arg.http.str = ist(strdup(args[cur_arg]));
Christopher Faulet81e20172019-12-12 16:40:30 +01001329 cur_arg++;
1330 }
1331
Christopher Fauletc20b3712020-01-27 15:51:56 +01001332 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001333 *orig_arg = cur_arg;
1334 return ACT_RET_PRS_OK;
1335}
1336
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001337/* This function executes a early-hint action. It adds an HTTP Early Hint HTTP
1338 * 103 response header with <.arg.http.str> name and with a value built
1339 * according to <.arg.http.fmt> log line format. If it is the first early-hint
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001340 * rule of series, the 103 response start-line is added first. At the end, if
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001341 * the next rule is not an early-hint rule or if it is the last rule, the EOH
1342 * block is added to terminate the response. On success, it returns
1343 * ACT_RET_CONT. If an error occurs while soft rewrites are enabled, the action
1344 * is canceled, but the rule processing continue. Otherwsize ACT_RET_ERR is
1345 * returned.
1346 */
1347static enum act_return http_action_early_hint(struct act_rule *rule, struct proxy *px,
1348 struct session *sess, struct stream *s, int flags)
1349{
1350 struct act_rule *prev_rule, *next_rule;
1351 struct channel *res = &s->res;
1352 struct htx *htx = htx_from_buf(&res->buf);
1353 struct buffer *value = alloc_trash_chunk();
1354 enum act_return ret = ACT_RET_CONT;
1355
1356 if (!(s->txn->req.flags & HTTP_MSGF_VER_11))
1357 goto leave;
1358
1359 if (!value) {
1360 if (!(s->flags & SF_ERR_MASK))
1361 s->flags |= SF_ERR_RESOURCE;
1362 goto error;
1363 }
1364
1365 /* get previous and next rules */
1366 prev_rule = LIST_PREV(&rule->list, typeof(rule), list);
1367 next_rule = LIST_NEXT(&rule->list, typeof(rule), list);
1368
1369 /* if no previous rule or previous rule is not early-hint, start a new response. Otherwise,
1370 * continue to add link to a previously started response */
1371 if (&prev_rule->list == s->current_rule_list || prev_rule->action_ptr != http_action_early_hint) {
1372 struct htx_sl *sl;
1373 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
1374 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
1375
1376 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
1377 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
1378 if (!sl)
1379 goto error;
1380 sl->info.res.status = 103;
1381 }
1382
1383 /* Add the HTTP Early Hint HTTP 103 response heade */
1384 value->data = build_logline(s, b_tail(value), b_room(value), &rule->arg.http.fmt);
1385 if (!htx_add_header(htx, rule->arg.http.str, ist2(b_head(value), b_data(value))))
1386 goto error;
1387
1388 /* if it is the last rule or the next one is not an early-hint, terminate the current
1389 * response. */
1390 if (&next_rule->list == s->current_rule_list || next_rule->action_ptr != http_action_early_hint) {
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001391 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
1392 /* If an error occurred during an Early-hint rule,
1393 * remove the incomplete HTTP 103 response from the
1394 * buffer */
1395 goto error;
1396 }
1397
Christopher Fauleta72a7e42020-01-28 09:28:11 +01001398 if (!http_forward_proxy_resp(s, 0))
1399 goto error;
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001400 }
1401
1402 leave:
1403 free_trash_chunk(value);
1404 return ret;
1405
1406 error:
1407 /* If an error occurred during an Early-hint rule, remove the incomplete
1408 * HTTP 103 response from the buffer */
1409 channel_htx_truncate(res, htx);
1410 ret = ACT_RET_ERR;
1411 goto leave;
1412}
1413
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001414/* This function executes a set-header or add-header actions. It builds a string
1415 * in the trash from the specified format string. It finds the action to be
1416 * performed in <.action>, previously filled by function parse_set_header(). The
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001417 * replacement action is executed by the function http_action_set_header(). On
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001418 * success, it returns ACT_RET_CONT. If an error occurs while soft rewrites are
1419 * enabled, the action is canceled, but the rule processing continue. Otherwsize
1420 * ACT_RET_ERR is returned.
1421 */
1422static enum act_return http_action_set_header(struct act_rule *rule, struct proxy *px,
1423 struct session *sess, struct stream *s, int flags)
1424{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001425 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1426 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001427 enum act_return ret = ACT_RET_CONT;
1428 struct buffer *replace;
1429 struct http_hdr_ctx ctx;
1430 struct ist n, v;
1431
1432 replace = alloc_trash_chunk();
1433 if (!replace)
1434 goto fail_alloc;
1435
1436 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1437 n = rule->arg.http.str;
1438 v = ist2(replace->area, replace->data);
1439
1440 if (rule->action == 0) { // set-header
1441 /* remove all occurrences of the header */
1442 ctx.blk = NULL;
1443 while (http_find_header(htx, n, &ctx, 1))
1444 http_remove_header(htx, &ctx);
1445 }
1446
1447 /* Now add header */
1448 if (!http_add_header(htx, n, v))
1449 goto fail_rewrite;
1450
1451 leave:
1452 free_trash_chunk(replace);
1453 return ret;
1454
1455 fail_alloc:
1456 if (!(s->flags & SF_ERR_MASK))
1457 s->flags |= SF_ERR_RESOURCE;
1458 ret = ACT_RET_ERR;
1459 goto leave;
1460
1461 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001462 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001463 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001464 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001465 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001466 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001467 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001468 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001469
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001470 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001471 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001472 if (!(s->flags & SF_ERR_MASK))
1473 s->flags |= SF_ERR_PRXCOND;
1474 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001475 goto leave;
1476}
1477
Christopher Faulet81e20172019-12-12 16:40:30 +01001478/* Parse a "set-header", "add-header" or "early-hint" actions. It takes an
1479 * header name and a log-format string as arguments. It returns ACT_RET_PRS_OK
1480 * on success, ACT_RET_PRS_ERR on error.
1481 *
1482 * Note: same function is used for the request and the response. However
1483 * "early-hint" rules are only supported for request rules.
1484 */
1485static enum act_parse_ret parse_http_set_header(const char **args, int *orig_arg, struct proxy *px,
1486 struct act_rule *rule, char **err)
1487{
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001488 int cap = 0, cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +01001489
Christopher Faulet91b3ec12020-01-17 22:30:06 +01001490 if (args[*orig_arg-1][0] == 'e') {
1491 rule->action = ACT_CUSTOM;
1492 rule->action_ptr = http_action_early_hint;
1493 }
Christopher Fauletd1f27e32019-12-17 09:33:38 +01001494 else {
1495 if (args[*orig_arg-1][0] == 's')
1496 rule->action = 0; // set-header
1497 else
1498 rule->action = 1; // add-header
1499 rule->action_ptr = http_action_set_header;
1500 }
Christopher Faulet2eb53962020-01-14 14:47:34 +01001501 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001502
1503 cur_arg = *orig_arg;
1504 if (!*args[cur_arg] || !*args[cur_arg+1]) {
1505 memprintf(err, "expects exactly 2 arguments");
1506 return ACT_RET_PRS_ERR;
1507 }
1508
Christopher Faulet81e20172019-12-12 16:40:30 +01001509
Tim Duesterhusf4f6c0f2022-03-15 13:11:08 +01001510 rule->arg.http.str = ist(strdup(args[cur_arg]));
Christopher Faulet96bff762019-12-17 13:46:18 +01001511 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001512
1513 if (rule->from == ACT_F_HTTP_REQ) {
1514 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001515 if (px->cap & PR_CAP_FE)
1516 cap |= SMP_VAL_FE_HRQ_HDR;
1517 if (px->cap & PR_CAP_BE)
1518 cap |= SMP_VAL_BE_HRQ_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001519 }
1520 else{
1521 px->conf.args.ctx = ARGC_HRS;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001522 if (px->cap & PR_CAP_FE)
1523 cap |= SMP_VAL_FE_HRS_HDR;
1524 if (px->cap & PR_CAP_BE)
1525 cap |= SMP_VAL_BE_HRS_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001526 }
1527
1528 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001529 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001530 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001531 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001532 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001533
1534 free(px->conf.lfs_file);
1535 px->conf.lfs_file = strdup(px->conf.args.file);
1536 px->conf.lfs_line = px->conf.args.line;
1537
1538 *orig_arg = cur_arg + 1;
1539 return ACT_RET_PRS_OK;
1540}
1541
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001542/* This function executes a replace-header or replace-value actions. It
1543 * builds a string in the trash from the specified format string. It finds
1544 * the action to be performed in <.action>, previously filled by function
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001545 * parse_replace_header(). The replacement action is executed by the function
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001546 * http_action_replace_header(). On success, it returns ACT_RET_CONT. If an error
1547 * occurs while soft rewrites are enabled, the action is canceled, but the rule
1548 * processing continue. Otherwsize ACT_RET_ERR is returned.
1549 */
1550static enum act_return http_action_replace_header(struct act_rule *rule, struct proxy *px,
1551 struct session *sess, struct stream *s, int flags)
1552{
Christopher Faulet91e31d82020-01-24 15:37:13 +01001553 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1554 struct htx *htx = htxbuf(&msg->chn->buf);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001555 enum act_return ret = ACT_RET_CONT;
1556 struct buffer *replace;
1557 int r;
1558
1559 replace = alloc_trash_chunk();
1560 if (!replace)
1561 goto fail_alloc;
1562
1563 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
1564
1565 r = http_replace_hdrs(s, htx, rule->arg.http.str, replace->area, rule->arg.http.re, (rule->action == 0));
1566 if (r == -1)
1567 goto fail_rewrite;
1568
1569 leave:
1570 free_trash_chunk(replace);
1571 return ret;
1572
1573 fail_alloc:
1574 if (!(s->flags & SF_ERR_MASK))
1575 s->flags |= SF_ERR_RESOURCE;
1576 ret = ACT_RET_ERR;
1577 goto leave;
1578
1579 fail_rewrite:
Willy Tarreau4781b152021-04-06 13:53:36 +02001580 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001581 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +02001582 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
William Lallemand36119de2021-03-08 15:26:48 +01001583 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001584 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001585 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001586 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001587
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001588 if (!(msg->flags & HTTP_MSGF_SOFT_RW)) {
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001589 ret = ACT_RET_ERR;
Christopher Faulet333bf8c2020-01-22 14:38:05 +01001590 if (!(s->flags & SF_ERR_MASK))
1591 s->flags |= SF_ERR_PRXCOND;
1592 }
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001593 goto leave;
1594}
1595
Christopher Faulet81e20172019-12-12 16:40:30 +01001596/* Parse a "replace-header" or "replace-value" actions. It takes an header name,
1597 * a regex and replacement string as arguments. It returns ACT_RET_PRS_OK on
1598 * success, ACT_RET_PRS_ERR on error.
1599 */
1600static enum act_parse_ret parse_http_replace_header(const char **args, int *orig_arg, struct proxy *px,
1601 struct act_rule *rule, char **err)
1602{
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001603 int cap = 0, cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +01001604
Christopher Faulet92d34fe2019-12-17 09:20:34 +01001605 if (args[*orig_arg-1][8] == 'h')
1606 rule->action = 0; // replace-header
1607 else
1608 rule->action = 1; // replace-value
1609 rule->action_ptr = http_action_replace_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001610 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001611
1612 cur_arg = *orig_arg;
1613 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2]) {
1614 memprintf(err, "expects exactly 3 arguments");
1615 return ACT_RET_PRS_ERR;
1616 }
1617
Tim Duesterhusf4f6c0f2022-03-15 13:11:08 +01001618 rule->arg.http.str = ist(strdup(args[cur_arg]));
Christopher Faulet96bff762019-12-17 13:46:18 +01001619 LIST_INIT(&rule->arg.http.fmt);
Christopher Faulet81e20172019-12-12 16:40:30 +01001620
1621 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001622 if (!(rule->arg.http.re = regex_comp(args[cur_arg], 1, 1, err))) {
Tim Duesterhused526372020-03-05 17:56:33 +01001623 istfree(&rule->arg.http.str);
Christopher Faulet81e20172019-12-12 16:40:30 +01001624 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001625 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001626
1627 if (rule->from == ACT_F_HTTP_REQ) {
1628 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001629 if (px->cap & PR_CAP_FE)
1630 cap |= SMP_VAL_FE_HRQ_HDR;
1631 if (px->cap & PR_CAP_BE)
1632 cap |= SMP_VAL_BE_HRQ_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001633 }
1634 else{
1635 px->conf.args.ctx = ARGC_HRS;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001636 if (px->cap & PR_CAP_FE)
1637 cap |= SMP_VAL_FE_HRS_HDR;
1638 if (px->cap & PR_CAP_BE)
1639 cap |= SMP_VAL_BE_HRS_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001640 }
1641
1642 cur_arg++;
Christopher Faulet1337b322020-01-14 14:50:55 +01001643 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.fmt, LOG_OPT_HTTP, cap, err)) {
Tim Duesterhused526372020-03-05 17:56:33 +01001644 istfree(&rule->arg.http.str);
Christopher Faulet1337b322020-01-14 14:50:55 +01001645 regex_free(rule->arg.http.re);
Christopher Faulet81e20172019-12-12 16:40:30 +01001646 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001647 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001648
1649 free(px->conf.lfs_file);
1650 px->conf.lfs_file = strdup(px->conf.args.file);
1651 px->conf.lfs_line = px->conf.args.line;
1652
1653 *orig_arg = cur_arg + 1;
1654 return ACT_RET_PRS_OK;
1655}
1656
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001657/* This function executes a del-header action with selected matching mode for
1658 * header name. It finds the matching method to be performed in <.action>, previously
1659 * filled by function parse_http_del_header(). On success, it returns ACT_RET_CONT.
1660 * Otherwise ACT_RET_ERR is returned.
1661 */
1662static enum act_return http_action_del_header(struct act_rule *rule, struct proxy *px,
1663 struct session *sess, struct stream *s, int flags)
1664{
1665 struct http_hdr_ctx ctx;
1666 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
1667 struct htx *htx = htxbuf(&msg->chn->buf);
1668 enum act_return ret = ACT_RET_CONT;
1669
1670 /* remove all occurrences of the header */
1671 ctx.blk = NULL;
1672 switch (rule->action) {
1673 case PAT_MATCH_STR:
1674 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
1675 http_remove_header(htx, &ctx);
1676 break;
1677 case PAT_MATCH_BEG:
1678 while (http_find_pfx_header(htx, rule->arg.http.str, &ctx, 1))
1679 http_remove_header(htx, &ctx);
1680 break;
1681 case PAT_MATCH_END:
1682 while (http_find_sfx_header(htx, rule->arg.http.str, &ctx, 1))
1683 http_remove_header(htx, &ctx);
1684 break;
1685 case PAT_MATCH_SUB:
1686 while (http_find_sub_header(htx, rule->arg.http.str, &ctx, 1))
1687 http_remove_header(htx, &ctx);
1688 break;
1689 case PAT_MATCH_REG:
1690 while (http_match_header(htx, rule->arg.http.re, &ctx, 1))
1691 http_remove_header(htx, &ctx);
1692 break;
1693 default:
1694 return ACT_RET_ERR;
1695 }
1696 return ret;
1697}
1698
1699/* Parse a "del-header" action. It takes string as a required argument,
1700 * optional flag (currently only -m) and optional matching method of input string
1701 * with header name to be deleted. Default matching method is exact match (-m str).
1702 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001703 */
1704static enum act_parse_ret parse_http_del_header(const char **args, int *orig_arg, struct proxy *px,
1705 struct act_rule *rule, char **err)
1706{
1707 int cur_arg;
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001708 int pat_idx;
Christopher Faulet81e20172019-12-12 16:40:30 +01001709
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001710 /* set exact matching (-m str) as default */
1711 rule->action = PAT_MATCH_STR;
1712 rule->action_ptr = http_action_del_header;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001713 rule->release_ptr = release_http_action;
Christopher Faulet81e20172019-12-12 16:40:30 +01001714
1715 cur_arg = *orig_arg;
1716 if (!*args[cur_arg]) {
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001717 memprintf(err, "expects at least 1 argument");
Christopher Faulet81e20172019-12-12 16:40:30 +01001718 return ACT_RET_PRS_ERR;
1719 }
1720
Tim Duesterhusf4f6c0f2022-03-15 13:11:08 +01001721 rule->arg.http.str = ist(strdup(args[cur_arg]));
Christopher Faulet81e20172019-12-12 16:40:30 +01001722 px->conf.args.ctx = (rule->from == ACT_F_HTTP_REQ ? ARGC_HRQ : ARGC_HRS);
1723
Maciej Zdeb6dee9962020-11-23 16:03:09 +00001724 LIST_INIT(&rule->arg.http.fmt);
Maciej Zdebebdd4c52020-11-20 13:58:48 +00001725 if (strcmp(args[cur_arg+1], "-m") == 0) {
1726 cur_arg++;
1727 if (!*args[cur_arg+1]) {
1728 memprintf(err, "-m flag expects exactly 1 argument");
1729 return ACT_RET_PRS_ERR;
1730 }
1731
1732 cur_arg++;
1733 pat_idx = pat_find_match_name(args[cur_arg]);
1734 switch (pat_idx) {
1735 case PAT_MATCH_REG:
1736 if (!(rule->arg.http.re = regex_comp(rule->arg.http.str.ptr, 1, 1, err)))
1737 return ACT_RET_PRS_ERR;
1738 /* fall through */
1739 case PAT_MATCH_STR:
1740 case PAT_MATCH_BEG:
1741 case PAT_MATCH_END:
1742 case PAT_MATCH_SUB:
1743 rule->action = pat_idx;
1744 break;
1745 default:
1746 memprintf(err, "-m with unsupported matching method '%s'", args[cur_arg]);
1747 return ACT_RET_PRS_ERR;
1748 }
1749 }
1750
Christopher Faulet81e20172019-12-12 16:40:30 +01001751 *orig_arg = cur_arg + 1;
1752 return ACT_RET_PRS_OK;
1753}
1754
Christopher Faulet2eb53962020-01-14 14:47:34 +01001755/* Release memory allocated by an http redirect action. */
1756static void release_http_redir(struct act_rule *rule)
1757{
1758 struct logformat_node *lf, *lfb;
1759 struct redirect_rule *redir;
1760
1761 redir = rule->arg.redir;
Willy Tarreau2b718102021-04-21 07:32:39 +02001762 LIST_DELETE(&redir->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001763 if (redir->cond) {
1764 prune_acl_cond(redir->cond);
1765 free(redir->cond);
1766 }
1767 free(redir->rdr_str);
1768 free(redir->cookie_str);
1769 list_for_each_entry_safe(lf, lfb, &redir->rdr_fmt, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001770 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001771 free(lf);
1772 }
1773 free(redir);
1774}
1775
Christopher Faulet81e20172019-12-12 16:40:30 +01001776/* Parse a "redirect" action. It returns ACT_RET_PRS_OK on success,
1777 * ACT_RET_PRS_ERR on error.
1778 */
1779static enum act_parse_ret parse_http_redirect(const char **args, int *orig_arg, struct proxy *px,
1780 struct act_rule *rule, char **err)
1781{
1782 struct redirect_rule *redir;
1783 int dir, cur_arg;
1784
1785 rule->action = ACT_HTTP_REDIR;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001786 rule->release_ptr = release_http_redir;
Christopher Faulet81e20172019-12-12 16:40:30 +01001787
1788 cur_arg = *orig_arg;
1789
1790 dir = (rule->from == ACT_F_HTTP_REQ ? 0 : 1);
1791 if ((redir = http_parse_redirect_rule(px->conf.args.file, px->conf.args.line, px, &args[cur_arg], err, 1, dir)) == NULL)
1792 return ACT_RET_PRS_ERR;
1793
Willy Tarreaubc1223b2021-09-02 16:54:33 +02001794 if (!(redir->flags & REDIRECT_FLAG_IGNORE_EMPTY))
1795 rule->flags |= ACT_FLAG_FINAL;
1796
Christopher Faulet81e20172019-12-12 16:40:30 +01001797 rule->arg.redir = redir;
1798 rule->cond = redir->cond;
1799 redir->cond = NULL;
1800
1801 /* skip all arguments */
1802 while (*args[cur_arg])
1803 cur_arg++;
1804
1805 *orig_arg = cur_arg;
1806 return ACT_RET_PRS_OK;
1807}
1808
Christopher Faulet046cf442019-12-17 15:45:23 +01001809/* This function executes a add-acl, del-acl, set-map or del-map actions. On
1810 * success, it returns ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1811 */
1812static enum act_return http_action_set_map(struct act_rule *rule, struct proxy *px,
1813 struct session *sess, struct stream *s, int flags)
1814{
1815 struct pat_ref *ref;
1816 struct buffer *key = NULL, *value = NULL;
1817 enum act_return ret = ACT_RET_CONT;
1818
1819 /* collect reference */
1820 ref = pat_ref_lookup(rule->arg.map.ref);
1821 if (!ref)
1822 goto leave;
1823
1824 /* allocate key */
1825 key = alloc_trash_chunk();
1826 if (!key)
1827 goto fail_alloc;
1828
1829 /* collect key */
1830 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
1831 key->area[key->data] = '\0';
1832
1833 switch (rule->action) {
1834 case 0: // add-acl
1835 /* add entry only if it does not already exist */
1836 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1837 if (pat_ref_find_elt(ref, key->area) == NULL)
1838 pat_ref_add(ref, key->area, NULL, NULL);
1839 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1840 break;
1841
1842 case 1: // set-map
1843 /* allocate value */
1844 value = alloc_trash_chunk();
1845 if (!value)
1846 goto fail_alloc;
1847
1848 /* collect value */
1849 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
1850 value->area[value->data] = '\0';
1851
1852 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1853 if (pat_ref_find_elt(ref, key->area) != NULL) {
1854 /* update entry if it exists */
1855 pat_ref_set(ref, key->area, value->area, NULL);
1856 }
1857 else {
1858 /* insert a new entry */
1859 pat_ref_add(ref, key->area, value->area, NULL);
1860 }
1861 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1862 break;
1863
1864 case 2: // del-acl
1865 case 3: // del-map
1866 /* returned code: 1=ok, 0=ko */
1867 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
1868 pat_ref_delete(ref, key->area);
1869 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
1870 break;
1871
1872 default:
1873 ret = ACT_RET_ERR;
1874 }
1875
1876
1877 leave:
1878 free_trash_chunk(key);
1879 free_trash_chunk(value);
1880 return ret;
1881
1882 fail_alloc:
1883 if (!(s->flags & SF_ERR_MASK))
1884 s->flags |= SF_ERR_RESOURCE;
1885 ret = ACT_RET_ERR;
1886 goto leave;
1887}
1888
Christopher Faulet2eb53962020-01-14 14:47:34 +01001889/* Release memory allocated by an http map/acl action. */
1890static void release_http_map(struct act_rule *rule)
1891{
1892 struct logformat_node *lf, *lfb;
1893
1894 free(rule->arg.map.ref);
1895 list_for_each_entry_safe(lf, lfb, &rule->arg.map.key, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001896 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001897 release_sample_expr(lf->expr);
1898 free(lf->arg);
1899 free(lf);
1900 }
1901 if (rule->action == 1) {
1902 list_for_each_entry_safe(lf, lfb, &rule->arg.map.value, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001903 LIST_DELETE(&lf->list);
Christopher Faulet2eb53962020-01-14 14:47:34 +01001904 release_sample_expr(lf->expr);
1905 free(lf->arg);
1906 free(lf);
1907 }
1908 }
1909}
1910
Christopher Faulet81e20172019-12-12 16:40:30 +01001911/* Parse a "add-acl", "del-acl", "set-map" or "del-map" actions. It takes one or
Christopher Faulet046cf442019-12-17 15:45:23 +01001912 * two log-format string as argument depending on the action. The action is
1913 * stored in <.action> as an int (0=add-acl, 1=set-map, 2=del-acl,
1914 * 3=del-map). It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Christopher Faulet81e20172019-12-12 16:40:30 +01001915 */
1916static enum act_parse_ret parse_http_set_map(const char **args, int *orig_arg, struct proxy *px,
1917 struct act_rule *rule, char **err)
1918{
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001919 int cap = 0, cur_arg;
Christopher Faulet81e20172019-12-12 16:40:30 +01001920
Christopher Faulet046cf442019-12-17 15:45:23 +01001921 if (args[*orig_arg-1][0] == 'a') // add-acl
1922 rule->action = 0;
1923 else if (args[*orig_arg-1][0] == 's') // set-map
1924 rule->action = 1;
1925 else if (args[*orig_arg-1][4] == 'a') // del-acl
1926 rule->action = 2;
1927 else if (args[*orig_arg-1][4] == 'm') // del-map
1928 rule->action = 3;
1929 else {
1930 memprintf(err, "internal error: unhandled action '%s'", args[0]);
1931 return ACT_RET_PRS_ERR;
1932 }
1933 rule->action_ptr = http_action_set_map;
Christopher Faulet2eb53962020-01-14 14:47:34 +01001934 rule->release_ptr = release_http_map;
Christopher Faulet81e20172019-12-12 16:40:30 +01001935
1936 cur_arg = *orig_arg;
Christopher Faulet046cf442019-12-17 15:45:23 +01001937 if (rule->action == 1 && (!*args[cur_arg] || !*args[cur_arg+1])) {
1938 /* 2 args for set-map */
Christopher Faulet81e20172019-12-12 16:40:30 +01001939 memprintf(err, "expects exactly 2 arguments");
1940 return ACT_RET_PRS_ERR;
1941 }
1942 else if (!*args[cur_arg]) {
Christopher Faulet046cf442019-12-17 15:45:23 +01001943 /* only one arg for other actions */
Christopher Faulet81e20172019-12-12 16:40:30 +01001944 memprintf(err, "expects exactly 1 arguments");
1945 return ACT_RET_PRS_ERR;
1946 }
1947
1948 /*
1949 * '+ 8' for 'set-map(' (same for del-map)
1950 * '- 9' for 'set-map(' + trailing ')' (same for del-map)
1951 */
1952 rule->arg.map.ref = my_strndup(args[cur_arg-1] + 8, strlen(args[cur_arg-1]) - 9);
1953
1954 if (rule->from == ACT_F_HTTP_REQ) {
1955 px->conf.args.ctx = ARGC_HRQ;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001956 if (px->cap & PR_CAP_FE)
1957 cap |= SMP_VAL_FE_HRQ_HDR;
1958 if (px->cap & PR_CAP_BE)
1959 cap |= SMP_VAL_BE_HRQ_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001960 }
1961 else{
1962 px->conf.args.ctx = ARGC_HRS;
Christopher Faulet7a06ffb2021-10-13 17:22:17 +02001963 if (px->cap & PR_CAP_FE)
1964 cap |= SMP_VAL_FE_HRS_HDR;
1965 if (px->cap & PR_CAP_BE)
1966 cap |= SMP_VAL_BE_HRS_HDR;
Christopher Faulet81e20172019-12-12 16:40:30 +01001967 }
1968
1969 /* key pattern */
1970 LIST_INIT(&rule->arg.map.key);
Christopher Faulet1337b322020-01-14 14:50:55 +01001971 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.key, LOG_OPT_HTTP, cap, err)) {
1972 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001973 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001974 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001975
Christopher Faulet046cf442019-12-17 15:45:23 +01001976 if (rule->action == 1) {
Christopher Faulet81e20172019-12-12 16:40:30 +01001977 /* value pattern for set-map only */
1978 cur_arg++;
1979 LIST_INIT(&rule->arg.map.value);
Christopher Faulet1337b322020-01-14 14:50:55 +01001980 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.map.value, LOG_OPT_HTTP, cap, err)) {
1981 free(rule->arg.map.ref);
Christopher Faulet81e20172019-12-12 16:40:30 +01001982 return ACT_RET_PRS_ERR;
Christopher Faulet1337b322020-01-14 14:50:55 +01001983 }
Christopher Faulet81e20172019-12-12 16:40:30 +01001984 }
1985
1986 free(px->conf.lfs_file);
1987 px->conf.lfs_file = strdup(px->conf.args.file);
1988 px->conf.lfs_line = px->conf.args.line;
1989
1990 *orig_arg = cur_arg + 1;
1991 return ACT_RET_PRS_OK;
1992}
1993
Christopher Fauletac98d812019-12-18 09:20:16 +01001994/* This function executes a track-sc* actions. On success, it returns
1995 * ACT_RET_CONT. Otherwsize ACT_RET_ERR is returned.
1996 */
1997static enum act_return http_action_track_sc(struct act_rule *rule, struct proxy *px,
1998 struct session *sess, struct stream *s, int flags)
1999{
2000 struct stktable *t;
2001 struct stksess *ts;
2002 struct stktable_key *key;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002003 void *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *ptr6;
Christopher Fauletac98d812019-12-18 09:20:16 +01002004 int opt;
2005
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002006 ptr1 = ptr2 = ptr3 = ptr4 = ptr5 = ptr6 = NULL;
Christopher Fauletac98d812019-12-18 09:20:16 +01002007 opt = ((rule->from == ACT_F_HTTP_REQ) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES) | SMP_OPT_FINAL;
2008
2009 t = rule->arg.trk_ctr.table.t;
Emeric Brun362d25e2021-03-10 16:58:03 +01002010
2011 if (stkctr_entry(&s->stkctr[rule->action]))
2012 goto end;
2013
Christopher Fauletac98d812019-12-18 09:20:16 +01002014 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, NULL);
2015
2016 if (!key)
2017 goto end;
2018 ts = stktable_get_entry(t, key);
2019 if (!ts)
2020 goto end;
2021
2022 stream_track_stkctr(&s->stkctr[rule->action], t, ts);
2023
2024 /* let's count a new HTTP request as it's the first time we do it */
2025 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2026 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2027
2028 /* When the client triggers a 4xx from the server, it's most often due
2029 * to a missing object or permission. These events should be tracked
2030 * because if they happen often, it may indicate a brute force or a
2031 * vulnerability scan. Normally this is done when receiving the response
2032 * but here we're tracking after this ought to have been done so we have
2033 * to do it on purpose.
2034 */
2035 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 400) < 100) {
2036 ptr3 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2037 ptr4 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2038 }
2039
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002040 if (rule->from == ACT_F_HTTP_RES && (unsigned)(s->txn->status - 500) < 100 &&
2041 s->txn->status != 501 && s->txn->status != 505) {
2042 ptr5 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
2043 ptr6 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
2044 }
2045
2046 if (ptr1 || ptr2 || ptr3 || ptr4 || ptr5 || ptr6) {
Christopher Fauletac98d812019-12-18 09:20:16 +01002047 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2048
2049 if (ptr1)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002050 stktable_data_cast(ptr1, std_t_uint)++;
Christopher Fauletac98d812019-12-18 09:20:16 +01002051 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002052 update_freq_ctr_period(&stktable_data_cast(ptr2, std_t_frqp),
Christopher Fauletac98d812019-12-18 09:20:16 +01002053 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2054 if (ptr3)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002055 stktable_data_cast(ptr3, std_t_uint)++;
Christopher Fauletac98d812019-12-18 09:20:16 +01002056 if (ptr4)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002057 update_freq_ctr_period(&stktable_data_cast(ptr4, std_t_frqp),
Christopher Fauletac98d812019-12-18 09:20:16 +01002058 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002059 if (ptr5)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002060 stktable_data_cast(ptr5, std_t_uint)++;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002061 if (ptr6)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002062 update_freq_ctr_period(&stktable_data_cast(ptr6, std_t_frqp),
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002063 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u, 1);
Christopher Fauletac98d812019-12-18 09:20:16 +01002064
2065 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2066
2067 /* If data was modified, we need to touch to re-schedule sync */
2068 stktable_touch_local(t, ts, 0);
2069 }
2070
2071 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT);
2072 if (sess->fe != s->be)
2073 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND);
2074
2075 end:
2076 return ACT_RET_CONT;
2077}
Christopher Faulet81e20172019-12-12 16:40:30 +01002078
Christopher Faulet2eb53962020-01-14 14:47:34 +01002079static void release_http_track_sc(struct act_rule *rule)
2080{
2081 release_sample_expr(rule->arg.trk_ctr.expr);
2082}
2083
Christopher Faulet81e20172019-12-12 16:40:30 +01002084/* Parse a "track-sc*" actions. It returns ACT_RET_PRS_OK on success,
2085 * ACT_RET_PRS_ERR on error.
2086 */
2087static enum act_parse_ret parse_http_track_sc(const char **args, int *orig_arg, struct proxy *px,
2088 struct act_rule *rule, char **err)
2089{
2090 struct sample_expr *expr;
2091 unsigned int where;
2092 unsigned int tsc_num;
2093 const char *tsc_num_str;
2094 int cur_arg;
2095
2096 tsc_num_str = &args[*orig_arg-1][8];
2097 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1)
2098 return ACT_RET_PRS_ERR;
2099
2100 cur_arg = *orig_arg;
2101 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002102 err, &px->conf.args, NULL);
Christopher Faulet81e20172019-12-12 16:40:30 +01002103 if (!expr)
2104 return ACT_RET_PRS_ERR;
2105
2106 where = 0;
2107 if (px->cap & PR_CAP_FE)
2108 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
2109 if (px->cap & PR_CAP_BE)
2110 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
2111
2112 if (!(expr->fetch->val & where)) {
2113 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here",
2114 args[cur_arg-1], sample_src_names(expr->fetch->use));
Christopher Faulet1337b322020-01-14 14:50:55 +01002115 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002116 return ACT_RET_PRS_ERR;
2117 }
2118
2119 if (strcmp(args[cur_arg], "table") == 0) {
2120 cur_arg++;
2121 if (!*args[cur_arg]) {
2122 memprintf(err, "missing table name");
Christopher Faulet1337b322020-01-14 14:50:55 +01002123 release_sample_expr(expr);
Christopher Faulet81e20172019-12-12 16:40:30 +01002124 return ACT_RET_PRS_ERR;
2125 }
2126
2127 /* we copy the table name for now, it will be resolved later */
2128 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
2129 cur_arg++;
2130 }
2131
Christopher Fauletac98d812019-12-18 09:20:16 +01002132 rule->action = tsc_num;
Christopher Faulet81e20172019-12-12 16:40:30 +01002133 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +01002134 rule->action_ptr = http_action_track_sc;
Christopher Faulet2eb53962020-01-14 14:47:34 +01002135 rule->release_ptr = release_http_track_sc;
Christopher Faulet81e20172019-12-12 16:40:30 +01002136 rule->check_ptr = check_trk_action;
2137
2138 *orig_arg = cur_arg;
2139 return ACT_RET_PRS_OK;
2140}
2141
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002142static enum act_return action_timeout_set_stream_timeout(struct act_rule *rule,
2143 struct proxy *px,
2144 struct session *sess,
2145 struct stream *s,
2146 int flags)
2147{
2148 struct sample *key;
2149
2150 if (rule->arg.timeout.expr) {
2151 key = sample_fetch_as_type(px, sess, s, SMP_OPT_FINAL, rule->arg.timeout.expr, SMP_T_SINT);
2152 if (!key)
2153 return ACT_RET_CONT;
2154
2155 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(key->data.u.sint));
2156 }
2157 else {
2158 stream_set_timeout(s, rule->arg.timeout.type, MS_TO_TICKS(rule->arg.timeout.value));
2159 }
2160
2161 return ACT_RET_CONT;
2162}
2163
2164/* Parse a "set-timeout" action. Returns ACT_RET_PRS_ERR if parsing error.
2165 */
2166static enum act_parse_ret parse_http_set_timeout(const char **args,
2167 int *orig_arg,
2168 struct proxy *px,
2169 struct act_rule *rule, char **err)
2170{
2171 int cur_arg;
2172
2173 rule->action = ACT_CUSTOM;
2174 rule->action_ptr = action_timeout_set_stream_timeout;
2175 rule->release_ptr = release_timeout_action;
2176
2177 cur_arg = *orig_arg;
2178 if (!*args[cur_arg] || !*args[cur_arg + 1]) {
2179 memprintf(err, "expects exactly 2 arguments");
2180 return ACT_RET_PRS_ERR;
2181 }
2182
2183 if (!(px->cap & PR_CAP_BE)) {
2184 memprintf(err, "proxy '%s' has no backend capability", px->id);
2185 return ACT_RET_PRS_ERR;
2186 }
2187
2188 if (cfg_parse_rule_set_timeout(args, cur_arg,
2189 &rule->arg.timeout.value,
2190 &rule->arg.timeout.type,
2191 &rule->arg.timeout.expr,
2192 err,
2193 px->conf.args.file,
2194 px->conf.args.line, &px->conf.args) == -1) {
2195 return ACT_RET_PRS_ERR;
2196 }
2197
2198 *orig_arg = cur_arg + 2;
2199
2200 return ACT_RET_PRS_OK;
2201}
2202
Christopher Faulet46f95542019-12-20 10:07:22 +01002203/* This function executes a strict-mode actions. On success, it always returns
2204 * ACT_RET_CONT
2205 */
2206static enum act_return http_action_strict_mode(struct act_rule *rule, struct proxy *px,
2207 struct session *sess, struct stream *s, int flags)
2208{
2209 struct http_msg *msg = ((rule->from == ACT_F_HTTP_REQ) ? &s->txn->req : &s->txn->rsp);
2210
2211 if (rule->action == 0) // strict-mode on
2212 msg->flags &= ~HTTP_MSGF_SOFT_RW;
2213 else // strict-mode off
2214 msg->flags |= HTTP_MSGF_SOFT_RW;
2215 return ACT_RET_CONT;
2216}
2217
2218/* Parse a "strict-mode" action. It returns ACT_RET_PRS_OK on success,
2219 * ACT_RET_PRS_ERR on error.
2220 */
2221static enum act_parse_ret parse_http_strict_mode(const char **args, int *orig_arg, struct proxy *px,
2222 struct act_rule *rule, char **err)
2223{
2224 int cur_arg;
2225
Christopher Faulet46f95542019-12-20 10:07:22 +01002226 cur_arg = *orig_arg;
2227 if (!*args[cur_arg]) {
2228 memprintf(err, "expects exactly 1 arguments");
2229 return ACT_RET_PRS_ERR;
2230 }
2231
2232 if (strcasecmp(args[cur_arg], "on") == 0)
2233 rule->action = 0; // strict-mode on
2234 else if (strcasecmp(args[cur_arg], "off") == 0)
2235 rule->action = 1; // strict-mode off
2236 else {
2237 memprintf(err, "Unexpected value '%s'. Only 'on' and 'off' are supported", args[cur_arg]);
2238 return ACT_RET_PRS_ERR;
2239 }
2240 rule->action_ptr = http_action_strict_mode;
2241
2242 *orig_arg = cur_arg + 1;
2243 return ACT_RET_PRS_OK;
2244}
2245
Christopher Faulet24231ab2020-01-24 17:44:23 +01002246/* This function executes a return action. It builds an HTX message from an
2247 * errorfile, an raw file or a log-format string, depending on <.action>
2248 * value. On success, it returns ACT_RET_ABRT. If an error occurs ACT_RET_ERR is
2249 * returned.
2250 */
2251static enum act_return http_action_return(struct act_rule *rule, struct proxy *px,
2252 struct session *sess, struct stream *s, int flags)
2253{
2254 struct channel *req = &s->req;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002255
Christopher Faulet2d36df22021-02-19 11:41:01 +01002256 s->txn->status = rule->arg.http_reply->status;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002257 if (http_reply_message(s, rule->arg.http_reply) == -1)
2258 return ACT_RET_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002259
Christopher Faulet24231ab2020-01-24 17:44:23 +01002260 if (rule->from == ACT_F_HTTP_REQ) {
2261 /* let's log the request time */
2262 s->logs.tv_request = now;
2263 req->analysers &= AN_REQ_FLT_END;
2264
2265 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002266 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet24231ab2020-01-24 17:44:23 +01002267 }
2268
2269 if (!(s->flags & SF_ERR_MASK))
2270 s->flags |= SF_ERR_LOCAL;
2271 if (!(s->flags & SF_FINST_MASK))
2272 s->flags |= ((rule->from == ACT_F_HTTP_REQ) ? SF_FINST_R : SF_FINST_H);
2273
Christopher Faulet0e2ad612020-05-13 16:38:37 +02002274 return ACT_RET_ABRT;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002275}
2276
Christopher Faulet24231ab2020-01-24 17:44:23 +01002277/* Parse a "return" action. It returns ACT_RET_PRS_OK on success,
Christopher Faulet47e791e2020-05-13 14:36:55 +02002278 * ACT_RET_PRS_ERR on error. It relies on http_parse_http_reply() to set
2279 * <.arg.http_reply>.
Christopher Faulet24231ab2020-01-24 17:44:23 +01002280 */
2281static enum act_parse_ret parse_http_return(const char **args, int *orig_arg, struct proxy *px,
2282 struct act_rule *rule, char **err)
2283{
Christopher Faulet47e791e2020-05-13 14:36:55 +02002284 /* Prepare parsing of log-format strings */
2285 px->conf.args.ctx = ((rule->from == ACT_F_HTTP_REQ) ? ARGC_HRQ : ARGC_HRS);
2286 rule->arg.http_reply = http_parse_http_reply(args, orig_arg, px, 200, err);
2287 if (!rule->arg.http_reply)
2288 return ACT_RET_PRS_ERR;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002289
Christopher Fauletba946bf2020-05-13 08:50:07 +02002290 rule->flags |= ACT_FLAG_FINAL;
Christopher Faulet5ff0c642020-05-12 18:33:37 +02002291 rule->action = ACT_CUSTOM;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002292 rule->check_ptr = check_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002293 rule->action_ptr = http_action_return;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002294 rule->release_ptr = release_act_http_reply;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002295 return ACT_RET_PRS_OK;
Christopher Faulet24231ab2020-01-24 17:44:23 +01002296}
2297
Christopher Faulet021a8e42021-03-29 10:46:38 +02002298
2299
2300/* This function executes a wait-for-body action. It waits for the message
2301 * payload for a max configured time (.arg.p[0]) and eventually for only first
2302 * <arg.p[1]> bytes (0 means no limit). It relies on http_wait_for_msg_body()
2303 * function. it returns ACT_RET_CONT when conditions are met to stop to wait.
2304 * Otherwise ACT_RET_YIELD is returned to wait for more data. ACT_RET_INV is
2305 * returned if a parsing error is raised by lower level and ACT_RET_ERR if an
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05002306 * internal error occurred. Finally ACT_RET_ABRT is returned when a timeout
2307 * occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02002308 */
2309static enum act_return http_action_wait_for_body(struct act_rule *rule, struct proxy *px,
2310 struct session *sess, struct stream *s, int flags)
2311{
2312 struct channel *chn = ((rule->from == ACT_F_HTTP_REQ) ? &s->req : &s->res);
2313 unsigned int time = (uintptr_t)rule->arg.act.p[0];
2314 unsigned int bytes = (uintptr_t)rule->arg.act.p[1];
2315
2316 switch (http_wait_for_msg_body(s, chn, time, bytes)) {
2317 case HTTP_RULE_RES_CONT:
2318 return ACT_RET_CONT;
2319 case HTTP_RULE_RES_YIELD:
2320 return ACT_RET_YIELD;
2321 case HTTP_RULE_RES_BADREQ:
2322 return ACT_RET_INV;
2323 case HTTP_RULE_RES_ERROR:
2324 return ACT_RET_ERR;
2325 case HTTP_RULE_RES_ABRT:
2326 return ACT_RET_ABRT;
2327 default:
2328 return ACT_RET_ERR;
2329 }
2330}
2331
2332/* Parse a "wait-for-body" action. It returns ACT_RET_PRS_OK on success,
2333 * ACT_RET_PRS_ERR on error.
2334 */
2335static enum act_parse_ret parse_http_wait_for_body(const char **args, int *orig_arg, struct proxy *px,
2336 struct act_rule *rule, char **err)
2337{
2338 int cur_arg;
2339 unsigned int time, bytes;
2340 const char *res;
2341
2342 cur_arg = *orig_arg;
2343 if (!*args[cur_arg]) {
2344 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2345 return ACT_RET_PRS_ERR;
2346 }
2347
2348 time = UINT_MAX; /* To be sure it is set */
2349 bytes = 0; /* Default value, wait all the body */
2350 while (*(args[cur_arg])) {
2351 if (strcmp(args[cur_arg], "time") == 0) {
2352 if (!*args[cur_arg + 1]) {
2353 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2354 return ACT_RET_PRS_ERR;
2355 }
2356 res = parse_time_err(args[cur_arg+1], &time, TIME_UNIT_MS);
2357 if (res == PARSE_TIME_OVER) {
2358 memprintf(err, "time overflow (maximum value is 2147483647 ms or ~24.8 days)");
2359 return ACT_RET_PRS_ERR;
2360 }
2361 if (res == PARSE_TIME_UNDER) {
2362 memprintf(err, "time underflow (minimum non-null value is 1 ms)");
2363 return ACT_RET_PRS_ERR;
2364 }
2365 if (res) {
2366 memprintf(err, "unexpected character '%c'", *res);
2367 return ACT_RET_PRS_ERR;
2368 }
2369 cur_arg++;
2370 }
2371 else if (strcmp(args[cur_arg], "at-least") == 0) {
2372 if (!*args[cur_arg + 1]) {
2373 memprintf(err, "missing argument for '%s'", args[cur_arg]);
2374 return ACT_RET_PRS_ERR;
2375 }
2376 res = parse_size_err(args[cur_arg+1], &bytes);
2377 if (res) {
2378 memprintf(err, "unexpected character '%c'", *res);
2379 return ACT_RET_PRS_ERR;
2380 }
2381 cur_arg++;
2382 }
2383 else
2384 break;
2385 cur_arg++;
2386 }
2387
2388 if (time == UINT_MAX) {
2389 memprintf(err, "expects time <time> [ at-least <bytes> ]");
2390 return ACT_RET_PRS_ERR;
2391 }
2392
2393 rule->arg.act.p[0] = (void *)(uintptr_t)time;
2394 rule->arg.act.p[1] = (void *)(uintptr_t)bytes;
2395
2396 *orig_arg = cur_arg;
2397
2398 rule->action = ACT_CUSTOM;
2399 rule->action_ptr = http_action_wait_for_body;
2400 return ACT_RET_PRS_OK;
2401}
2402
Willy Tarreau79e57332018-10-02 16:01:16 +02002403/************************************************************************/
2404/* All supported http-request action keywords must be declared here. */
2405/************************************************************************/
2406
2407static struct action_kw_list http_req_actions = {
2408 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002409 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002410 { "add-header", parse_http_set_header, 0 },
2411 { "allow", parse_http_allow, 0 },
2412 { "auth", parse_http_auth, 0 },
2413 { "capture", parse_http_req_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002414 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002415 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002416 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002417 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002418 { "disable-l7-retry", parse_http_req_disable_l7_retry, 0 },
2419 { "early-hint", parse_http_set_header, 0 },
Amaury Denoyellea9e639a2021-05-06 15:50:12 +02002420 { "normalize-uri", parse_http_normalize_uri, KWF_EXPERIMENTAL },
Christopher Faulet81e20172019-12-12 16:40:30 +01002421 { "redirect", parse_http_redirect, 0 },
2422 { "reject", parse_http_action_reject, 0 },
2423 { "replace-header", parse_http_replace_header, 0 },
2424 { "replace-path", parse_replace_uri, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002425 { "replace-pathq", parse_replace_uri, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002426 { "replace-uri", parse_replace_uri, 0 },
2427 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002428 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002429 { "set-header", parse_http_set_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002430 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002431 { "set-method", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002432 { "set-path", parse_set_req_line, 0 },
Christopher Faulet312294f2020-09-02 17:17:44 +02002433 { "set-pathq", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002434 { "set-query", parse_set_req_line, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002435 { "set-uri", parse_set_req_line, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002436 { "strict-mode", parse_http_strict_mode, 0 },
Christopher Faulete0fca292020-01-13 21:49:03 +01002437 { "tarpit", parse_http_deny, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002438 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Amaury Denoyelle8d228232020-12-10 13:43:54 +01002439 { "set-timeout", parse_http_set_timeout, 0 },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002440 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002441 { NULL, NULL }
2442 }
2443};
2444
Willy Tarreau0108d902018-11-25 19:14:37 +01002445INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
2446
Willy Tarreau79e57332018-10-02 16:01:16 +02002447static struct action_kw_list http_res_actions = {
2448 .kw = {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002449 { "add-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002450 { "add-header", parse_http_set_header, 0 },
2451 { "allow", parse_http_allow, 0 },
2452 { "capture", parse_http_res_capture, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002453 { "del-acl", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002454 { "del-header", parse_http_del_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002455 { "del-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulete0fca292020-01-13 21:49:03 +01002456 { "deny", parse_http_deny, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002457 { "redirect", parse_http_redirect, 0 },
2458 { "replace-header", parse_http_replace_header, 0 },
2459 { "replace-value", parse_http_replace_header, 0 },
Christopher Faulet24231ab2020-01-24 17:44:23 +01002460 { "return", parse_http_return, 0 },
Christopher Faulet81e20172019-12-12 16:40:30 +01002461 { "set-header", parse_http_set_header, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002462 { "set-map", parse_http_set_map, KWF_MATCH_PREFIX },
Christopher Faulet81e20172019-12-12 16:40:30 +01002463 { "set-status", parse_http_set_status, 0 },
Christopher Faulet46f95542019-12-20 10:07:22 +01002464 { "strict-mode", parse_http_strict_mode, 0 },
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02002465 { "track-sc", parse_http_track_sc, KWF_MATCH_PREFIX },
Christopher Faulet021a8e42021-03-29 10:46:38 +02002466 { "wait-for-body", parse_http_wait_for_body, 0 },
Willy Tarreau79e57332018-10-02 16:01:16 +02002467 { NULL, NULL }
2468 }
2469};
2470
Willy Tarreau0108d902018-11-25 19:14:37 +01002471INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
Willy Tarreau79e57332018-10-02 16:01:16 +02002472
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002473static struct action_kw_list http_after_res_actions = {
2474 .kw = {
2475 { "add-header", parse_http_set_header, 0 },
2476 { "allow", parse_http_allow, 0 },
Christopher Fauletba8f0632021-12-06 08:43:22 +01002477 { "capture", parse_http_res_capture, 0 },
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002478 { "del-header", parse_http_del_header, 0 },
2479 { "replace-header", parse_http_replace_header, 0 },
2480 { "replace-value", parse_http_replace_header, 0 },
2481 { "set-header", parse_http_set_header, 0 },
2482 { "set-status", parse_http_set_status, 0 },
2483 { "strict-mode", parse_http_strict_mode, 0 },
2484 { NULL, NULL }
2485 }
2486};
2487
2488INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_actions);
2489
Willy Tarreau79e57332018-10-02 16:01:16 +02002490/*
2491 * Local variables:
2492 * c-indent-level: 8
2493 * c-basic-offset: 8
2494 * End:
2495 */