Skip to content

Commit b9465b2

Browse files
committed
Get near full autobahn suite passing
1 parent 11e2521 commit b9465b2

File tree

3 files changed

+180
-110
lines changed

3 files changed

+180
-110
lines changed

statuscode.go

+30
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"math/bits"
8+
"unicode/utf8"
89

910
"golang.org/x/xerrors"
1011
)
@@ -53,9 +54,38 @@ func parseClosePayload(p []byte) (code StatusCode, reason string, err error) {
5354
code = StatusCode(binary.BigEndian.Uint16(p))
5455
reason = string(p[2:])
5556

57+
if !utf8.ValidString(reason) {
58+
return 0, "", xerrors.Errorf("invalid utf-8: %q", reason)
59+
}
60+
if !isValidReceivedCloseCode(code) {
61+
return 0, "", xerrors.Errorf("invalid code %v", code)
62+
}
63+
5664
return code, reason, nil
5765
}
5866

67+
var validReceivedCloseCodes = map[StatusCode]bool{
68+
// see http://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
69+
StatusNormalClosure: true,
70+
StatusGoingAway: true,
71+
StatusProtocolError: true,
72+
StatusUnsupportedData: true,
73+
StatusNoStatusRcvd: false,
74+
StatusAbnormalClosure: false,
75+
StatusInvalidFramePayloadData: true,
76+
StatusPolicyViolation: true,
77+
StatusMessageTooBig: true,
78+
StatusMandatoryExtension: true,
79+
StatusInternalError: true,
80+
StatusServiceRestart: true,
81+
StatusTryAgainLater: true,
82+
StatusTLSHandshake: false,
83+
}
84+
85+
func isValidReceivedCloseCode(code StatusCode) bool {
86+
return validReceivedCloseCodes[code] || (code >= 3000 && code <= 4999)
87+
}
88+
5989
const maxControlFramePayload = 125
6090

6191
func closePayload(code StatusCode, reason string) ([]byte, error) {

0 commit comments

Comments
 (0)