From: Remi Tricot-Le Breton Date: Thu, 19 Nov 2026 15:44:02 +0000 (+0100) Subject: BUG/MINOR: httpclient/cli: fix NULL dereference when the response has no header X-Git-Url: https://git.kaiwu.me/http/ngx_http_auth_basic_module.html?a=commitdiff_plain;h=bd2c92daba75b31b72375b1636c29bfc904275ec;p=haproxy.git BUG/MINOR: httpclient/cli: fix NULL dereference when the response has no header 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. --- diff --git a/reg-tests/cli/httpclient_no_headers.vtc b/reg-tests/cli/httpclient_no_headers.vtc new file mode 100644 index 000000000..5ac28bd81 --- /dev/null +++ b/reg-tests/cli/httpclient_no_headers.vtc @@ -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" +} diff --git a/src/httpclient_cli.c b/src/httpclient_cli.c index 7de4c8911..5e4e58d01 100644 --- a/src/httpclient_cli.c +++ b/src/httpclient_cli.c @@ -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; }