From d6275d1fc322c35d13ac73b09e2a7aba3e336ee0 Mon Sep 17 00:00:00 2001 From: yummybomb <19238148+yummybomb@users.noreply.github.com> Date: Wed, 18 Nov 2026 21:01:45 +0000 Subject: [PATCH] Recheck watch permission before closing the WebRTC peer --- server/internal/session/session.go | 11 +++- server/internal/session/session_test.go | 72 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/server/internal/session/session.go b/server/internal/session/session.go index 0f4e73b9e..80202d3cc 100644 --- a/server/internal/session/session.go +++ b/server/internal/session/session.go @@ -49,12 +49,19 @@ func (session *SessionCtx) profileChanged() { session.ClearHost() } - if (!session.profile.CanConnect || !session.profile.CanLogin || !session.profile.CanWatch) && session.state.IsWatching { + if !session.profile.CanConnect || !session.profile.CanLogin || !session.profile.CanWatch { // TODO: Needed for legacy implementation. Websocket must die before webrtc and deliver signal close message // otherwise webrtc destroy would trigger websocket reconnect. In case of kick event, webrtc destroy is called // before websocket destroy that delivers the information about the kick. time.AfterFunc(time.Second, func() { - if peer := session.GetWebRTCPeer(); peer != nil { + // Select the peer before allowing a profile update to grant access + // again. A peer installed after that grant must not be closed here. + session.manager.sessionsMu.Lock() + profile := session.profile + peer := session.GetWebRTCPeer() + session.manager.sessionsMu.Unlock() + + if peer != nil && (!profile.CanConnect || !profile.CanLogin || !profile.CanWatch) { peer.Destroy() } }) diff --git a/server/internal/session/session_test.go b/server/internal/session/session_test.go index f6b550301..c7dc86b9b 100644 --- a/server/internal/session/session_test.go +++ b/server/internal/session/session_test.go @@ -61,6 +61,11 @@ type fakeWebRTCPeer struct { func (p *fakeWebRTCPeer) Destroy() { p.destroyed = true } +// A change of profile also sets the paused state of the peer. +type pausableWebRTCPeer struct{ fakeWebRTCPeer } + +func (*pausableWebRTCPeer) SetPaused(isPaused bool) error { return nil } + func newTestSession(t *testing.T) *SessionCtx { t.Helper() manager := New(&config.Session{}) @@ -258,6 +263,73 @@ func TestSessionThatCountsAsWatchingWithoutAPeerCanLoseItsLogin(t *testing.T) { }) } +// A session that may no longer watch keeps its websocket, so a peer that has +// not connected is not taken with it. Left alone it could still connect, and +// the session would watch without being allowed to. +func TestWebRTCPeerThatNeverConnectedIsDestroyedWhenItsSessionMayNoLongerWatch(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + session, _ := newConnectedTestSession(t) + peer := &pausableWebRTCPeer{} + session.SetWebRTCPeer(peer) + + profile := session.Profile() + profile.CanWatch = false + if err := session.manager.Update(session.ID(), profile); err != nil { + t.Fatalf("update session: %v", err) + } + + // the peer is destroyed from a timer + time.Sleep(2 * time.Second) + synctest.Wait() + if !peer.destroyed { + t.Error("peer was not destroyed") + } + }) +} + +// The peer of a session that may no longer watch is destroyed a moment later. +// A session that may watch again by then keeps the peer it has. +func TestWebRTCPeerIsKeptWhenItsSessionMayWatchAgain(t *testing.T) { + tests := []struct { + name string + connected bool + }{ + {name: "peer that has not connected"}, + {name: "peer that is streaming", connected: true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + session, _ := newConnectedTestSession(t) + session.SetWebRTCPeer(&pausableWebRTCPeer{}) + + profile := session.Profile() + revoked := profile + revoked.CanWatch = false + if err := session.manager.Update(session.ID(), revoked); err != nil { + t.Fatalf("update session: %v", err) + } + if err := session.manager.Update(session.ID(), profile); err != nil { + t.Fatalf("update session: %v", err) + } + + peer := &pausableWebRTCPeer{} + session.SetWebRTCPeer(peer) + if tt.connected { + session.SetWebRTCConnected(peer, true) + } + + time.Sleep(2 * time.Second) + synctest.Wait() + if peer.destroyed { + t.Error("peer was destroyed") + } + }) + }) + } +} + // A client can be back on a new websocket before the old one is done leaving, // because the session's listeners run in between. A peer the new websocket // asked for is not the old one's to destroy.