Skip to content

Commit 1116d03

Browse files
Fix panic while reading invalid trailers
1 parent 856ca8e commit 1116d03

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

header.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2442,6 +2442,10 @@ func (h *RequestHeader) parseTrailer(buf []byte) (int, error) {
24422442
}
24432443

24442444
func isBadTrailer(key []byte) bool {
2445+
if len(key) == 0 {
2446+
return true
2447+
}
2448+
24452449
switch key[0] | 0x20 {
24462450
case 'a':
24472451
return caseInsensitiveCompare(key, strAuthorization)

http_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ func TestInvalidTrailers(t *testing.T) {
3232
if err := (&Response{}).Read(bufio.NewReader(bytes.NewReader([]byte{0x54, 0x52, 0x61, 0x49, 0x4c, 0x65, 0x52, 0x3a, 0x2c, 0x0a, 0x0a}))); !strings.Contains(err.Error(), "cannot find whitespace in the first line of response") {
3333
t.Fatal(err)
3434
}
35+
if err := (&Request{}).Read(bufio.NewReader(bytes.NewReader([]byte{0xff, 0x20, 0x0a, 0x54, 0x52, 0x61, 0x49, 0x4c, 0x65, 0x52, 0x3a, 0x2c, 0x0a, 0x0a}))); !strings.Contains(err.Error(), "contain forbidden trailer") {
36+
t.Fatal(err)
37+
}
3538

3639
b, _ := base64.StdEncoding.DecodeString("tCAKIDoKCToKICAKCToKICAKCToKIAogOgoJOgogIAoJOgovIC8vOi4KOh0KVFJhSUxlUjo9HT09HQpUUmFJTGVSOicQAApUUmFJTGVSOj0gHSAKCT09HQoKOgoKCgo=")
3740
if err := (&Request{}).Read(bufio.NewReader(bytes.NewReader(b))); !strings.Contains(err.Error(), "error when reading request headers: invalid header key") {

0 commit comments

Comments
 (0)