Skip to content

Commit 427f66f

Browse files
Keep the Handshake read cap at 1MB (#4588)
## Summary - `ReadSizedMsg` accepts a Handshake up to a fixed 1MB again. `Handshake.MaxSize()` is the worst case of the fields declared today, so using it as the cap made the message inextensible: adding a field moved the on-wire limit, and an older node would reject a newer Handshake before it could ignore unknown fields. - `init` panics if `MaxSize()` exceeds that constant, the same check giga RPC registration uses against its configured message sizes. ## Test plan - [x] `GOWORK=off go test ./sei-tendermint/internal/p2p/ -count=1 -run 'TestHandshake'` (9 passed) Made with [Cursor](https://cursor.com) Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent f4f3183 commit 427f66f

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

‎sei-tendermint/internal/p2p/handshake.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,22 @@ import (
2020
"github.com/sei-protocol/sei-chain/sei-tendermint/types"
2121
)
2222

23+
// handshakeMsgMaxSize is the maximum encoded size accepted for a Handshake message.
24+
const handshakeMsgMaxSize = 1024 * 1024
25+
2326
var (
2427
gigaValidatorHandshakeTag = utils.OrPanic1(ed25519.NewTag("SEI_GIGA_VALIDATOR_HANDSHAKE_V1"))
2528
errMissingGigaClaim = errors.New("missing validator_auth_key, validator_auth_sig, and evm_rpc")
2629
errGigaClaimRequiresAddr = errors.New("validator giga handshake requires SelfAddr")
2730
errGigaClaimOnNonGiga = errors.New("validator giga claim on non-giga connection")
2831
)
2932

33+
func init() {
34+
if m := (&pb.Handshake{}).MaxSize(); m > handshakeMsgMaxSize {
35+
panic(fmt.Sprintf("*pb.Handshake.MaxSize() = %d exceeds handshakeMsgMaxSize %d", m, handshakeMsgMaxSize))
36+
}
37+
}
38+
3039
// handshakeOffer is the Autobahn committee identity this node claims on a giga handshake.
3140
type handshakeOffer struct {
3241
ValidatorKey atypes.SecretKey
@@ -104,7 +113,7 @@ func handshake(
104113
}
105114
return nil
106115
})
107-
msgBytes, err := conn.ReadSizedMsg(ctx, sc, uint64((&pb.Handshake{}).MaxSize())) //nolint:gosec // MaxSize() returns a small positive constant
116+
msgBytes, err := conn.ReadSizedMsg(ctx, sc, handshakeMsgMaxSize)
108117
if err != nil {
109118
return nil, fmt.Errorf("conn.ReadSizedMsg(): %w", err)
110119
}

‎sei-tendermint/internal/p2p/handshake_test.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
atypes "github.com/sei-protocol/sei-chain/sei-tendermint/autobahn/types"
1313
"github.com/sei-protocol/sei-chain/sei-tendermint/crypto/ed25519"
1414
"github.com/sei-protocol/sei-chain/sei-tendermint/internal/p2p/conn"
15-
"github.com/sei-protocol/sei-chain/sei-tendermint/internal/p2p/pb"
1615
"github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils"
1716
"github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils/require"
1817
"github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils/scope"
@@ -277,7 +276,7 @@ func handshakeAgainst(
277276
// Only the local handshake's verdict is asserted. This read keeps
278277
// the pipe open until it reaches one, and sees EOF when the local
279278
// side rejects and closes first.
280-
_, _ = conn.ReadSizedMsg(ctx, sc, uint64((&pb.Handshake{}).MaxSize()))
279+
_, _ = conn.ReadSizedMsg(ctx, sc, handshakeMsgMaxSize)
281280
return nil
282281
})
283282
_, err := handshake(ctx, a, localKey, localSpec, localOffer)

0 commit comments

Comments
 (0)