Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert " Update go version & add verification/testing tools (#840)" #908

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
run:
skip-dirs:
- examples/*.go
apoorvajagtap marked this conversation as resolved.
Show resolved Hide resolved
timeout: "5m"
# will not run golangci-lint against *_test.go
tests: false
issues:
exclude-dirs:
- examples/*.go
exclude-rules:
# excluding error checks from all the .go files
- path: ./*.go
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 changes: 4 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"errors"
"fmt"
"io"

"net"
"net/http"
"net/http/httptrace"
Expand Down Expand Up @@ -227,7 +226,6 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
k == "Connection" ||
k == "Sec-Websocket-Key" ||
k == "Sec-Websocket-Version" ||
//#nosec G101 (CWE-798): Potential HTTP request smuggling via parameter pollution
k == "Sec-Websocket-Extensions" ||
(k == "Sec-Websocket-Protocol" && len(d.Subprotocols) > 0):
return nil, nil, errors.New("websocket: duplicate header not allowed: " + k)
Expand Down Expand Up @@ -293,7 +291,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
}
err = c.SetDeadline(deadline)
if err != nil {
c.Close() //#nosec G104 (CWE-703): Errors unhandled
c.Close()
return nil, err
}
return c, nil
Expand Down Expand Up @@ -333,9 +331,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h

defer func() {
if netConn != nil {
// As mentioned in https://github.com/gorilla/websocket/pull/897#issuecomment-1947108098:
// It's safe to ignore the errors for netconn.Close()
netConn.Close() //#nosec G104 (CWE-703): Errors unhandled
netConn.Close()
}
}()

Expand Down Expand Up @@ -426,8 +422,8 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
resp.Body = io.NopCloser(bytes.NewReader([]byte{}))
conn.subprotocol = resp.Header.Get("Sec-Websocket-Protocol")

netConn.SetDeadline(time.Time{}) //#nosec G104 (CWE-703): Errors unhandled
netConn = nil // to avoid close in defer.
netConn.SetDeadline(time.Time{})
netConn = nil // to avoid close in defer.
return conn, resp, nil
}

Expand Down
17 changes: 9 additions & 8 deletions client_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type cstHandler struct{ *testing.T }
type cstServer struct {
*httptest.Server
URL string
t *testing.T
}

const (
Expand Down Expand Up @@ -113,7 +114,7 @@ func (t cstHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
if _, err = io.Copy(wr, rd); err != nil {
t.Logf("Copy: %v", err)
t.Logf("NextWriter: %v", err)
return
}
if err := wr.Close(); err != nil {
Expand Down Expand Up @@ -438,7 +439,7 @@ func TestDialBadOrigin(t *testing.T) {
if resp == nil {
t.Fatalf("resp=nil, err=%v", err)
}
if resp.StatusCode != http.StatusForbidden { // nolint:staticcheck
if resp.StatusCode != http.StatusForbidden {
t.Fatalf("status=%d, want %d", resp.StatusCode, http.StatusForbidden)
}
}
Expand Down Expand Up @@ -544,7 +545,7 @@ func TestRespOnBadHandshake(t *testing.T) {

s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(expectedStatus)
io.WriteString(w, expectedBody) // nolint:errcheck
io.WriteString(w, expectedBody)
}))
defer s.Close()

Expand All @@ -554,11 +555,11 @@ func TestRespOnBadHandshake(t *testing.T) {
t.Fatalf("Dial: nil")
}

if resp == nil { // nolint:staticcheck
if resp == nil {
t.Fatalf("resp=nil, err=%v", err)
}

if resp.StatusCode != expectedStatus { // nolint:staticcheck
if resp.StatusCode != expectedStatus {
t.Errorf("resp.StatusCode=%d, want %d", resp.StatusCode, expectedStatus)
}

Expand Down Expand Up @@ -797,7 +798,7 @@ func TestSocksProxyDial(t *testing.T) {
}
defer c1.Close()

c1.SetDeadline(time.Now().Add(30 * time.Second)) // nolint:errcheck
c1.SetDeadline(time.Now().Add(30 * time.Second))

buf := make([]byte, 32)
if _, err := io.ReadFull(c1, buf[:3]); err != nil {
Expand Down Expand Up @@ -836,10 +837,10 @@ func TestSocksProxyDial(t *testing.T) {
defer c2.Close()
done := make(chan struct{})
go func() {
io.Copy(c1, c2) // nolint:errcheck
io.Copy(c1, c2)
close(done)
}()
io.Copy(c2, c1) // nolint:errcheck
io.Copy(c2, c1)
<-done
}()

Expand Down
4 changes: 2 additions & 2 deletions compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func decompressNoContextTakeover(r io.Reader) io.ReadCloser {
"\x01\x00\x00\xff\xff"

fr, _ := flateReaderPool.Get().(io.ReadCloser)
fr.(flate.Resetter).Reset(io.MultiReader(r, strings.NewReader(tail)), nil) //#nosec G104 (CWE-703): Errors unhandled
fr.(flate.Resetter).Reset(io.MultiReader(r, strings.NewReader(tail)), nil)
return &flateReadWrapper{fr}
}

Expand Down Expand Up @@ -132,7 +132,7 @@ func (r *flateReadWrapper) Read(p []byte) (int, error) {
// Preemptively place the reader back in the pool. This helps with
// scenarios where the application does not call NextReader() soon after
// this final read.
r.Close() //#nosec G104 (CWE-703): Errors unhandled
r.Close()
}
return n, err
}
Expand Down
6 changes: 3 additions & 3 deletions compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestTruncWriter(t *testing.T) {
if m > n {
m = n
}
w.Write(p[:m]) // nolint:errcheck
w.Write(p[:m])
p = p[m:]
}
if b.String() != data[:len(data)-len(w.p)] {
Expand All @@ -47,7 +47,7 @@ func BenchmarkWriteNoCompression(b *testing.B) {
messages := textMessages(100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.WriteMessage(TextMessage, messages[i%len(messages)]) // nolint:errcheck
c.WriteMessage(TextMessage, messages[i%len(messages)])
}
b.ReportAllocs()
}
Expand All @@ -60,7 +60,7 @@ func BenchmarkWriteWithCompression(b *testing.B) {
c.newCompressionWriter = compressNoContextTakeover
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.WriteMessage(TextMessage, messages[i%len(messages)]) // nolint:errcheck
c.WriteMessage(TextMessage, messages[i%len(messages)])
}
b.ReportAllocs()
}
Expand Down
40 changes: 14 additions & 26 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,7 @@ func (c *Conn) read(n int) ([]byte, error) {
if err == io.EOF {
err = errUnexpectedEOF
}
if _, err := c.br.Discard(len(p)); err != nil {
return p, err
}
c.br.Discard(len(p))
return p, err
}

Expand All @@ -387,9 +385,7 @@ func (c *Conn) write(frameType int, deadline time.Time, buf0, buf1 []byte) error
return err
}

if err := c.conn.SetWriteDeadline(deadline); err != nil {
return c.writeFatal(err)
}
c.conn.SetWriteDeadline(deadline)
if len(buf1) == 0 {
_, err = c.conn.Write(buf0)
} else {
Expand All @@ -399,7 +395,7 @@ func (c *Conn) write(frameType int, deadline time.Time, buf0, buf1 []byte) error
return c.writeFatal(err)
}
if frameType == CloseMessage {
_ = c.writeFatal(ErrCloseSent)
c.writeFatal(ErrCloseSent)
}
return nil
}
Expand Down Expand Up @@ -468,15 +464,13 @@ func (c *Conn) WriteControl(messageType int, data []byte, deadline time.Time) er
return err
}

if err := c.conn.SetWriteDeadline(deadline); err != nil {
return c.writeFatal(err)
}
c.conn.SetWriteDeadline(deadline)
_, err = c.conn.Write(buf)
if err != nil {
return c.writeFatal(err)
}
if messageType == CloseMessage {
_ = c.writeFatal(ErrCloseSent)
c.writeFatal(ErrCloseSent)
}
return err
}
Expand All @@ -487,7 +481,7 @@ func (c *Conn) beginMessage(mw *messageWriter, messageType int) error {
// probably better to return an error in this situation, but we cannot
// change this without breaking existing applications.
if c.writer != nil {
_ = c.writer.Close()
c.writer.Close()
c.writer = nil
}

Expand Down Expand Up @@ -640,7 +634,7 @@ func (w *messageWriter) flushFrame(final bool, extra []byte) error {
}

if final {
_ = w.endMessage(errWriteClosed)
w.endMessage(errWriteClosed)
return nil
}

Expand Down Expand Up @@ -827,9 +821,7 @@ func (c *Conn) advanceFrame() (int, error) {
rsv2 := p[0]&rsv2Bit != 0
rsv3 := p[0]&rsv3Bit != 0
mask := p[1]&maskBit != 0
if err := c.setReadRemaining(int64(p[1] & 0x7f)); err != nil {
return noFrame, err
}
c.setReadRemaining(int64(p[1] & 0x7f))

c.readDecompress = false
if rsv1 {
Expand Down Expand Up @@ -934,7 +926,7 @@ func (c *Conn) advanceFrame() (int, error) {
}

if c.readLimit > 0 && c.readLength > c.readLimit {
c.WriteControl(CloseMessage, FormatCloseMessage(CloseMessageTooBig, ""), time.Now().Add(writeWait)) //#nosec G104 (CWE-703): Errors unhandled
c.WriteControl(CloseMessage, FormatCloseMessage(CloseMessageTooBig, ""), time.Now().Add(writeWait))
return noFrame, ErrReadLimit
}

Expand All @@ -946,9 +938,7 @@ func (c *Conn) advanceFrame() (int, error) {
var payload []byte
if c.readRemaining > 0 {
payload, err = c.read(int(c.readRemaining))
if err := c.setReadRemaining(0); err != nil {
return noFrame, err
}
c.setReadRemaining(0)
if err != nil {
return noFrame, err
}
Expand Down Expand Up @@ -995,7 +985,7 @@ func (c *Conn) handleProtocolError(message string) error {
if len(data) > maxControlFramePayloadSize {
data = data[:maxControlFramePayloadSize]
}
c.WriteControl(CloseMessage, data, time.Now().Add(writeWait)) //#nosec G104 (CWE-703): Errors unhandled
c.WriteControl(CloseMessage, data, time.Now().Add(writeWait))
return errors.New("websocket: " + message)
}

Expand All @@ -1012,7 +1002,7 @@ func (c *Conn) handleProtocolError(message string) error {
func (c *Conn) NextReader() (messageType int, r io.Reader, err error) {
// Close previous reader, only relevant for decompression.
if c.reader != nil {
_ = c.reader.Close()
c.reader.Close()
c.reader = nil
}

Expand Down Expand Up @@ -1068,9 +1058,7 @@ func (r *messageReader) Read(b []byte) (int, error) {
}
rem := c.readRemaining
rem -= int64(n)
if err := c.setReadRemaining(rem); err != nil {
return 0, err
}
c.setReadRemaining(rem)
if c.readRemaining > 0 && c.readErr == io.EOF {
c.readErr = errUnexpectedEOF
}
Expand Down Expand Up @@ -1152,7 +1140,7 @@ func (c *Conn) SetCloseHandler(h func(code int, text string) error) {
if h == nil {
h = func(code int, text string) error {
message := FormatCloseMessage(code, "")
_ = c.WriteControl(CloseMessage, message, time.Now().Add(writeWait))
c.WriteControl(CloseMessage, message, time.Now().Add(writeWait))
return nil
}
}
Expand Down
8 changes: 2 additions & 6 deletions conn_broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,9 @@ func (b *broadcastBench) makeConns(numConns int) {
select {
case msg := <-c.msgCh:
if msg.prepared != nil {
if err := c.conn.WritePreparedMessage(msg.prepared); err != nil {
panic(err)
}
c.conn.WritePreparedMessage(msg.prepared)
} else {
if err := c.conn.WriteMessage(TextMessage, msg.payload); err != nil {
panic(err)
}
c.conn.WriteMessage(TextMessage, msg.payload)
}
val := atomic.AddInt32(&b.count, 1)
if val%int32(numConns) == 0 {
Expand Down
Loading
Loading