Skip to content

Commit

Permalink
Remove maximum payload length in Unix Socket API message
Browse files Browse the repository at this point in the history
Signed-off-by: hwipl <[email protected]>
  • Loading branch information
hwipl committed May 16, 2024
1 parent ef1286c commit 4e7204e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 36 deletions.
9 changes: 0 additions & 9 deletions internal/api/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import (
)

const (
// MaxPayloadLength is the maximum allowed length of a message payload.
MaxPayloadLength = 2097152

// TokenLength is the length of the message token in bytes.
TokenLength = 16
)
Expand Down Expand Up @@ -45,9 +42,6 @@ type Message struct {

// NewMessage returns a new message with type t and payload p.
func NewMessage(t uint16, p []byte) *Message {
if len(p) > MaxPayloadLength {
return nil
}
return &Message{
Header: Header{
Type: t,
Expand Down Expand Up @@ -81,9 +75,6 @@ func ReadMessage(r io.Reader) (*Message, error) {
if h.Type == TypeNone || h.Type >= TypeUndefined {
return nil, errors.New("invalid message type")
}
if h.Length > MaxPayloadLength {
return nil, errors.New("invalid message length")
}
if h.Token != token {
return nil, errors.New("invalid message token")
}
Expand Down
11 changes: 1 addition & 10 deletions internal/api/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ func TestNewMessage(t *testing.T) {
t.Errorf("got %d, want %d", msg.Type, typ)
}
}

// invalid payload length
p := [MaxPayloadLength + 1]byte{}
if NewMessage(TypeOK, p[:]) != nil {
t.Error("should not create message with invalid payload length")
}
}

// TestNewOK tests NewOK.
Expand Down Expand Up @@ -63,11 +57,8 @@ func TestReadMessageErrors(t *testing.T) {
// invalid type
{Header: Header{Type: TypeUndefined}},

// invalid length
{Header: Header{Type: TypeOK, Length: MaxPayloadLength + 1}},

// short message
{Header: Header{Type: TypeOK, Length: MaxPayloadLength}},
{Header: Header{Type: TypeOK, Length: 4096}},

// invalid token
{Header: Header{Type: TypeOK, Token: [16]byte{1}}},
Expand Down
23 changes: 6 additions & 17 deletions internal/vpncscript/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestRunClient(t *testing.T) {
return confUpdate
}

// test with maximum payload length
// test with varying payload lengths
server = api.NewServer(config)
go func() {
for r := range server.Requests() {
Expand All @@ -79,23 +79,12 @@ func TestRunClient(t *testing.T) {
if err := server.Start(); err != nil {
t.Fatal(err)
}
if err := runClient(sockfile, getConfUpdate(api.MaxPayloadLength)); err != nil {
t.Fatal(err)
}
server.Stop()

// test with more than maximum payload length
server = api.NewServer(config)
go func() {
for r := range server.Requests() {
r.Close()
for _, length := range []int{
2048, 4096, 8192, 65536, 2097152,
} {
if err := runClient(sockfile, getConfUpdate(length)); err != nil {
t.Errorf("length %d returned error: %v", length, err)
}
}()
if err := server.Start(); err != nil {
t.Fatal(err)
}
if err := runClient(sockfile, getConfUpdate(api.MaxPayloadLength+1)); err == nil {
t.Fatal("too long message should return error")
}
server.Stop()
}

0 comments on commit 4e7204e

Please sign in to comment.