From 06787ad486cd5216a1d7aaa72af40b4a8d2cdc7e Mon Sep 17 00:00:00 2001 From: Andreas Bigger Date: Fri, 17 Mar 2023 14:26:23 -0400 Subject: [PATCH] max frame size config validation --- op-batcher/batcher/channel_builder.go | 13 +++++++++++-- op-batcher/batcher/channel_builder_test.go | 6 +++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/op-batcher/batcher/channel_builder.go b/op-batcher/batcher/channel_builder.go index 7bb59416fd74b..070c4a0e90f87 100644 --- a/op-batcher/batcher/channel_builder.go +++ b/op-batcher/batcher/channel_builder.go @@ -12,7 +12,8 @@ import ( ) var ( - ErrInvalidMaxFrameSize = errors.New("max frame size cannot be zero") + ErrZeroMaxFrameSize = errors.New("max frame size cannot be zero") + ErrSmallMaxFrameSize = errors.New("max frame size cannot be less than 23") ErrInvalidChannelTimeout = errors.New("channel timeout is less than the safety margin") ErrInputTargetReached = errors.New("target amount of input data reached") ErrMaxFrameIndex = errors.New("max frame index reached (uint16)") @@ -82,7 +83,15 @@ func (cc *ChannelConfig) Check() error { // will infinitely loop when trying to create frames in the // [channelBuilder.OutputFrames] function. if cc.MaxFrameSize == 0 { - return ErrInvalidMaxFrameSize + return ErrZeroMaxFrameSize + } + + // If the [MaxFrameSize] is set to < 23, the channel out + // will underflow the maxSize variable in the [derive.ChannelOut]. + // Since it is of type uint64, it will wrap around to a very large + // number, making the frame size extremely large. + if cc.MaxFrameSize < 23 { + return ErrSmallMaxFrameSize } return nil diff --git a/op-batcher/batcher/channel_builder_test.go b/op-batcher/batcher/channel_builder_test.go index c63acdd31aa2a..16854a09ba51d 100644 --- a/op-batcher/batcher/channel_builder_test.go +++ b/op-batcher/batcher/channel_builder_test.go @@ -37,7 +37,11 @@ func TestConfigValidation(t *testing.T) { // Set the config to have a zero max frame size. validChannelConfig.MaxFrameSize = 0 - require.ErrorIs(t, validChannelConfig.Check(), ErrInvalidMaxFrameSize) + require.ErrorIs(t, validChannelConfig.Check(), ErrZeroMaxFrameSize) + + // Set the config to have a max frame size less than 23. + validChannelConfig.MaxFrameSize = 22 + require.ErrorIs(t, validChannelConfig.Check(), ErrSmallMaxFrameSize) // Reset the config and test the Timeout error. // NOTE: We should be fuzzing these values with the constraint that