This is an automated email from the ASF dual-hosted git repository.

chenBright pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new eb201399f reject bare LF and CTLs in http chunk extensions (#3758)
eb201399f is described below

commit eb201399f9924376717758bbeb2cd8aa617cac6f
Author: sahvx655-wq <[email protected]>
AuthorDate: Fri Nov 20 14:38:58 2026 +0530

    reject bare LF and CTLs in http chunk extensions (#3758)
    
    * reject bare LF and CTLs in http chunk extensions
    
    * test valid chunk extension as control before mutating it
---
 src/brpc/details/http_parser.cpp    |  9 +++++++
 test/brpc_http_message_unittest.cpp | 53 +++++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+)

diff --git a/src/brpc/details/http_parser.cpp b/src/brpc/details/http_parser.cpp
index e983683d3..7f99f1cda 100644
--- a/src/brpc/details/http_parser.cpp
+++ b/src/brpc/details/http_parser.cpp
@@ -2117,6 +2117,15 @@ size_t http_parser_execute (http_parser *parser,
           parser->state = s_chunk_size_almost_done;
           break;
         }
+        /* RFC 9112 7.1.1 allows no CTL other than HTAB in a chunk extension.
+         * A bare LF here used to be skipped as part of the extension, while
+         * a peer that ends the chunk-size line at LF reads the next bytes as
+         * chunk data, so the two disagree about where the request ends.
+         */
+        if (ch == LF || !IS_HEADER_CHAR(ch)) {
+          SET_ERRNO(HPE_INVALID_CHUNK_SIZE);
+          goto error;
+        }
         break;
       }
 
diff --git a/test/brpc_http_message_unittest.cpp 
b/test/brpc_http_message_unittest.cpp
index 91853c600..5a27efd1a 100644
--- a/test/brpc_http_message_unittest.cpp
+++ b/test/brpc_http_message_unittest.cpp
@@ -630,6 +630,59 @@ TEST(HttpMessageTest, chunked_trailer_is_not_a_header) {
     ASSERT_EQ("10.0.0.1", *xff);
 }
 
+TEST(HttpMessageTest, ctl_in_chunk_extension) {
+    const std::string valid =
+        "POST /a HTTP/1.1\r\n"
+        "Host: a.com\r\n"
+        "Transfer-Encoding: chunked\r\n\r\n"
+        "5;a=b\r\nhello\r\n"
+        "0;c=d\r\n\r\n";
+    {
+        butil::IOBuf buf;
+        buf.append(valid);
+        brpc::HttpMessage http_message;
+        ASSERT_EQ((ssize_t)valid.size(), http_message.ParseFromIOBuf(buf))
+            << http_message._parser;
+        ASSERT_TRUE(http_message.Completed());
+        ASSERT_EQ("hello", http_message.body().to_string());
+    }
+
+    // Same message with only the extension bytes changed. A bare LF used to
+    // be skipped as part of the extension, so a peer that ends the chunk-size
+    // line at LF disagreed with bRPC about where the chunk data starts.
+    struct { const char* ext; const char* replacement; } mutations[] = {
+        { "a=b", "a\nb" },
+        { "a=b", "a\x7f" "b" },
+        { "a=b", "a\x01" "b" },
+        { "c=d", "c\nd" },
+    };
+    for (size_t i = 0; i < arraysize(mutations); ++i) {
+        std::string request = valid;
+        request.replace(request.find(mutations[i].ext), 3,
+                        mutations[i].replacement);
+        butil::IOBuf buf;
+        buf.append(request);
+        brpc::HttpMessage http_message;
+        ASSERT_EQ(-1, http_message.ParseFromIOBuf(buf)) << request;
+        ASSERT_FALSE(http_message.Completed());
+        ASSERT_EQ(brpc::HPE_INVALID_CHUNK_SIZE,
+                  (brpc::http_errno)http_message._parser.http_errno)
+            << request;
+    }
+
+    const char* accepted[] = {
+        "POST / HTTP/1.1\r\nHost: a.com\r\nTransfer-Encoding: chunked\r\n\r\n"
+        "5;name=val\r\nhello\r\n0;a=b\r\n\r\n",
+        "POST / HTTP/1.1\r\nHost: a.com\r\nTransfer-Encoding: chunked\r\n\r\n"
+        "5;n=\"a b;c\"\r\nhello\r\n0\r\n\r\n",
+        "POST / HTTP/1.1\r\nHost: a.com\r\nTransfer-Encoding: chunked\r\n\r\n"
+        "5 ;\tn\t=\tv\r\nhello\r\n0\r\n\r\n",
+    };
+    for (size_t i = 0; i < arraysize(accepted); ++i) {
+        ASSERT_EQ(brpc::HPE_OK, ParseHttpErrno(accepted[i])) << accepted[i];
+    }
+}
+
 // Puts `data' in a single IOBuf block, the way one read() returns pipelined
 // messages that arrive together.
 void AppendInOneBlock(butil::IOBuf* buf, const std::string& data) {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to