73 lines
4.5 KiB
C#
73 lines
4.5 KiB
C#
// SPDX-License-Identifier: MPL-2.0
|
|
using System.Text.Json;
|
|
using System.Text.RegularExpressions;
|
|
using Nini.Config;
|
|
using OpenGenesisLINK.SysApi;
|
|
|
|
// Read-only staging validation against the operator-provided existing MariaDB.
|
|
// DO NOT print raw rows, UUIDs, names, URLs, or connection strings.
|
|
string robust = File.ReadAllText("/opt/robust/bin/Robust.HG.ini");
|
|
Match m = Regex.Match(robust, @"(?m)^\s*ConnectionString\s*=\s*""?([^""\r\n]+)");
|
|
if (!m.Success) throw new Exception("Database configuration unavailable");
|
|
var config = new IniConfigSource();
|
|
config.Configs.Add("DatabaseService").Set("ConnectionString", m.Groups[1].Value);
|
|
var data = new MySqlSysApiReadModel(config);
|
|
static void Check(bool yes, string message) { if (!yes) throw new Exception(message); }
|
|
foreach (string resource in new[] { "UserAccounts", "GridUser", "Presence", "Regions" })
|
|
{
|
|
using JsonDocument result = JsonDocument.Parse(JsonSerializer.Serialize(data.List(resource, 7, 0, null)));
|
|
var root = result.RootElement;
|
|
int total = root.GetProperty("pagination").GetProperty("total").GetInt32();
|
|
var items = root.GetProperty("items");
|
|
Check(total > 0 && items.GetArrayLength() > 0 && items.GetArrayLength() <= 7, "Live listing failure " + resource);
|
|
Check(root.GetProperty("pagination").GetProperty("limit").GetInt32() == 7, "Pagination wrong");
|
|
string json = root.GetRawText();
|
|
foreach (string secret in new[] { "Email", "ServiceURLs", "password", "regionSecret", "SessionID", "Token" })
|
|
Check(!json.Contains('"' + secret + '"', StringComparison.OrdinalIgnoreCase), "Sensitive field in " + resource);
|
|
foreach (var item in items.EnumerateArray())
|
|
{
|
|
Check(item.TryGetProperty("uuid", out _), "UUID missing in " + resource);
|
|
if (resource is "UserAccounts" or "GridUser" or "Presence")
|
|
Check(item.TryGetProperty("avatarName", out _), "Avatar-name field missing in " + resource);
|
|
if (resource == "Regions") Check(item.TryGetProperty("ownerAvatarName", out _), "Owner label missing");
|
|
}
|
|
Console.WriteLine($"PASS {resource}: live rows={total}, page-size={items.GetArrayLength()}, no secret fields");
|
|
}
|
|
using (JsonDocument list = JsonDocument.Parse(JsonSerializer.Serialize(data.List("UserAccounts", 1, 0, null))))
|
|
{
|
|
string uuid = list.RootElement.GetProperty("items")[0].GetProperty("uuid").GetString()!;
|
|
Check(data.Account(uuid) is not null, "Single account cannot resolve");
|
|
using JsonDocument resolved = JsonDocument.Parse(JsonSerializer.Serialize(data.Resolve(uuid)));
|
|
Check(resolved.RootElement.GetProperty("found").GetBoolean(), "UUID not resolved");
|
|
Check(resolved.RootElement.GetProperty("avatarName").GetString() is { Length: > 0 }, "Resolved name absent");
|
|
}
|
|
Console.WriteLine("PASS UUID→avatar display name, live MariaDB, protected column allowlist");
|
|
|
|
foreach (string dataset in new[] { "GridUser", "Presence", "Regions" })
|
|
{
|
|
using JsonDocument list = JsonDocument.Parse(JsonSerializer.Serialize(data.List(dataset, 100, 0, null)));
|
|
var items = list.RootElement.GetProperty("items");
|
|
int labeled = 0;
|
|
int unresolved = 0;
|
|
foreach (var item in items.EnumerateArray())
|
|
{
|
|
string field = dataset == "Regions" ? "ownerAvatarName" : "avatarName";
|
|
if (!item.TryGetProperty(field, out var name) || name.ValueKind == JsonValueKind.Null ||
|
|
string.IsNullOrWhiteSpace(name.GetString())) unresolved++;
|
|
else labeled++;
|
|
}
|
|
Console.WriteLine($"NAME_RESOLUTION_SAMPLE {dataset}: labeled={labeled}, unknown={unresolved}");
|
|
}
|
|
|
|
using (JsonDocument injection = JsonDocument.Parse(JsonSerializer.Serialize(data.List("UserAccounts", 10, 0, "' OR 1=1 --"))))
|
|
Check(injection.RootElement.GetProperty("pagination").GetProperty("total").GetInt32() == 0,
|
|
"SQL metacharacters affected account search");
|
|
using (JsonDocument escapedWildcard = JsonDocument.Parse(JsonSerializer.Serialize(data.List("UserAccounts", 10, 0, "%"))))
|
|
Check(escapedWildcard.RootElement.GetProperty("pagination").GetProperty("total").GetInt32() == 0,
|
|
"LIKE wildcard escaped incorrectly");
|
|
using (JsonDocument laterPage = JsonDocument.Parse(JsonSerializer.Serialize(data.List("UserAccounts", 10, 1000000, null))))
|
|
Check(laterPage.RootElement.GetProperty("items").GetArrayLength() == 0,
|
|
"Pagination offset not enforced");
|
|
Check(data.Account("00000000-0000-0000-0000-000000000000") is null, "Unexpected UUID.Zero account");
|
|
Console.WriteLine("PASS SQL injection/LIKE search, empty high-offset page and UUID miss");
|