Skip to content

Commit cc944ea

Browse files
imp: limit payload size for both v1 and v2 packet (#7935)
* fix: limit max size of packet payloads for v1 and v2 * rename
1 parent bf71bdd commit cc944ea

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

modules/core/04-channel/types/packet.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"github.com/cosmos/ibc-go/v9/modules/core/exported"
1313
)
1414

15+
const MaximumPayloadsSize = 262144 // 256 KiB. This is the maximum size of all payloads combined
16+
1517
// CommitPacket returns the packet commitment bytes. The commitment consists of:
1618
// sha256_hash(timeout_timestamp + timeout_height.RevisionNumber + timeout_height.RevisionHeight + sha256_hash(data))
1719
// from a given packet. This results in a fixed length preimage.
@@ -110,6 +112,10 @@ func (p Packet) ValidateBasic() error {
110112
return errorsmod.Wrap(ErrInvalidPacket, "packet data bytes cannot be empty")
111113
}
112114

115+
if len(p.Data) > MaximumPayloadsSize {
116+
return errorsmod.Wrapf(ErrInvalidPacket, "packet data bytes cannot exceed %d bytes", MaximumPayloadsSize)
117+
}
118+
113119
return nil
114120
}
115121

modules/core/04-channel/types/packet_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func TestPacketValidateBasic(t *testing.T) {
6060
{types.NewPacket(validPacketData, 1, portid, chanid, invalidPort, cpchanid, timeoutHeight, timeoutTimestamp), errors.New("invalid destination port")},
6161
{types.NewPacket(validPacketData, 1, portid, chanid, cpportid, invalidChannel, timeoutHeight, timeoutTimestamp), errors.New("invalid destination channel")},
6262
{types.NewPacket(validPacketData, 1, portid, chanid, cpportid, cpchanid, disabledTimeout, 0), errors.New("packet timeout height and packet timeout timestamp cannot both be 0: invalid packet")},
63+
{types.NewPacket(make([]byte, types.MaximumPayloadsSize+1), 1, portid, chanid, cpportid, cpchanid, timeoutHeight, timeoutTimestamp), errors.New("packet data bytes cannot exceed 262144 bytes: invalid packet")},
6364
}
6465

6566
for _, tc := range testCases {

modules/core/04-channel/v2/types/packet.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
errorsmod "cosmossdk.io/errors"
77

8+
channeltypesv1 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
89
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
910
)
1011

@@ -36,10 +37,16 @@ func (p Packet) ValidateBasic() error {
3637
return errorsmod.Wrap(ErrInvalidPacket, "payloads must contain exactly one payload")
3738
}
3839

40+
totalPayloadsSize := 0
3941
for _, pd := range p.Payloads {
4042
if err := pd.ValidateBasic(); err != nil {
4143
return errorsmod.Wrap(err, "invalid Payload")
4244
}
45+
totalPayloadsSize += len(pd.Value)
46+
}
47+
48+
if totalPayloadsSize > channeltypesv1.MaximumPayloadsSize {
49+
return errorsmod.Wrapf(ErrInvalidPacket, "packet data bytes cannot exceed %d bytes", channeltypesv1.MaximumPayloadsSize)
4350
}
4451

4552
if err := host.ChannelIdentifierValidator(p.SourceClient); err != nil {

modules/core/04-channel/v2/types/packet_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/stretchr/testify/require"
88

99
transfertypes "github.com/cosmos/ibc-go/v9/modules/apps/transfer/types"
10+
channeltypesv1 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
1011
"github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
1112
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
1213
ibctesting "github.com/cosmos/ibc-go/v9/testing"
@@ -26,6 +27,22 @@ func TestValidateBasic(t *testing.T) {
2627
func() {},
2728
nil,
2829
},
30+
{
31+
"success, single payload just below MaxPayloadsSize",
32+
func() {
33+
packet.Payloads[0].Value = make([]byte, channeltypesv1.MaximumPayloadsSize-1)
34+
},
35+
nil,
36+
},
37+
{
38+
"failure: invalid single payloads size",
39+
func() {
40+
// bytes that are larger than MaxPayloadsSize
41+
packet.Payloads[0].Value = make([]byte, channeltypesv1.MaximumPayloadsSize+1)
42+
},
43+
types.ErrInvalidPacket,
44+
},
45+
// TODO: add test cases for multiple payloads when enabled (#7008)
2946
{
3047
"failure: payloads is nil",
3148
func() {

0 commit comments

Comments
 (0)