fix(secrets-ui): components re-check their own policy; pin the real policy matrix (scadaproj#45)

Secrets-016: RevealButton (RevealPolicy), SecretEditor and ConfirmDeleteModal (ManagePolicy) now
evaluate their policy through IAuthorizationService at the top of the revealing/mutating handler
and fail closed (no auth state, no IAuthorizationService, or a failed policy), instead of relying
on the host to wrap them in AuthorizeView. New tests build the REAL AddSecretsAuthorization
policies and evaluate each role against each policy, so secrets-manager satisfying RevealPolicy
would now fail a test.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 15:20:03 -05:00
parent a84c64e26f
commit 649d4a0616
5 changed files with 218 additions and 0 deletions
@@ -81,6 +81,12 @@
private async Task ConfirmAsync()
{
_error = null;
if (!await SecretsComponentAuthorization.IsAuthorizedAsync(Services, AuthState, SecretsAuthorization.ManagePolicy))
{
_error = "You are not authorized to delete secrets.";
return;
}
_busy = true;
bool deleted = false;
try
@@ -56,6 +56,14 @@ else
// value costs only a retry.
private async Task RevealAsync()
{
if (!await SecretsComponentAuthorization.IsAuthorizedAsync(Services, AuthState, SecretsAuthorization.RevealPolicy))
{
_value = null;
_error = "You are not authorized to reveal secrets.";
_revealed = true;
return;
}
_busy = true;
// Captured now: if the component is disposed mid-reveal, reading _cts.Token later would throw
// ObjectDisposedException; a captured token just reports cancellation, which fails closed.
@@ -110,6 +110,12 @@
{
_error = null;
if (!await SecretsComponentAuthorization.IsAuthorizedAsync(Services, AuthState, SecretsAuthorization.ManagePolicy))
{
_error = "You are not authorized to manage secrets.";
return;
}
SecretName name;
try
{
@@ -0,0 +1,38 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
namespace ZB.MOM.WW.Secrets.Ui;
/// <summary>
/// The in-component policy check every revealing or mutating handler runs before it touches the
/// store (Secrets-016). The public components used to rely entirely on the host rendering them
/// inside an <c>AuthorizeView</c>; a host that composed <c>&lt;RevealButton/&gt;</c> into a
/// lower-privilege page handed plaintext to anyone who could see that page. Now each component
/// re-checks its own policy and fails closed — no authentication state, no
/// <see cref="IAuthorizationService"/>, or a failed policy all refuse.
/// </summary>
internal static class SecretsComponentAuthorization
{
public static async Task<bool> IsAuthorizedAsync(
IServiceProvider services, Task<AuthenticationState>? cascadedState, string policy)
{
IAuthorizationService? authorization = services.GetService<IAuthorizationService>();
if (authorization is null)
{
return false;
}
Task<AuthenticationState>? stateTask = cascadedState
?? services.GetService<AuthenticationStateProvider>()?.GetAuthenticationStateAsync();
if (stateTask is null)
{
return false;
}
AuthenticationState state = await stateTask.ConfigureAwait(false);
AuthorizationResult result = await authorization.AuthorizeAsync(state.User, resource: null, policy)
.ConfigureAwait(false);
return result.Succeeded;
}
}