Skip to content

Commit

Permalink
enforce a message size limit
Browse files Browse the repository at this point in the history
so we do not allocate loads of memory when receiving (accidental or
deliberate) garbage
  • Loading branch information
rade committed Jul 20, 2015
1 parent 6e6df96 commit c27f6d6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions router/consts.go
Expand Up @@ -25,6 +25,7 @@ const (
MaxDuration = time.Duration(math.MaxInt64)
MaxMissedHeartbeats = 6
HeartbeatTimeout = MaxMissedHeartbeats * SlowHeartbeat
MaxTCPMsgSize = 10 * 1024 * 1024
)

var (
Expand Down
6 changes: 6 additions & 0 deletions router/crypto.go
Expand Up @@ -361,6 +361,9 @@ func NewLengthPrefixTCPSender(writer io.Writer) *LengthPrefixTCPSender {

func (sender *LengthPrefixTCPSender) Send(msg []byte) error {
l := len(msg)
if l > MaxTCPMsgSize {
return fmt.Errorf("outgoing message exceeds maximum size: %d > %d", l, MaxTCPMsgSize)
}
prefixedMsg := make([]byte, 4+l)
binary.BigEndian.PutUint32(prefixedMsg, uint32(l))
copy(prefixedMsg[4:], msg)
Expand Down Expand Up @@ -417,6 +420,9 @@ func (receiver *LengthPrefixTCPReceiver) Receive() ([]byte, error) {
return nil, err
}
l := binary.BigEndian.Uint32(lenPrefix)
if l > MaxTCPMsgSize {
return nil, fmt.Errorf("incoming message exceeds maximum size: %d > %d", l, MaxTCPMsgSize)
}
msg := make([]byte, l)
_, err := io.ReadFull(receiver.reader, msg)
return msg, err
Expand Down

0 comments on commit c27f6d6

Please sign in to comment.