Skip to content

fix popCollectionId over-consuming the couchbase response body - #3614

Open
sahvx655-wq wants to merge 2 commits into
apache:masterfrom
sahvx655-wq:couchbase-collection-id-overconsume
Open

sahvx655-wq wants to merge 2 commits into
apache:masterfrom
sahvx655-wq:couchbase-collection-id-overconsume

Conversation

@sahvx655-wq

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: N/A

Problem Summary:

CouchbaseOperations::CouchbaseResponse::popCollectionId in src/brpc/couchbase.cpp parses a CB_COLLECTIONS_GET_CID response whose total_body_length already counts the 12-byte extras (8-byte manifest id plus 4-byte collection id). On the success path it pops the 24-byte header, then pops 8 bytes for the manifest id, 4 bytes for the collection id, and then pops total_body_length again. Those 12 extras bytes are part of total_body_length, so the handler consumes 12 bytes too many and reads into the next pipelined response, desyncing every following response on the connection. The header fields come straight from the (untrusted) server peer, and a response with extras_length >= 12 but total_body_length < 12 reaches the fixed 8+4-byte reads with no lower bound.

What is changed and the side effects?

Changed:

Reject the response when total_body_length < 12 next to the existing extras_length < 12 check, and pop only total_body_length - 12 remaining body bytes after the extras so the handler consumes exactly one message. The sibling pop* handlers already size their value against the body, so only this one was affected.

CouchbaseUnitTest.CollectionIdSuccessConsumesExactBody feeds a valid GET_CID response followed by a second response and checks the second one is left intact: it fails on master (12 bytes of the next response are eaten, leaving 52 instead of 64) and passes with the fix.

Side effects:

  • Performance effects: none

  • Breaking backward compatibility: no

@sahvx655-wq
sahvx655-wq force-pushed the couchbase-collection-id-overconsume branch from e174c88 to 89c47cc Compare November 29, 2026 11:25
@sahvx655-wq

Copy link
Copy Markdown
Contributor Author

Rebased onto current master to clear a stale red X. The macOS compile-with-make-cmake-protobuf21 failure on the old run wasn't from this change: it was ssl_helper.cpp failing to compile against the openssl@4 4.0.3 that landed on the runner image back in October, and that file is untouched here. All 29 checks are green on the rebased head.

The fix itself is unchanged, and master still over-consumes 12 bytes on the GET_CID success path. Anything you'd like adjusted?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The change still has an uncovered success-path boundary case and a misleading error message for short bodies.

2 open issues
What changed in this PR

Fixes Couchbase collection-ID response parsing so popCollectionId validates the minimum body size and consumes exactly one response, preserving subsequent pipelined responses.

Changes:

  • Rejects successful responses whose body is shorter than the required 12-byte extras.
  • Consumes only the remaining body after reading the manifest and collection IDs.
  • Adds a regression test covering pipelined-response preservation.
File Description
test/​brpc_couchbase_unittest.cpp Adds a regression test verifying that successful collection-ID parsing leaves the following pipelined response intact.
src/​brpc/​couchbase.cpp Validates the minimum collection-ID response body size and corrects response-buffer consumption.

Review effort: Lite

Comment thread src/brpc/couchbase.cpp
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) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, the existing case bails out in the error branch before it ever reaches the new guard. Added CollectionIdSuccessConsumesExactBody's sibling, CollectionIdSuccessBodyShorterThanExtras: status=0, extras_length=12, total_body_length=4, four body bytes, then a 64-byte sentinel response.

Worth noting what that case does without the guard, because it's worse than the pipelined over-read alone: extras_length passes on its own, so the success path copies 8 then 4 bytes out of a 4-byte body, reads 12 bytes of the sentinel as a manifest/collection id, returns true with a collection id derived from the next response, and then pops total_body_length - 12, which underflows to a huge size_t. The test asserts false, collection_id left at its sentinel 7, and rawBuffer() back to exactly 64 bytes, so only the malformed message is dropped and the connection stays framed.

Comment thread src/brpc/couchbase.cpp Outdated
// 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");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, the old wording pointed at the wrong field once the condition covers both. It now prints both lengths and the requirement:

extras_length=12/total_body_length=4 is smaller than the 12 bytes a collection ID requires

That matters here because the two header fields are independent and peer-controlled, so when this fires the only useful thing in a log is which of the two was short. The sibling pop* handlers already report their computed value_size the same way, so this keeps the diagnostics in that shape.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants