From e9186b2a1586eeeb2af5966fd46d1569aba0da4e Mon Sep 17 00:00:00 2001 From: Freddie Lamble Date: Wed, 2 Dec 2026 13:38:12 +0000 Subject: [PATCH] Validate the channel before assembling content frames `Connection::processNextFrame` handed every frame to the `Framer` before looking at the channel. Check the channel first and reject frames for an id with no open channel. Channel 0 only carries connection methods and heartbeats, so reject any other frame type there rather than exempting the whole channel. Once a `Connection.Close` is in flight the broker keeps sending, so discard any further rejected frames rather than closing a second time. --- src/rmq/rmqamqp/rmqamqp_channelmap.cpp | 7 + src/rmq/rmqamqp/rmqamqp_channelmap.h | 5 + src/rmq/rmqamqp/rmqamqp_connection.cpp | 25 +++ src/tests/rmqamqp/rmqamqp_channelmap.t.cpp | 66 ++++++ src/tests/rmqamqp/rmqamqp_connection.t.cpp | 227 +++++++++++++++++++++ 5 files changed, 330 insertions(+) diff --git a/src/rmq/rmqamqp/rmqamqp_channelmap.cpp b/src/rmq/rmqamqp/rmqamqp_channelmap.cpp index b2c33050..03c73419 100644 --- a/src/rmq/rmqamqp/rmqamqp_channelmap.cpp +++ b/src/rmq/rmqamqp/rmqamqp_channelmap.cpp @@ -152,6 +152,13 @@ void ChannelMap::resumeReceiveChannels(const bool respectHostHealth) } } +bool ChannelMap::hasChannel(uint16_t channelId) const +{ + ChannelPtrMap::const_iterator it = d_channels.find(channelId); + + return it != d_channels.end() && it->second; +} + bool ChannelMap::processReceived(uint16_t channelId, const rmqamqp::Message& message) { diff --git a/src/rmq/rmqamqp/rmqamqp_channelmap.h b/src/rmq/rmqamqp/rmqamqp_channelmap.h index ca0ddae3..40f89b29 100644 --- a/src/rmq/rmqamqp/rmqamqp_channelmap.h +++ b/src/rmq/rmqamqp/rmqamqp_channelmap.h @@ -66,6 +66,11 @@ class ChannelMap { /// Call `open` on every channel stored in the container void openAll(); + /// Returns true if `channelId` has a channel associated with it. + /// `assignId` reserves an id before there is a channel for it, so a + /// reserved id returns false until `associateChannel` fills it in. + bool hasChannel(uint16_t channelId) const; + /// Call `processReceived` on the channel associated with `channelId` /// Returns false if there is no channel associated with that id bool processReceived(uint16_t channelId, const rmqamqp::Message& message); diff --git a/src/rmq/rmqamqp/rmqamqp_connection.cpp b/src/rmq/rmqamqp/rmqamqp_connection.cpp index 75a453d8..8e4bc6bd 100644 --- a/src/rmq/rmqamqp/rmqamqp_connection.cpp +++ b/src/rmq/rmqamqp/rmqamqp_connection.cpp @@ -919,6 +919,31 @@ void Connection::processNextFrame(const rmqamqpt::Frame& frame) { d_heartbeatManager->notifyMessageReceived(); + if (frame.channel() == 0) { + // Channel 0 only carries connection methods and heartbeats. Reject + // anything else before the framer, which would buffer a content + // frame against channel 0 the same as any other id. + if (frame.type() != rmqamqpt::Constants::METHOD && + frame.type() != rmqamqpt::Constants::HEARTBEAT) { + if (!isClosingState(d_state)) { + connectionException( + rmqamqpt::Constants::UNEXPECTED_FRAME, + "Received unexpected frame channel=" + + bsl::to_string(frame.channel()) + " frameType=" + + bsl::to_string(static_cast(frame.type()))); + } + return; + } + } + else if (!d_channels.hasChannel(frame.channel())) { + if (!isClosingState(d_state)) { + connectionException(rmqamqpt::Constants::CHANNEL_ERROR, + "Received frame on unknown channel: " + + bsl::to_string(frame.channel())); + } + return; + } + uint16_t channel = 0; Message received; const Framer::ReturnCode rc = diff --git a/src/tests/rmqamqp/rmqamqp_channelmap.t.cpp b/src/tests/rmqamqp/rmqamqp_channelmap.t.cpp index f8f98adc..76fba235 100644 --- a/src/tests/rmqamqp/rmqamqp_channelmap.t.cpp +++ b/src/tests/rmqamqp/rmqamqp_channelmap.t.cpp @@ -107,6 +107,72 @@ TEST(ChannelMap, OpenCall) map.openAll(); } +TEST(ChannelMap, HasChannelUnknownId) +{ + rmqamqp::ChannelMap map; + + EXPECT_FALSE(map.hasChannel(1)); +} + +TEST(ChannelMap, HasChannelReservedIdIsNotAssociated) +{ + rmqamqp::ChannelMap map; + + EXPECT_THAT(map.assignId(), Eq(1)); + + // assignId reserves the id, but no channel has been associated yet + EXPECT_FALSE(map.hasChannel(1)); +} + +TEST(ChannelMap, HasChannelAssociatedId) +{ + rmqamqp::ChannelMap map; + EXPECT_THAT(map.assignId(), Eq(1)); + bsl::shared_ptr channel = + bsl::make_shared(); + map.associateChannel(1, bsl::shared_ptr(channel)); + + EXPECT_TRUE(map.hasChannel(1)); + EXPECT_FALSE(map.hasChannel(2)); +} + +TEST(ChannelMap, HasChannelAfterRemove) +{ + rmqamqp::ChannelMap map; + EXPECT_THAT(map.assignId(), Eq(1)); + bsl::shared_ptr channel = + bsl::make_shared(); + map.associateChannel(1, bsl::shared_ptr(channel)); + + map.removeChannel(1); + + EXPECT_FALSE(map.hasChannel(1)); +} + +TEST(ChannelMap, HasChannelAgreesWithProcessReceived) +{ + rmqamqp::ChannelMap map; + rmqamqp::Message msg; + + // Unknown, reserved and removed ids must all be rejected by both + EXPECT_FALSE(map.hasChannel(1)); + EXPECT_FALSE(map.processReceived(1, msg)); + + EXPECT_THAT(map.assignId(), Eq(1)); + EXPECT_FALSE(map.hasChannel(1)); + EXPECT_FALSE(map.processReceived(1, msg)); + + bsl::shared_ptr channel = + bsl::make_shared(); + map.associateChannel(1, bsl::shared_ptr(channel)); + + EXPECT_CALL(*channel, processReceived(_)) + .WillOnce(Return(rmqamqp::Channel::KEEP)); + + EXPECT_TRUE(map.hasChannel(1)); + EXPECT_TRUE(map.processReceived(1, msg)); +} + TEST(ChannelMap, ProcessReceivedWithChannel) { rmqamqp::ChannelMap map; diff --git a/src/tests/rmqamqp/rmqamqp_connection.t.cpp b/src/tests/rmqamqp/rmqamqp_connection.t.cpp index c3d3dddd..66a0970c 100644 --- a/src/tests/rmqamqp/rmqamqp_connection.t.cpp +++ b/src/tests/rmqamqp/rmqamqp_connection.t.cpp @@ -708,6 +708,64 @@ class ConnectionTests : public ::testing::Test { } } + /// Queue an inbound content header announcing `bodySize` body bytes. + /// No body frames follow it, so the content stays incomplete. + void expectInboundContentHeader(uint16_t channel, size_t bodySize) + { + const rmqt::Message message( + bsl::make_shared >(bodySize, 0)); + + d_replayFrame.pushInbound(bsl::make_shared( + Framer::makeContentHeaderFrame(message, channel))); + } + + /// Queue an inbound content body frame of `bodySize` bytes + void expectInboundContentBody(uint16_t channel, size_t bodySize) + { + const bsl::vector payload(bodySize, 0); + + d_replayFrame.pushInbound(bsl::make_shared( + Framer::makeContentBodyFrame(&payload[0], + Frame::calculateFrameSize(bodySize), + bodySize, + channel))); + } + + /// Queue an inbound empty frame carrying a type the protocol does not + /// define. Built by hand because the Framer only emits valid types. + void expectInboundUndefinedFrameType(uint16_t channel) + { + const uint8_t undefinedType = 7; + + bsl::vector raw; + raw.push_back(undefinedType); + raw.push_back(static_cast(channel >> 8)); + raw.push_back(static_cast(channel & 0xFF)); + raw.push_back(0); // payload length, four octets + raw.push_back(0); + raw.push_back(0); + raw.push_back(0); + raw.push_back(Constants::FRAME_END); + + ASSERT_THAT(raw.size(), Eq(Frame::frameOverhead())); + + d_replayFrame.pushInbound( + bsl::make_shared(&raw[0], raw.size())); + } + + /// Queue the outbound Connection.Close the client is expected to send + /// when it rejects a frame + void expectConnectionCloseFrame(Constants::AMQPReplyCode replyCode, + const bsl::string& replyText) + { + ConnectionClose closeMethod(replyCode, replyText); + Frame closeFrame; + Framer::makeMethodFrame( + &closeFrame, 0, Method(ConnectionMethod(closeMethod))); + d_replayFrame.pushOutbound( + bsl::make_shared(closeFrame)); + } + bsl::shared_ptr createAndStartConnection(const bsl::string& name = "test-connection") { @@ -1300,6 +1358,175 @@ TEST_F(ConnectionTests, AddSendChannelWhenConnected) d_eventLoop.run(); } +TEST_F(ConnectionTests, ContentHeaderOnUnknownChannelClosesConnection) +{ + expectFirstHandshakeFrames(); + + { + bsl::shared_ptr conn = createAndStartConnection(); + + // 1. Setup connection + d_eventLoop.run(); + d_eventLoop.restart(); + + EXPECT_THAT(d_replayFrame.getLength(), Eq(0)); + + // No channel has been created, so channel 1 is unknown. The header + // must be rejected before the framer buffers anything for it. + expectInboundContentHeader(1, 64 * 1024 * 1024); + + expectConnectionCloseFrame(Constants::CHANNEL_ERROR, + "Received frame on unknown channel: 1"); + expectCloseOkFrame(false); + + resetExpectations(); + expectHandshakeFrames(); + + feedNextFrame(); + + // 2. Reject the frame, close and reconnect + d_eventLoop.run(); + d_eventLoop.restart(); + + EXPECT_THAT(d_replayFrame.getLength(), Eq(0)); + EXPECT_THAT(conn->state(), Eq(rmqamqp::Connection::CONNECTED)); + expectShutdownCalls(); + } + + // 3. Process shutdown + d_eventLoop.run(); +} + +TEST_F(ConnectionTests, ContentHeaderOnChannelZeroClosesConnection) +{ + expectFirstHandshakeFrames(); + + { + bsl::shared_ptr conn = createAndStartConnection(); + + // 1. Setup connection + d_eventLoop.run(); + d_eventLoop.restart(); + + EXPECT_THAT(d_replayFrame.getLength(), Eq(0)); + + // Channel 0 is exempt from the unknown channel check, so content + // frames must be rejected on their own account + expectInboundContentHeader(0, 64 * 1024 * 1024); + + expectConnectionCloseFrame( + Constants::UNEXPECTED_FRAME, + "Received unexpected frame channel=0 frameType=2"); + expectCloseOkFrame(false); + + resetExpectations(); + expectHandshakeFrames(); + + feedNextFrame(); + + // 2. Reject the frame, close and reconnect + d_eventLoop.run(); + d_eventLoop.restart(); + + EXPECT_THAT(d_replayFrame.getLength(), Eq(0)); + EXPECT_THAT(conn->state(), Eq(rmqamqp::Connection::CONNECTED)); + expectShutdownCalls(); + } + + // 3. Process shutdown + d_eventLoop.run(); +} + +TEST_F(ConnectionTests, FramesFollowingARejectionAreDiscarded) +{ + expectFirstHandshakeFrames(); + + { + bsl::shared_ptr conn = createAndStartConnection(); + + // 1. Setup connection + d_eventLoop.run(); + d_eventLoop.restart(); + + EXPECT_THAT(d_replayFrame.getLength(), Eq(0)); + + // A broker sends body frames straight after a content header, so the + // rejection has to cope with more frames arriving on the same unknown + // channel. Only the header may close the connection. The body which + // follows is discarded while the close is in flight. + expectInboundContentHeader(1, 4096); + expectConnectionCloseFrame(Constants::CHANNEL_ERROR, + "Received frame on unknown channel: 1"); + expectInboundContentBody(1, 4096); + + feedNextFrame(); + + // 2. Reject the header, which writes the close and feeds the body + d_eventLoop.run(); + d_eventLoop.restart(); + + EXPECT_THAT(conn->state(), + Eq(rmqamqp::Connection::CONNECTION_CLOSE_SENT_RECONNECT)); + + expectCloseOkFrame(false); + resetExpectations(); + expectHandshakeFrames(); + + feedNextFrame(); + + // 3. Accept close-ok and reconnect + d_eventLoop.run(); + d_eventLoop.restart(); + + EXPECT_THAT(d_replayFrame.getLength(), Eq(0)); + EXPECT_THAT(conn->state(), Eq(rmqamqp::Connection::CONNECTED)); + expectShutdownCalls(); + } + + // 4. Process shutdown + d_eventLoop.run(); +} + +TEST_F(ConnectionTests, UndefinedFrameTypeOnChannelZeroClosesConnection) +{ + expectFirstHandshakeFrames(); + + { + bsl::shared_ptr conn = createAndStartConnection(); + + // 1. Setup connection + d_eventLoop.run(); + d_eventLoop.restart(); + + EXPECT_THAT(d_replayFrame.getLength(), Eq(0)); + + // Only method and heartbeat frames belong on channel 0, so a type + // the protocol does not define is rejected the same way content is + expectInboundUndefinedFrameType(0); + + expectConnectionCloseFrame( + Constants::UNEXPECTED_FRAME, + "Received unexpected frame channel=0 frameType=7"); + expectCloseOkFrame(false); + + resetExpectations(); + expectHandshakeFrames(); + + feedNextFrame(); + + // 2. Reject the frame, close and reconnect + d_eventLoop.run(); + d_eventLoop.restart(); + + EXPECT_THAT(d_replayFrame.getLength(), Eq(0)); + EXPECT_THAT(conn->state(), Eq(rmqamqp::Connection::CONNECTED)); + expectShutdownCalls(); + } + + // 3. Process shutdown + d_eventLoop.run(); +} + TEST_F(ConnectionTests, DisconnectTriggersChannelReset) { expectFirstHandshakeFrames();