Skip to content
Merged
Show file tree
Hide file tree
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
50 changes: 36 additions & 14 deletions wire/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ const MessageHeaderSize = 24
const CommandSize = 12

// MaxMessagePayload is the maximum bytes a message can be regardless of other
// individual limits imposed by messages themselves.
const MaxMessagePayload = (1024 * 1024 * 4) // 4MB
// individual limits imposed by messages themselves. This is used as a
// serialization bound for all contexts (disk, RPC, network, etc.).
const MaxMessagePayload = (1024 * 1024 * 32) // 32MiB

// MaxProtocolMessageLength is the maximum length of an incoming/outgoing p2p
// protocol message. This is separate from MaxMessagePayload which is used as a
// general serialization bound. No current valid p2p message exceeds 4MB.
// This mirrors Bitcoin Core's MAX_PROTOCOL_MESSAGE_LENGTH introduced in
// bitcoin/bitcoin#5843.
const MaxProtocolMessageLength = (4 * 1000 * 1000) // ~4MB

// Commands used in bitcoin message headers which describe the type of message.
const (
Expand Down Expand Up @@ -398,11 +406,11 @@ func WriteV2MessageN(w io.Writer, msg Message, pver uint32,
payload := bw.Bytes()
lenp := len(payload)

// Enforce maximum overall message payload.
if lenp > MaxMessagePayload {
// Enforce maximum protocol message payload.
if lenp > MaxProtocolMessageLength {
str := fmt.Sprintf("message payload is too large - encoded "+
"%d bytes, but maximum message payload is %d bytes",
lenp, MaxMessagePayload)
lenp, MaxProtocolMessageLength)
return totalBytes, messageError("WriteMessage", str)
}

Expand Down Expand Up @@ -453,11 +461,11 @@ func WriteMessageWithEncodingN(w io.Writer, msg Message, pver uint32,
payload := bw.Bytes()
lenp := len(payload)

// Enforce maximum overall message payload.
if lenp > MaxMessagePayload {
// Enforce maximum protocol message payload.
if lenp > MaxProtocolMessageLength {
str := fmt.Sprintf("message payload is too large - encoded "+
"%d bytes, but maximum message payload is %d bytes",
lenp, MaxMessagePayload)
lenp, MaxProtocolMessageLength)
return totalBytes, messageError("WriteMessage", str)
}

Expand Down Expand Up @@ -506,7 +514,8 @@ func ReadV2MessageN(plaintext []byte, pver uint32, enc MessageEncoding) (
Message, []byte, error) {

if len(plaintext) == 0 {
return nil, nil, fmt.Errorf("invalid plaintext length")
return nil, nil, messageError("ReadV2MessageN",
"invalid plaintext length")
}

var msgCmd string
Expand All @@ -515,7 +524,8 @@ func ReadV2MessageN(plaintext []byte, pver uint32, enc MessageEncoding) (
// message this is.
if plaintext[0] == 0x00 {
if len(plaintext) < CommandSize+1 {
return nil, nil, fmt.Errorf("invalid plaintext length")
return nil, nil, messageError("ReadV2MessageN",
"invalid plaintext length")
}

// Slice off the first 0x00 and the trailing 0x00 bytes.
Expand All @@ -537,9 +547,21 @@ func ReadV2MessageN(plaintext []byte, pver uint32, enc MessageEncoding) (
return nil, nil, err
}

// Enforce maximum protocol message payload.
if len(plaintext) > MaxProtocolMessageLength {
str := fmt.Sprintf("message payload is too large - "+
"%d bytes, but max message payload is %d bytes",
len(plaintext), MaxProtocolMessageLength)
return nil, nil, messageError("ReadV2MessageN", str)
}

// Check for maximum length based on the message type.
mpl := msg.MaxPayloadLength(pver)
if len(plaintext) > int(mpl) {
return nil, nil, fmt.Errorf("payload exceeds max length")
str := fmt.Sprintf("payload exceeds max length - "+
"%d bytes, but max payload size for messages of "+
"type [%v] is %v.", len(plaintext), msgCmd, mpl)
return nil, nil, messageError("ReadV2MessageN", str)
}

buf := bytes.NewBuffer(plaintext)
Expand Down Expand Up @@ -599,11 +621,11 @@ func readMessageWithEncodingNInternal(r io.Reader, pver uint32,
hdr *messageHeader, btcnet BitcoinNet, enc MessageEncoding,
totalBytes int) (int, Message, []byte, error) {

// Enforce maximum message payload.
if hdr.length > MaxMessagePayload {
// Enforce maximum protocol message payload.
if hdr.length > MaxProtocolMessageLength {
str := fmt.Sprintf("message payload is too large - header "+
"indicates %d bytes, but max message payload is %d "+
"bytes.", hdr.length, MaxMessagePayload)
"bytes.", hdr.length, MaxProtocolMessageLength)
return totalBytes, nil, nil, messageError("ReadMessage", str)

}
Expand Down
4 changes: 2 additions & 2 deletions wire/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func TestReadMessageWireErrors(t *testing.T) {

// Wire encoded bytes for a message that exceeds max overall message
// length.
mpl := uint32(MaxMessagePayload)
mpl := uint32(MaxProtocolMessageLength)
exceedMaxPayloadBytes := makeHeader(btcnet, "getaddr", mpl+1, 0)

// Wire encoded bytes for a command which is invalid utf-8.
Expand Down Expand Up @@ -392,7 +392,7 @@ func TestWriteMessageWireErrors(t *testing.T) {
encodeErrMsg := &fakeMessage{forceEncodeErr: true}

// Fake message that has payload which exceeds max overall message size.
exceedOverallPayload := make([]byte, MaxMessagePayload+1)
exceedOverallPayload := make([]byte, MaxProtocolMessageLength+1)
exceedOverallPayloadErrMsg := &fakeMessage{payload: exceedOverallPayload}

// Fake message that has payload which exceeds max allowed per message.
Expand Down
2 changes: 1 addition & 1 deletion wire/msgreject.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (msg *MsgReject) MaxPayloadLength(pver uint32) uint32 {
// Unfortunately the bitcoin protocol does not enforce a sane
// limit on the length of the reason, so the max payload is the
// overall maximum message payload.
plen = MaxMessagePayload
plen = MaxProtocolMessageLength
}

return plen
Expand Down
2 changes: 1 addition & 1 deletion wire/msgreject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestRejectLatest(t *testing.T) {
}

// Ensure max payload is expected value for latest protocol version.
wantPayload := uint32(MaxMessagePayload)
wantPayload := uint32(MaxProtocolMessageLength)
maxPayload := msg.MaxPayloadLength(pver)
if maxPayload != wantPayload {
t.Errorf("MaxPayloadLength: wrong max payload length for "+
Expand Down
Loading