Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P2P: fix msg validation for new msg type #1894

Merged
merged 1 commit into from
May 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions go/host/p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const (
msgTypeBatches
msgTypeBatchRequest
msgTypeRegisterForBroadcasts
// bounds for msgType validation (must update if adding new type)
_minMsgType = msgTypeTx
_maxMsgType = msgTypeRegisterForBroadcasts
)

var (
Expand Down Expand Up @@ -258,7 +261,8 @@ func (p *Service) RegisterForBroadcasts() error {
if p.isSequencer {
return errors.New("sequencer cannot register for broadcasts")
}
msg := message{Sender: p.ourPublicAddress, Type: msgTypeRegisterForBroadcasts}
// note: contents are not read, but p2p server expects message contents to be non-empty
msg := message{Sender: p.ourPublicAddress, Type: msgTypeRegisterForBroadcasts, Contents: []byte{1}}
return p.send(msg, p.getSequencer())
}

Expand Down Expand Up @@ -417,8 +421,8 @@ func (p *Service) broadcast(msg message) error {
// Sends a message to the provided address.
func (p *Service) send(msg message, to string) error {
// sanity check the message to discover bugs
if !(msg.Type >= 1 && msg.Type <= 3) {
p.logger.Error(fmt.Sprintf("Sending message with wrong message type: %v", msg))
if !(msg.Type >= _minMsgType && msg.Type <= _maxMsgType) {
p.logger.Error(fmt.Sprintf("Sending message with invalid message type: %v", msg))
}
if len(msg.Sender) == 0 {
p.logger.Error(fmt.Sprintf("Sending message with wrong sender type: %v", msg))
Expand Down
Loading