From: Willy Tarreau Date: Mon, 23 Nov 2026 10:46:27 +0000 (+0100) Subject: MEDIUM: http/hardening: default to rejecting untrusted duplicate C-L X-Git-Url: https://git.kaiwu.me/web/%22data:,/doc/static/%7B@url%7D?a=commitdiff_plain;h=d35887c6d1da762dae6f6f5208375058d9e32513;p=haproxy.git MEDIUM: http/hardening: default to rejecting untrusted duplicate C-L 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. --- diff --git a/doc/configuration.txt b/doc/configuration.txt index 408525b8b..99acdfb08 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -35183,6 +35183,17 @@ http-hardening [from ] 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 diff --git a/include/haproxy/http-t.h b/include/haproxy/http-t.h index 49ef142f5..7832b260c 100644 --- a/include/haproxy/http-t.h +++ b/include/haproxy/http-t.h @@ -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 diff --git a/reg-tests/http-messaging/http_hardening_profiles.vtc b/reg-tests/http-messaging/http_hardening_profiles.vtc index e9354dcbb..f8b4b83b3 100644 --- a/reg-tests/http-messaging/http_hardening_profiles.vtc +++ b/reg-tests/http-messaging/http_hardening_profiles.vtc @@ -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" diff --git a/src/http.c b/src/http.c index b07851ff8..2b12121a2 100644 --- a/src/http.c +++ b/src/http.c @@ -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 },