]> git.kaiwu.me - haproxy.git/commitdiff
MEDIUM: http/hardening: default to rejecting untrusted duplicate C-L
authorWilly Tarreau <w@1wt.eu>
Mon, 23 Nov 2026 10:46:27 +0000 (11:46 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 23 Nov 2026 22:24:23 +0000 (23:24 +0100)
We're currently doing great efforts trying to detect abnormal
content-length headers and even normalize duplicates, but it's still
possible for upstream agents to be fooled by them (e.g. sum them) and
pass them, so let's add "reject-duplicate-content-length", a new
hardening option to reject them, and enable it by default on public-
facing connections since this is really not something that is seen
outside of attacks.

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

index 408525b8b7636e34b06932dbc417506ef105eff7..99acdfb0883cbc1ef25b2695b11881358d23fbe5 100644 (file)
@@ -35183,6 +35183,17 @@ http-hardening <name> [from <name2>]
         listeners. See also the has_ctl() converter used for detecting them.
         It is enabled by default on untrusted connections.
 
+  - reject-duplicate-content-length (inbound request and response)
+        Rejects requests and responses having a duplicate Content-Length header
+        field even if all values perfectly match. While these are tolerated by
+        the standard and is properly sanitized by HAProxy, it can still fool
+        some other reverse-proxies seeing the message before HAProxy and not
+        parsing it correctly. These are still seldom seen in legacy client or
+        server applications, sometimes in relation with compression and/or
+        caching. While HAProxy properly normalizes them, it is generally safe
+        to reject them on public facing listeners. It is enabled by default on
+        untrusted connections.
+
   - reject-duplicate-host-header (inbound request)
         Rejects requests containing more than one "Host" header field, even if
         valid and identical. This is recommended on untrusted listeners. Note that
index 49ef142f5492ec24285adf6aa90fe8ed5052c527..7832b260c0985e635a707299d651a6d5bc0f1bc0 100644 (file)
@@ -255,6 +255,8 @@ struct http_violation {
 #define HTTP_HRD_REJ_CTL_IN_HDR             0x00000400
 #define HTTP_HRD_REJ_CL_TE                  0x00000800
 #define HTTP_HRD_REJ_CL_LEAD_ZERO           0x00001000
+#define HTTP_HRD_REJ_CL_DUP                 0x00002000
+
 /* insert new masks above this line */
 /* update this one with new rejects */
 #define HTTP_HRD_ALL_REJECT                 (HTTP_HRD_REJ_DUP_HOST_HDR   | \
@@ -264,7 +266,8 @@ struct http_violation {
                                              HTTP_HRD_REJ_HDR_FOLD       | \
                                              HTTP_HRD_REJ_CTL_IN_HDR     | \
                                              HTTP_HRD_REJ_CL_TE          | \
-                                             HTTP_HRD_REJ_CL_LEAD_ZERO)
+                                             HTTP_HRD_REJ_CL_LEAD_ZERO   | \
+                                             HTTP_HRD_REJ_CL_DUP)
 
 #define HTTP_HRD_ALL_DELETE                 (HTTP_HRD_DEL_USCORE_HDR_NAM         | \
                                              HTTP_HRD_DEL_USCORE_TLR_NAM         | \
@@ -284,7 +287,8 @@ struct http_violation {
                                                 HTTP_HRD_DEL_USCORE_HDR_NAM  | \
                                                 HTTP_HRD_DEL_USCORE_TLR_NAM  | \
                                                 HTTP_HRD_REJ_CL_TE           | \
-                                                HTTP_HRD_REJ_CL_LEAD_ZERO)
+                                                HTTP_HRD_REJ_CL_LEAD_ZERO    | \
+                                                HTTP_HRD_REJ_CL_DUP)
 
 #define HTTP_HRD_TRUSTED_DEFAULT               HTTP_HRD_DEFAULT
 
index e9354dcbb4350f8fe44ecaf92563f5a21c1376f4..f8b4b83b3a080eb6d575a7da921a8ab004bcf4ab 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\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\n"
 
        send "show http-hardening other"
        expect ~ " 2 reject-lowercase-method +req +off\n(.*\n)*10 reject-header-trailer-folding +- +on\n"
index b07851ff8633364eae189560122af02ce7f36e7f..2b12121a2cb3767aee12d587b1a0b6601de11183 100644 (file)
@@ -849,7 +849,7 @@ int http_parse_cont_len_header(struct ist *value, unsigned long long *body_len,
                }
 
                /* if duplicate, must be equal */
-               if (check_prev && cl != *body_len)
+               if (check_prev && (cl != *body_len || http_harden(HTTP_HRD_REJ_CL_DUP)))
                        goto fail;
 
                /* OK, store this result as the one to be indexed */
@@ -1800,6 +1800,7 @@ const struct http_hardening_kw http_hardening_kws[] = {
        { "reject-ctl-in-header-trailer",      HTTP_HRD_REJ_CTL_IN_HDR, HTTP_DIR_BOTH },
        { "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 },
        /* 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 },