Skip to content

Commit a2be2f9

Browse files
authored
fix: avoid derp-map updates endpoint leak (#9390)
1 parent ce9b048 commit a2be2f9

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

coderd/workspaceagents.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -878,13 +878,15 @@ func (api *API) derpMapUpdates(rw http.ResponseWriter, r *http.Request) {
878878
})
879879
return
880880
}
881-
nconn := websocket.NetConn(ctx, ws, websocket.MessageBinary)
881+
ctx, nconn := websocketNetConn(ctx, ws, websocket.MessageBinary)
882882
defer nconn.Close()
883883

884884
// Slurp all packets from the connection into io.Discard so pongs get sent
885-
// by the websocket package.
885+
// by the websocket package. We don't do any reads ourselves so this is
886+
// necessary.
886887
go func() {
887888
_, _ = io.Copy(io.Discard, nconn)
889+
_ = nconn.Close()
888890
}()
889891

890892
go func(ctx context.Context) {
@@ -899,13 +901,11 @@ func (api *API) derpMapUpdates(rw http.ResponseWriter, r *http.Request) {
899901
return
900902
}
901903

902-
// We don't need a context that times out here because the ping will
903-
// eventually go through. If the context times out, then other
904-
// websocket read operations will receive an error, obfuscating the
905-
// actual problem.
904+
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
906905
err := ws.Ping(ctx)
906+
cancel()
907907
if err != nil {
908-
_ = ws.Close(websocket.StatusInternalError, err.Error())
908+
_ = nconn.Close()
909909
return
910910
}
911911
}
@@ -920,7 +920,7 @@ func (api *API) derpMapUpdates(rw http.ResponseWriter, r *http.Request) {
920920
if lastDERPMap == nil || !tailnet.CompareDERPMaps(lastDERPMap, derpMap) {
921921
err := json.NewEncoder(nconn).Encode(derpMap)
922922
if err != nil {
923-
_ = ws.Close(websocket.StatusInternalError, err.Error())
923+
_ = nconn.Close()
924924
return
925925
}
926926
lastDERPMap = derpMap

0 commit comments

Comments
 (0)