From 89c47cc5cf3facc76856e0bf2ad7c1fe5e54fd34 Mon Sep 17 00:00:00 2001 From: Sahana Bogar Date: Sun, 29 Nov 2026 16:54:52 +0530 Subject: [PATCH 1/2] fix popCollectionId over-consuming the couchbase response body --- src/brpc/couchbase.cpp | 10 +++++++--- test/brpc_couchbase_unittest.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/brpc/couchbase.cpp b/src/brpc/couchbase.cpp index 57a86b93b4..2f99ad0f3b 100644 --- a/src/brpc/couchbase.cpp +++ b/src/brpc/couchbase.cpp @@ -1532,8 +1532,9 @@ bool CouchbaseOperations::CouchbaseResponse::popCollectionId( } // Success case: we expect extras_length >= 12 (8 bytes manifest + 4 bytes - // collection id) - if (header.extras_length < 12) { + // collection id). total_body_length counts those extras, so it must be at + // least 12 as well; both fields are attacker-controlled and independent. + if (header.extras_length < 12 || header.total_body_length < 12) { butil::string_printf(&_err, "Extras too small to contain collection ID"); // remove the response from buffer so you don't re‐process _buf.pop_front(sizeof(header) + header.total_body_length); @@ -1559,7 +1560,10 @@ bool CouchbaseOperations::CouchbaseResponse::popCollectionId( *collection_id = static_cast(cid_host); _buf.pop_front(sizeof(cid_net)); - _buf.pop_front(header.total_body_length); + // The 12 extras bytes read above are part of total_body_length; only the + // remainder of the body is still in the buffer. Popping total_body_length + // again would consume 12 bytes of the next pipelined response. + _buf.pop_front(header.total_body_length - 12); _err.clear(); return true; } diff --git a/test/brpc_couchbase_unittest.cpp b/test/brpc_couchbase_unittest.cpp index 6ec6584d9f..4467966757 100644 --- a/test/brpc_couchbase_unittest.cpp +++ b/test/brpc_couchbase_unittest.cpp @@ -152,6 +152,35 @@ TEST_F(CouchbaseUnitTest, CollectionResponseWithInconsistentBodyLength) { EXPECT_EQ(sizeof(header) + next_response.size(), res2.rawBuffer().size()); } +// total_body_length already counts the 12-byte extras (8-byte manifest id + +// 4-byte collection id). popCollectionId must consume exactly one message on +// success; popping total_body_length again after the extras would eat 12 bytes +// of the next pipelined response and desync the connection. +TEST_F(CouchbaseUnitTest, CollectionIdSuccessConsumesExactBody) { + const std::string next_response(64, 'A'); + + brpc::policy::CouchbaseResponseHeader header = {}; + header.magic = brpc::policy::CB_MAGIC_RESPONSE; + header.command = brpc::policy::CB_COLLECTIONS_GET_CID; + header.status = 0; + header.extras_length = 12; + header.total_body_length = 12; // 8-byte manifest id + 4-byte collection id + + char extras[12] = {0}; + extras[11] = 42; // collection id in the low byte of the 4-byte field + + brpc::CouchbaseOperations::CouchbaseResponse res; + res.rawBuffer().append(&header, sizeof(header)); + res.rawBuffer().append(extras, sizeof(extras)); + res.rawBuffer().append(next_response); + + uint8_t collection_id = 0; + EXPECT_TRUE(res.popCollectionId(&collection_id)); + EXPECT_EQ(42, collection_id); + // Exactly one message consumed; the following response is left intact. + EXPECT_EQ(next_response.size(), res.rawBuffer().size()); +} + TEST_F(CouchbaseUnitTest, EdgeCases) { brpc::CouchbaseOperations::CouchbaseRequest req; req.addRequest("", "value", 0, 0, 0); From 1710902d6948a855e7661af386917ef82c3bcdbc Mon Sep 17 00:00:00 2001 From: Sahana Bogar Date: Mon, 30 Nov 2026 15:47:52 +0530 Subject: [PATCH 2/2] cover the short-body collection id guard and name both length fields --- src/brpc/couchbase.cpp | 6 +++++- test/brpc_couchbase_unittest.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/brpc/couchbase.cpp b/src/brpc/couchbase.cpp index 2f99ad0f3b..c0554b89fc 100644 --- a/src/brpc/couchbase.cpp +++ b/src/brpc/couchbase.cpp @@ -1535,7 +1535,11 @@ bool CouchbaseOperations::CouchbaseResponse::popCollectionId( // collection id). total_body_length counts those extras, so it must be at // least 12 as well; both fields are attacker-controlled and independent. if (header.extras_length < 12 || header.total_body_length < 12) { - butil::string_printf(&_err, "Extras too small to contain collection ID"); + butil::string_printf(&_err, + "extras_length=%u/total_body_length=%u is smaller " + "than the 12 bytes a collection ID requires", + static_cast(header.extras_length), + static_cast(header.total_body_length)); // remove the response from buffer so you don't re‐process _buf.pop_front(sizeof(header) + header.total_body_length); return false; diff --git a/test/brpc_couchbase_unittest.cpp b/test/brpc_couchbase_unittest.cpp index 4467966757..60e319aa04 100644 --- a/test/brpc_couchbase_unittest.cpp +++ b/test/brpc_couchbase_unittest.cpp @@ -181,6 +181,34 @@ TEST_F(CouchbaseUnitTest, CollectionIdSuccessConsumesExactBody) { EXPECT_EQ(next_response.size(), res.rawBuffer().size()); } +// A success response may claim extras_length >= 12 while total_body_length is +// shorter than the 12 bytes the manifest id + collection id reads need. The +// guard must reject it and drop exactly the malformed message, leaving the next +// pipelined response aligned. +TEST_F(CouchbaseUnitTest, CollectionIdSuccessBodyShorterThanExtras) { + const std::string next_response(64, 'A'); + const std::string body(4, 'B'); + + brpc::policy::CouchbaseResponseHeader header = {}; + header.magic = brpc::policy::CB_MAGIC_RESPONSE; + header.command = brpc::policy::CB_COLLECTIONS_GET_CID; + header.status = 0; + header.extras_length = 12; + // Shorter than the 12-byte extras the success path reads. + header.total_body_length = static_cast(body.size()); + + brpc::CouchbaseOperations::CouchbaseResponse res; + res.rawBuffer().append(&header, sizeof(header)); + res.rawBuffer().append(body); + res.rawBuffer().append(next_response); + + uint8_t collection_id = 7; + EXPECT_FALSE(res.popCollectionId(&collection_id)); + EXPECT_EQ(7, collection_id); + // Only the malformed message is dropped; the next response is untouched. + EXPECT_EQ(next_response.size(), res.rawBuffer().size()); +} + TEST_F(CouchbaseUnitTest, EdgeCases) { brpc::CouchbaseOperations::CouchbaseRequest req; req.addRequest("", "value", 0, 0, 0);