Skip to content

Commit

Permalink
Fix parseTrailer panic
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdubbelboer committed Dec 6, 2021
1 parent da7ff7a commit 4aadf9a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
16 changes: 14 additions & 2 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -2339,9 +2339,15 @@ func (h *ResponseHeader) parse(buf []byte) (int, error) {
}

func (h *ResponseHeader) parseTrailer(buf []byte) (int, error) {
// Skip any 0 length chunk.
if buf[0] == '0' {
buf = buf[len(strCRLF)+1:]
skip := len(strCRLF) + 1
if len(buf) < skip {
return 0, io.EOF
}
buf = buf[skip:]
}

var s headerScanner
s.b = buf
s.disableNormalizing = h.disableNormalizing
Expand Down Expand Up @@ -2392,9 +2398,15 @@ func (h *RequestHeader) parse(buf []byte) (int, error) {
}

func (h *RequestHeader) parseTrailer(buf []byte) (int, error) {
// Skip any 0 length chunk.
if buf[0] == '0' {
buf = buf[len(strCRLF)+1:]
skip := len(strCRLF) + 1
if len(buf) < skip {
return 0, io.EOF
}
buf = buf[skip:]
}

var s headerScanner
s.b = buf
s.disableNormalizing = h.disableNormalizing
Expand Down
10 changes: 10 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ import (
"github.com/valyala/bytebufferpool"
)

func TestInvalidTrailers(t *testing.T) {
t.Parallel()

br := bufio.NewReader(bytes.NewReader([]byte{0x20, 0x30, 0x0a, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x66, 0x65, 0x72, 0x2d, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x3a, 0xff, 0x0a, 0x0a, 0x30, 0x0d, 0x0a, 0x30}))
err := (&Response{}).Read(br)
if err == io.EOF {
t.Fatal(err)
}
}

func TestResponseEmptyTransferEncoding(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 4aadf9a

Please sign in to comment.