Skip to content

Commit

Permalink
ttrpc: Block sending of oversize messages
Browse files Browse the repository at this point in the history
When trying to send a message that is bigger than the max message length
the send function will return an GRPC error 8 (RESOURCE_EXHAUSTED). It
will also close the connection so that the read function discards the
unused message.

Signed-off-by: Alessio Cantillo <cantillo.trd@gmail.com>
  • Loading branch information
acclassic committed Jan 30, 2023
1 parent 5cc9169 commit 6fd1b7a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
9 changes: 5 additions & 4 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,11 @@ func (ch *channel) recv() (messageHeader, []byte, error) {
}

func (ch *channel) send(streamID uint32, t messageType, flags uint8, p []byte) error {
// TODO: Error on send rather than on recv
//if len(p) > messageLengthMax {
// return status.Errorf(codes.InvalidArgument, "refusing to send, message length %v exceed maximum message size of %v", len(p), messageLengthMax)
//}
if len(p) > messageLengthMax {
defer ch.conn.Close()
return status.Errorf(codes.ResourceExhausted, "refusing to send, message length %v exceed maximum message size of %v", len(p), messageLengthMax)
}

if err := writeMessageHeader(ch.bw, ch.hwbuf[:], messageHeader{Length: uint32(len(p)), StreamID: streamID, Type: t, Flags: flags}); err != nil {
return err
}
Expand Down
25 changes: 12 additions & 13 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,22 @@ func TestMessageOversize(t *testing.T) {
}()

_, _, err := rch.recv()
if err == nil {
t.Fatalf("error expected reading with small buffer")
}

status, ok := status.FromError(err)
if !ok {
t.Fatalf("expected grpc status error: %v", err)
}

if status.Code() != codes.ResourceExhausted {
t.Fatalf("expected grpc status code: %v != %v", status.Code(), codes.ResourceExhausted)
if err != nil {
status, _ := status.FromError(err)
if status.Code() == codes.ResourceExhausted {
t.Fatalf("no error expected, oversize message should not be send.")
}
}

select {
case err := <-errs:
if err != nil {
t.Fatal(err)
status, ok := status.FromError(err)
if !ok {
t.Fatalf("expected grpc status error: %v", err)
}

if status.Code() != codes.ResourceExhausted {
t.Fatalf("expected grpc status code: %v != %v", status.Code(), codes.ResourceExhausted)
}
default:
}
Expand Down

0 comments on commit 6fd1b7a

Please sign in to comment.