Skip to content

Commit

Permalink
Postgres connector validates packet lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
doodlesbykumbi committed Jan 15, 2020
1 parent 06bb65a commit 2a37666
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
7 changes: 6 additions & 1 deletion internal/plugin/connectors/tcp/pg/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package protocol

import (
"encoding/binary"
"errors"
"io"
)

Expand Down Expand Up @@ -66,7 +67,7 @@ func ReadStartupMessage(client io.Reader) ([]byte, error) {
// ReadMessage accepts an incoming message. The first byte is the message type, the second int32
// is the message length, and the rest of the bytes are the message body.
func ReadMessage(client io.Reader) (messageType byte, message []byte, err error) {
var messageTypeBytes = make([]byte, 1)
messageTypeBytes := make([]byte, 1)
if err = binary.Read(client, binary.BigEndian, &messageTypeBytes); err != nil {
return
}
Expand All @@ -84,6 +85,10 @@ func readMessage(client io.Reader) (message []byte, err error) {
return
}

if messageLength < 4 {
err = errors.New("invalid message length < 4")
return
}
// Build a buffer of the appropriate size and fill it
message = make([]byte, messageLength-4)
if _, err = io.ReadFull(client, message); err != nil {
Expand Down
63 changes: 63 additions & 0 deletions internal/plugin/connectors/tcp/pg/protocol/protocol_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package protocol

import (
"encoding/binary"
"net"
"testing"

"github.com/stretchr/testify/assert"
)

func TestReadMessage(t *testing.T) {
t.Run("parses contents", func(t *testing.T) {
r, w := net.Pipe()
expectedMessageType := byte(12)
expectedMessage := []byte{0,1,2,3,4}

go func() {
err := binary.Write(w, binary.BigEndian, expectedMessageType)
if err != nil {
panic(err)
}
err = binary.Write(w, binary.BigEndian, int32(len(expectedMessage) + 4))
if err != nil {
panic(err)
}

_, err = w.Write(expectedMessage)
if err != nil {
panic(err)
}
}()
messageType, message, err := ReadMessage(r)

if !assert.NoError(t, err) {
return
}

assert.Equal(t, expectedMessage, message)
assert.Equal(t, expectedMessageType, messageType)
})

t.Run("validates message length", func(t *testing.T) {
r, w := net.Pipe()
expectedMessageType := byte(12)
// a message length less than 4 is invalid
expectedMessageLength := int32(3)

go func() {
err := binary.Write(w, binary.BigEndian, expectedMessageType)
if err != nil {
panic(err)
}
err = binary.Write(w, binary.BigEndian, expectedMessageLength)
if err != nil {
panic(err)
}
}()
_, _, err := ReadMessage(r)
if assert.Error(t, err) {
return
}
})
}
5 changes: 4 additions & 1 deletion internal/plugin/connectors/tcp/pg/protocol/startup.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ func ParseStartupMessage(message []byte) (version int32, options map[string]stri
options = make(map[string]string)
for {
param, err := messageBuffer.ReadString()
value, err := messageBuffer.ReadString()
if err != nil || param == "\x00" {
break
}
value, err := messageBuffer.ReadString()
if err != nil || value == "\x00" {
break
}

options[param] = value
}
Expand Down

0 comments on commit 2a37666

Please sign in to comment.