From a31d578ec5de5bc987df04ba3d1456e798696345 Mon Sep 17 00:00:00 2001 From: Tuhalf <37873266+tuhalf@users.noreply.github.com> Date: Wed, 17 Dec 2025 18:26:19 +0300 Subject: [PATCH] Prevent doom loop on broken RPC frames MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Validate every inbound message is valid single-value RLP before processing; if framing is broken, log and close the connection since recovery is impossible. Add regression tests covering invalid RLP closure and the customer-reported broken frame, while ensuring valid-but-unsupported requests don’t close the client. --- rpc/bridge.go | 39 ++++++++++++++++ rpc/bridge_inbound_test.go | 94 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 rpc/bridge_inbound_test.go diff --git a/rpc/bridge.go b/rpc/bridge.go index 7fb0613..8a0730f 100644 --- a/rpc/bridge.go +++ b/rpc/bridge.go @@ -11,6 +11,7 @@ import ( "github.com/diodechain/diode_client/config" "github.com/diodechain/diode_client/edge" + "github.com/diodechain/diode_client/rlp" ) var ( @@ -265,6 +266,16 @@ func (client *Client) isAllowlisted(port *config.Port, addr Address) bool { // handleInboundMessage handle inbound message func (client *Client) handleInboundMessage(msg edge.Message) { + // If message framing is broken, the payload will not be valid RLP. + // This is non-recoverable because we won't be able to find the beginning of + // the next frame again, so close the connection to prevent a loop. + if err := validateRLPMessage(msg.Buffer); err != nil { + client.Log().Error("Invalid RLP frame received, closing connection: %v", err) + client.Log().Debug("Invalid RLP data: %x", msg.Buffer) + client.Close() + return + } + if msg.IsResponse() { defer client.timer.profile(time.Now(), "handleResponse") @@ -320,6 +331,34 @@ func (client *Client) handleInboundMessage(msg edge.Message) { client.handleInboundRequest(inboundRequest) } +func validateRLPMessage(b []byte) error { + k, content, rest, err := rlp.Split(b) + if err != nil { + return err + } + if len(rest) != 0 { + return rlp.ErrMoreThanOneValue + } + return validateRLPValue(k, content) +} + +func validateRLPValue(k rlp.Kind, content []byte) error { + if k != rlp.List { + return nil + } + for len(content) > 0 { + innerKind, innerContent, rest, err := rlp.Split(content) + if err != nil { + return err + } + if err := validateRLPValue(innerKind, innerContent); err != nil { + return err + } + content = rest + } + return nil +} + // recvMessageLoop infinite loop to read message from server func (client *Client) recvMessageLoop() { msgBuffer := make(chan edge.Message, 20) diff --git a/rpc/bridge_inbound_test.go b/rpc/bridge_inbound_test.go new file mode 100644 index 0000000..050b34a --- /dev/null +++ b/rpc/bridge_inbound_test.go @@ -0,0 +1,94 @@ +// Diode Network Client +// Copyright 2021 Diode +// Licensed under the Diode License, Version 1.1 +package rpc + +import ( + "testing" + + "github.com/diodechain/diode_client/config" + "github.com/diodechain/diode_client/edge" + "github.com/diodechain/diode_client/rlp" +) + +func TestHandleInboundMessage_ClosesOnInvalidRLP(t *testing.T) { + cfg := testConfig() + config.AppConfig = cfg + pool := NewPool() + client := NewClient("localhost:0", nil, cfg, pool) + + // 0xb8 indicates a long string with 1 byte length-of-length, but the length byte is missing. + msg := edge.Message{Len: 1, Buffer: []byte{0xb8}} + client.handleInboundMessage(msg) + + if !client.Closed() { + t.Fatalf("expected client to be closed on invalid RLP frame") + } +} + +func TestHandleInboundMessage_DoesNotCloseOnValidUnsupportedRequest(t *testing.T) { + cfg := testConfig() + config.AppConfig = cfg + pool := NewPool() + client := NewClient("localhost:0", nil, cfg, pool) + + buf, err := rlp.EncodeToBytes([]interface{}{[]byte("not-a-known-rpc")}) + if err != nil { + t.Fatalf("failed to encode rlp: %v", err) + } + + msg := edge.Message{Len: len(buf), Buffer: buf} + client.handleInboundMessage(msg) + + if client.Closed() { + t.Fatalf("did not expect client to be closed on a valid but unsupported request") + } +} + +func TestHandleInboundMessage_ClosesOnBrokenFrameFromLog(t *testing.T) { + cfg := testConfig() + config.AppConfig = cfg + pool := NewPool() + client := NewClient("localhost:0", nil, cfg, pool) + + // Reported by customer as a doom-loop trigger; parsing indicates framing is broken. + // The message contains extra/truncated data and must be treated as non-recoverable. + buf := []byte{ + 0xf9, 0x01, 0x89, 0x84, 0x01, 0x00, 0x00, 0x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 0x86, + 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x83, 0x9a, 0x28, 0x73, 0xf0, 0x8e, 0x70, 0x72, 0x65, 0x76, + 0x69, 0x6f, 0x75, 0x73, 0x5f, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0xa0, 0x00, 0x00, 0x04, 0xad, 0xb4, + 0x33, 0xa9, 0x67, 0x07, 0x8e, 0x8f, 0xd4, 0x1d, 0x5b, 0x20, 0x81, 0x31, 0x50, 0x70, 0xfb, 0x18, + 0xf9, 0x6b, 0x1d, 0x9c, 0xd4, 0xa3, 0xce, 0xbf, 0x59, 0x0f, 0x8c, 0xec, 0x8a, 0x73, 0x74, 0x61, + 0x74, 0x65, 0x5f, 0x68, 0x61, 0x73, 0x68, 0xa0, 0x2e, 0x49, 0x56, 0x87, 0x0c, 0xa5, 0x96, 0xa4, + 0x92, 0x6d, 0x57, 0xfe, 0xa5, 0xee, 0x06, 0x8a, 0x0f, 0x6d, 0xfd, 0x5e, 0x8d, 0x62, 0xbc, 0x5b, + 0x35, 0x6b, 0x80, 0x0a, 0xe4, 0x06, 0x18, 0x06, 0xcf, 0x89, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, + 0x61, 0x6d, 0x70, 0x84, 0x69, 0x35, 0x70, 0xd4, 0xf2, 0x90, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x61, + 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x68, 0x61, 0x73, 0x68, 0xa0, 0xe7, 0xaf, 0x85, 0xcf, 0x72, + 0x40, 0xe4, 0x4d, 0x5d, 0xb3, 0x3a, 0xfa, 0x4e, 0x4e, 0x0a, 0xed, 0x10, 0x23, 0x71, 0xab, 0x7b, + 0x23, 0x8c, 0xc0, 0x3f, 0xec, 0x75, 0xe0, 0x0a, 0xf9, 0x8a, 0xc2, 0xa1, 0x02, 0x10, 0x07, 0xad, + 0xc7, 0xe9, 0xd3, 0x3f, 0xec, 0x5a, 0xab, 0x31, 0xf8, 0x83, 0x29, 0x30, 0xc1, 0x60, 0x1b, 0x64, + 0x9a, 0x6c, 0xa0, 0x86, 0x48, 0x9a, 0x45, 0xc6, 0xe7, 0x6b, 0x3c, 0xb8, 0x32, + } + + if err := validateRLPMessage(buf); err == nil { + t.Fatalf("expected invalid RLP for reported frame") + } + + msg := edge.Message{Len: 398, Buffer: buf} + client.handleInboundMessage(msg) + + if !client.Closed() { + t.Fatalf("expected client to be closed for reported broken frame") + } +}