fix(security): enforce resource isolation across API and hubs
This commit is contained in:
1 parent
21f0530b6c
commit
1cd4e2b4b5
39 files changed
+1081
-497
No files matched your search
@@ -0,0 +1,30 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Heimdall.Api.Security;
|
||||
|
||||
/// <summary>Connection subscriptions only. Authorization is re-evaluated from the database on every send.</summary>
|
||||
public sealed class LiveSubscriptions
|
||||
{
|
||||
private readonly ConcurrentDictionary<(string Hub, string Connection, string Channel), ClaimsPrincipal> _entries = new();
|
||||
public void Join(string hub, string connection, string channel, ClaimsPrincipal principal)
|
||||
=> _entries[(hub, connection, channel)] = principal;
|
||||
public void Leave(string hub, string connection, string channel)
|
||||
=> _entries.TryRemove((hub, connection, channel), out _);
|
||||
public void Disconnect(string hub, string connection)
|
||||
{
|
||||
foreach (var key in _entries.Keys.Where(k => k.Hub == hub && k.Connection == connection))
|
||||
_entries.TryRemove(key, out _);
|
||||
}
|
||||
public async Task<IReadOnlyList<string>> AuthorizedAsync(string hub, string channel, ResourceAccess access,
|
||||
Func<Requester, Task<bool>> authorize, CancellationToken ct = default)
|
||||
{
|
||||
var result = new List<string>();
|
||||
foreach (var (key, principal) in _entries.Where(e => e.Key.Hub == hub && e.Key.Channel == channel))
|
||||
{
|
||||
var requester = await access.ResolveAsync(principal, ct);
|
||||
if (requester is not null && await authorize(requester)) result.Add(key.Connection);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user