1
0
mirror of https://github.com/golang/go synced 2024-11-17 13:54:46 -07:00

net/http/internal: return unexpected EOF on incomplete chunk read

Fixes #48861
This commit is contained in:
Alexander Yastrebov 2021-10-11 07:44:17 +02:00
parent ed1c8db308
commit 0ecd790b87
2 changed files with 35 additions and 0 deletions

View File

@ -81,6 +81,11 @@ func (cr *chunkedReader) Read(b []uint8) (n int, err error) {
cr.err = errors.New("malformed chunked encoding")
break
}
} else {
if cr.err == io.EOF {
cr.err = io.ErrUnexpectedEOF
}
break
}
cr.checkEnd = false
}
@ -109,6 +114,8 @@ func (cr *chunkedReader) Read(b []uint8) (n int, err error) {
// bytes to verify they are "\r\n".
if cr.n == 0 && cr.err == nil {
cr.checkEnd = true
} else if cr.err == io.EOF {
cr.err = io.ErrUnexpectedEOF
}
}
return n, cr.err

View File

@ -11,6 +11,7 @@ import (
"io"
"strings"
"testing"
"testing/iotest"
)
func TestChunk(t *testing.T) {
@ -211,3 +212,30 @@ func TestChunkReadPartial(t *testing.T) {
}
}
// Issue 48861: ChunkedReader should report incomplete chunks
func TestIncompleteChunk(t *testing.T) {
const valid = "4\r\nabcd\r\n" + "5\r\nabc\r\n\r\n" + "0\r\n"
for i := 0; i < len(valid); i++ {
incomplete := valid[:i]
r := NewChunkedReader(strings.NewReader(incomplete))
if _, err := io.ReadAll(r); err != io.ErrUnexpectedEOF {
t.Errorf("expected io.ErrUnexpectedEOF for %q, got %v", incomplete, err)
}
}
r := NewChunkedReader(strings.NewReader(valid))
if _, err := io.ReadAll(r); err != nil {
t.Errorf("unexpected error for %q: %v", valid, err)
}
}
func TestChunkEndReadError(t *testing.T) {
readErr := fmt.Errorf("chunk end read error")
r := NewChunkedReader(io.MultiReader(strings.NewReader("4\r\nabcd"), iotest.ErrReader(readErr)))
if _, err := io.ReadAll(r); err != readErr {
t.Errorf("expected %v, got %v", readErr, err)
}
}