validate lengths when parsing frames

ref #123
This commit is contained in:
Lucas Clemente
2016-05-27 23:07:29 +02:00
parent c546f5c9dc
commit 7c23ba7b07
4 changed files with 20 additions and 0 deletions

View File

@@ -38,6 +38,10 @@ func ParseConnectionCloseFrame(r *bytes.Reader) (*ConnectionCloseFrame, error) {
return nil, err
}
if reasonPhraseLen > uint16(protocol.MaxPacketSize) {
return nil, qerr.Error(qerr.InvalidConnectionCloseData, "reason phrase too long")
}
reasonPhrase := make([]byte, reasonPhraseLen)
if _, err := io.ReadFull(r, reasonPhrase); err != nil {
return nil, err

View File

@@ -29,6 +29,12 @@ var _ = Describe("ConnectionCloseFrame", func() {
Expect(frame.ReasonPhrase).To(BeEmpty())
Expect(b.Len()).To(Equal(0))
})
It("rejects long reason phrases", func() {
b := bytes.NewReader([]byte{0x02, 0xAD, 0xFB, 0xCA, 0xDE, 0xff, 0xf})
_, err := ParseConnectionCloseFrame(b)
Expect(err).To(MatchError(qerr.Error(qerr.InvalidConnectionCloseData, "reason phrase too long")))
})
})
Context("when writing", func() {

View File

@@ -62,6 +62,10 @@ func ParseStreamFrame(r *bytes.Reader) (*StreamFrame, error) {
}
}
if dataLen > uint16(protocol.MaxPacketSize) {
return nil, qerr.Error(qerr.InvalidStreamData, "data len too large")
}
if dataLen == 0 {
// The rest of the packet is data
frame.Data, err = ioutil.ReadAll(r)

View File

@@ -47,6 +47,12 @@ var _ = Describe("StreamFrame", func() {
_, err := ParseStreamFrame(b)
Expect(err).To(MatchError(qerr.EmptyStreamFrameNoFin))
})
It("rejects frames to too large dataLen", func() {
b := bytes.NewReader([]byte{0xa0, 0x1, 0xff, 0xf})
_, err := ParseStreamFrame(b)
Expect(err).To(MatchError(qerr.Error(qerr.InvalidStreamData, "data len too large")))
})
})
Context("when writing", func() {