Adds a CSRF-protected admin-only alert command with severity presets and 5-second rate limit; signs a NexBus event and fans out via native IDialogModule.SendGeneralAlert across all hosted regions, including EventSim when configured. Default remains opt-in. NexVerse CI, .NET10 full build, SYS.API HTTP/HMAC tests and security contracts passed. Cross-platform builds/ROBUST boot passed; release-artifact upload remains blocked by GitHub storage quota. No production deployment.
102 lines
4.1 KiB
C#
102 lines
4.1 KiB
C#
// SPDX-License-Identifier: MPL-2.0
|
|
// Explicit "alert <message>" command only; never execute arbitrary console text.
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using NexVerse.Core.Messaging;
|
|
using OpenMetaverse;
|
|
|
|
namespace OpenGenesisLINK.SysApi;
|
|
|
|
public interface ISysApiAlertPublisher : IDisposable
|
|
{
|
|
Task<Guid> PublishAsync(string message, UUID administrator, CancellationToken cancellationToken);
|
|
}
|
|
|
|
public static class SysApiAlertCommand
|
|
{
|
|
public static bool TryParse(string? command, out string message)
|
|
{
|
|
message = "";
|
|
if (command is null || command.Length > 512 ||
|
|
!command.StartsWith("alert ", StringComparison.OrdinalIgnoreCase))
|
|
return false;
|
|
|
|
string value = command[6..].Trim();
|
|
if (value.Length is < 1 or > 500)
|
|
return false;
|
|
foreach (char ch in value)
|
|
{
|
|
// Viewer alerts are single-line messages; avoid injected console commands.
|
|
if (char.IsControl(ch) || ch is '\u2028' or '\u2029')
|
|
return false;
|
|
}
|
|
message = value;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Submits one HMAC-authenticated NexBus event to the *local* ROBUST listener.
|
|
/// NexBus then forwards the same event ID to each configured simulator peer.
|
|
/// HTTP 202 means queued by ROBUST, not verified delivered to every viewer.
|
|
/// </summary>
|
|
public sealed class NexBusSysApiAlertPublisher : ISysApiAlertPublisher
|
|
{
|
|
private readonly HttpClient _client;
|
|
private readonly Uri _localEndpoint;
|
|
private readonly string _sharedKey;
|
|
|
|
public NexBusSysApiAlertPublisher(int localRobustPort, string inboundPath, string sharedKey)
|
|
{
|
|
if (localRobustPort is < 1 or > 65535)
|
|
throw new ArgumentOutOfRangeException(nameof(localRobustPort));
|
|
if (inboundPath != "/internal/nexbus/v1/events")
|
|
throw new ArgumentException("Untrusted NexBus inbound path", nameof(inboundPath));
|
|
NexBusProtocol.ValidateSharedKey(sharedKey);
|
|
_sharedKey = sharedKey;
|
|
_localEndpoint = new Uri("http://127.0.0.1:" + localRobustPort + inboundPath);
|
|
_client = new HttpClient(new HttpClientHandler
|
|
{
|
|
AllowAutoRedirect = false,
|
|
UseProxy = false
|
|
}) { Timeout = TimeSpan.FromSeconds(5) };
|
|
}
|
|
|
|
public async Task<Guid> PublishAsync(
|
|
string message, UUID administrator, CancellationToken cancellationToken)
|
|
{
|
|
if (administrator == UUID.Zero || message.Length is < 1 or > 500)
|
|
throw new ArgumentException("Invalid alert or administrator");
|
|
var outgoing = new NexEvent("admin.alert.requested", "opengenesislink.sysapi",
|
|
new Dictionary<string, string>
|
|
{
|
|
["command"] = "alert",
|
|
["message"] = message,
|
|
["administrator"] = administrator.ToString()
|
|
});
|
|
byte[] payload = NexBusProtocol.Serialize(outgoing);
|
|
using var request = new HttpRequestMessage(HttpMethod.Post, _localEndpoint);
|
|
request.Headers.TryAddWithoutValidation("X-NexBus-Node", "SysApi");
|
|
request.Headers.TryAddWithoutValidation("X-NexBus-Signature",
|
|
"sha256=" + NexBusProtocol.Sign(payload, _sharedKey));
|
|
request.Content = new ByteArrayContent(payload);
|
|
request.Content.Headers.ContentType =
|
|
new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
|
|
using HttpResponseMessage response =
|
|
await _client.SendAsync(request, cancellationToken);
|
|
if (response.StatusCode != HttpStatusCode.Accepted)
|
|
throw new InvalidOperationException("NexBus did not accept the alert");
|
|
|
|
using JsonDocument receipt =
|
|
JsonDocument.Parse(await response.Content.ReadAsStringAsync(cancellationToken));
|
|
if (!receipt.RootElement.TryGetProperty("accepted", out JsonElement accepted) ||
|
|
accepted.ValueKind != JsonValueKind.True)
|
|
throw new InvalidOperationException("NexBus rejected the alert");
|
|
return outgoing.EventId;
|
|
}
|
|
|
|
public void Dispose() => _client.Dispose();
|
|
}
|