fix(security): no role, no login — refuse zero-role and mapper-fault AdminUI sign-in and tokens (#913)
/auth/login signed users in with an empty role set (and did so when the role mapper threw), and AuthenticatedRead / the fallback policy admit any authenticated user — with a domain-root SearchBase any AD user would get AdminUI read access and a token. - Empty mapped role set: refused like bad credentials (JSON 401; form redirect to /login with a generic message), no cookie, Warning logged with the groups. - Mapper throws: refused, fail closed (JSON 503 like an LDAP outage; form redirect with a generic "temporarily unavailable"), exception logged. - /auth/token: refuses (401) a cookie session carrying no role claim. - Replaces Login_when_role_mapper_throws_signs_in_with_no_role_claims with refusal tests (JSON + form), adds zero-role refused (JSON + form), one-mapped-role signs in, zero-role cookie gets no token. JwtConfigurationGateTests' stub user now holds a mapped group. Claude-Session: https://claude.ai/code/session_01GT67AFKC45MBz1wPZnVbX8
This commit is contained in:
@@ -99,29 +99,31 @@ public static class AuthEndpoints
|
||||
{
|
||||
// R03:2.7 — a directory outage used to leave NOTHING in the server log from this path; the
|
||||
// operator had to infer every 503 from the directory side.
|
||||
http.RequestServices.GetService<ILoggerFactory>()?
|
||||
.CreateLogger("ZB.MOM.WW.OtOpcUa.Security.AuthEndpoints")
|
||||
.LogWarning(ex, "LDAP authentication failed for {User}; answering 503", username);
|
||||
Logger(http).LogWarning(ex, "LDAP authentication failed for {User}; answering 503", username);
|
||||
return Results.StatusCode(StatusCodes.Status503ServiceUnavailable);
|
||||
}
|
||||
|
||||
if (!result.Success)
|
||||
{
|
||||
if (!isForm) return Results.Unauthorized();
|
||||
|
||||
var qs = $"?error={Uri.EscapeDataString(result.Error ?? "Invalid credentials")}";
|
||||
if (!string.IsNullOrWhiteSpace(returnUrl))
|
||||
qs += $"&returnUrl={Uri.EscapeDataString(returnUrl)}";
|
||||
return Results.Redirect("/login" + qs);
|
||||
return RedirectToLogin(result.Error ?? "Invalid credentials", returnUrl);
|
||||
}
|
||||
|
||||
// Role resolution now lives behind the shared IGroupRoleMapper<string> seam
|
||||
// Role resolution lives behind the shared IGroupRoleMapper<string> seam
|
||||
// (OtOpcUaGroupRoleMapper): it applies the appsettings GroupToRole baseline AND merges
|
||||
// system-wide DB grants from the user's LDAP groups. result.Roles is empty on the real
|
||||
// LDAP path (the library returns groups, not roles); it is only pre-populated on the
|
||||
// DevStub success path (FleetAdmin) — union that pre-resolved set in so the dev grant
|
||||
// DevStub success path (Administrator) — union that pre-resolved set in so the dev grant
|
||||
// survives the move to the mapper.
|
||||
IReadOnlyList<string> roles = result.Roles;
|
||||
//
|
||||
// No role, no login (lmxopcua#913; family standard, scadaproj NAM cutover plan §3.2):
|
||||
// - a mapper fault REFUSES the sign-in (fail closed) — it used to sign the user in with
|
||||
// the baseline roles, which are empty on the real path;
|
||||
// - an empty mapped role set REFUSES the sign-in exactly like bad credentials. The
|
||||
// fallback policy and AdminUiPolicies.AuthenticatedRead only require an authenticated
|
||||
// user, so a role-less cookie would grant AdminUI read access to ANY directory user
|
||||
// once SearchBase is a whole AD domain.
|
||||
IReadOnlyList<string> roles;
|
||||
try
|
||||
{
|
||||
var mapping = await roleMapper.MapAsync(result.Groups, ct);
|
||||
@@ -129,17 +131,19 @@ public static class AuthEndpoints
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
// A DB hiccup (or any mapper fault) must never block sign-in — fall back to the
|
||||
// pre-resolved baseline roles (empty on the real path, FleetAdmin under DevStub).
|
||||
// This is intentionally FAIL-CLOSED on the real LDAP path: result.Roles is empty there
|
||||
// (the library returns groups, never roles — the mapper is the sole role source), so a
|
||||
// mapper fault signs the user in AUTHENTICATED but with ZERO role claims. They can prove
|
||||
// identity but are denied every role-gated action until the mapper recovers — strictly
|
||||
// safer than failing open with a stale/guessed role set. (See AuthEndpoints test
|
||||
// Login_when_role_mapper_throws_signs_in_with_no_role_claims.)
|
||||
http.RequestServices.GetService<ILoggerFactory>()?
|
||||
.CreateLogger("ZB.MOM.WW.OtOpcUa.Security.AuthEndpoints")
|
||||
.LogWarning(ex, "Role-map lookup failed for {User}; using pre-resolved baseline roles", username);
|
||||
Logger(http).LogWarning(ex,
|
||||
"Role-map lookup failed for {User}; refusing sign-in (fail closed)", username);
|
||||
if (!isForm) return Results.StatusCode(StatusCodes.Status503ServiceUnavailable);
|
||||
return RedirectToLogin(SignInUnavailableMessage, returnUrl);
|
||||
}
|
||||
|
||||
if (roles.Count == 0)
|
||||
{
|
||||
Logger(http).LogWarning(
|
||||
"Sign-in refused for {User}: authenticated, but groups [{Groups}] map to no role",
|
||||
username, string.Join(", ", result.Groups));
|
||||
if (!isForm) return Results.Unauthorized();
|
||||
return RedirectToLogin(NotAuthorizedMessage, returnUrl);
|
||||
}
|
||||
|
||||
var claims = new List<Claim>
|
||||
@@ -167,6 +171,28 @@ public static class AuthEndpoints
|
||||
return Results.Redirect(destination);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Login-page text for an authenticated user whose groups map to no role. Deliberately generic:
|
||||
/// it does not say which of "bad password" / "no role" applied.
|
||||
/// </summary>
|
||||
internal const string NotAuthorizedMessage = "Invalid username or password, or the account is not authorized.";
|
||||
|
||||
/// <summary>Login-page text when role resolution itself failed (the mapper threw).</summary>
|
||||
internal const string SignInUnavailableMessage = "Sign-in is temporarily unavailable. Try again later.";
|
||||
|
||||
private static ILogger Logger(HttpContext http) =>
|
||||
http.RequestServices.GetService<ILoggerFactory>()?
|
||||
.CreateLogger("ZB.MOM.WW.OtOpcUa.Security.AuthEndpoints")
|
||||
?? Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance;
|
||||
|
||||
private static IResult RedirectToLogin(string error, string returnUrl)
|
||||
{
|
||||
var qs = $"?error={Uri.EscapeDataString(error)}";
|
||||
if (!string.IsNullOrWhiteSpace(returnUrl))
|
||||
qs += $"&returnUrl={Uri.EscapeDataString(returnUrl)}";
|
||||
return Results.Redirect("/login" + qs);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns <see langword="true"/> when <paramref name="url"/> is a relative (local) URL
|
||||
/// safe to redirect to after login. Rejects absolute URLs (with a scheme, e.g.
|
||||
@@ -205,6 +231,15 @@ public static class AuthEndpoints
|
||||
var displayName = user.FindFirst(JwtTokenService.DisplayNameClaimType)?.Value ?? username;
|
||||
var roles = user.FindAll(ZbClaimTypes.Role).Select(c => c.Value).ToArray();
|
||||
|
||||
// No role, no token (lmxopcua#913). /auth/login no longer issues a role-less cookie, but a
|
||||
// cookie minted before that fix (or by any other sign-in path) must not be turned into a
|
||||
// bearer token that the fallback policy would then accept for AdminUI read access.
|
||||
if (roles.Length == 0)
|
||||
{
|
||||
Logger(http).LogWarning("Token refused for {User}: the session carries no role", username);
|
||||
return Results.Unauthorized();
|
||||
}
|
||||
|
||||
return Results.Ok(new TokenResponse(jwt.Issue(displayName, username, roles)));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user