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:
Joseph Doherty
2026-12-03 10:02:42 -05:00
parent 0a62f33ab5
commit 6dd6c50e6f
4 changed files with 122 additions and 3 deletions
@@ -285,6 +285,17 @@ public sealed class CookieSessionValidator
identity.AddClaim(new Claim(JwtTokenService.LastActivityClaimType, lastActivity));
}
else _activity?.Record(rebuilt, now);
// SEC-1: a passive request cannot persist the rebuilt ticket, so remember on this node that the
// refresh ran - but only when it changed nothing, so a changed grant keeps being re-derived
// until a non-passive request writes it into the cookie.
if (!recordActivity && _activity is not null && SameGrants(principal, rebuilt))
{
var roleAnchor = ReadAnchor(rebuilt, JwtTokenService.LastRoleRefreshClaimType) ?? now;
var groupAnchor2 = ReadAnchor(rebuilt, JwtTokenService.LastGroupRefreshClaimType) ?? now;
_activity.RecordRefresh(principal, roleAnchor, groupAnchor2);
}
return SessionValidationResult.Replace(rebuilt);
}
@@ -304,9 +315,25 @@ public sealed class CookieSessionValidator
return false;
var anchor = ReadAnchor(principal, JwtTokenService.LastGroupRefreshClaimType);
if (_activity?.LastRefreshes(principal) is { } seen && (anchor is null || seen.Group > anchor))
anchor = seen.Group;
return anchor is null || now - anchor.Value >= _directory.Interval;
}
// True when two principals carry the same grant claims (roles, groups, scope, identity), ignoring
// the refresh/activity timestamps and the session id.
private static bool SameGrants(ClaimsPrincipal before, ClaimsPrincipal after)
{
static HashSet<string> Grants(ClaimsPrincipal p) => p.Claims
.Where(c => c.Type is not (JwtTokenService.LastActivityClaimType
or JwtTokenService.LastRoleRefreshClaimType
or JwtTokenService.LastGroupRefreshClaimType
or SessionActivityTracker.SessionIdClaim))
.Select(c => c.Type + "\u0000" + c.Value)
.ToHashSet(StringComparer.Ordinal);
return Grants(before).SetEquals(Grants(after));
}
private static DateTimeOffset? ReadAnchor(ClaimsPrincipal principal, string claimType)
=> principal.FindFirst(claimType) is { } claim && DateTimeOffset.TryParse(claim.Value, out var value) ? value : null;
@@ -321,11 +348,14 @@ public sealed class CookieSessionValidator
public bool IsRoleRefreshDue(ClaimsPrincipal principal, DateTimeOffset now)
{
var claim = principal.FindFirst(JwtTokenService.LastRoleRefreshClaimType);
if (claim is null || !DateTimeOffset.TryParse(claim.Value, out var lastRefresh))
DateTimeOffset? lastRefresh = claim is not null && DateTimeOffset.TryParse(claim.Value, out var parsed) ? parsed : null;
if (_activity?.LastRefreshes(principal) is { } seen && (lastRefresh is null || seen.Role > lastRefresh))
lastRefresh = seen.Role;
if (lastRefresh is null)
{
return true;
}
return (now - lastRefresh).TotalMinutes > _options.RoleRefreshThresholdMinutes;
return (now - lastRefresh.Value).TotalMinutes > _options.RoleRefreshThresholdMinutes;
}
}
@@ -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);
}
}
}