]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MINOR: http-ana: don't emit a network-path Location for a "/" redirect prefix
authorGihyeon Ryu <rlgusryu@gmail.com>
Wed, 11 Nov 2026 10:00:12 +0000 (10:00 +0000)
committerOlivier Houchard <cognet@ci0.org>
Fri, 13 Nov 2026 15:55:27 +0000 (16:55 +0100)
With the special prefix "/" (redirect prefix /, or server ... redir /),
or a log-format prefix which evaluates to an empty string, the Location
header is the request path alone. A request for "//evil.example/x" or
"/\evil.example/x" then gives "Location: //evil.example/x", which
browsers resolve to another host: an open redirect on a rule which is
only meant to keep the client on the same site.

When nothing is written before the path and it starts with "//" or "/\",
"/." is now written first. "/.//evil.example/x" resolves to the same
path on the same host. Prefixes which write something are unchanged.

The reg-test redirect-self-path.vtc covers it.

This should be backported to all stable branches.

reg-tests/http-rules/redirect-self-path.vtc [new file with mode: 0644]
src/http_ana.c

diff --git a/reg-tests/http-rules/redirect-self-path.vtc b/reg-tests/http-rules/redirect-self-path.vtc
new file mode 100644 (file)
index 0000000..f8d5a96
--- /dev/null
@@ -0,0 +1,102 @@
+varnishtest "redirect to the same path must not produce a network-path reference"
+
+# With the special prefix "/" (and with a prefix which evaluates to an empty
+# string), the Location header carries the request path alone. A path starting
+# with "//" or "/\" must not be emitted as is, otherwise browsers redirect to
+# the host which follows ("//evil.example/x" -> https://evil.example/x).
+
+feature ignore_unknown_macro
+
+server s1 {
+} -start
+
+haproxy h1 -conf {
+    global
+    .if feature(THREAD)
+        thread-groups 1
+    .endif
+
+    defaults
+        mode http
+        timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
+        timeout client  "${HAPROXY_TEST_TIMEOUT-5s}"
+        timeout server  "${HAPROXY_TEST_TIMEOUT-5s}"
+
+    frontend fe1
+        bind "fd@${fe1}"
+        redirect prefix / set-cookie SEEN=1
+
+    frontend fe2
+        bind "fd@${fe2}"
+        http-request redirect prefix / code 301 drop-query append-slash
+
+    frontend fe3
+        bind "fd@${fe3}"
+        http-request redirect prefix "%[var(txn.nope)]"
+
+    frontend fe4
+        bind "fd@${fe4}"
+        http-request redirect prefix //cdn.example
+
+    frontend fe5
+        bind "fd@${fe5}"
+        default_backend be5
+
+    backend be5
+        server s1 ${s1_addr}:${s1_port} redir /
+} -start
+
+client c1 -connect ${h1_fe1_sock} {
+    txreq -url "//evil.example/x"
+    rxresp
+    expect resp.status == 302
+    expect resp.http.location == "/.//evil.example/x"
+    txreq -url "/\\evil.example/x"
+    rxresp
+    expect resp.status == 302
+    expect resp.http.location == "/./\\evil.example/x"
+    txreq -url "http://h1//evil.example/x" -hdr "host: h1"
+    rxresp
+    expect resp.status == 302
+    expect resp.http.location == "/.//evil.example/x"
+    txreq -url "/ok/x?q=1"
+    rxresp
+    expect resp.status == 302
+    expect resp.http.location == "/ok/x?q=1"
+} -run
+
+client c2 -connect ${h1_fe2_sock} {
+    txreq -url "//evil.example?q=1"
+    rxresp
+    expect resp.status == 301
+    expect resp.http.location == "/.//evil.example/"
+    txreq -url "/ok?q=1"
+    rxresp
+    expect resp.status == 301
+    expect resp.http.location == "/ok/"
+} -run
+
+client c3 -connect ${h1_fe3_sock} {
+    txreq -url "//evil.example/x"
+    rxresp
+    expect resp.status == 302
+    expect resp.http.location == "/.//evil.example/x"
+} -run
+
+client c4 -connect ${h1_fe4_sock} {
+    txreq -url "//x"
+    rxresp
+    expect resp.status == 302
+    expect resp.http.location == "//cdn.example//x"
+} -run
+
+client c5 -connect ${h1_fe5_sock} {
+    txreq -url "//evil.example/x"
+    rxresp
+    expect resp.status == 302
+    expect resp.http.location == "/.//evil.example/x"
+    txreq -url "/ok"
+    rxresp
+    expect resp.status == 302
+    expect resp.http.location == "/ok"
+} -run
index 1afa11b65cd8d999fbb3c8ff0051792cae368059..130fa5974db094addcf13839a6ecfe84f83f81fa 100644 (file)
@@ -2400,6 +2400,17 @@ int http_response_forward_body(struct stream *s, struct channel *res, int an_bit
        return 0;
 }
 
+/* Don't emit a path which browsers would resolve as a network-path reference
+ * ("//host" or "/\host"): prepend "/." so it stays on the same host. Returns
+ * 0 if <chunk> is too small.
+ */
+static int http_redirect_protect_self_path(struct buffer *chunk, const struct ist path)
+{
+       if (path.len >= 2 && path.ptr[0] == '/' && (path.ptr[1] == '/' || path.ptr[1] == '\\'))
+               return chunk_memcat(chunk, "/.", 2);
+       return 1;
+}
+
 /* Perform an HTTP redirect based on the information in <rule>. The function
  * returns zero in case of an irrecoverable error such as too large a request
  * to build a valid response, 1 in case of successful redirect (hence the rule
@@ -2524,6 +2535,10 @@ int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struc
                                                             &rule->rdr_fmt);
                        }
 
+                       /* no prefix (special prefix "/" or empty log format) */
+                       if (!chunk->data && !http_redirect_protect_self_path(chunk, path))
+                               goto fail;
+
                        /* add path */
                        if (!chunk_memcat(chunk, path.ptr, path.len))
                                goto fail;
@@ -4500,6 +4515,10 @@ void http_perform_server_redirect(struct stream *s, struct stconn *sc)
        if (!isttest(path))
                return;
 
+       /* no prefix (special prefix "/") */
+       if (!trash.data && !http_redirect_protect_self_path(&trash, path))
+               return;
+
        if (!chunk_memcat(&trash, path.ptr, path.len))
                return;
        location = ist2(trash.area, trash.data);