From 5556edbcad3df786071133dec7583d566cf30001 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Fri, 20 Oct 2017 23:11:57 +0700 Subject: [PATCH] fix parsing of long reason phrases in CONNECTION_CLOSE frames --- internal/wire/connection_close_frame.go | 8 ++++++-- internal/wire/connection_close_frame_test.go | 5 +++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/internal/wire/connection_close_frame.go b/internal/wire/connection_close_frame.go index 96c25c1ea..432c6a866 100644 --- a/internal/wire/connection_close_frame.go +++ b/internal/wire/connection_close_frame.go @@ -38,12 +38,16 @@ func ParseConnectionCloseFrame(r *bytes.Reader, version protocol.VersionNumber) return nil, err } - if reasonPhraseLen > uint16(protocol.MaxPacketSize) { - return nil, qerr.Error(qerr.InvalidConnectionCloseData, "reason phrase too long") + // shortcut to prevent the unneccessary allocation of dataLen bytes + // if the dataLen is larger than the remaining length of the packet + // reading the whole reason phrase would result in EOF when attempting to READ + if int(reasonPhraseLen) > r.Len() { + return nil, io.EOF } reasonPhrase := make([]byte, reasonPhraseLen) if _, err := io.ReadFull(r, reasonPhrase); err != nil { + // this should never happen, since we already checked the reasonPhraseLen earlier return nil, err } frame.ReasonPhrase = string(reasonPhrase) diff --git a/internal/wire/connection_close_frame_test.go b/internal/wire/connection_close_frame_test.go index 351fa843f..012ca02df 100644 --- a/internal/wire/connection_close_frame_test.go +++ b/internal/wire/connection_close_frame_test.go @@ -2,6 +2,7 @@ package wire import ( "bytes" + "io" "strings" "github.com/lucas-clemente/quic-go/internal/protocol" @@ -32,7 +33,7 @@ var _ = Describe("ConnectionCloseFrame", func() { 0x0, 0xff, // reason phrase length }) _, err := ParseConnectionCloseFrame(b, versionLittleEndian) - Expect(err).To(MatchError(qerr.Error(qerr.InvalidConnectionCloseData, "reason phrase too long"))) + Expect(err).To(MatchError(io.EOF)) }) It("errors on EOFs", func() { @@ -70,7 +71,7 @@ var _ = Describe("ConnectionCloseFrame", func() { 0xff, 0x0, // reason phrase length }) _, err := ParseConnectionCloseFrame(b, versionBigEndian) - Expect(err).To(MatchError(qerr.Error(qerr.InvalidConnectionCloseData, "reason phrase too long"))) + Expect(err).To(MatchError(io.EOF)) }) It("errors on EOFs", func() {