diff --git a/ZB.MOM.WW.Secrets/Directory.Packages.props b/ZB.MOM.WW.Secrets/Directory.Packages.props
index 5ab76d6..17eb20a 100644
--- a/ZB.MOM.WW.Secrets/Directory.Packages.props
+++ b/ZB.MOM.WW.Secrets/Directory.Packages.props
@@ -72,7 +72,7 @@
-
+
diff --git a/ZB.MOM.WW.Secrets/src/ZB.MOM.WW.Secrets.Ui/Components/RevealButton.razor b/ZB.MOM.WW.Secrets/src/ZB.MOM.WW.Secrets.Ui/Components/RevealButton.razor
index 0a26908..cc8397a 100644
--- a/ZB.MOM.WW.Secrets/src/ZB.MOM.WW.Secrets.Ui/Components/RevealButton.razor
+++ b/ZB.MOM.WW.Secrets/src/ZB.MOM.WW.Secrets.Ui/Components/RevealButton.razor
@@ -1,46 +1,66 @@
@namespace ZB.MOM.WW.Secrets.Ui.Components
@implements IDisposable
+@using Microsoft.JSInterop
+@using Microsoft.Extensions.Logging
@inject ISecretStore Store
@inject ISecretCipher Cipher
@inject IAuditWriter Audit
@inject IServiceProvider Services
-@using Microsoft.Extensions.Logging
-@* Components/RevealButton.razor — a policy-gated, audited reveal. The plaintext is loaded and
- shown only AFTER the click, straight from the store+cipher (a human reveal shows the CURRENT
- value, distinct from any cached machine resolve). The value never enters an audit field. *@
+@inject IJSRuntime JS
+@* Values are read only on an authorized request and disclosed only after successful audit.
+ The stable control/status never contains plaintext; target changes cancel obsolete work. *@
-@if (!_revealed)
-{
-
- Reveal
-
-}
-else
-{
-
- @if (_value is not null)
- {
- @_value
- }
- else
- {
- @(_error ?? "Unavailable.")
- }
-
- Hide
-
-
-}
+@{ long renderedGeneration = _generation; bool renderedHide = _revealed; }
+
+
ActivateAsync(renderedGeneration, renderedHide)">
+ @(_revealed ? "Hide" : "Reveal")
+
+
@_status
+ @if (_revealed)
+ {
+
+ @if (_value is not null)
+ {
+
+ @_value
+
+
Visible until Hide or leaving this row or page.
+ }
+ else
+ {
+
@(_error ?? "Unavailable.")
+ }
+
+ }
+
@code {
private readonly CancellationTokenSource _cts = new();
+ private CancellationTokenSource? _operationCts;
+ private readonly string _id = $"secret-reveal-{Guid.NewGuid():N}";
+ private string ControlId => $"{_id}-control";
+ private string StatusId => $"{_id}-status";
+ private string ErrorId => $"{_id}-error";
+ private string ControlDescriptionIds => _revealed && !string.IsNullOrEmpty(_error) ? $"{StatusId} {ErrorId}" : StatusId;
+ private string PanelId => $"{_id}-panel";
+ private ElementReference _root;
+ private SecretName? _identity;
+ private long _generation;
+ private bool _disposed;
private bool _revealed;
private string? _value;
private string? _error;
+ private string _status = "";
private bool _busy;
+ private bool _focusControl;
/// The secret whose plaintext this button reveals.
[Parameter] public SecretName Name { get; set; }
@@ -48,110 +68,157 @@ else
/// The cascading authentication state, used as the fallback source of the audit actor.
[CascadingParameter] private Task? AuthState { get; set; }
- // Fail CLOSED on audit (Secrets-015): the plaintext is shown only after its secret.reveal
- // Success audit has been written. Reveal is the most privileged operation here and the audit is
- // the only record that a human saw the value; if the sink is down (or made to fail), nothing is
- // shown and the failure is logged (actor + name, never the value). Unlike add/rotate/delete
- // (scadaproj#34), nothing has happened yet that a failed audit would misreport — withholding the
- // value costs only a retry.
+ private bool IsCurrent(long generation) => !_disposed && generation == _generation;
+
+ ///
+ protected override void OnParametersSet()
+ {
+ if (_disposed || _identity == Name) return;
+ _identity = Name;
+ ++_generation;
+ _operationCts?.Cancel();
+ _operationCts?.Dispose();
+ _operationCts = null;
+ _value = null;
+ _error = null;
+ _status = "";
+ _busy = false;
+ _revealed = false;
+ _focusControl = false;
+ }
+
+ ///
+ protected override async Task OnAfterRenderAsync(bool firstRender)
+ {
+ if (!_focusControl || _busy || _disposed) return;
+ _focusControl = false;
+ long generation = _generation;
+ try
+ {
+ // Only a DOM control reference crosses interop, never a value or status payload.
+ await using IJSObjectReference module = await JS.InvokeAsync("import", "./_content/ZB.MOM.WW.Secrets.Ui/js/secrets-reveal.js");
+ if (IsCurrent(generation)) await module.InvokeVoidAsync("focusIfUnclaimed", _root);
+ }
+ catch (JSDisconnectedException) { } // The browser no longer has a focus destination.
+ }
+
+ private Task ActivateAsync(long generation, bool hide)
+ {
+ // A delayed click from a previous target/mode cannot become a reveal of a new target.
+ if (!IsCurrent(generation) || _busy) return Task.CompletedTask;
+ if (hide) { Hide(); return Task.CompletedTask; }
+ return RevealAsync();
+ }
+
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;
- }
-
+ if (_disposed || _busy) return;
+ long generation = _generation;
+ SecretName name = Name;
+ Task? authState = AuthState;
+ ILogger? logger = Services.GetService>();
+ var readCts = CancellationTokenSource.CreateLinkedTokenSource(_cts.Token);
+ _operationCts = readCts;
+ CancellationToken ct = readCts.Token;
_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.
- CancellationToken ct = _cts.Token;
+ _value = null;
+ _error = null;
+ _status = "Revealing value…";
string? plaintext = null;
- AuditOutcome outcome;
- string? error;
try
{
- StoredSecret? row = await Store.GetAsync(Name, ct);
- if (row is null)
+ if (!await SecretsComponentAuthorization.IsAuthorizedAsync(Services, authState, SecretsAuthorization.RevealPolicy))
{
- error = "Secret not found.";
- outcome = AuditOutcome.Failure;
+ if (IsCurrent(generation)) SetOutcome(null, "You are not authorized to reveal secrets.");
+ return;
}
- else
+ if (!IsCurrent(generation) || ct.IsCancellationRequested) return;
+ // Resolve optional host actor services before waiting on the store, avoiding late
+ // resolution through a circuit scope disposed while a non-cooperative read finishes.
+ string actor = await SecretActorResolver.ResolveAsync(Services, authState);
+ if (!IsCurrent(generation) || ct.IsCancellationRequested) return;
+ AuditOutcome outcome;
+ string? error;
+ try
{
- plaintext = Cipher.Decrypt(row);
- error = null;
- outcome = AuditOutcome.Success;
+ StoredSecret? row = await Store.GetAsync(name, ct);
+ if (!IsCurrent(generation) || ct.IsCancellationRequested) return;
+ if (row is null) { error = "Secret not found."; outcome = AuditOutcome.Failure; }
+ else { plaintext = Cipher.Decrypt(row); error = null; outcome = AuditOutcome.Success; }
}
- }
- catch (SecretDecryptionException)
- {
- error = "Unable to decrypt secret.";
- outcome = AuditOutcome.Failure;
- }
- catch (Exception)
- {
- // Any other unexpected store/cipher fault (e.g. master key unavailable, timeout) must
- // still be audited as a Failure and surfaced to the user, not left to tear down the
- // circuit. The value is never shown.
- error = "Unable to reveal secret.";
- outcome = AuditOutcome.Failure;
- }
+ catch (OperationCanceledException) when (ct.IsCancellationRequested) { return; }
+ catch (SecretDecryptionException) { error = "Unable to decrypt secret."; outcome = AuditOutcome.Failure; }
+ catch (Exception) { error = "Unable to reveal secret."; outcome = AuditOutcome.Failure; }
- string actor = await SecretActorResolver.ResolveAsync(Services, AuthState);
- bool audited;
- try
- {
- await Audit.WriteAsync(
- new AuditEvent
+ bool audited;
+ try
+ {
+ await Audit.WriteAsync(new AuditEvent
{
- EventId = Guid.NewGuid(),
- OccurredAtUtc = DateTimeOffset.UtcNow,
- Actor = actor,
- Action = "secret.reveal",
- Outcome = outcome,
- Category = "Secrets",
- Target = Name.Value,
+ EventId = Guid.NewGuid(), OccurredAtUtc = DateTimeOffset.UtcNow,
+ Actor = actor, Action = "secret.reveal", Outcome = outcome,
+ Category = "Secrets", Target = name.Value,
DetailsJson = outcome == AuditOutcome.Failure ? "{\"reason\":\"error\"}" : null,
- },
- ct);
- audited = true;
+ }, ct);
+ audited = true;
+ }
+ catch (Exception ex)
+ {
+ audited = false;
+ logger?.LogWarning(ex,
+ "The secret.reveal audit of {SecretName} by {Actor} could not be written; {Disposition}.",
+ name.Value, actor, outcome == AuditOutcome.Success ? "the value was NOT shown" : "the reveal had already failed");
+ }
+ if (!IsCurrent(generation) || ct.IsCancellationRequested) return;
+ if (outcome == AuditOutcome.Success && !audited)
+ {
+ plaintext = null;
+ error = "Reveal is unavailable: the audit record could not be written.";
+ }
+ // Fail CLOSED: only now may plaintext enter rendered component state.
+ SetOutcome(plaintext, error);
}
- catch (Exception ex)
- {
- audited = false;
- Services.GetService>()?.LogWarning(
- ex,
- "The secret.reveal audit of {SecretName} by {Actor} could not be written; {Disposition}.",
- Name.Value,
- actor,
- outcome == AuditOutcome.Success ? "the value was NOT shown" : "the reveal had already failed");
- }
-
- if (outcome == AuditOutcome.Success && !audited)
+ finally
{
plaintext = null;
- error = "Reveal is unavailable: the audit record could not be written.";
+ if (IsCurrent(generation)) { _busy = false; _focusControl = true; }
+ if (ReferenceEquals(_operationCts, readCts)) _operationCts = null;
+ readCts.Dispose();
}
+ }
- _value = plaintext;
+ private void SetOutcome(string? value, string? error)
+ {
+ _value = value;
_error = error;
- _busy = false;
_revealed = true;
+ _status = value is not null ? "Value revealed." : "Reveal failed.";
}
private void Hide()
{
+ bool hadValue = _value is not null;
+ ++_generation;
+ _operationCts?.Cancel();
+ _operationCts?.Dispose();
+ _operationCts = null;
_revealed = false;
_value = null;
_error = null;
+ _status = hadValue ? "Value hidden." : "Reveal dismissed.";
+ _focusControl = true;
}
///
public void Dispose()
{
+ if (_disposed) return;
+ _disposed = true;
+ ++_generation;
+ _operationCts?.Cancel();
+ _operationCts?.Dispose();
+ _operationCts = null;
+ _value = null;
_cts.Cancel();
_cts.Dispose();
}
diff --git a/ZB.MOM.WW.Secrets/src/ZB.MOM.WW.Secrets.Ui/Components/SecretsList.razor b/ZB.MOM.WW.Secrets/src/ZB.MOM.WW.Secrets.Ui/Components/SecretsList.razor
index 2c7532a..419e03b 100644
--- a/ZB.MOM.WW.Secrets/src/ZB.MOM.WW.Secrets.Ui/Components/SecretsList.razor
+++ b/ZB.MOM.WW.Secrets/src/ZB.MOM.WW.Secrets.Ui/Components/SecretsList.razor
@@ -5,6 +5,23 @@
ciphertext. Add/rotate use the in-page SecretEditor; delete uses the in-page ConfirmDeleteModal;
reveal is the policy-gated, audited RevealButton. The list reloads after any mutation. *@
+
+