]> git.kaiwu.me - haproxy.git/commitdiff
MEDIUM: http/hardening: reject forbidden payload on 1xx/204 responses
authorWilly Tarreau <w@1wt.eu>
Mon, 23 Nov 2026 12:39:03 +0000 (13:39 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 23 Nov 2026 22:24:23 +0000 (23:24 +0100)
When a content-length greater than zero or a transfer-encoding header
are presented with responses that may not contain any body, the new
hardening rule "reject-forbidden-payload" will block them to limit the
risk of attacks (e.g. cache poisonning). It is enabled by default on
untrusted connections.

For h2 and h3 it's not easy to test. haproxy was used with an
http-request return action for this, along with a code modification
in http_parse_http_reply() to disable the 204+CL check.

doc/configuration.txt
include/haproxy/http-t.h
reg-tests/http-messaging/http_hardening_profiles.vtc
src/h1_htx.c
src/h2.c
src/h3.c
src/http.c

index 99acdfb0883cbc1ef25b2695b11881358d23fbe5..7eb20e2efe96757698cf74db65fc1cd876a07f5b 100644 (file)
@@ -35200,6 +35200,14 @@ http-hardening <name> [from <name2>]
         even if not rejected, duplicate host header fields are folded into a
         single one.
 
+  - reject-forbidden-payload (inbound response)
+        Rejects responses with status codes 204 or below 200 that may never
+        contain any payload, if a "content-length" header field is present with
+        a value greater than zero or if a "transfer-encoding" header field is
+        present. These may only be caused by bugs or by attempts by servers to
+        inject contents (e.g. cache poisonning attacks). It is enabled by
+        default on untrusted connections.
+
   - reject-header-trailer-folding (inbound request and response)
         Rejects requests and responses which contain folded header fields or
         trailer fields. Folding consists in having a single field span multiple
index 7832b260c0985e635a707299d651a6d5bc0f1bc0..18cf32131c3a96318b8ee962bd96e45510684ded 100644 (file)
@@ -256,6 +256,7 @@ struct http_violation {
 #define HTTP_HRD_REJ_CL_TE                  0x00000800
 #define HTTP_HRD_REJ_CL_LEAD_ZERO           0x00001000
 #define HTTP_HRD_REJ_CL_DUP                 0x00002000
+#define HTTP_HRD_REJ_FORB_PAYLOAD           0x00004000
 
 /* insert new masks above this line */
 /* update this one with new rejects */
@@ -267,7 +268,8 @@ struct http_violation {
                                              HTTP_HRD_REJ_CTL_IN_HDR     | \
                                              HTTP_HRD_REJ_CL_TE          | \
                                              HTTP_HRD_REJ_CL_LEAD_ZERO   | \
-                                             HTTP_HRD_REJ_CL_DUP)
+                                             HTTP_HRD_REJ_CL_DUP         | \
+                                             HTTP_HRD_REJ_FORB_PAYLOAD)
 
 #define HTTP_HRD_ALL_DELETE                 (HTTP_HRD_DEL_USCORE_HDR_NAM         | \
                                              HTTP_HRD_DEL_USCORE_TLR_NAM         | \
@@ -288,7 +290,8 @@ struct http_violation {
                                                 HTTP_HRD_DEL_USCORE_TLR_NAM  | \
                                                 HTTP_HRD_REJ_CL_TE           | \
                                                 HTTP_HRD_REJ_CL_LEAD_ZERO    | \
-                                                HTTP_HRD_REJ_CL_DUP)
+                                                HTTP_HRD_REJ_CL_DUP          | \
+                                                HTTP_HRD_REJ_FORB_PAYLOAD)
 
 #define HTTP_HRD_TRUSTED_DEFAULT               HTTP_HRD_DEFAULT
 
