@@ -2,6 +2,7 @@ package websocket
22
33import (
44 "encoding/binary"
5+ "fmt"
56 "io"
67
78 "golang.org/x/xerrors"
@@ -16,7 +17,6 @@ const maxHeaderSize = 1 + 1 + 8 + 4
1617
1718// header represents a WebSocket frame header.
1819// See https://tools.ietf.org/html/rfc6455#section-5.2
19- // The fields are exported for easy printing for debugging.
2020type header struct {
2121 fin bool
2222 rsv1 bool
@@ -34,7 +34,7 @@ type header struct {
3434
3535// bytes returns the bytes of the header.
3636// See https://tools.ietf.org/html/rfc6455#section-5.2
37- func marshalHeader (h header ) ( []byte , error ) {
37+ func marshalHeader (h header ) []byte {
3838 b := make ([]byte , 2 , maxHeaderSize )
3939
4040 if h .fin {
@@ -54,7 +54,7 @@ func marshalHeader(h header) ([]byte, error) {
5454
5555 switch {
5656 case h .payloadLength < 0 :
57- return nil , xerrors . Errorf ( " invalid header: negative length: %v" , h .payloadLength )
57+ panic ( fmt . Sprintf ( "websocket: invalid header: negative length: %v" , h .payloadLength ) )
5858 case h .payloadLength <= 125 :
5959 b [1 ] = byte (h .payloadLength )
6060 case h .payloadLength <= 1 << 16 :
@@ -73,7 +73,7 @@ func marshalHeader(h header) ([]byte, error) {
7373 copy (b [len (b )- 4 :], h .maskKey [:])
7474 }
7575
76- return b , nil
76+ return b
7777}
7878
7979// readHeader reads a header from the reader.
@@ -130,6 +130,9 @@ func readHeader(r io.Reader) (header, error) {
130130 b = b [2 :]
131131 case payloadLength == 127 :
132132 h .payloadLength = int64 (binary .BigEndian .Uint64 (b ))
133+ if h .payloadLength < 0 {
134+ return header {}, xerrors .Errorf ("websocket: header has negative payload length: %v" , h .payloadLength )
135+ }
133136 b = b [8 :]
134137 }
135138
0 commit comments