Skip to content
This repository was archived by the owner on May 28, 2022. It is now read-only.

Commit 9d9627f

Browse files
committed
TUN-5836: Avoid websocket#Stream function from crashing cloudflared with unexpected memory access
1 parent 5c6207d commit 9d9627f

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

websocket/websocket.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"sync/atomic"
1313
"time"
1414

15+
"github.com/getsentry/raven-go"
1516
"github.com/gorilla/websocket"
1617
"github.com/rs/zerolog"
1718
)
@@ -72,13 +73,25 @@ func unidirectionalStream(dst io.Writer, src io.Reader, dir string, status *bidi
7273
// close. In such case, if the other direction did not stop (due to application level stopping, e.g., if a
7374
// server/origin listens forever until closure), it may read/write from the underlying ReadWriter (backed by
7475
// the Edge<->cloudflared transport) in an unexpected state.
75-
76-
if status.isAnyDone() {
77-
// Because of this, we set this recover() logic, which kicks-in *only* if any stream is known to have
78-
// exited. In such case, we stop a possible panic from propagating upstream.
79-
if r := recover(); r != nil {
76+
// Because of this, we set this recover() logic.
77+
if r := recover(); r != nil {
78+
if status.isAnyDone() {
8079
// We handle such unexpected errors only when we detect that one side of the streaming is done.
8180
log.Debug().Msgf("Gracefully handled error %v in Streaming for %s, error %s", r, dir, debug.Stack())
81+
} else {
82+
// Otherwise, this is unexpected, but we prevent the program from crashing anyway.
83+
log.Warn().Msgf("Gracefully handled unexpected error %v in Streaming for %s, error %s", r, dir, debug.Stack())
84+
85+
tags := make(map[string]string)
86+
tags["root"] = "websocket.stream"
87+
tags["dir"] = dir
88+
switch rval := r.(type) {
89+
case error:
90+
raven.CaptureError(rval, tags)
91+
default:
92+
rvalStr := fmt.Sprint(rval)
93+
raven.CaptureMessage(rvalStr, tags)
94+
}
8295
}
8396
}
8497
}()

0 commit comments

Comments
 (0)