index f8b4b83b3a080eb6d575a7da921a8ab004bcf4ab..4b2f0cc5358c8c23af9654bbf94e1d248b6db3c6 100644 (file)
@@ -285,7 +285,7 @@ haproxy h2 -cli {
        expect !~ " on\n"
 
        send "show http-hardening untrusted"
-       expect ~ " 1 reject-duplicate-host-header +req +off\n 2 reject-lowercase-method +req +on\n 3 reject-underscore-in-header-name +- +off\n 4 reject-underscore-in-trailer-name +- +off\n 5 delete-underscore-in-header-name +- +on\n 6 delete-underscore-in-trailer-name +- +on\n 7 delete-unregistered-header-fields +- +off\n 8 delete-unregistered-trailer-fields +- +off\n 9 delete-all-trailer-fields +- +off\n10 reject-header-trailer-folding +- +on\n11 reject-ctl-in-header-trailer +- +on\n12 reject-content-length-with-te +- +on\n13 reject-content-length-leading-zero +- +on\n14 reject-duplicate-content-length +- +on\n"
+       expect ~ " 1 reject-duplicate-host-header +req +off\n 2 reject-lowercase-method +req +on\n 3 reject-underscore-in-header-name +- +off\n 4 reject-underscore-in-trailer-name +- +off\n 5 delete-underscore-in-header-name +- +on\n 6 delete-underscore-in-trailer-name +- +on\n 7 delete-unregistered-header-fields +- +off\n 8 delete-unregistered-trailer-fields +- +off\n 9 delete-all-trailer-fields +- +off\n10 reject-header-trailer-folding +- +on\n11 reject-ctl-in-header-trailer +- +on\n12 reject-content-length-with-te +- +on\n13 reject-content-length-leading-zero +- +on\n14 reject-duplicate-content-length +- +on\n15 reject-forbidden-payload +res +on\n"
 
        send "show http-hardening other"
        expect ~ " 2 reject-lowercase-method +req +off\n(.*\n)*10 reject-header-trailer-folding +- +on\n"
index ae848a54f086a10d3a3530e2be73ace46a164841..c9f37bbceb9491cd745f38978f9799849d262353 100644 (file)
@@ -355,6 +355,15 @@ static int h1_postparse_res_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx
                 (code == 204) || (code == 304)) {
                /* Responses known to have no body. */
                if ((code >= 100 && code < 200) || (code == 204)) {
+                       if (((h1m->flags & H1_MF_XFER_ENC) ||
+                            ((h1m->flags & H1_MF_CLEN) && h1m->body_len)) &&
+                           http_harden(HTTP_HRD_REJ_FORB_PAYLOAD)) {
+                               /* a non-null content-length or a transfer-encoding
+                                * with a bodyless status code.
+                                */
+                               goto error;
+                       }
+
                        if (h1m->flags & H1_MF_XFER_ENC)
                                http_del_hdr(hdrs, ist("transfer-encoding"));
                        if (h1m->flags & H1_MF_CLEN)
index 5f635e21f768da61ed93f2fa19e5f535ae81165a..d49c042fb572c105ed66bfc6ceb17bfdc1441946 100644 (file)
--- a/src/h2.c
+++ b/src/h2.c
@@ -807,6 +807,13 @@ int h2_make_htx_response(struct http_hdr *list, struct htx *htx, unsigned int *m
                        goto fail;
        }
 
+       if (sl->info.res.status < 200 || sl->info.res.status == 204) {
+               /* these responses must not have a payload */
+               if ((*msgf & H2_MSGF_BODY_CL) && *body_len &&
+                   http_harden(HTTP_HRD_REJ_FORB_PAYLOAD))
+                       goto fail;
+       }
+
        if (sl->info.res.status == 101 && upgrade_protocol) {
                if (!htx_add_header(htx, ist("connection"), ist("upgrade")))
                        goto fail;
index 84e89f47daa05cbb535453af4be1d41ed2c65ca8..4cd378fd9ab47d450f3dae283b80fdb799344d73 100644 (file)
--- a/src/h3.c
+++ b/src/h3.c
@@ -1523,6 +1523,17 @@ static ssize_t h3_resp_headers_to_htx(struct qcs *qcs, const struct buffer *buf,
                goto out;
        }
 
+       if (sl->info.res.status < 200 || sl->info.res.status == 204) {
+               /* these responses must not have a payload */
+               if ((h3s->flags & H3_SF_HAVE_CLEN) && h3s->body_len && http_harden(HTTP_HRD_REJ_FORB_PAYLOAD)) {
+                       TRACE_ERROR("unexpected response payload", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
+                       h3s->err = H3_ERR_MESSAGE_ERROR;
+                       qcc_report_glitch(h3c->qcc, 1);
+                       len = -1;
+                       goto out;
+               }
+       }
+
        /* Detect if an interim or final response was handled. */
        if (sl->info.res.status >= 100 && sl->info.res.status < 200) {
                TRACE_USER("receiving interim response", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs);
index 2b12121a2cb3767aee12d587b1a0b6601de11183..20979b62224f79eaa31f3b35b9a84b51e2d67612 100644 (file)
@@ -1801,6 +1801,7 @@ const struct http_hardening_kw http_hardening_kws[] = {
        { "reject-content-length-with-te",     HTTP_HRD_REJ_CL_TE, HTTP_DIR_BOTH },
        { "reject-content-length-leading-zero",HTTP_HRD_REJ_CL_LEAD_ZERO, HTTP_DIR_BOTH },
        { "reject-duplicate-content-length",   HTTP_HRD_REJ_CL_DUP, HTTP_DIR_BOTH },
+       { "reject-forbidden-payload",          HTTP_HRD_REJ_FORB_PAYLOAD, HTTP_DIR_RES },
        /* insert new hardening options above this line */
        { "all-reject",                     HTTP_HRD_ALL_REJECT,       HTTP_DIR_BOTH },
        { "all-delete",                     HTTP_HRD_ALL_DELETE,       HTTP_DIR_BOTH },