Skip to content

Commit

Permalink
plumbing: protocol/packp, Add validation for decodeLine
Browse files Browse the repository at this point in the history
Signed-off-by: Paulo Gomes <pjbgf@linux.com>
  • Loading branch information
pjbgf committed Oct 7, 2023
1 parent 19fe126 commit de26d4a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
7 changes: 6 additions & 1 deletion plumbing/protocol/packp/srvresp.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ func (r *ServerResponse) decodeLine(line []byte) error {
return fmt.Errorf("unexpected flush")
}

err := fmt.Errorf("unexpected content %q", string(line))
if len(line) < 3 {
return err
}

if bytes.Equal(line[0:3], ack) {
return r.decodeACKLine(line)
}
Expand All @@ -109,7 +114,7 @@ func (r *ServerResponse) decodeLine(line []byte) error {
return nil
}

return fmt.Errorf("unexpected content %q", string(line))
return err
}

func (r *ServerResponse) decodeACKLine(line []byte) error {
Expand Down
27 changes: 27 additions & 0 deletions plumbing/protocol/packp/srvresp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package packp
import (
"bufio"
"bytes"
"fmt"

"github.com/go-git/go-git/v5/plumbing"

Expand All @@ -23,6 +24,32 @@ func (s *ServerResponseSuite) TestDecodeNAK(c *C) {
c.Assert(sr.ACKs, HasLen, 0)
}

func (s *ServerResponseSuite) TestDecodeNewLine(c *C) {
raw := "\n"

sr := &ServerResponse{}
err := sr.Decode(bufio.NewReader(bytes.NewBufferString(raw)), false)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "invalid pkt-len found")
}

func (s *ServerResponseSuite) TestDecodeEmpty(c *C) {
raw := ""

sr := &ServerResponse{}
err := sr.Decode(bufio.NewReader(bytes.NewBufferString(raw)), false)
c.Assert(err, IsNil)
}

func (s *ServerResponseSuite) TestDecodePartial(c *C) {
raw := "000600\n"

sr := &ServerResponse{}
err := sr.Decode(bufio.NewReader(bytes.NewBufferString(raw)), false)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, fmt.Sprintf("unexpected content %q", "00"))
}

func (s *ServerResponseSuite) TestDecodeACK(c *C) {
raw := "0031ACK 6ecf0ef2c2dffb796033e5a02219af86ec6584e5\n"

Expand Down

0 comments on commit de26d4a

Please sign in to comment.