Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions API.IntegrationTests/Tests/PublicSharePasswordTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using OpenShock.API.IntegrationTests.Helpers;
using OpenShock.API.Services.Account;
using OpenShock.Common.Constants;
using OpenShock.Common.Models.WebSocket.User;
using OpenShock.Common.OpenShockDb;
Expand Down Expand Up @@ -455,6 +456,25 @@ await scope.ServiceProvider.GetRequiredService<ISessionService>()
await AssertConnectionClosesAsync(connection);
}

[Test]
public async Task Hub_DeactivatingTheOwner_ClosesGuestsAndLoggedInVisitors()
{
var (owner, shareId) = await CreateShareWithShocker("pwhubownerdeact", password: null);
var visitor = await TestHelper.CreateAndLoginUser(WebApplicationFactory,
TestHelper.UniqueUsername("pwhubownerdeactv"), TestHelper.UniqueEmail("pwhubownerdeactv"), "SecurePassword123#");
await using var guestConnection = await ConnectAsync(shareId, unlockToken: null);
await using var visitorConnection = await ConnectAsync(shareId, unlockToken: null, sessionToken: visitor.SessionToken);

await using (var scope = WebApplicationFactory.Services.CreateAsyncScope())
{
await scope.ServiceProvider.GetRequiredService<IAccountService>()
.DeactivateAccountAsync(owner.Id, owner.Id, deleteLater: false);
}

await AssertConnectionClosesAsync(guestConnection);
await AssertConnectionClosesAsync(visitorConnection);
}

// --- Helpers ---

private async Task<(AuthenticatedUser Owner, Guid ShareId)> CreateShareWithShocker(string prefix,
Expand Down
1 change: 1 addition & 0 deletions API/Controller/Shares/Links/DeletePublicShare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public sealed partial class ShareLinksController
/// </summary>
/// <param name="publicShareId"></param>
/// <param name="deviceUpdateService"></param>
/// <param name="redisPubService"></param>
/// <param name="cancellationToken"></param>
/// <response code="200">Deleted public share</response>
/// <response code="404">Public share not found or does not belong to you</response>
Expand Down
2 changes: 1 addition & 1 deletion API/Realtime/RedisSubscriberService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void HandleUserAuthRevoked(RedisChannel _, RedisValue message)
if (!Guid.TryParse((string?)message, out var userId)) return;

var closed = _hubConnections.AbortUser(userId);
if (closed > 0) _logger.LogDebug("Closed {Count} hub connection(s) of user [{UserId}]", closed, userId);
if (closed > 0) _logger.LogDebug("Closed {Count} hub connection(s) of user [{UserId}] or to their public shares", closed, userId);
}

private void HandlePublicShareAccessRevoked(RedisChannel _, RedisValue message)
Expand Down
17 changes: 12 additions & 5 deletions Common/Hubs/HubConnectionTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ public sealed class HubConnectionTracker
/// Starts tracking a public share connection, or marks an already tracked (logged in) one as such, so it is
/// closed when the share's access changes.
/// </summary>
/// <param name="context">The connection.</param>
/// <param name="publicShareId">The share the connection controls.</param>
/// <param name="publicShareOwnerId">The share's owner, whose deactivation closes the connection too.</param>
/// <param name="passwordStamp">The share's password stamp the visitor got in with, null when it had no password.</param>
public TrackedHubConnection TrackPublicShare(HubCallerContext context, Guid publicShareId, Guid? passwordStamp)
public TrackedHubConnection TrackPublicShare(HubCallerContext context, Guid publicShareId, Guid publicShareOwnerId, Guid? passwordStamp)
{
var tracked = _connections.GetOrAdd(context.ConnectionId, _ => new TrackedHubConnection(context, userId: null, sessionId: null, apiTokenId: null));
tracked.AttachPublicShare(publicShareId, passwordStamp);
tracked.AttachPublicShare(publicShareId, publicShareOwnerId, passwordStamp);
// A logged in connection was already checked, but not against the share: the next call has to.
tracked.ResetValidation();
// Kept on the connection too, so a call that arrives after a revocation untracked it can be told apart
Expand All @@ -84,9 +87,9 @@ public TrackedHubConnection TrackPublicShare(HubCallerContext context, Guid publ
/// <returns>How many connections were closed.</returns>
public int AbortSession(string sessionId) => Abort(c => c.SessionId == sessionId);

/// <summary>Closes every connection of this user, whatever it authenticated with.</summary>
/// <summary>Closes every connection of this user, whatever it authenticated with, and the connections to their public shares.</summary>
/// <returns>How many connections were closed.</returns>
public int AbortUser(Guid userId) => Abort(c => c.UserId == userId);
public int AbortUser(Guid userId) => Abort(c => c.UserId == userId || c.PublicShareOwnerId == userId);

/// <summary>Closes the connections that authenticated with this API token.</summary>
/// <returns>How many connections were closed.</returns>
Expand Down Expand Up @@ -134,12 +137,16 @@ public TrackedHubConnection(HubCallerContext context, Guid? userId, string? sess
/// <summary>The public share this connection controls, null for other hubs.</summary>
public Guid? PublicShareId { get; private set; }

/// <summary>The owner of <see cref="PublicShareId"/>.</summary>
public Guid? PublicShareOwnerId { get; private set; }

/// <summary>The share's password stamp when this connection was let in, null when it had no password.</summary>
public Guid? PasswordStamp { get; private set; }

public void AttachPublicShare(Guid publicShareId, Guid? passwordStamp)
public void AttachPublicShare(Guid publicShareId, Guid publicShareOwnerId, Guid? passwordStamp)
{
PublicShareId = publicShareId;
PublicShareOwnerId = publicShareOwnerId;
PasswordStamp = passwordStamp;
}

Expand Down
4 changes: 2 additions & 2 deletions Common/Hubs/PublicShareHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ public override async Task OnConnectedAsync()
AdditionalItems = additionalItems
}
};
// Closed when the share's password is set or changed, or the share is deleted. Left unvalidated so the
// Closed when the share's password is set or changed, the share is deleted or its owner deactivated. Left unvalidated so the
// first call checks the share again, covering a change made since it was read above.
_connectionTracker.TrackPublicShare(Context, id, share.PasswordStamp);
_connectionTracker.TrackPublicShare(Context, id, share.OwnerId, share.PasswordStamp);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Close connections that race with owner revocation

If the owner is deactivated after the share query at line 70 but before this connection is tracked, the UserAuthRevoked handler can run before PublicShareOwnerId is attached and miss the connection. The reset validation only checks the share on a later hub invocation, so an idle connection can remain open indefinitely despite the owner deactivation.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By design. The same window exists for password changes and share deletion (#416) and for session revocation (#407): a connection tracked after the revocation was published is not closed, but it is left unvalidated, so its first hub call checks the share against the database before anything runs. An idle connection that slipped through can't control any shocker.


await Groups.AddToGroupAsync(Context.ConnectionId, GroupName(id));
await Clients.Caller.Welcome(user is null ? AuthType.Guest : AuthType.Authenticated);
Expand Down