Skip to content

Commit adf16b3

Browse files
committed
Add safe maskBytes
Fixes #200.
1 parent 5ddbd28 commit adf16b3

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

conn.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"errors"
1111
"io"
1212
"io/ioutil"
13+
"math/rand"
1314
"net"
1415
"strconv"
1516
"sync"
@@ -180,6 +181,11 @@ var (
180181
errInvalidControlFrame = errors.New("websocket: invalid control frame")
181182
)
182183

184+
func newMaskKey() [4]byte {
185+
n := rand.Uint32()
186+
return [4]byte{byte(n), byte(n >> 8), byte(n >> 16), byte(n >> 24)}
187+
}
188+
183189
func hideTempErr(err error) error {
184190
if e, ok := err.(net.Error); ok && e.Temporary() {
185191
err = &netError{msg: e.Error(), timeout: e.Timeout()}

mask.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,14 @@
22
// this source code is governed by a BSD-style license that can be found in the
33
// LICENSE file.
44

5+
// +build !appengine
6+
57
package websocket
68

7-
import (
8-
"math/rand"
9-
"unsafe"
10-
)
9+
import "unsafe"
1110

1211
const wordSize = int(unsafe.Sizeof(uintptr(0)))
1312

14-
func newMaskKey() [4]byte {
15-
n := rand.Uint32()
16-
return [4]byte{byte(n), byte(n >> 8), byte(n >> 16), byte(n >> 24)}
17-
}
18-
1913
func maskBytes(key [4]byte, pos int, b []byte) int {
2014

2115
// Mask one byte at a time for small buffers.

mask_safe.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2016 The Gorilla WebSocket Authors. All rights reserved. Use of
2+
// this source code is governed by a BSD-style license that can be found in the
3+
// LICENSE file.
4+
5+
// +build appengine
6+
7+
package websocket
8+
9+
func maskBytes(key [4]byte, pos int, b []byte) int {
10+
for i := range b {
11+
b[i] ^= key[pos&3]
12+
pos++
13+
}
14+
return pos & 3
15+
}

mask_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// LICENSE file.
44

55
// Require 1.7 for sub-bencmarks
6-
// +build go1.7
6+
// +build go1.7,!appengine
77

88
package websocket
99

0 commit comments

Comments
 (0)