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:
@@ -257,6 +257,58 @@ public class CookieSessionValidatorTests
|
||||
Assert.Equal(SessionValidationAction.Reject, result.Action);
|
||||
}
|
||||
|
||||
private static CookieSessionValidator CreateTrackedValidator(
|
||||
IGroupRoleMapper<string> mapper, TimeProvider clock, SessionActivityTracker tracker) =>
|
||||
new(mapper, Microsoft.Extensions.Options.Options.Create(Options()), clock,
|
||||
NullLogger<CookieSessionValidator>.Instance, tracker);
|
||||
|
||||
/// <summary>
|
||||
/// SEC-1: a passive request cannot write the cookie, so an UNCHANGED role refresh is remembered on the
|
||||
/// node; the next ping (still carrying the old cookie anchors) must not repeat the re-map.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task ValidateAsync_PassiveUnchangedRefresh_IsNotRepeatedOnTheNextPing()
|
||||
{
|
||||
var clock = new TestTimeProvider(Start);
|
||||
var mapping = new RoleMappingResult([Roles.Administrator], [], true);
|
||||
var mapper = new FakeGroupRoleMapper(mapping);
|
||||
var sut = CreateTrackedValidator(mapper, clock, new SessionActivityTracker());
|
||||
var cookie = Session(Start, ["SCADA-Admins"], mapping); // the ticket never changes: passive requests cannot re-issue it
|
||||
|
||||
clock.Advance(TimeSpan.FromMinutes(16));
|
||||
var first = await sut.ValidateAsync(cookie, recordActivity: false);
|
||||
var second = await sut.ValidateAsync(cookie, recordActivity: false);
|
||||
clock.Advance(TimeSpan.FromMinutes(14)); // 30 min since login, 14 since the remembered refresh
|
||||
var third = await sut.ValidateAsync(cookie, recordActivity: false);
|
||||
|
||||
Assert.Equal(SessionValidationAction.Replace, first.Action);
|
||||
Assert.Equal(SessionValidationAction.Keep, second.Action);
|
||||
Assert.Equal(1, mapper.CallCount);
|
||||
Assert.Equal(SessionValidationAction.Keep, third.Action);
|
||||
Assert.Equal(1, mapper.CallCount);
|
||||
}
|
||||
|
||||
/// <summary>A passive refresh that CHANGED the grants is not remembered: it is re-derived until a non-passive request persists it.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task ValidateAsync_PassiveChangedRefresh_KeepsBeingRederived()
|
||||
{
|
||||
var clock = new TestTimeProvider(Start);
|
||||
var mapper = new FakeGroupRoleMapper(new RoleMappingResult([Roles.Viewer], [], false));
|
||||
var sut = CreateTrackedValidator(mapper, clock, new SessionActivityTracker());
|
||||
var cookie = Session(Start, ["SCADA-Admins"], new RoleMappingResult([Roles.Administrator], [], true));
|
||||
|
||||
clock.Advance(TimeSpan.FromMinutes(16));
|
||||
var first = await sut.ValidateAsync(cookie, recordActivity: false);
|
||||
var second = await sut.ValidateAsync(cookie, recordActivity: false);
|
||||
|
||||
Assert.Equal(SessionValidationAction.Replace, first.Action);
|
||||
Assert.Equal(SessionValidationAction.Replace, second.Action);
|
||||
Assert.Equal([Roles.Viewer], second.Principal!.FindAll(JwtTokenService.RoleClaimType).Select(c => c.Value));
|
||||
Assert.Equal(2, mapper.CallCount);
|
||||
}
|
||||
|
||||
/// <summary>A refresh that keeps one role still replaces the principal (the zero-role rule is not over-broad).</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
|
||||
Reference in New Issue
Block a user