diff --git a/src/brpc/couchbase.cpp b/src/brpc/couchbase.cpp index 57a86b93b4..c0554b89fc 100644 --- a/src/brpc/couchbase.cpp +++ b/src/brpc/couchbase.cpp @@ -1532,9 +1532,14 @@ bool CouchbaseOperations::CouchbaseResponse::popCollectionId( } // Success case: we expect extras_length >= 12 (8 bytes manifest + 4 bytes - // collection id) - if (header.extras_length < 12) { - butil::string_printf(&_err, "Extras too small to contain collection ID"); + // 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_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; @@ -1559,7 +1564,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..60e319aa04 100644 --- a/test/brpc_couchbase_unittest.cpp +++ b/test/brpc_couchbase_unittest.cpp @@ -152,6 +152,63 @@ 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()); +} + +// 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);