BUG/MEDIUM: http-act: Don't replace URI if path is not found or invalid
authorChristopher Faulet <cfaulet@haproxy.com>
Fri, 8 Apr 2022 08:44:21 +0000 (10:44 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 29 Apr 2022 13:24:45 +0000 (15:24 +0200)
For replace-path, replace-pathq and replace-uri actions, we must take care
to not match on the selected element if it is not defined.

regex_exec_match2() function expects to be called with a defined
subject. However, if the request path is invalid or not found, the function
is called with a NULL subject, leading to a crash when compiled without the
PRCE/PCRE2 support.

For instance the following rules crashes HAProxy on a CONNECT request:

  http-request replace-path /short/(.) /\1

This patch must be backported as far as 2.0.

(cherry picked from commit 114e759d5d5e9d93e0c5993f49e3de3ec5dcbf3b)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit 27ef430cd71c42453089747e5ccdfcf1a58efad8)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit 5d24269008f40df5a46ea6203251e72b89fffd52)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>

src/http_act.c

index 688c29a..ff804e5 100644 (file)
@@ -227,6 +227,9 @@ static enum act_return http_action_replace_uri(struct act_rule *rule, struct pro
        else if (rule->action == 4) // replace-pathq
                uri = http_get_path(uri);
 
+       if (!istlen(uri))
+               goto leave;
+
        if (!regex_exec_match2(rule->arg.http.re, uri.ptr, uri.len, MAX_MATCH, pmatch, 0))
                goto leave;