fix(auth): end a cookie session whose roles drop to zero on refresh (#144)
The mid-session refresh ended a session only when the directory returned no groups (or the user was gone/disabled/ambiguous). A user still in some groups but in no mapped one, or whose DB mappings were all revoked, kept a signed-in, role-less session that could read the bare-[Authorize] pages. CookieSessionValidator now rejects when the re-map yields zero roles, on ordinary and fail-closed paths. A mapper fault keeps its existing policy (keep grants; fail closed when asked). Docs: Component-Security "No role, no login" (incl. the HTTP Basic gap left as a follow-up), Component-CentralUI, CLAUDE.md. Claude-Session: https://claude.ai/code/session_01GT67AFKC45MBz1wPZnVbX8
This commit is contained in:
@@ -27,6 +27,9 @@ public class DirectoryGroupRefreshTests
|
||||
}
|
||||
|
||||
/// <summary>Maps each group to a role by name (Admins→Administrator, Designers→Designer, else Viewer).</summary>
|
||||
// A group that maps to no ScadaBridge role (e.g. "Domain Users" under a domain-wide search base).
|
||||
private const string UnmappedGroup = "Domain Users";
|
||||
|
||||
private sealed class NamingMapper : IGroupRoleMapper<string>
|
||||
{
|
||||
public int Calls { get; private set; }
|
||||
@@ -40,8 +43,9 @@ public class DirectoryGroupRefreshTests
|
||||
{
|
||||
"SCADA-Admins" => Roles.Administrator,
|
||||
"SCADA-Designers" => Roles.Designer,
|
||||
UnmappedGroup => null,
|
||||
_ => Roles.Viewer,
|
||||
}).Distinct().ToList();
|
||||
}).OfType<string>().Distinct().ToList();
|
||||
var result = new RoleMappingResult(roles, [], IsSystemWideDeployment: true);
|
||||
return Task.FromResult(new GroupRoleMapping<string>(roles, Scope: result));
|
||||
}
|
||||
@@ -255,6 +259,41 @@ public class DirectoryGroupRefreshTests
|
||||
Assert.Equal(SessionValidationAction.Reject, result.Action);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ScadaBridge#144: the directory still answers with groups, but none of them maps to a role
|
||||
/// (the user was removed from every mapped AD group and keeps only e.g. "Domain Users"). Before
|
||||
/// #144 this kept a signed-in, role-less session; a new login would now be refused, so the
|
||||
/// session ends — on ordinary validation and on the fail-closed authoritative path.
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task FoundWithOnlyUnmappedGroups_EndsTheSession(bool failClosed)
|
||||
{
|
||||
var rig = Build();
|
||||
rig.Lookup.Answer = _ => FoundWith(UnmappedGroup);
|
||||
rig.Clock.Advance(TimeSpan.FromMinutes(16));
|
||||
|
||||
var result = await rig.Sut.ValidateAsync(Login(["SCADA-Admins"]), forceRefresh: failClosed, failClosed: failClosed);
|
||||
|
||||
Assert.Equal(SessionValidationAction.Reject, result.Action);
|
||||
Assert.Equal([UnmappedGroup], rig.Mapper.LastGroups);
|
||||
}
|
||||
|
||||
/// <summary>A directory answer that keeps one mapped group alongside unmapped ones keeps the session.</summary>
|
||||
[Fact]
|
||||
public async Task FoundWithOneMappedGroup_KeepsTheSession()
|
||||
{
|
||||
var rig = Build();
|
||||
rig.Lookup.Answer = _ => FoundWith(UnmappedGroup, "SCADA-Designers");
|
||||
rig.Clock.Advance(TimeSpan.FromMinutes(16));
|
||||
|
||||
var result = await rig.Sut.ValidateAsync(Login(["SCADA-Admins"]));
|
||||
|
||||
Assert.Equal(SessionValidationAction.Replace, result.Action);
|
||||
Assert.Equal([Roles.Designer], RolesOf(result.Principal!));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(LdapGroupLookupStatus.NotSupported)]
|
||||
[InlineData(LdapGroupLookupStatus.ProviderDisabled)]
|
||||
|
||||
Reference in New Issue
Block a user