mirror of
https://github.com/hanzoai/team.git
synced 2026-09-30 01:57:35 +00:00
* MM-68543 Invalidate active WebConn session cache on global session revocation Mirrors the per-user revocation pattern (ClearUserSessionCache -> ClearSessionCacheForUserSkipClusterSend -> hub fan-out) for the global revocation path so that ClearAllUsersSessionCache invokes the same local-side primitive on the originating node as the cluster handler runs on remote nodes. Also covers single-node deployments where the cluster broadcast was previously the only trigger of the WebConn invalidation. Adds a Hub.InvalidateAll fan-out primitive on the websocket hub and two contract tests covering the SkipClusterSend variant and the production RevokeSessionsFromAllUsers entry point. Made-with: Cursor * MM-68543 Restore error propagation on ClearAllUsersSessionCache The previous commit moved the local-side work into ClearSessionCacheForAllUsersSkipClusterSend, which returned no error, so ClearAllUsersSessionCache started always returning nil even when the underlying session-cache purge failed. Make the helper return the cache-purge error and propagate it back through ClearAllUsersSessionCache, restoring the historical error contract for callers (RevokeSessionsFromAllUsers, App.ClearSessionCacheForAllUsers, TestCache). The hub fan-out and the cluster broadcast still run unconditionally so security invalidation happens even on local-purge failure. Made-with: Cursor * MM-68543 Trim comments and fix unchecked errcheck on App wrapper Address review feedback: trim verbose comments across the touched files and check the error returned by ClearSessionCacheForAllUsersSkipClusterSend in the App-level wrapper to fix the golangci-lint errcheck failure introduced when the helper started returning an error. Made-with: Cursor * MM-68543 Wipe channel routing index instead of rebuilding it on global revoke Rebuilding byChannelID per user via InvalidateCMCacheForUser issues a GetAllChannelMembersForUser DB query for every user with a live conn on the hub, which is wasted work when those conns have just been invalidated. Replace it with a single clear() of byChannelID, hidden behind a small clearChannels() helper. Index entries repopulate naturally as conns re-handshake or fully reconnect. --------- Co-authored-by: Hanzo Dev <[email protected]>
112 lines
3.6 KiB
Go
112 lines
3.6 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package platform
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
)
|
|
|
|
// waitForWebConnRegistered blocks until the hub has processed the
|
|
// WebConn registration. Without it, a test can race past the async
|
|
// register channel and signal invalidation against an empty connIndex.
|
|
func waitForWebConnRegistered(t *testing.T, th *TestHelper, session *model.Session) {
|
|
t.Helper()
|
|
require.Eventually(t, func() bool {
|
|
return th.Service.SessionIsRegistered(*session)
|
|
}, 2*time.Second, 10*time.Millisecond,
|
|
"WebConn for session %q (user %q) was not registered with the hub in time",
|
|
session.Id, session.UserId)
|
|
}
|
|
|
|
// preWarmStatusOnline marks the user online before any WebConn is
|
|
// created so the async SetStatusOnline goroutine in NewWebConn skips
|
|
// the broadcast path. Otherwise the broadcast can race with the
|
|
// invalidation and re-populate the WebConn's cached session via
|
|
// IsBasicAuthenticated, flipping the post-invalidate assertions.
|
|
func preWarmStatusOnline(th *TestHelper, userID string) {
|
|
th.Service.AddStatusCacheSkipClusterSend(&model.Status{
|
|
UserId: userID,
|
|
Status: model.StatusOnline,
|
|
LastActivityAt: model.GetMillis(),
|
|
})
|
|
}
|
|
|
|
// TestClearSessionCacheInvalidatesWebConnSession asserts that after either
|
|
// the per-user or the global session-cache clear runs, every matching
|
|
// active WebSocket connection has its cached session reset to the
|
|
// authenticated-as-no-one state (GetSession() == nil and
|
|
// GetSessionExpiresAt() == 0).
|
|
func TestClearSessionCacheInvalidatesWebConnSession(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
|
|
tests := []struct {
|
|
name string
|
|
revoke func(ps *PlatformService, userID string)
|
|
}{
|
|
{
|
|
name: "PerUserRevokeInvalidatesWebConnSession",
|
|
revoke: func(ps *PlatformService, userID string) {
|
|
ps.ClearSessionCacheForUserSkipClusterSend(userID)
|
|
},
|
|
},
|
|
{
|
|
name: "GlobalRevokeInvalidatesWebConnSession",
|
|
revoke: func(ps *PlatformService, _ string) {
|
|
_ = ps.ClearSessionCacheForAllUsersSkipClusterSend()
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
th := Setup(t).InitBasic(t)
|
|
|
|
s := httptest.NewServer(dummyWebsocketHandler(t))
|
|
defer s.Close()
|
|
|
|
session, err := th.Service.CreateSession(th.Context, &model.Session{
|
|
UserId: th.BasicUser.Id,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
// Pin a future expiry so IsBasicAuthenticated trusts the
|
|
// cached session and doesn't re-validate against the store.
|
|
session.ExpiresAt = model.GetMillis() + time.Hour.Milliseconds()
|
|
|
|
preWarmStatusOnline(th, th.BasicUser.Id)
|
|
|
|
wc := registerDummyWebConn(t, th, s.Listener.Addr(), session)
|
|
defer wc.Close()
|
|
|
|
waitForWebConnRegistered(t, th, session)
|
|
|
|
require.NotNil(t, wc.GetSession(),
|
|
"precondition: webconn must have a cached session before revoke")
|
|
require.Greater(t, wc.GetSessionExpiresAt(), model.GetMillis(),
|
|
"precondition: webconn cached session expiry must be in the future before revoke")
|
|
|
|
tt.revoke(th.Service, th.BasicUser.Id)
|
|
|
|
// Hub invalidation is async, so poll for the end state.
|
|
require.Eventually(t, func() bool {
|
|
return wc.GetSession() == nil && wc.GetSessionExpiresAt() == 0
|
|
}, 2*time.Second, 25*time.Millisecond,
|
|
"webconn cached session was not invalidated after %s; "+
|
|
"expected GetSession()==nil and GetSessionExpiresAt()==0, "+
|
|
"but got GetSession()!=nil=%t, GetSessionExpiresAt()=%d, GetSessionToken()=%q",
|
|
tt.name,
|
|
wc.GetSession() != nil,
|
|
wc.GetSessionExpiresAt(),
|
|
wc.GetSessionToken(),
|
|
)
|
|
})
|
|
}
|
|
}
|