]> git.kaiwu.me - haproxy.git/commitdiff
MEDIUM: http/hardening: filter out-of-range status codes from untrusted servers
authorWilly Tarreau <w@1wt.eu>
Mon, 23 Nov 2026 15:32:46 +0000 (16:32 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 23 Nov 2026 22:24:23 +0000 (23:24 +0100)
The new rule "reject-out-of-range-status" is used to reject status codes
below 100 or above 599. They are never used except in local deployments,
and are handled differently by different implementations, so let's block
them by default on untrusted connections.

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 7eb20e2efe96757698cf74db65fc1cd876a07f5b..f332518277c1fa99b8c3760e3021c8efcc7a52f3 100644 (file)
@@ -35228,6 +35228,14 @@ http-hardening <name> [from <name2>]
         recommended on untrusted listeners. It is enabled by default on
         untrusted connections.
 
+  - reject-out-of-range-status (inbound, response)
+        Rejects responses containing a status code lower than 100 or higher
+        than 599. These ones are not specified by the standard and are handled
+        differently by multiple implementations. They are never used over the
+        internet and usually only exist in local deployments. This is
+        recommended on public-facing backends. It is enabled by default on
+        untrusted connections.
+
   - reject-underscore-in-header-name (inbound, request and response)
         Rejects requests and responses in which one header field name contains
         an underscore character ('_' or \x5C). This character is perfectly
index 18cf32131c3a96318b8ee962bd96e45510684ded..07df4b30b80c9e959c5a0c9f87829947d85b3397 100644 (file)
@@ -257,6 +257,7 @@ struct http_violation {
 #define HTTP_HRD_REJ_CL_LEAD_ZERO           0x00001000
 #define HTTP_HRD_REJ_CL_DUP                 0x00002000
 #define HTTP_HRD_REJ_FORB_PAYLOAD           0x00004000
+#define HTTP_HRD_REJ_OOR_STATUS             0x00008000
 
 /* insert new masks above this line */
 /* update this one with new rejects */
@@ -269,7 +270,8 @@ struct http_violation {
                                              HTTP_HRD_REJ_CL_TE          | \
                                              HTTP_HRD_REJ_CL_LEAD_ZERO   | \
                                              HTTP_HRD_REJ_CL_DUP         | \
-                                             HTTP_HRD_REJ_FORB_PAYLOAD)
+                                             HTTP_HRD_REJ_FORB_PAYLOAD   | \
+                                             HTTP_HRD_REJ_OOR_STATUS)
 
 #define HTTP_HRD_ALL_DELETE                 (HTTP_HRD_DEL_USCORE_HDR_NAM         | \
                                              HTTP_HRD_DEL_USCORE_TLR_NAM         | \
@@ -291,7 +293,8 @@ struct http_violation {
                                                 HTTP_HRD_REJ_CL_TE           | \
                                                 HTTP_HRD_REJ_CL_LEAD_ZERO    | \
                                                 HTTP_HRD_REJ_CL_DUP          | \
-                                                HTTP_HRD_REJ_FORB_PAYLOAD)
+                                                HTTP_HRD_REJ_FORB_PAYLOAD    | \
+                                                HTTP_HRD_REJ_OOR_STATUS)
 
 #define HTTP_HRD_TRUSTED_DEFAULT               HTTP_HRD_DEFAULT
 
index 4b2f0cc5358c8c23af9654bbf94e1d248b6db3c6..d2670e85419a5821de843ddfbf3a35c90be7de8a 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\n15 reject-forbidden-payload +res +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\n16 reject-out-of-range-status +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 c9f37bbceb9491cd745f38978f9799849d262353..5055456b7af9d9a39b9a8b2295799fced10487b1 100644 (file)
@@ -328,6 +328,11 @@ static int h1_postparse_res_hdrs(struct h1m *h1m, union h1_sl *h1sl, struct htx
                /* FIXME: Check the codes 1xx ? */
        }
 
+       /* reject-status-out-of-range allows to reject out-of-range statuses */
+       if (unlikely(code < 100 || code > 599) && http_harden(HTTP_HRD_REJ_OOR_STATUS)) {
+               goto error;
+       }
+
        /* Be sure the message, once converted into HTX, will not exceed the max
         * size allowed.
         */
index d49c042fb572c105ed66bfc6ceb17bfdc1441946..75ac04c4d11843de900281cc36a1b6e50dbd1628 100644 (file)
--- a/src/h2.c
+++ b/src/h2.c
@@ -807,6 +807,12 @@ int h2_make_htx_response(struct http_hdr *list, struct htx *htx, unsigned int *m
                        goto fail;
        }
 
+       /* reject-status-out-of-range allows to reject out-of-range statuses */
+       if (unlikely(sl->info.res.status < 100 || sl->info.res.status > 599) &&
+           http_harden(HTTP_HRD_REJ_OOR_STATUS)) {
+               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 &&
index 4cd378fd9ab47d450f3dae283b80fdb799344d73..91e4004dd99fde09aefb105ac71f5ee2e35d954b 100644 (file)
--- a/src/h3.c
+++ b/src/h3.c
@@ -1523,6 +1523,16 @@ static ssize_t h3_resp_headers_to_htx(struct qcs *qcs, const struct buffer *buf,
                goto out;
        }
 
+       /* reject-status-out-of-range allows to reject out-of-range statuses */
+       if (unlikely(sl->info.res.status < 100 || sl->info.res.status > 599) &&
+           http_harden(HTTP_HRD_REJ_OOR_STATUS)) {
+               TRACE_ERROR("hardening rejected out of range status code", 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;
+       }
+
        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)) {
index 20979b62224f79eaa31f3b35b9a84b51e2d67612..b047b31c462afed74347622e2f204cb8e8f3f0bf 100644 (file)
@@ -1802,6 +1802,7 @@ const struct http_hardening_kw http_hardening_kws[] = {
        { "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 },
+       { "reject-out-of-range-status",        HTTP_HRD_REJ_OOR_STATUS, 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 },