2018-10-02 10:43:32 -04:00
|
|
|
/*
|
|
|
|
|
* HTTP rules parsing and registration
|
|
|
|
|
*
|
|
|
|
|
* Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
2020-06-04 13:11:43 -04:00
|
|
|
#include <haproxy/acl.h>
|
2020-06-04 04:15:32 -04:00
|
|
|
#include <haproxy/action.h>
|
2020-05-27 06:58:42 -04:00
|
|
|
#include <haproxy/api.h>
|
2020-06-09 03:07:15 -04:00
|
|
|
#include <haproxy/arg.h>
|
|
|
|
|
#include <haproxy/capture-t.h>
|
2020-06-04 18:00:29 -04:00
|
|
|
#include <haproxy/cfgparse.h>
|
2020-06-02 04:22:45 -04:00
|
|
|
#include <haproxy/chunk.h>
|
2020-06-04 17:46:14 -04:00
|
|
|
#include <haproxy/global.h>
|
2020-06-02 13:11:26 -04:00
|
|
|
#include <haproxy/http.h>
|
2021-10-06 10:38:53 -04:00
|
|
|
#include <haproxy/http_ana-t.h>
|
2020-06-04 05:40:28 -04:00
|
|
|
#include <haproxy/http_rules.h>
|
2020-06-05 11:27:29 -04:00
|
|
|
#include <haproxy/log.h>
|
2020-06-02 03:38:52 -04:00
|
|
|
#include <haproxy/pool.h>
|
2021-05-08 14:30:37 -04:00
|
|
|
#include <haproxy/proxy.h>
|
2020-06-09 03:07:15 -04:00
|
|
|
#include <haproxy/sample.h>
|
2020-06-03 12:09:46 -04:00
|
|
|
#include <haproxy/tools.h>
|
2020-05-27 09:59:00 -04:00
|
|
|
#include <haproxy/version.h>
|
2018-10-02 10:43:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/* List head of all known action keywords for "http-request" */
|
|
|
|
|
struct action_kw_list http_req_keywords = {
|
|
|
|
|
.list = LIST_HEAD_INIT(http_req_keywords.list)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* List head of all known action keywords for "http-response" */
|
|
|
|
|
struct action_kw_list http_res_keywords = {
|
|
|
|
|
.list = LIST_HEAD_INIT(http_res_keywords.list)
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-22 03:26:35 -05:00
|
|
|
/* List head of all known action keywords for "http-after-response" */
|
|
|
|
|
struct action_kw_list http_after_res_keywords = {
|
|
|
|
|
.list = LIST_HEAD_INIT(http_after_res_keywords.list)
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-02 08:50:38 -05:00
|
|
|
void http_req_keywords_register(struct action_kw_list *kw_list)
|
|
|
|
|
{
|
|
|
|
|
LIST_APPEND(&http_req_keywords.list, &kw_list->list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void http_res_keywords_register(struct action_kw_list *kw_list)
|
|
|
|
|
{
|
|
|
|
|
LIST_APPEND(&http_res_keywords.list, &kw_list->list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void http_after_res_keywords_register(struct action_kw_list *kw_list)
|
|
|
|
|
{
|
|
|
|
|
LIST_APPEND(&http_after_res_keywords.list, &kw_list->list);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 10:43:32 -04:00
|
|
|
/*
|
|
|
|
|
* Return the struct http_req_action_kw associated to a keyword.
|
|
|
|
|
*/
|
2020-11-28 11:40:24 -05:00
|
|
|
struct action_kw *action_http_req_custom(const char *kw)
|
2018-10-02 10:43:32 -04:00
|
|
|
{
|
|
|
|
|
return action_lookup(&http_req_keywords.list, kw);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Return the struct http_res_action_kw associated to a keyword.
|
|
|
|
|
*/
|
2020-11-28 11:40:24 -05:00
|
|
|
struct action_kw *action_http_res_custom(const char *kw)
|
2018-10-02 10:43:32 -04:00
|
|
|
{
|
|
|
|
|
return action_lookup(&http_res_keywords.list, kw);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-22 03:26:35 -05:00
|
|
|
/*
|
|
|
|
|
* Return the struct http_after_res_action_kw associated to a keyword.
|
|
|
|
|
*/
|
2020-11-28 11:40:24 -05:00
|
|
|
struct action_kw *action_http_after_res_custom(const char *kw)
|
2020-01-22 03:26:35 -05:00
|
|
|
{
|
|
|
|
|
return action_lookup(&http_after_res_keywords.list, kw);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 10:43:32 -04:00
|
|
|
/* parse an "http-request" rule */
|
|
|
|
|
struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
|
|
|
|
|
{
|
|
|
|
|
struct act_rule *rule;
|
2021-03-12 06:01:34 -05:00
|
|
|
const struct action_kw *custom = NULL;
|
2018-10-02 10:43:32 -04:00
|
|
|
int cur_arg;
|
|
|
|
|
|
2021-10-11 02:49:26 -04:00
|
|
|
rule = new_act_rule(ACT_F_HTTP_REQ, file, linenum);
|
2018-10-02 10:43:32 -04:00
|
|
|
if (!rule) {
|
|
|
|
|
ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
|
2022-03-21 03:21:19 -04:00
|
|
|
goto out;
|
2018-10-02 10:43:32 -04:00
|
|
|
}
|
|
|
|
|
|
2019-12-12 10:40:30 -05:00
|
|
|
if (((custom = action_http_req_custom(args[0])) != NULL)) {
|
2018-10-02 10:43:32 -04:00
|
|
|
char *errmsg = NULL;
|
|
|
|
|
|
|
|
|
|
cur_arg = 1;
|
|
|
|
|
/* try in the module list */
|
|
|
|
|
rule->kw = custom;
|
2021-05-07 08:25:01 -04:00
|
|
|
|
|
|
|
|
if (custom->flags & KWF_EXPERIMENTAL) {
|
|
|
|
|
if (!experimental_directives_allowed) {
|
|
|
|
|
ha_alert("parsing [%s:%d] : '%s' action is experimental, must be allowed via a global 'expose-experimental-directives'\n",
|
|
|
|
|
file, linenum, custom->kw);
|
|
|
|
|
goto out_err;
|
|
|
|
|
}
|
|
|
|
|
mark_tainted(TAINTED_CONFIG_EXP_KW_DECLARED);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 10:43:32 -04:00
|
|
|
if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
|
|
|
|
|
ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
|
|
|
|
|
file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
|
|
|
|
|
free(errmsg);
|
|
|
|
|
goto out_err;
|
|
|
|
|
}
|
2019-12-12 10:40:30 -05:00
|
|
|
else if (errmsg) {
|
|
|
|
|
ha_warning("parsing [%s:%d] : %s.\n", file, linenum, errmsg);
|
|
|
|
|
free(errmsg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2021-03-12 06:01:34 -05:00
|
|
|
const char *best = action_suggest(args[0], &http_req_keywords.list, NULL);
|
|
|
|
|
|
2018-10-02 10:43:32 -04:00
|
|
|
action_build_list(&http_req_keywords.list, &trash);
|
2021-03-12 06:01:34 -05:00
|
|
|
ha_alert("parsing [%s:%d]: 'http-request' expects %s, but got '%s'%s.%s%s%s\n",
|
|
|
|
|
file, linenum, trash.area,
|
|
|
|
|
args[0], *args[0] ? "" : " (missing argument)",
|
|
|
|
|
best ? " Did you mean '" : "",
|
|
|
|
|
best ? best : "",
|
|
|
|
|
best ? "' maybe ?" : "");
|
2018-10-02 10:43:32 -04:00
|
|
|
goto out_err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
|
|
|
|
|
struct acl_cond *cond;
|
|
|
|
|
char *errmsg = NULL;
|
|
|
|
|
|
|
|
|
|
if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
|
|
|
|
|
ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
|
|
|
|
|
file, linenum, args[0], errmsg);
|
|
|
|
|
free(errmsg);
|
|
|
|
|
goto out_err;
|
|
|
|
|
}
|
|
|
|
|
rule->cond = cond;
|
|
|
|
|
}
|
|
|
|
|
else if (*args[cur_arg]) {
|
2019-12-12 10:40:30 -05:00
|
|
|
ha_alert("parsing [%s:%d]: 'http-request %s' expects"
|
2018-10-02 10:43:32 -04:00
|
|
|
" either 'if' or 'unless' followed by a condition but found '%s'.\n",
|
|
|
|
|
file, linenum, args[0], args[cur_arg]);
|
|
|
|
|
goto out_err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rule;
|
|
|
|
|
out_err:
|
2022-03-17 15:29:06 -04:00
|
|
|
free_act_rule(rule);
|
2022-03-21 03:21:19 -04:00
|
|
|
out:
|
2018-10-02 10:43:32 -04:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* parse an "http-respose" rule */
|
|
|
|
|
struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
|
|
|
|
|
{
|
|
|
|
|
struct act_rule *rule;
|
2021-03-12 06:01:34 -05:00
|
|
|
const struct action_kw *custom = NULL;
|
2018-10-02 10:43:32 -04:00
|
|
|
int cur_arg;
|
|
|
|
|
|
2021-10-11 02:49:26 -04:00
|
|
|
rule = new_act_rule(ACT_F_HTTP_RES, file, linenum);
|
2018-10-02 10:43:32 -04:00
|
|
|
if (!rule) {
|
|
|
|
|
ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
|
2022-03-21 03:21:19 -04:00
|
|
|
goto out;
|
2018-10-02 10:43:32 -04:00
|
|
|
}
|
|
|
|
|
|
2019-12-12 10:40:30 -05:00
|
|
|
if (((custom = action_http_res_custom(args[0])) != NULL)) {
|
2018-10-02 10:43:32 -04:00
|
|
|
char *errmsg = NULL;
|
|
|
|
|
|
|
|
|
|
cur_arg = 1;
|
|
|
|
|
/* try in the module list */
|
|
|
|
|
rule->kw = custom;
|
2021-05-07 08:25:01 -04:00
|
|
|
|
|
|
|
|
if (custom->flags & KWF_EXPERIMENTAL) {
|
|
|
|
|
if (!experimental_directives_allowed) {
|
|
|
|
|
ha_alert("parsing [%s:%d] : '%s' action is experimental, must be allowed via a global 'expose-experimental-directives'\n",
|
|
|
|
|
file, linenum, custom->kw);
|
|
|
|
|
goto out_err;
|
|
|
|
|
}
|
|
|
|
|
mark_tainted(TAINTED_CONFIG_EXP_KW_DECLARED);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 10:43:32 -04:00
|
|
|
if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
|
|
|
|
|
ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
|
|
|
|
|
file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
|
|
|
|
|
free(errmsg);
|
|
|
|
|
goto out_err;
|
|
|
|
|
}
|
2019-12-12 10:40:30 -05:00
|
|
|
else if (errmsg) {
|
|
|
|
|
ha_warning("parsing [%s:%d] : %s.\n", file, linenum, errmsg);
|
|
|
|
|
free(errmsg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2021-03-12 06:01:34 -05:00
|
|
|
const char *best = action_suggest(args[0], &http_res_keywords.list, NULL);
|
|
|
|
|
|
2018-10-02 10:43:32 -04:00
|
|
|
action_build_list(&http_res_keywords.list, &trash);
|
2021-03-12 06:01:34 -05:00
|
|
|
ha_alert("parsing [%s:%d]: 'http-response' expects %s, but got '%s'%s.%s%s%s\n",
|
|
|
|
|
file, linenum, trash.area,
|
|
|
|
|
args[0], *args[0] ? "" : " (missing argument)",
|
|
|
|
|
best ? " Did you mean '" : "",
|
|
|
|
|
best ? best : "",
|
|
|
|
|
best ? "' maybe ?" : "");
|
2018-10-02 10:43:32 -04:00
|
|
|
goto out_err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
|
|
|
|
|
struct acl_cond *cond;
|
|
|
|
|
char *errmsg = NULL;
|
|
|
|
|
|
|
|
|
|
if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
|
|
|
|
|
ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
|
|
|
|
|
file, linenum, args[0], errmsg);
|
|
|
|
|
free(errmsg);
|
|
|
|
|
goto out_err;
|
|
|
|
|
}
|
|
|
|
|
rule->cond = cond;
|
|
|
|
|
}
|
|
|
|
|
else if (*args[cur_arg]) {
|
|
|
|
|
ha_alert("parsing [%s:%d]: 'http-response %s' expects"
|
|
|
|
|
" either 'if' or 'unless' followed by a condition but found '%s'.\n",
|
|
|
|
|
file, linenum, args[0], args[cur_arg]);
|
|
|
|
|
goto out_err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rule;
|
|
|
|
|
out_err:
|
2022-03-17 15:29:06 -04:00
|
|
|
free_act_rule(rule);
|
2022-03-21 03:21:19 -04:00
|
|
|
out:
|
2018-10-02 10:43:32 -04:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-22 03:26:35 -05:00
|
|
|
|
|
|
|
|
/* parse an "http-after-response" rule */
|
|
|
|
|
struct act_rule *parse_http_after_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
|
|
|
|
|
{
|
|
|
|
|
struct act_rule *rule;
|
2021-03-12 06:01:34 -05:00
|
|
|
const struct action_kw *custom = NULL;
|
2020-01-22 03:26:35 -05:00
|
|
|
int cur_arg;
|
|
|
|
|
|
2021-10-11 02:49:26 -04:00
|
|
|
rule = new_act_rule(ACT_F_HTTP_RES, file, linenum);
|
2020-01-22 03:26:35 -05:00
|
|
|
if (!rule) {
|
|
|
|
|
ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
|
2022-03-21 03:21:19 -04:00
|
|
|
goto out;
|
2020-01-22 03:26:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (((custom = action_http_after_res_custom(args[0])) != NULL)) {
|
|
|
|
|
char *errmsg = NULL;
|
|
|
|
|
|
|
|
|
|
cur_arg = 1;
|
|
|
|
|
/* try in the module list */
|
|
|
|
|
rule->kw = custom;
|
|
|
|
|
if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
|
|
|
|
|
ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-after-response %s' rule : %s.\n",
|
|
|
|
|
file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
|
|
|
|
|
free(errmsg);
|
|
|
|
|
goto out_err;
|
|
|
|
|
}
|
|
|
|
|
else if (errmsg) {
|
|
|
|
|
ha_warning("parsing [%s:%d] : %s.\n", file, linenum, errmsg);
|
|
|
|
|
free(errmsg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2021-03-12 06:01:34 -05:00
|
|
|
const char *best = action_suggest(args[0], &http_after_res_keywords.list, NULL);
|
|
|
|
|
|
2020-01-22 03:26:35 -05:00
|
|
|
action_build_list(&http_after_res_keywords.list, &trash);
|
2021-03-12 06:01:34 -05:00
|
|
|
ha_alert("parsing [%s:%d]: 'http-after-response' expects %s, but got '%s'%s.%s%s%s\n",
|
|
|
|
|
file, linenum, trash.area,
|
|
|
|
|
args[0], *args[0] ? "" : " (missing argument)",
|
|
|
|
|
best ? " Did you mean '" : "",
|
|
|
|
|
best ? best : "",
|
|
|
|
|
best ? "' maybe ?" : "");
|
2020-01-22 03:26:35 -05:00
|
|
|
goto out_err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
|
|
|
|
|
struct acl_cond *cond;
|
|
|
|
|
char *errmsg = NULL;
|
|
|
|
|
|
|
|
|
|
if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
|
|
|
|
|
ha_alert("parsing [%s:%d] : error detected while parsing an 'http-after-response %s' condition : %s.\n",
|
|
|
|
|
file, linenum, args[0], errmsg);
|
|
|
|
|
free(errmsg);
|
|
|
|
|
goto out_err;
|
|
|
|
|
}
|
|
|
|
|
rule->cond = cond;
|
|
|
|
|
}
|
|
|
|
|
else if (*args[cur_arg]) {
|
|
|
|
|
ha_alert("parsing [%s:%d]: 'http-after-response %s' expects"
|
|
|
|
|
" either 'if' or 'unless' followed by a condition but found '%s'.\n",
|
|
|
|
|
file, linenum, args[0], args[cur_arg]);
|
|
|
|
|
goto out_err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rule;
|
|
|
|
|
out_err:
|
2022-03-17 15:29:06 -04:00
|
|
|
free_act_rule(rule);
|
2022-03-21 03:21:19 -04:00
|
|
|
out:
|
2020-01-22 03:26:35 -05:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-11 04:30:27 -04:00
|
|
|
/* completely free redirect rule */
|
|
|
|
|
void http_free_redirect_rule(struct redirect_rule *rdr)
|
|
|
|
|
{
|
2023-05-11 06:29:51 -04:00
|
|
|
free_acl_cond(rdr->cond);
|
2023-05-11 04:30:27 -04:00
|
|
|
free(rdr->rdr_str);
|
2024-11-18 12:03:23 -05:00
|
|
|
if ((rdr->flags & REDIRECT_FLAG_COOKIE_FMT))
|
|
|
|
|
lf_expr_deinit(&rdr->cookie.fmt);
|
|
|
|
|
else
|
|
|
|
|
istfree(&rdr->cookie.str);
|
2024-02-23 09:57:21 -05:00
|
|
|
lf_expr_deinit(&rdr->rdr_fmt);
|
2023-05-11 04:30:27 -04:00
|
|
|
free(rdr);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 10:43:32 -04:00
|
|
|
/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
|
|
|
|
|
* with <err> filled with the error message. If <use_fmt> is not null, builds a
|
|
|
|
|
* dynamic log-format rule instead of a static string. Parameter <dir> indicates
|
|
|
|
|
* the direction of the rule, and equals 0 for request, non-zero for responses.
|
|
|
|
|
*/
|
|
|
|
|
struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
|
|
|
|
|
const char **args, char **errmsg, int use_fmt, int dir)
|
|
|
|
|
{
|
BUG/MINOR: http_rules: fix errors paths in http_parse_redirect_rule()
http_parse_redirect_rule() doesn't perform enough checks around NULL
returning allocating functions.
Moreover, existing error paths don't perform cleanups. This could lead to
memory leaks.
Adding a few checks and a cleanup path to ensure memory errors are
properly handled and that no memory leaks occurs within the function
(already allocated structures are freed on error path).
It should partially fix GH #2130.
This patch depends on ("MINOR: proxy: add http_free_redirect_rule() function")
This could be backported up to 2.4. The patch is also relevant for
2.2 but "MINOR: proxy: add http_free_redirect_rule() function" would
need to be adapted first.
==
Backport notes:
-> For 2.2 only:
Replace:
(strcmp(args[cur_arg], "drop-query") == 0)
with:
(!strcmp(args[cur_arg],"drop-query"))
-> For 2.2 and 2.4:
Replace:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query', 'ignore-empty' or 'append-slash' (was '%s')",
with:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
2023-05-11 04:34:49 -04:00
|
|
|
struct redirect_rule *rule = NULL;
|
2018-10-02 10:43:32 -04:00
|
|
|
int cur_arg;
|
|
|
|
|
int type = REDIRECT_TYPE_NONE;
|
|
|
|
|
int code = 302;
|
|
|
|
|
const char *destination = NULL;
|
|
|
|
|
const char *cookie = NULL;
|
|
|
|
|
int cookie_set = 0;
|
2024-11-18 12:03:23 -05:00
|
|
|
size_t cookie_len = 0;
|
2020-01-28 03:13:41 -05:00
|
|
|
unsigned int flags = (!dir ? REDIRECT_FLAG_FROM_REQ : REDIRECT_FLAG_NONE);
|
2018-10-02 10:43:32 -04:00
|
|
|
struct acl_cond *cond = NULL;
|
|
|
|
|
|
|
|
|
|
cur_arg = 0;
|
|
|
|
|
while (*(args[cur_arg])) {
|
|
|
|
|
if (strcmp(args[cur_arg], "location") == 0) {
|
|
|
|
|
if (!*args[cur_arg + 1])
|
|
|
|
|
goto missing_arg;
|
|
|
|
|
|
|
|
|
|
type = REDIRECT_TYPE_LOCATION;
|
|
|
|
|
cur_arg++;
|
|
|
|
|
destination = args[cur_arg];
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp(args[cur_arg], "prefix") == 0) {
|
|
|
|
|
if (!*args[cur_arg + 1])
|
|
|
|
|
goto missing_arg;
|
|
|
|
|
type = REDIRECT_TYPE_PREFIX;
|
|
|
|
|
cur_arg++;
|
|
|
|
|
destination = args[cur_arg];
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp(args[cur_arg], "scheme") == 0) {
|
|
|
|
|
if (!*args[cur_arg + 1])
|
|
|
|
|
goto missing_arg;
|
|
|
|
|
|
|
|
|
|
type = REDIRECT_TYPE_SCHEME;
|
|
|
|
|
cur_arg++;
|
|
|
|
|
destination = args[cur_arg];
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp(args[cur_arg], "set-cookie") == 0) {
|
|
|
|
|
if (!*args[cur_arg + 1])
|
|
|
|
|
goto missing_arg;
|
|
|
|
|
|
|
|
|
|
cur_arg++;
|
|
|
|
|
cookie = args[cur_arg];
|
|
|
|
|
cookie_set = 1;
|
|
|
|
|
}
|
2024-11-18 12:03:23 -05:00
|
|
|
else if (strcmp(args[cur_arg], "set-cookie-fmt") == 0) {
|
|
|
|
|
if (!*args[cur_arg + 1])
|
|
|
|
|
goto missing_arg;
|
|
|
|
|
|
|
|
|
|
cur_arg++;
|
|
|
|
|
cookie = args[cur_arg];
|
|
|
|
|
cookie_set = 2;
|
|
|
|
|
}
|
2018-10-02 10:43:32 -04:00
|
|
|
else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
|
|
|
|
|
if (!*args[cur_arg + 1])
|
|
|
|
|
goto missing_arg;
|
|
|
|
|
|
|
|
|
|
cur_arg++;
|
|
|
|
|
cookie = args[cur_arg];
|
|
|
|
|
cookie_set = 0;
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp(args[cur_arg], "code") == 0) {
|
|
|
|
|
if (!*args[cur_arg + 1])
|
|
|
|
|
goto missing_arg;
|
|
|
|
|
|
|
|
|
|
cur_arg++;
|
|
|
|
|
code = atol(args[cur_arg]);
|
|
|
|
|
if (code < 301 || code > 308 || (code > 303 && code < 307)) {
|
|
|
|
|
memprintf(errmsg,
|
|
|
|
|
"'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
|
|
|
|
|
args[cur_arg - 1], args[cur_arg]);
|
BUG/MINOR: http_rules: fix errors paths in http_parse_redirect_rule()
http_parse_redirect_rule() doesn't perform enough checks around NULL
returning allocating functions.
Moreover, existing error paths don't perform cleanups. This could lead to
memory leaks.
Adding a few checks and a cleanup path to ensure memory errors are
properly handled and that no memory leaks occurs within the function
(already allocated structures are freed on error path).
It should partially fix GH #2130.
This patch depends on ("MINOR: proxy: add http_free_redirect_rule() function")
This could be backported up to 2.4. The patch is also relevant for
2.2 but "MINOR: proxy: add http_free_redirect_rule() function" would
need to be adapted first.
==
Backport notes:
-> For 2.2 only:
Replace:
(strcmp(args[cur_arg], "drop-query") == 0)
with:
(!strcmp(args[cur_arg],"drop-query"))
-> For 2.2 and 2.4:
Replace:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query', 'ignore-empty' or 'append-slash' (was '%s')",
with:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
2023-05-11 04:34:49 -04:00
|
|
|
goto err;
|
2018-10-02 10:43:32 -04:00
|
|
|
}
|
|
|
|
|
}
|
CLEANUP: Compare the return value of `XXXcmp()` functions with zero
According to coding-style.txt it is recommended to use:
`strcmp(a, b) == 0` instead of `!strcmp(a, b)`
So let's do this.
The change was performed by running the following (very long) coccinelle patch
on src/:
@@
statement S;
expression E;
expression F;
@@
if (
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) != 0
)
(
S
|
{ ... }
)
@@
statement S;
expression E;
expression F;
@@
if (
- !
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) == 0
)
(
S
|
{ ... }
)
@@
expression E;
expression F;
expression G;
@@
(
G &&
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) != 0
)
@@
expression E;
expression F;
expression G;
@@
(
G ||
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) != 0
)
@@
expression E;
expression F;
expression G;
@@
(
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) != 0
&& G
)
@@
expression E;
expression F;
expression G;
@@
(
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) != 0
|| G
)
@@
expression E;
expression F;
expression G;
@@
(
G &&
- !
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) == 0
)
@@
expression E;
expression F;
expression G;
@@
(
G ||
- !
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) == 0
)
@@
expression E;
expression F;
expression G;
@@
(
- !
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) == 0
&& G
)
@@
expression E;
expression F;
expression G;
@@
(
- !
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) == 0
|| G
)
@@
expression E;
expression F;
expression G;
@@
(
- !
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) == 0
)
2021-01-02 16:31:53 -05:00
|
|
|
else if (strcmp(args[cur_arg], "drop-query") == 0) {
|
2018-10-02 10:43:32 -04:00
|
|
|
flags |= REDIRECT_FLAG_DROP_QS;
|
|
|
|
|
}
|
2024-11-15 11:03:06 -05:00
|
|
|
else if (strcmp(args[cur_arg], "keep-query") == 0) {
|
|
|
|
|
flags |= REDIRECT_FLAG_KEEP_QS;
|
|
|
|
|
}
|
CLEANUP: Compare the return value of `XXXcmp()` functions with zero
According to coding-style.txt it is recommended to use:
`strcmp(a, b) == 0` instead of `!strcmp(a, b)`
So let's do this.
The change was performed by running the following (very long) coccinelle patch
on src/:
@@
statement S;
expression E;
expression F;
@@
if (
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) != 0
)
(
S
|
{ ... }
)
@@
statement S;
expression E;
expression F;
@@
if (
- !
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) == 0
)
(
S
|
{ ... }
)
@@
expression E;
expression F;
expression G;
@@
(
G &&
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) != 0
)
@@
expression E;
expression F;
expression G;
@@
(
G ||
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) != 0
)
@@
expression E;
expression F;
expression G;
@@
(
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) != 0
&& G
)
@@
expression E;
expression F;
expression G;
@@
(
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) != 0
|| G
)
@@
expression E;
expression F;
expression G;
@@
(
G &&
- !
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) == 0
)
@@
expression E;
expression F;
expression G;
@@
(
G ||
- !
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) == 0
)
@@
expression E;
expression F;
expression G;
@@
(
- !
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) == 0
&& G
)
@@
expression E;
expression F;
expression G;
@@
(
- !
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) == 0
|| G
)
@@
expression E;
expression F;
expression G;
@@
(
- !
(
dns_hostname_cmp
|
eb_memcmp
|
memcmp
|
strcasecmp
|
strcmp
|
strncasecmp
|
strncmp
)
- (E, F)
+ (E, F) == 0
)
2021-01-02 16:31:53 -05:00
|
|
|
else if (strcmp(args[cur_arg], "append-slash") == 0) {
|
2018-10-02 10:43:32 -04:00
|
|
|
flags |= REDIRECT_FLAG_APPEND_SLASH;
|
|
|
|
|
}
|
2021-09-02 10:54:33 -04:00
|
|
|
else if (strcmp(args[cur_arg], "ignore-empty") == 0) {
|
|
|
|
|
flags |= REDIRECT_FLAG_IGNORE_EMPTY;
|
|
|
|
|
}
|
2018-10-02 10:43:32 -04:00
|
|
|
else if (strcmp(args[cur_arg], "if") == 0 ||
|
|
|
|
|
strcmp(args[cur_arg], "unless") == 0) {
|
|
|
|
|
cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
|
|
|
|
|
if (!cond) {
|
|
|
|
|
memprintf(errmsg, "error in condition: %s", *errmsg);
|
BUG/MINOR: http_rules: fix errors paths in http_parse_redirect_rule()
http_parse_redirect_rule() doesn't perform enough checks around NULL
returning allocating functions.
Moreover, existing error paths don't perform cleanups. This could lead to
memory leaks.
Adding a few checks and a cleanup path to ensure memory errors are
properly handled and that no memory leaks occurs within the function
(already allocated structures are freed on error path).
It should partially fix GH #2130.
This patch depends on ("MINOR: proxy: add http_free_redirect_rule() function")
This could be backported up to 2.4. The patch is also relevant for
2.2 but "MINOR: proxy: add http_free_redirect_rule() function" would
need to be adapted first.
==
Backport notes:
-> For 2.2 only:
Replace:
(strcmp(args[cur_arg], "drop-query") == 0)
with:
(!strcmp(args[cur_arg],"drop-query"))
-> For 2.2 and 2.4:
Replace:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query', 'ignore-empty' or 'append-slash' (was '%s')",
with:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
2023-05-11 04:34:49 -04:00
|
|
|
goto err;
|
2018-10-02 10:43:32 -04:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
memprintf(errmsg,
|
2024-11-18 12:03:23 -05:00
|
|
|
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'set-cookie-fmt',"
|
|
|
|
|
" 'clear-cookie', 'drop-query', 'keep-query', 'ignore-empty' or 'append-slash' (was '%s')",
|
2018-10-02 10:43:32 -04:00
|
|
|
args[cur_arg]);
|
BUG/MINOR: http_rules: fix errors paths in http_parse_redirect_rule()
http_parse_redirect_rule() doesn't perform enough checks around NULL
returning allocating functions.
Moreover, existing error paths don't perform cleanups. This could lead to
memory leaks.
Adding a few checks and a cleanup path to ensure memory errors are
properly handled and that no memory leaks occurs within the function
(already allocated structures are freed on error path).
It should partially fix GH #2130.
This patch depends on ("MINOR: proxy: add http_free_redirect_rule() function")
This could be backported up to 2.4. The patch is also relevant for
2.2 but "MINOR: proxy: add http_free_redirect_rule() function" would
need to be adapted first.
==
Backport notes:
-> For 2.2 only:
Replace:
(strcmp(args[cur_arg], "drop-query") == 0)
with:
(!strcmp(args[cur_arg],"drop-query"))
-> For 2.2 and 2.4:
Replace:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query', 'ignore-empty' or 'append-slash' (was '%s')",
with:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
2023-05-11 04:34:49 -04:00
|
|
|
goto err;
|
2018-10-02 10:43:32 -04:00
|
|
|
}
|
|
|
|
|
cur_arg++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type == REDIRECT_TYPE_NONE) {
|
|
|
|
|
memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
|
BUG/MINOR: http_rules: fix errors paths in http_parse_redirect_rule()
http_parse_redirect_rule() doesn't perform enough checks around NULL
returning allocating functions.
Moreover, existing error paths don't perform cleanups. This could lead to
memory leaks.
Adding a few checks and a cleanup path to ensure memory errors are
properly handled and that no memory leaks occurs within the function
(already allocated structures are freed on error path).
It should partially fix GH #2130.
This patch depends on ("MINOR: proxy: add http_free_redirect_rule() function")
This could be backported up to 2.4. The patch is also relevant for
2.2 but "MINOR: proxy: add http_free_redirect_rule() function" would
need to be adapted first.
==
Backport notes:
-> For 2.2 only:
Replace:
(strcmp(args[cur_arg], "drop-query") == 0)
with:
(!strcmp(args[cur_arg],"drop-query"))
-> For 2.2 and 2.4:
Replace:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query', 'ignore-empty' or 'append-slash' (was '%s')",
with:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
2023-05-11 04:34:49 -04:00
|
|
|
goto err;
|
2018-10-02 10:43:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dir && type != REDIRECT_TYPE_LOCATION) {
|
|
|
|
|
memprintf(errmsg, "response only supports redirect type 'location'");
|
BUG/MINOR: http_rules: fix errors paths in http_parse_redirect_rule()
http_parse_redirect_rule() doesn't perform enough checks around NULL
returning allocating functions.
Moreover, existing error paths don't perform cleanups. This could lead to
memory leaks.
Adding a few checks and a cleanup path to ensure memory errors are
properly handled and that no memory leaks occurs within the function
(already allocated structures are freed on error path).
It should partially fix GH #2130.
This patch depends on ("MINOR: proxy: add http_free_redirect_rule() function")
This could be backported up to 2.4. The patch is also relevant for
2.2 but "MINOR: proxy: add http_free_redirect_rule() function" would
need to be adapted first.
==
Backport notes:
-> For 2.2 only:
Replace:
(strcmp(args[cur_arg], "drop-query") == 0)
with:
(!strcmp(args[cur_arg],"drop-query"))
-> For 2.2 and 2.4:
Replace:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query', 'ignore-empty' or 'append-slash' (was '%s')",
with:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
2023-05-11 04:34:49 -04:00
|
|
|
goto err;
|
2018-10-02 10:43:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rule = calloc(1, sizeof(*rule));
|
BUG/MINOR: http_rules: fix errors paths in http_parse_redirect_rule()
http_parse_redirect_rule() doesn't perform enough checks around NULL
returning allocating functions.
Moreover, existing error paths don't perform cleanups. This could lead to
memory leaks.
Adding a few checks and a cleanup path to ensure memory errors are
properly handled and that no memory leaks occurs within the function
(already allocated structures are freed on error path).
It should partially fix GH #2130.
This patch depends on ("MINOR: proxy: add http_free_redirect_rule() function")
This could be backported up to 2.4. The patch is also relevant for
2.2 but "MINOR: proxy: add http_free_redirect_rule() function" would
need to be adapted first.
==
Backport notes:
-> For 2.2 only:
Replace:
(strcmp(args[cur_arg], "drop-query") == 0)
with:
(!strcmp(args[cur_arg],"drop-query"))
-> For 2.2 and 2.4:
Replace:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query', 'ignore-empty' or 'append-slash' (was '%s')",
with:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
2023-05-11 04:34:49 -04:00
|
|
|
if (!rule)
|
|
|
|
|
goto out_of_memory;
|
2018-10-02 10:43:32 -04:00
|
|
|
rule->cond = cond;
|
2024-02-23 09:57:21 -05:00
|
|
|
lf_expr_init(&rule->rdr_fmt);
|
2018-10-02 10:43:32 -04:00
|
|
|
|
|
|
|
|
if (!use_fmt) {
|
|
|
|
|
/* old-style static redirect rule */
|
|
|
|
|
rule->rdr_str = strdup(destination);
|
BUG/MINOR: http_rules: fix errors paths in http_parse_redirect_rule()
http_parse_redirect_rule() doesn't perform enough checks around NULL
returning allocating functions.
Moreover, existing error paths don't perform cleanups. This could lead to
memory leaks.
Adding a few checks and a cleanup path to ensure memory errors are
properly handled and that no memory leaks occurs within the function
(already allocated structures are freed on error path).
It should partially fix GH #2130.
This patch depends on ("MINOR: proxy: add http_free_redirect_rule() function")
This could be backported up to 2.4. The patch is also relevant for
2.2 but "MINOR: proxy: add http_free_redirect_rule() function" would
need to be adapted first.
==
Backport notes:
-> For 2.2 only:
Replace:
(strcmp(args[cur_arg], "drop-query") == 0)
with:
(!strcmp(args[cur_arg],"drop-query"))
-> For 2.2 and 2.4:
Replace:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query', 'ignore-empty' or 'append-slash' (was '%s')",
with:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
2023-05-11 04:34:49 -04:00
|
|
|
if (!rule->rdr_str)
|
|
|
|
|
goto out_of_memory;
|
2018-10-02 10:43:32 -04:00
|
|
|
rule->rdr_len = strlen(destination);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* log-format based redirect rule */
|
2021-10-13 11:22:17 -04:00
|
|
|
int cap = 0;
|
2018-10-02 10:43:32 -04:00
|
|
|
|
|
|
|
|
/* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
|
|
|
|
|
* if prefix == "/", we don't want to add anything, otherwise it
|
|
|
|
|
* makes it hard for the user to configure a self-redirection.
|
|
|
|
|
*/
|
|
|
|
|
curproxy->conf.args.ctx = ARGC_RDR;
|
2021-10-13 11:22:17 -04:00
|
|
|
if (curproxy->cap & PR_CAP_FE)
|
|
|
|
|
cap |= (dir ? SMP_VAL_FE_HRS_HDR : SMP_VAL_FE_HRQ_HDR);
|
|
|
|
|
if (curproxy->cap & PR_CAP_BE)
|
|
|
|
|
cap |= (dir ? SMP_VAL_BE_HRS_HDR : SMP_VAL_BE_HRQ_HDR);
|
2018-10-02 10:43:32 -04:00
|
|
|
if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
|
2021-10-13 11:22:17 -04:00
|
|
|
if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP, cap, errmsg)) {
|
BUG/MINOR: http_rules: fix errors paths in http_parse_redirect_rule()
http_parse_redirect_rule() doesn't perform enough checks around NULL
returning allocating functions.
Moreover, existing error paths don't perform cleanups. This could lead to
memory leaks.
Adding a few checks and a cleanup path to ensure memory errors are
properly handled and that no memory leaks occurs within the function
(already allocated structures are freed on error path).
It should partially fix GH #2130.
This patch depends on ("MINOR: proxy: add http_free_redirect_rule() function")
This could be backported up to 2.4. The patch is also relevant for
2.2 but "MINOR: proxy: add http_free_redirect_rule() function" would
need to be adapted first.
==
Backport notes:
-> For 2.2 only:
Replace:
(strcmp(args[cur_arg], "drop-query") == 0)
with:
(!strcmp(args[cur_arg],"drop-query"))
-> For 2.2 and 2.4:
Replace:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query', 'ignore-empty' or 'append-slash' (was '%s')",
with:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
2023-05-11 04:34:49 -04:00
|
|
|
goto err;
|
2018-10-02 10:43:32 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cookie) {
|
|
|
|
|
/* depending on cookie_set, either we want to set the cookie, or to clear it.
|
|
|
|
|
* a clear consists in appending "; path=/; Max-Age=0;" at the end.
|
|
|
|
|
*/
|
2024-11-18 12:03:23 -05:00
|
|
|
cookie_len = strlen(cookie);
|
|
|
|
|
if (cookie_set == 1) { // set-cookie
|
|
|
|
|
rule->cookie.str = istalloc(cookie_len+9);
|
|
|
|
|
if (!isttest(rule->cookie.str))
|
BUG/MINOR: http_rules: fix errors paths in http_parse_redirect_rule()
http_parse_redirect_rule() doesn't perform enough checks around NULL
returning allocating functions.
Moreover, existing error paths don't perform cleanups. This could lead to
memory leaks.
Adding a few checks and a cleanup path to ensure memory errors are
properly handled and that no memory leaks occurs within the function
(already allocated structures are freed on error path).
It should partially fix GH #2130.
This patch depends on ("MINOR: proxy: add http_free_redirect_rule() function")
This could be backported up to 2.4. The patch is also relevant for
2.2 but "MINOR: proxy: add http_free_redirect_rule() function" would
need to be adapted first.
==
Backport notes:
-> For 2.2 only:
Replace:
(strcmp(args[cur_arg], "drop-query") == 0)
with:
(!strcmp(args[cur_arg],"drop-query"))
-> For 2.2 and 2.4:
Replace:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query', 'ignore-empty' or 'append-slash' (was '%s')",
with:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
2023-05-11 04:34:49 -04:00
|
|
|
goto out_of_memory;
|
2024-11-18 12:03:23 -05:00
|
|
|
istcpy(&rule->cookie.str, ist2(cookie, cookie_len), cookie_len);
|
|
|
|
|
istcat(&rule->cookie.str, ist2("; path=/;", 9), cookie_len+10);
|
|
|
|
|
}
|
|
|
|
|
else if (cookie_set == 2) { // set-cookie-fmt
|
|
|
|
|
int cap = 0;
|
|
|
|
|
|
|
|
|
|
lf_expr_init(&rule->cookie.fmt);
|
|
|
|
|
curproxy->conf.args.ctx = ARGC_RDR;
|
|
|
|
|
if (curproxy->cap & PR_CAP_FE)
|
|
|
|
|
cap |= (dir ? SMP_VAL_FE_HRS_HDR : SMP_VAL_FE_HRQ_HDR);
|
|
|
|
|
if (curproxy->cap & PR_CAP_BE)
|
|
|
|
|
cap |= (dir ? SMP_VAL_BE_HRS_HDR : SMP_VAL_BE_HRQ_HDR);
|
|
|
|
|
|
|
|
|
|
chunk_memcpy(&trash, cookie, cookie_len);
|
|
|
|
|
chunk_strcat(&trash, "; path=/;");
|
|
|
|
|
if (!parse_logformat_string(trash.area, curproxy, &rule->cookie.fmt, LOG_OPT_HTTP, cap, errmsg)) {
|
|
|
|
|
goto err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flags |= REDIRECT_FLAG_COOKIE_FMT;
|
|
|
|
|
}
|
|
|
|
|
else { // clear-cookie
|
|
|
|
|
rule->cookie.str = istalloc(cookie_len+20);
|
|
|
|
|
if (!isttest(rule->cookie.str))
|
BUG/MINOR: http_rules: fix errors paths in http_parse_redirect_rule()
http_parse_redirect_rule() doesn't perform enough checks around NULL
returning allocating functions.
Moreover, existing error paths don't perform cleanups. This could lead to
memory leaks.
Adding a few checks and a cleanup path to ensure memory errors are
properly handled and that no memory leaks occurs within the function
(already allocated structures are freed on error path).
It should partially fix GH #2130.
This patch depends on ("MINOR: proxy: add http_free_redirect_rule() function")
This could be backported up to 2.4. The patch is also relevant for
2.2 but "MINOR: proxy: add http_free_redirect_rule() function" would
need to be adapted first.
==
Backport notes:
-> For 2.2 only:
Replace:
(strcmp(args[cur_arg], "drop-query") == 0)
with:
(!strcmp(args[cur_arg],"drop-query"))
-> For 2.2 and 2.4:
Replace:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query', 'ignore-empty' or 'append-slash' (was '%s')",
with:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
2023-05-11 04:34:49 -04:00
|
|
|
goto out_of_memory;
|
2024-11-18 12:03:23 -05:00
|
|
|
istcpy(&rule->cookie.str, ist2(cookie, cookie_len), cookie_len);
|
|
|
|
|
istcat(&rule->cookie.str, ist2("; path=/; Max-Age=0;", 20), cookie_len+21);
|
2018-10-02 10:43:32 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
rule->type = type;
|
|
|
|
|
rule->code = code;
|
|
|
|
|
rule->flags = flags;
|
|
|
|
|
LIST_INIT(&rule->list);
|
|
|
|
|
return rule;
|
|
|
|
|
|
|
|
|
|
missing_arg:
|
|
|
|
|
memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
|
BUG/MINOR: http_rules: fix errors paths in http_parse_redirect_rule()
http_parse_redirect_rule() doesn't perform enough checks around NULL
returning allocating functions.
Moreover, existing error paths don't perform cleanups. This could lead to
memory leaks.
Adding a few checks and a cleanup path to ensure memory errors are
properly handled and that no memory leaks occurs within the function
(already allocated structures are freed on error path).
It should partially fix GH #2130.
This patch depends on ("MINOR: proxy: add http_free_redirect_rule() function")
This could be backported up to 2.4. The patch is also relevant for
2.2 but "MINOR: proxy: add http_free_redirect_rule() function" would
need to be adapted first.
==
Backport notes:
-> For 2.2 only:
Replace:
(strcmp(args[cur_arg], "drop-query") == 0)
with:
(!strcmp(args[cur_arg],"drop-query"))
-> For 2.2 and 2.4:
Replace:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query', 'ignore-empty' or 'append-slash' (was '%s')",
with:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
2023-05-11 04:34:49 -04:00
|
|
|
goto err;
|
|
|
|
|
out_of_memory:
|
|
|
|
|
memprintf(errmsg, "parsing [%s:%d]: out of memory.", file, linenum);
|
|
|
|
|
err:
|
|
|
|
|
if (rule)
|
|
|
|
|
http_free_redirect_rule(rule);
|
|
|
|
|
else if (cond) {
|
|
|
|
|
/* rule not yet allocated, but cond already is */
|
2023-05-11 06:29:51 -04:00
|
|
|
free_acl_cond(cond);
|
BUG/MINOR: http_rules: fix errors paths in http_parse_redirect_rule()
http_parse_redirect_rule() doesn't perform enough checks around NULL
returning allocating functions.
Moreover, existing error paths don't perform cleanups. This could lead to
memory leaks.
Adding a few checks and a cleanup path to ensure memory errors are
properly handled and that no memory leaks occurs within the function
(already allocated structures are freed on error path).
It should partially fix GH #2130.
This patch depends on ("MINOR: proxy: add http_free_redirect_rule() function")
This could be backported up to 2.4. The patch is also relevant for
2.2 but "MINOR: proxy: add http_free_redirect_rule() function" would
need to be adapted first.
==
Backport notes:
-> For 2.2 only:
Replace:
(strcmp(args[cur_arg], "drop-query") == 0)
with:
(!strcmp(args[cur_arg],"drop-query"))
-> For 2.2 and 2.4:
Replace:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query', 'ignore-empty' or 'append-slash' (was '%s')",
with:
"expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
2023-05-11 04:34:49 -04:00
|
|
|
}
|
|
|
|
|
|
2018-10-02 10:43:32 -04:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Local variables:
|
|
|
|
|
* c-indent-level: 8
|
|
|
|
|
* c-basic-offset: 8
|
|
|
|
|
* End:
|
|
|
|
|
*/
|