Files
ScadaBridge/src/ZB.MOM.WW.ScadaBridge.Security/SessionActivityTracker.cs
T
Joseph Doherty 6dd6c50e6f fix(security): remember an unchanged passive-request role refresh per session instead of re-issuing the cookie (SEC-1, WP7 task 7.6)
The first attempt (renew the cookie on a passive Replace) contradicted the documented rule that passive requests never re-issue the cookie and was reverted; the per-session anchor record in SessionActivityTracker honours it.
2026-12-03 10:02:42 -05:00

83 lines
4.2 KiB
C#

using System.Security.Claims;
namespace ZB.MOM.WW.ScadaBridge.Security;
/// <summary>
/// Bounded, process-local activity shared by an HTTP cookie and its existing Blazor
/// circuit. Only authenticated, validated interaction requests advance this clock.
/// The protected cookie remains the durable anchor on restart or node change.
/// </summary>
public sealed class SessionActivityTracker
{
/// <summary>Opaque per-login identifier carried in the protected cookie.</summary>
public const string SessionIdClaim = "zb:sessionid";
private const int Capacity = 10_000;
private readonly object _sync = new();
private readonly Dictionary<string, DateTimeOffset> _activity = new(StringComparer.Ordinal);
private readonly Dictionary<string, (DateTimeOffset Role, DateTimeOffset Group)> _refreshes = new(StringComparer.Ordinal);
/// <summary>Returns the newer of the protected cookie anchor and this node's observed activity.</summary>
/// <param name="principal">The principal whose session activity is queried.</param>
/// <returns>The most recent activity time, or <see langword="null"/> when the principal carries no activity claim.</returns>
public DateTimeOffset? LastActivity(ClaimsPrincipal principal)
{
if (!DateTimeOffset.TryParse(principal.FindFirst(JwtTokenService.LastActivityClaimType)?.Value, out var stamp))
return null;
var id = principal.FindFirst(SessionIdClaim)?.Value;
lock (_sync)
return id is not null && _activity.TryGetValue(id, out var seen) && seen > stamp ? seen : stamp;
}
/// <summary>Records a validated interaction without changing another login's activity.</summary>
/// <param name="principal">The principal whose session is recorded.</param>
/// <param name="now">The time of the interaction.</param>
public void Record(ClaimsPrincipal principal, DateTimeOffset now)
{
var id = principal.FindFirst(SessionIdClaim)?.Value;
if (id is null || principal.Identity?.IsAuthenticated != true) return;
lock (_sync)
{
if (!_activity.ContainsKey(id) && _activity.Count >= Capacity)
_activity.Remove(_activity.MinBy(p => p.Value).Key);
if (!_activity.TryGetValue(id, out var previous) || now > previous)
_activity[id] = now;
}
}
/// <summary>
/// Returns the refresh anchors this node recorded for the principal's session, or
/// <see langword="null"/> when none (SEC-1). They supersede the older anchors in the cookie.
/// </summary>
/// <param name="principal">The principal whose session is queried.</param>
/// <returns>The recorded role and group refresh instants, if any.</returns>
public (DateTimeOffset Role, DateTimeOffset Group)? LastRefreshes(ClaimsPrincipal principal)
{
var id = principal.FindFirst(SessionIdClaim)?.Value;
if (id is null) return null;
lock (_sync)
return _refreshes.TryGetValue(id, out var seen) ? seen : null;
}
/// <summary>
/// Records that a role/group refresh ran for the session without changing its grants. A passive
/// request (<c>/auth/ping</c>, <c>/_blazor</c>) never re-issues the cookie, so without this the old
/// anchors in the cookie made every later ping repeat the refresh (SEC-1). Only an UNCHANGED outcome
/// may be recorded: a changed one must keep being re-derived until a non-passive request persists it
/// in the cookie, otherwise the stale cookie roles would outlive the refresh.
/// </summary>
/// <param name="principal">The principal whose session is recorded.</param>
/// <param name="role">The role-refresh anchor now in effect.</param>
/// <param name="group">The group-refresh anchor now in effect.</param>
public void RecordRefresh(ClaimsPrincipal principal, DateTimeOffset role, DateTimeOffset group)
{
var id = principal.FindFirst(SessionIdClaim)?.Value;
if (id is null || principal.Identity?.IsAuthenticated != true) return;
lock (_sync)
{
if (!_refreshes.ContainsKey(id) && _refreshes.Count >= Capacity)
_refreshes.Remove(_refreshes.MinBy(p => p.Value.Role).Key);
_refreshes[id] = (role, group);
}
}
}