-
Notifications
You must be signed in to change notification settings - Fork 313
/
Copy pathaccept.go
223 lines (187 loc) · 6.48 KB
/
accept.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// +build !js
package websocket
import (
"bytes"
"crypto/sha1"
"encoding/base64"
"errors"
"fmt"
"io"
"net/http"
"net/textproto"
"net/url"
"strings"
)
// AcceptOptions represents the options available to pass to Accept.
type AcceptOptions struct {
// Subprotocols lists the websocket subprotocols that Accept will negotiate with a client.
// The empty subprotocol will always be negotiated as per RFC 6455. If you would like to
// reject it, close the connection if c.Subprotocol() == "".
Subprotocols []string
// InsecureSkipVerify disables Accept's origin verification
// behaviour. By default Accept only allows the handshake to
// succeed if the javascript that is initiating the handshake
// is on the same domain as the server. This is to prevent CSRF
// attacks when secure data is stored in a cookie as there is no same
// origin policy for WebSockets. In other words, javascript from
// any domain can perform a WebSocket dial on an arbitrary server.
// This dial will include cookies which means the arbitrary javascript
// can perform actions as the authenticated user.
//
// See https://stackoverflow.com/a/37837709/4283659
//
// The only time you need this is if your javascript is running on a different domain
// than your WebSocket server.
// Think carefully about whether you really need this option before you use it.
// If you do, remember that if you store secure data in cookies, you wil need to verify the
// Origin header yourself otherwise you are exposing yourself to a CSRF attack.
InsecureSkipVerify bool
}
func verifyClientRequest(w http.ResponseWriter, r *http.Request) error {
if !r.ProtoAtLeast(1, 1) {
err := fmt.Errorf("websocket protocol violation: handshake request must be at least HTTP/1.1: %q", r.Proto)
http.Error(w, err.Error(), http.StatusBadRequest)
return err
}
if !headerValuesContainsToken(r.Header, "Connection", "Upgrade") {
err := fmt.Errorf("websocket protocol violation: Connection header %q does not contain Upgrade", r.Header.Get("Connection"))
http.Error(w, err.Error(), http.StatusBadRequest)
return err
}
if !headerValuesContainsToken(r.Header, "Upgrade", "WebSocket") {
err := fmt.Errorf("websocket protocol violation: Upgrade header %q does not contain websocket", r.Header.Get("Upgrade"))
http.Error(w, err.Error(), http.StatusBadRequest)
return err
}
if r.Method != "GET" {
err := fmt.Errorf("websocket protocol violation: handshake request method is not GET but %q", r.Method)
http.Error(w, err.Error(), http.StatusBadRequest)
return err
}
if r.Header.Get("Sec-WebSocket-Version") != "13" {
err := fmt.Errorf("unsupported websocket protocol version (only 13 is supported): %q", r.Header.Get("Sec-WebSocket-Version"))
http.Error(w, err.Error(), http.StatusBadRequest)
return err
}
if r.Header.Get("Sec-WebSocket-Key") == "" {
err := errors.New("websocket protocol violation: missing Sec-WebSocket-Key")
http.Error(w, err.Error(), http.StatusBadRequest)
return err
}
return nil
}
// Accept accepts a WebSocket handshake from a client and upgrades the
// the connection to a WebSocket.
//
// Accept will reject the handshake if the Origin domain is not the same as the Host unless
// the InsecureSkipVerify option is set. In other words, by default it does not allow
// cross origin requests.
//
// If an error occurs, Accept will always write an appropriate response so you do not
// have to.
func Accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (*Conn, error) {
c, err := accept(w, r, opts)
if err != nil {
return nil, fmt.Errorf("failed to accept websocket connection: %w", err)
}
return c, nil
}
func accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (*Conn, error) {
if opts == nil {
opts = &AcceptOptions{}
}
err := verifyClientRequest(w, r)
if err != nil {
return nil, err
}
if !opts.InsecureSkipVerify {
err = authenticateOrigin(r)
if err != nil {
http.Error(w, err.Error(), http.StatusForbidden)
return nil, err
}
}
hj, ok := w.(http.Hijacker)
if !ok {
err = errors.New("passed ResponseWriter does not implement http.Hijacker")
http.Error(w, http.StatusText(http.StatusNotImplemented), http.StatusNotImplemented)
return nil, err
}
w.Header().Set("Upgrade", "websocket")
w.Header().Set("Connection", "Upgrade")
handleSecWebSocketKey(w, r)
subproto := selectSubprotocol(r, opts.Subprotocols)
if subproto != "" {
w.Header().Set("Sec-WebSocket-Protocol", subproto)
}
w.WriteHeader(http.StatusSwitchingProtocols)
netConn, brw, err := hj.Hijack()
if err != nil {
err = fmt.Errorf("failed to hijack connection: %w", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return nil, err
}
// https://github.com/golang/go/issues/32314
b, _ := brw.Reader.Peek(brw.Reader.Buffered())
brw.Reader.Reset(io.MultiReader(bytes.NewReader(b), netConn))
c := &Conn{
subprotocol: w.Header().Get("Sec-WebSocket-Protocol"),
br: brw.Reader,
bw: brw.Writer,
closer: netConn,
}
c.init()
return c, nil
}
func headerValuesContainsToken(h http.Header, key, token string) bool {
key = textproto.CanonicalMIMEHeaderKey(key)
for _, val2 := range h[key] {
if headerValueContainsToken(val2, token) {
return true
}
}
return false
}
func headerValueContainsToken(val2, token string) bool {
val2 = strings.TrimSpace(val2)
for _, val2 := range strings.Split(val2, ",") {
val2 = strings.TrimSpace(val2)
if strings.EqualFold(val2, token) {
return true
}
}
return false
}
func selectSubprotocol(r *http.Request, subprotocols []string) string {
for _, sp := range subprotocols {
if headerValuesContainsToken(r.Header, "Sec-WebSocket-Protocol", sp) {
return sp
}
}
return ""
}
var keyGUID = []byte("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
func handleSecWebSocketKey(w http.ResponseWriter, r *http.Request) {
key := r.Header.Get("Sec-WebSocket-Key")
w.Header().Set("Sec-WebSocket-Accept", secWebSocketAccept(key))
}
func secWebSocketAccept(secWebSocketKey string) string {
h := sha1.New()
h.Write([]byte(secWebSocketKey))
h.Write(keyGUID)
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
func authenticateOrigin(r *http.Request) error {
origin := r.Header.Get("Origin")
if origin == "" {
return nil
}
u, err := url.Parse(origin)
if err != nil {
return fmt.Errorf("failed to parse Origin header %q: %w", origin, err)
}
if strings.EqualFold(u.Host, r.Host) {
return nil
}
return fmt.Errorf("request Origin %q is not authorized for Host %q", origin, r.Host)
}