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.
This commit is contained in:
@@ -14,6 +14,7 @@ public sealed class SessionActivityTracker
|
||||
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>
|
||||
@@ -42,4 +43,40 @@ public sealed class SessionActivityTracker
|
||||
_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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user