Merge #973: fleet diagnostics only dials known cluster members (SSRF closed)
This commit is contained in:
@@ -42,6 +42,18 @@ public sealed class FleetDiagnosticsClient : IFleetDiagnosticsClient
|
||||
/// <inheritdoc />
|
||||
public async Task<NodeDiagnosticsSnapshot> GetDiagnosticsAsync(NodeId nodeId, CancellationToken ct)
|
||||
{
|
||||
// Defence in depth (#973 / A-1): the page resolves the target against the closed member set, but this
|
||||
// client is the one place that builds the address, so it refuses anything that is not a plain host:port
|
||||
// (a '/' or '@' would re-target the actor path) BEFORE an ActorSelection — and so an outbound
|
||||
// association — can exist.
|
||||
if (!FleetDiagnosticsTargets.IsWellFormed(nodeId.Value))
|
||||
{
|
||||
_log.LogWarning(
|
||||
"Fleet diagnostics refused node {NodeId}: not a plain host:port; no connection attempted",
|
||||
nodeId.Value);
|
||||
return Empty(nodeId);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Inside the try: a malformed node value makes ActorSelection throw, and that must degrade to
|
||||
@@ -57,11 +69,10 @@ public sealed class FleetDiagnosticsClient : IFleetDiagnosticsClient
|
||||
_log.LogWarning(ex,
|
||||
"Fleet diagnostics for node {NodeId} unavailable (no answer within {Timeout}s); returning an empty snapshot",
|
||||
nodeId.Value, AskTimeout.TotalSeconds);
|
||||
return new NodeDiagnosticsSnapshot(
|
||||
nodeId,
|
||||
CurrentRevision: null,
|
||||
Drivers: Array.Empty<DriverInstanceDiagnostics>(),
|
||||
AsOfUtc: DateTime.UtcNow);
|
||||
return Empty(nodeId);
|
||||
}
|
||||
}
|
||||
|
||||
private static NodeDiagnosticsSnapshot Empty(NodeId nodeId) =>
|
||||
new(nodeId, CurrentRevision: null, Drivers: Array.Empty<DriverInstanceDiagnostics>(), AsOfUtc: DateTime.UtcNow);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Clients;
|
||||
|
||||
/// <summary>
|
||||
/// The closed set of nodes <c>/fleet/diagnostics</c> may dial (#973 / review A-1).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The page used to pass <c>?node=</c> (or the typed box) straight into an Akka
|
||||
/// <c>ActorSelection</c>; <c>Ask</c> on a remote selection makes Akka.Remote open an outbound
|
||||
/// association to whatever <c>host:port</c> is named, so any signed-in user could make the admin node
|
||||
/// dial an arbitrary endpoint (SSRF into the control plane). The target is now resolved server-side
|
||||
/// against a closed set — Up driver-role cluster members ∪ <c>Host:AkkaPort</c> of every enabled
|
||||
/// <c>ClusterNode</c> row — and the dialled string is the <i>canonical member</i>, never the caller's
|
||||
/// text. <see cref="IsWellFormed"/> is the second, independent layer that
|
||||
/// <see cref="FleetDiagnosticsClient"/> applies to whatever it is handed.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public static class FleetDiagnosticsTargets
|
||||
{
|
||||
// host:port where host is a DNS name / IPv4 literal. No '/', '@', whitespace, '[' or '?' — anything that
|
||||
// could re-target the actor path or smuggle a second address into the akka.tcp URI. (IPv6 literals are
|
||||
// not representable in the NodeId host:port form the fleet uses.)
|
||||
private static readonly Regex WellFormed = new(
|
||||
@"^[A-Za-z0-9._-]{1,253}:\d{1,5}$",
|
||||
RegexOptions.Compiled | RegexOptions.CultureInvariant,
|
||||
TimeSpan.FromMilliseconds(100));
|
||||
|
||||
/// <summary>True when <paramref name="value"/> is a plain <c>host:port</c> with a port in 1..65535.</summary>
|
||||
/// <param name="value">The candidate node id.</param>
|
||||
/// <returns><see langword="true"/> when it is safe to interpolate into an <c>akka.tcp://</c> address.</returns>
|
||||
public static bool IsWellFormed(string? value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value) || value.Length > 260) return false;
|
||||
if (!WellFormed.IsMatch(value)) return false;
|
||||
var port = int.Parse(value[(value.LastIndexOf(':') + 1)..], System.Globalization.CultureInfo.InvariantCulture);
|
||||
return port is >= 1 and <= 65535;
|
||||
}
|
||||
|
||||
/// <summary>The <c>Host:AkkaPort</c> dial target of every enabled <c>ClusterNode</c> row.</summary>
|
||||
/// <param name="db">The config DB context.</param>
|
||||
/// <param name="ct">Cancellation token.</param>
|
||||
/// <returns>Dial targets (only the well-formed ones).</returns>
|
||||
public static async Task<IReadOnlyList<string>> LoadEnabledNodeTargetsAsync(
|
||||
OtOpcUaConfigDbContext db, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(db);
|
||||
var rows = await db.ClusterNodes.AsNoTracking()
|
||||
.Where(n => n.Enabled)
|
||||
.Select(n => new { n.Host, n.AkkaPort })
|
||||
.ToListAsync(ct);
|
||||
return rows.Select(r => $"{r.Host}:{r.AkkaPort}").Where(IsWellFormed).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves a requested node against the closed set.
|
||||
/// </summary>
|
||||
/// <param name="requested">The raw <c>?node=</c> / typed value.</param>
|
||||
/// <param name="upDriverMembers">Up driver-role cluster members (<c>host:port</c>).</param>
|
||||
/// <param name="enabledNodeTargets">Enabled <c>ClusterNode</c> <c>Host:AkkaPort</c> targets.</param>
|
||||
/// <returns>
|
||||
/// The canonical allowed value (as it appears in the set, not the caller's casing), or
|
||||
/// <see langword="null"/> when <paramref name="requested"/> is not a member — the caller must then not dial.
|
||||
/// </returns>
|
||||
public static string? Resolve(
|
||||
string? requested, IEnumerable<string> upDriverMembers, IEnumerable<string> enabledNodeTargets)
|
||||
{
|
||||
var wanted = requested?.Trim();
|
||||
if (!IsWellFormed(wanted)) return null;
|
||||
foreach (var candidate in upDriverMembers.Concat(enabledNodeTargets))
|
||||
{
|
||||
if (IsWellFormed(candidate) && string.Equals(candidate, wanted, StringComparison.OrdinalIgnoreCase))
|
||||
return candidate;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,10 @@
|
||||
its live child set + each child's last-published health; the "running from cache" flag is the one
|
||||
fact /hosts cannot show (a node booted from its artifact cache looks healthy but is frozen).
|
||||
Reach it as /fleet/diagnostics?node=<host:port> (the NodeId shown on /fleet and /hosts), or pick
|
||||
a driver-role member from the selector. *@
|
||||
a driver-role member from the selector. The target is resolved against a closed set (Up driver members
|
||||
plus enabled ClusterNode Host:AkkaPort) — anything else is refused without dialling (#973 / A-1).
|
||||
Policy stays AuthenticatedRead: /fleet and /hosts, which link here and show the same data, are the same
|
||||
tier, and with the closed set a Viewer can no longer steer the admin node's outbound connections. *@
|
||||
@attribute [Authorize(Policy = AdminUiPolicies.AuthenticatedRead)]
|
||||
@rendermode RenderMode.InteractiveServer
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@@ -37,7 +40,7 @@
|
||||
{
|
||||
<option value="@member">@member</option>
|
||||
}
|
||||
@if (!string.IsNullOrEmpty(_selectedNode) && !_members.Contains(_selectedNode))
|
||||
@if (!string.IsNullOrEmpty(_selectedNode) && !_members.Contains(_selectedNode, StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
<option value="@_selectedNode">@_selectedNode (not an Up member)</option>
|
||||
}
|
||||
@@ -60,7 +63,14 @@
|
||||
</span>
|
||||
</div>
|
||||
|
||||
@if (string.IsNullOrEmpty(_selectedNode))
|
||||
@if (!string.IsNullOrEmpty(_rejectedNode))
|
||||
{
|
||||
<section class="panel notice rise" role="alert" style="animation-delay:.02s">
|
||||
Unknown node <span class="mono">@_rejectedNode</span> — it is not an Up driver member or an enabled
|
||||
cluster node, so no connection was attempted. Pick a node from the list.
|
||||
</section>
|
||||
}
|
||||
else if (string.IsNullOrEmpty(_selectedNode))
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.02s">
|
||||
Pick a driver-role node to fetch its driver diagnostics. The node answers from its own
|
||||
@@ -177,6 +187,9 @@ else
|
||||
|
||||
private IReadOnlyList<string> _members = Array.Empty<string>();
|
||||
private string _selectedNode = "";
|
||||
// A ?node= / typed value that is not in the closed set: shown to the user, never dialled.
|
||||
private string _rejectedNode = "";
|
||||
private IReadOnlyList<string> _enabledNodeTargets = Array.Empty<string>();
|
||||
private string _typedNode = "";
|
||||
private NodeDiagnosticsSnapshot? _snapshot;
|
||||
private IReadOnlyDictionary<string, string> _displayNames = new Dictionary<string, string>();
|
||||
@@ -200,14 +213,39 @@ else
|
||||
.OrderBy(s => s, StringComparer.OrdinalIgnoreCase)
|
||||
.ToList();
|
||||
var requested = Node?.Trim() ?? "";
|
||||
if (!string.Equals(requested, _selectedNode, StringComparison.Ordinal))
|
||||
var key = _rejectedNode.Length > 0 ? _rejectedNode : _selectedNode;
|
||||
if (!string.Equals(requested, key, StringComparison.Ordinal))
|
||||
{
|
||||
_selectedNode = requested;
|
||||
_snapshot = null;
|
||||
_selectedNode = "";
|
||||
_rejectedNode = "";
|
||||
if (requested.Length > 0)
|
||||
{
|
||||
_enabledNodeTargets = await LoadEnabledNodeTargetsAsync();
|
||||
// Resolve to the CANONICAL member string: the dialled value is never the caller's text.
|
||||
var resolved = FleetDiagnosticsTargets.Resolve(requested, _members, _enabledNodeTargets);
|
||||
if (resolved is null) _rejectedNode = requested;
|
||||
else _selectedNode = resolved;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(_selectedNode)) await LoadAsync();
|
||||
}
|
||||
}
|
||||
|
||||
// ConfigDb hiccup: fall back to the Up members alone (a stricter set, never a looser one).
|
||||
private async Task<IReadOnlyList<string>> LoadEnabledNodeTargetsAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var db = await DbFactory.CreateDbContextAsync(_cts.Token);
|
||||
return await FleetDiagnosticsTargets.LoadEnabledNodeTargetsAsync(db, _cts.Token);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException and not ObjectDisposedException)
|
||||
{
|
||||
return Array.Empty<string>();
|
||||
}
|
||||
}
|
||||
|
||||
private Task OnNodeSelected(ChangeEventArgs e)
|
||||
{
|
||||
var value = e.Value?.ToString() ?? "";
|
||||
@@ -275,7 +313,11 @@ else
|
||||
}
|
||||
|
||||
var snapshots = new List<NodeDiagnosticsSnapshot> { selected };
|
||||
var peers = nodeIds.Where(n => !string.Equals(n, selected.NodeId.Value, StringComparison.OrdinalIgnoreCase)).ToList();
|
||||
// Peers come from ClusterNode.NodeId rows; only those that are in the closed set are dialled.
|
||||
var peers = nodeIds
|
||||
.Where(n => !string.Equals(n, selected.NodeId.Value, StringComparison.OrdinalIgnoreCase))
|
||||
.Where(n => FleetDiagnosticsTargets.Resolve(n, _members, _enabledNodeTargets) is not null)
|
||||
.ToList();
|
||||
var peerSnapshots = await Task.WhenAll(peers.Select(FetchPeerAsync));
|
||||
snapshots.AddRange(peerSnapshots.OfType<NodeDiagnosticsSnapshot>());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user