Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plumbing: protocol/packp, Add validation for decodeLine #868

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions plumbing/protocol/packp/srvresp.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,14 @@ func (r *ServerResponse) decodeLine(line []byte) error {
return fmt.Errorf("unexpected flush")
}

if bytes.Equal(line[0:3], ack) {
return r.decodeACKLine(line)
}
if len(line) >= 3 {
if bytes.Equal(line[0:3], ack) {
return r.decodeACKLine(line)
}

if bytes.Equal(line[0:3], nak) {
return nil
if bytes.Equal(line[0:3], nak) {
return nil
}
}

return fmt.Errorf("unexpected content %q", string(line))
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