fix: guard TeamServiceBase against null current user#63
Merged
Conversation
Loading a <TeamComponent /> page before authentication (or after session expiry) crashed the Blazor circuit with NullReferenceException because TeamServiceBase.GetTeamsAsync took the result of GetCurrentUserAsync() and passed it straight to the MongoDB GetTeamsAsync(IUser) override, which dereferenced user.Key. The same unguarded GetCurrentUserAsync pattern existed at 7 call sites in TeamServiceBase. Each call site now handles null per its semantic: - Read paths (GetTeamsAsync, GetTeamsAsync<TMember>) yield break silently. Empty result is the correct semantic for "no current user, no teams to show". - Touch path (SetMemberLastSeenAsync) returns silently. It's a passive heartbeat; throwing would surprise no-op callers. - Side-effect paths (CreateTeamAsync, RemoveMemberAsync, TransferOwnershipAsync, AssureAccessLevel) call a new private RequireCurrentUserAsync() helper that throws UnauthorizedAccessException with "Authentication required." Surfaces as 401 in pipelines that map UnauthorizedAccessException, otherwise propagates. Also adds a defensive null guard at the data-layer boundary in TeamServiceRepositoryBase.GetTeamsAsync(IUser user) — returns AsyncEnumerable.Empty<ITeam>() for null input. Belt-and-suspenders so the same bug class can't be reintroduced from a future caller that forgets the upstream guard. 6 new tests in UnauthenticatedTeamServiceTests cover each public API path. 280 / 280 tests pass; build clean.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Loading a
<TeamComponent />page before authentication (or after session expiry) crashed the Blazor circuit withNullReferenceException.TeamServiceBase.GetTeamsAsync<TMember>()took the result ofGetCurrentUserAsync()and passed it straight to the MongoDBGetTeamsAsync(IUser)override, which dereferenceduser.Key. The same unguardedGetCurrentUserAsyncpattern existed at 7 call sites inTeamServiceBase.Each call site now handles a null current user according to its semantic:
GetTeamsAsync()(non-generic)yield break(empty stream)GetTeamsAsync<TMember>()yield breakCreateTeamAsync(string name)UnauthorizedAccessException("Authentication required.")RemoveMemberAsyncSetMemberLastSeenAsyncTransferOwnershipAsync<TMember>AssureAccessLevel<TMember>(private gate)A new private
RequireCurrentUserAsync()helper centralises the throw; the existingGetCurrentUserAsync()is kept for read paths that handle null themselves.A defensive null guard at the data layer was also added:
TeamServiceRepositoryBase.GetTeamsAsync(IUser user)returnsAsyncEnumerable.Empty<ITeam>()for null input. Belt-and-suspenders so the same bug class can't be reintroduced from a future caller that forgets the upstream guard.Consumer impact
TeamServiceBase.UnauthorizedAccessExceptionisSystem.UnauthorizedAccessException(not a custom Tharga type) so it integrates cleanly with ASP.NET pipelines that map it to 401.<TeamComponent />rendered to an unauthenticated principal now show the empty state ("You are not member of a team.") instead of crashing the circuit. Authenticated flow is unchanged.Branch contents
This branch carries three commits to master:
fa36d13 feat: invited-member-edit-fix complete— close-out for #62.ed7346f fix: guard TeamServiceBase against null current user— this fix.4e9aeb8 feat: unauthenticated-team-service-guards complete— finalize commit removing the feature's workingplan/so master never carries it.Test plan
dotnet build -c Release-- 0 warnings, 0 errors.dotnet test -c Release-- 280 / 280 passing (was 274, +6 new inUnauthenticatedTeamServiceTests):GetTeamsAsync_Unauthenticated_ReturnsEmptyGetTeamsAsync_Generic_Unauthenticated_ReturnsEmptyCreateTeamAsync_Unauthenticated_ThrowsRemoveMemberAsync_Unauthenticated_Throws(targets a non-owner row -- the owner-protection check fires first for the owner row)SetMemberLastSeenAsync_Unauthenticated_DoesNotThrowTransferOwnershipAsync_Unauthenticated_Throws