]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MINOR: httpclient/cli: fix NULL dereference when the response has no header
authorRemi Tricot-Le Breton <rlebreton@haproxy.com>
Thu, 19 Nov 2026 15:44:02 +0000 (16:44 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 20 Nov 2026 11:36:57 +0000 (12:36 +0100)
The CLI "httpclient" command (expert mode) displays the response status
line, then its headers, then its body. The httpclient applet only
allocates the response headers array when the response contains at least
one header.

A response without any header is perfectly legal for HTTP/1.0, so any
server which answers a header-less response crashes the process when the
"httpclient" command is used against it. Only an admin can run this
command, so it cannot be triggered by untrusted input and stays MEDIUM,
but a crash remains a crash for users who rely on this command to probe
their backends.

This patch must be backported to 3.3.

reg-tests/cli/httpclient_no_headers.vtc [new file with mode: 0644]
src/httpclient_cli.c

diff --git a/reg-tests/cli/httpclient_no_headers.vtc b/reg-tests/cli/httpclient_no_headers.vtc
new file mode 100644 (file)
index 0000000..5ac28bd
--- /dev/null
@@ -0,0 +1,45 @@
+varnishtest "CLI httpclient: response without any header"
+
+# A response may legitimately contain no header at all (HTTP/1.0 does not
+# require any). In that case the httpclient applet does not allocate its
+# response headers array but it still notifies the caller, which iterates
+# over it. The CLI applet did not check the array and dereferenced NULL.
+
+feature ignore_unknown_macro
+
+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 fe
+        bind "fd@${fe}"
+        http-request return status 200
+} -start
+
+# legal header-less response: the body is read until the connection closes
+server s1 {
+    rxreq
+    send "HTTP/1.0 200 OK\r\n\r\n"
+} -start
+
+# a response with headers, to be sure the normal dump still works
+server s2 {
+    rxreq
+    txresp -hdr "x-test: v"
+} -start
+
+haproxy h1 -cli {
+    send "expert-mode on; httpclient GET http://127.0.0.1:${s1_port}/"
+    expect ~ "\nHTTP/1.0 200 OK\n\r\n"
+
+    send "expert-mode on; httpclient GET http://127.0.0.1:${s2_port}/"
+    expect ~ "\nHTTP/1.1 200 OK\nx-test: v\r\n"
+}
index 7de4c891174df06c5efeb882081dddc2a25b6c69..5e4e58d01ddf7a7c1b693182acd0be7d2f14d115 100644 (file)
@@ -201,7 +201,7 @@ static int hc_cli_io_handler(struct appctx *appctx)
                if (!ctx->is_htx) {
                        chunk_reset(&trash);
                        hdrs = hc->res.hdrs;
-                       for (hdr = hdrs; isttest(hdr->v); hdr++) {
+                       for (hdr = hdrs; hdr && isttest(hdr->n); hdr++) {
                                if (!h1_format_htx_hdr(hdr->n, hdr->v, &trash, NULL))
                                        goto too_many_hdrs;
                        }