Web panel: an in-game WebView2 page for plugins (API v94) #12
5 changed files with 468 additions and 0 deletions
Web panel: URL, window and profile policies with offline tests
Pure C# policies (allowlist/redaction, visibility/layout, profile folder) plus tools/WebPanelTests (107 assertions). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
commit
0b0d2cb072
42
src/RynthCore.Engine/WebPanel/WebPanelProfile.cs
Normal file
42
src/RynthCore.Engine/WebPanel/WebPanelProfile.cs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace RynthCore.Engine.WebPanel;
|
||||
|
||||
/// <summary>Pure per-world, per-profile-key WebView2 user data folder naming. No file access.</summary>
|
||||
internal static class WebPanelProfile
|
||||
{
|
||||
/// <summary>
|
||||
/// <localAppData>\RynthCore\WebPanel\<world>\<hex>. World: [A-Za-z0-9 _-] kept, others
|
||||
/// '_', trimmed, max 64, empty -> "_default". Hex: first 16 bytes of
|
||||
/// SHA-256(UTF-8(key.Trim().ToLowerInvariant())) as lowercase hex; empty key -> "default".
|
||||
/// </summary>
|
||||
public static string Folder(string localAppData, string? worldName, string? profileKey)
|
||||
{
|
||||
return Path.Combine(localAppData, "RynthCore", "WebPanel", SanitizeWorld(worldName), KeyHex(profileKey));
|
||||
}
|
||||
|
||||
private static string SanitizeWorld(string? world)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(world)) return "_default";
|
||||
var sb = new StringBuilder();
|
||||
foreach (char c in world.Trim())
|
||||
{
|
||||
bool keep = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')
|
||||
|| c == ' ' || c == '_' || c == '-';
|
||||
sb.Append(keep ? c : '_');
|
||||
}
|
||||
string s = sb.ToString();
|
||||
if (s.Length > 64) s = s.Substring(0, 64);
|
||||
s = s.Trim(); // a trailing space is not a valid Windows folder name
|
||||
return s.Length == 0 ? "_default" : s;
|
||||
}
|
||||
|
||||
private static string KeyHex(string? key)
|
||||
{
|
||||
string k = (key ?? "").Trim().ToLowerInvariant();
|
||||
if (k.Length == 0) return "default";
|
||||
byte[] hash = SHA256.HashData(Encoding.UTF8.GetBytes(k));
|
||||
return Convert.ToHexString(hash, 0, 16).ToLowerInvariant();
|
||||
}
|
||||
}
|
||||
119
src/RynthCore.Engine/WebPanel/WebPanelUrlPolicy.cs
Normal file
119
src/RynthCore.Engine/WebPanel/WebPanelUrlPolicy.cs
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
using System.Text;
|
||||
|
||||
namespace RynthCore.Engine.WebPanel;
|
||||
|
||||
/// <summary>
|
||||
/// Pure URL rules for the web panel: the navigation allowlist, the "safe to hand to the system
|
||||
/// browser" test, and log redaction. No Win32, no WebView2.
|
||||
/// </summary>
|
||||
internal static class WebPanelUrlPolicy
|
||||
{
|
||||
public const int MaxEntries = 16, MaxEntryLength = 253, MaxUrlBytes = 4096;
|
||||
|
||||
/// <summary>
|
||||
/// Parses a '\n'-separated allowlist (blank lines ignored, '\r' tolerated). An entry is
|
||||
/// "host" (exact) or "*.suffix" (any subdomain depth, not the suffix itself; the suffix must
|
||||
/// contain a dot). No scheme, path, port or other '*'. False on 0 or more than 16 entries or
|
||||
/// any bad entry.
|
||||
/// </summary>
|
||||
public static bool TryParseAllowlist(string? text, out string[] entries)
|
||||
{
|
||||
entries = Array.Empty<string>();
|
||||
if (string.IsNullOrWhiteSpace(text)) return false;
|
||||
|
||||
var list = new List<string>();
|
||||
foreach (string raw in text.Split('\n'))
|
||||
{
|
||||
string e = raw.Trim().ToLowerInvariant();
|
||||
if (e.Length == 0) continue;
|
||||
if (list.Count >= MaxEntries) return false;
|
||||
if (!IsValidEntry(e)) return false;
|
||||
list.Add(e);
|
||||
}
|
||||
if (list.Count == 0) return false;
|
||||
entries = list.ToArray();
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool IsValidEntry(string e)
|
||||
{
|
||||
if (e.Length > MaxEntryLength) return false;
|
||||
string host = e;
|
||||
if (e.StartsWith("*.", StringComparison.Ordinal))
|
||||
{
|
||||
host = e.Substring(2);
|
||||
if (host.IndexOf('.') < 0) return false; // "*.com" would allow a whole TLD
|
||||
}
|
||||
return IsValidHostName(host);
|
||||
}
|
||||
|
||||
private static bool IsValidHostName(string host)
|
||||
{
|
||||
if (host.Length == 0) return false;
|
||||
foreach (string label in host.Split('.'))
|
||||
{
|
||||
if (label.Length == 0 || label.Length > 63) return false;
|
||||
if (label[0] == '-' || label[^1] == '-') return false;
|
||||
foreach (char c in label)
|
||||
{
|
||||
bool ok = (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-';
|
||||
if (!ok) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// https only, no userinfo, default port; host compared via Uri.IdnHost, case-insensitive,
|
||||
/// one trailing dot stripped. "about:blank" is allowed.
|
||||
/// </summary>
|
||||
public static bool IsAllowed(string? url, IReadOnlyList<string> entries)
|
||||
{
|
||||
if (string.IsNullOrEmpty(url)) return false;
|
||||
if (Encoding.UTF8.GetByteCount(url) > MaxUrlBytes) return false;
|
||||
if (string.Equals(url, "about:blank", StringComparison.OrdinalIgnoreCase)) return true;
|
||||
if (!Uri.TryCreate(url, UriKind.Absolute, out Uri? uri)) return false;
|
||||
if (uri.Scheme != Uri.UriSchemeHttps) return false;
|
||||
if (uri.UserInfo.Length != 0) return false;
|
||||
if (!uri.IsDefaultPort) return false;
|
||||
|
||||
string host = uri.IdnHost.ToLowerInvariant();
|
||||
if (host.EndsWith('.')) host = host.Substring(0, host.Length - 1);
|
||||
if (host.Length == 0) return false;
|
||||
|
||||
foreach (string entry in entries)
|
||||
{
|
||||
if (entry.StartsWith("*.", StringComparison.Ordinal))
|
||||
{
|
||||
string suffix = entry.Substring(1); // ".example.com"
|
||||
if (host.Length > suffix.Length && host.EndsWith(suffix, StringComparison.Ordinal)) return true;
|
||||
}
|
||||
else if (host == entry)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>Absolute http/https without userinfo: the only URLs that may reach ShellExecute.</summary>
|
||||
public static bool IsBrowserSafe(string? url)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(url)) return false;
|
||||
if (!Uri.TryCreate(url, UriKind.Absolute, out Uri? uri)) return false;
|
||||
if (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps) return false;
|
||||
if (uri.UserInfo.Length != 0) return false;
|
||||
return uri.Host.Length != 0;
|
||||
}
|
||||
|
||||
/// <summary>"scheme://host[:port]/path" with no userinfo, query or fragment; "<invalid url>" if unparseable.</summary>
|
||||
public static string Redact(string? url)
|
||||
{
|
||||
const string Invalid = "<invalid url>";
|
||||
if (string.IsNullOrEmpty(url)) return Invalid;
|
||||
if (!Uri.TryCreate(url, UriKind.Absolute, out Uri? uri)) return Invalid;
|
||||
if (uri.Host.Length == 0) return Invalid;
|
||||
string port = uri.IsDefaultPort ? "" : ":" + uri.Port;
|
||||
return uri.Scheme + "://" + uri.Host + port + uri.AbsolutePath;
|
||||
}
|
||||
}
|
||||
67
src/RynthCore.Engine/WebPanel/WebPanelWindowPolicy.cs
Normal file
67
src/RynthCore.Engine/WebPanel/WebPanelWindowPolicy.cs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
namespace RynthCore.Engine.WebPanel;
|
||||
|
||||
internal readonly record struct PanelRect(int Left, int Top, int Right, int Bottom)
|
||||
{
|
||||
public int Width => Right - Left;
|
||||
public int Height => Bottom - Top;
|
||||
}
|
||||
|
||||
/// <summary>Panel placement as fractions (0..1) of the game client rect.</summary>
|
||||
internal readonly record struct PanelFractions(double X, double Y, double W, double H);
|
||||
|
||||
/// <summary>Pure visibility and layout rules for the web panel window. No Win32.</summary>
|
||||
internal static class WebPanelWindowPolicy
|
||||
{
|
||||
public const int DefaultPercent = 88, MinPercent = 30, MaxPercent = 100, MinWidthPx = 480, MinHeightPx = 320;
|
||||
|
||||
/// <summary>
|
||||
/// false when closed, the game is gone, iconic or invisible; null (keep current) when the
|
||||
/// foreground window is null mid alt-tab, so the panel never flickers; otherwise true only
|
||||
/// when the foreground window belongs to the game process.
|
||||
/// </summary>
|
||||
public static bool? Visible(bool open, bool gameAlive, bool gameIconic, bool gameVisible,
|
||||
bool foregroundNull, uint foregroundPid, uint ourPid)
|
||||
{
|
||||
if (!open || !gameAlive || gameIconic || !gameVisible) return false;
|
||||
if (foregroundNull) return null;
|
||||
return foregroundPid == ourPid;
|
||||
}
|
||||
|
||||
/// <summary>Centered fractions for a size in percent of the client; 0 means the default (88).</summary>
|
||||
public static PanelFractions Default(int widthPercent, int heightPercent)
|
||||
{
|
||||
double w = Pct(widthPercent) / 100.0;
|
||||
double h = Pct(heightPercent) / 100.0;
|
||||
return new PanelFractions((1 - w) / 2, (1 - h) / 2, w, h);
|
||||
}
|
||||
|
||||
private static int Pct(int p) => p == 0 ? DefaultPercent : Math.Clamp(p, MinPercent, MaxPercent);
|
||||
|
||||
/// <summary>Screen rect for the fractions: at least 480x320 (or the whole client if smaller), never outside the client.</summary>
|
||||
public static PanelRect Place(PanelRect gameClientScreen, PanelFractions f)
|
||||
{
|
||||
int cw = Math.Max(0, gameClientScreen.Width);
|
||||
int ch = Math.Max(0, gameClientScreen.Height);
|
||||
int w = Math.Clamp((int)Math.Round(Clamp01(f.W) * cw), Math.Min(MinWidthPx, cw), cw);
|
||||
int h = Math.Clamp((int)Math.Round(Clamp01(f.H) * ch), Math.Min(MinHeightPx, ch), ch);
|
||||
int x = Math.Clamp((int)Math.Round(Clamp01(f.X) * cw), 0, cw - w);
|
||||
int y = Math.Clamp((int)Math.Round(Clamp01(f.Y) * ch), 0, ch - h);
|
||||
int left = gameClientScreen.Left + x;
|
||||
int top = gameClientScreen.Top + y;
|
||||
return new PanelRect(left, top, left + w, top + h);
|
||||
}
|
||||
|
||||
/// <summary>Fractions of the client for a panel rect the user dragged or resized, clamped to 0..1.</summary>
|
||||
public static PanelFractions FromUserRect(PanelRect gameClientScreen, PanelRect panelScreen)
|
||||
{
|
||||
int cw = gameClientScreen.Width, ch = gameClientScreen.Height;
|
||||
if (cw <= 0 || ch <= 0) return Default(0, 0);
|
||||
double w = Clamp01((double)panelScreen.Width / cw);
|
||||
double h = Clamp01((double)panelScreen.Height / ch);
|
||||
double x = Math.Clamp((double)(panelScreen.Left - gameClientScreen.Left) / cw, 0, 1 - w);
|
||||
double y = Math.Clamp((double)(panelScreen.Top - gameClientScreen.Top) / ch, 0, 1 - h);
|
||||
return new PanelFractions(x, y, w, h);
|
||||
}
|
||||
|
||||
private static double Clamp01(double v) => double.IsNaN(v) ? 0 : Math.Clamp(v, 0, 1);
|
||||
}
|
||||
216
tools/WebPanelTests/Program.cs
Normal file
216
tools/WebPanelTests/Program.cs
Normal file
|
|
@ -0,0 +1,216 @@
|
|||
using RynthCore.Engine.WebPanel;
|
||||
|
||||
namespace WebPanelTests;
|
||||
|
||||
/// <summary>The web panel's pure policies: URL allowlist, redaction, visibility, layout, profile folder.</summary>
|
||||
internal static class Program
|
||||
{
|
||||
private static int _checks, _failed;
|
||||
private static string _case = "";
|
||||
|
||||
private static readonly string[] Dw = { "*.acdreamweave.com" };
|
||||
|
||||
private static int Main()
|
||||
{
|
||||
Allowlist_ExactAndWildcard();
|
||||
Allowlist_LookalikesRejected();
|
||||
Allowlist_CaseIdnTrailingDot();
|
||||
Allowlist_BadEntries();
|
||||
Allowlist_ParseGood();
|
||||
BrowserSafe();
|
||||
Redact();
|
||||
Visibility();
|
||||
Layout();
|
||||
Profile();
|
||||
|
||||
Console.WriteLine(_failed == 0 ? $"PASS ({_checks} assertions)" : $"FAIL ({_failed} of {_checks} assertions)");
|
||||
return _failed == 0 ? 0 : 1;
|
||||
}
|
||||
|
||||
private static void Case(string name) { _case = name; Console.WriteLine("ok " + name); }
|
||||
|
||||
private static void Check(bool ok, string what)
|
||||
{
|
||||
_checks++;
|
||||
if (ok) return;
|
||||
_failed++;
|
||||
Console.WriteLine($"FAIL {_case}: {what}");
|
||||
}
|
||||
|
||||
private static void Eq<T>(T got, T want, string what) =>
|
||||
Check(EqualityComparer<T>.Default.Equals(got, want), $"{what}: got {got}, want {want}");
|
||||
|
||||
private static void Allowlist_ExactAndWildcard()
|
||||
{
|
||||
Case("Allowlist_ExactAndWildcard");
|
||||
Check(WebPanelUrlPolicy.IsAllowed("https://www.acdreamweave.com/x", Dw), "www subdomain allowed");
|
||||
Check(WebPanelUrlPolicy.IsAllowed("https://a.b.acdreamweave.com/", Dw), "deep subdomain allowed");
|
||||
Check(!WebPanelUrlPolicy.IsAllowed("https://acdreamweave.com/", Dw), "wildcard does not match the suffix itself");
|
||||
Check(WebPanelUrlPolicy.IsAllowed("https://acdreamweave.com/", new[] { "acdreamweave.com" }), "exact entry matches");
|
||||
Check(!WebPanelUrlPolicy.IsAllowed("https://www.acdreamweave.com/", new[] { "acdreamweave.com" }), "exact entry does not match subdomain");
|
||||
Check(WebPanelUrlPolicy.IsAllowed("about:blank", Dw), "about:blank allowed");
|
||||
Check(!WebPanelUrlPolicy.IsAllowed(null, Dw), "null rejected");
|
||||
Check(!WebPanelUrlPolicy.IsAllowed("", Dw), "empty rejected");
|
||||
Check(!WebPanelUrlPolicy.IsAllowed("https://www.acdreamweave.com/", Array.Empty<string>()), "empty allowlist rejects all");
|
||||
}
|
||||
|
||||
private static void Allowlist_LookalikesRejected()
|
||||
{
|
||||
Case("Allowlist_LookalikesRejected");
|
||||
foreach (string u in new[]
|
||||
{
|
||||
"https://www.acdreamweave.com.evil.com/",
|
||||
"https://evilacdreamweave.com/",
|
||||
"https://www.acdreamweave.com@evil.com/",
|
||||
"https://evil.com/?h=.acdreamweave.com",
|
||||
"http://www.acdreamweave.com/",
|
||||
"https://www.acdreamweave.com:8443/",
|
||||
"https://user:pw@www.acdreamweave.com/",
|
||||
"file:///C:/x",
|
||||
"javascript:alert(1)",
|
||||
})
|
||||
Check(!WebPanelUrlPolicy.IsAllowed(u, Dw), "rejects " + u);
|
||||
Check(!WebPanelUrlPolicy.IsAllowed("https://www.acdreamweave.com/" + new string('a', 5000), Dw), "over MaxUrlBytes rejected");
|
||||
}
|
||||
|
||||
private static void Allowlist_CaseIdnTrailingDot()
|
||||
{
|
||||
Case("Allowlist_CaseIdnTrailingDot");
|
||||
Check(WebPanelUrlPolicy.IsAllowed("https://WWW.AcDreamWeave.com./", Dw), "case and one trailing dot");
|
||||
Check(WebPanelUrlPolicy.IsAllowed("https://www.acdreamweave.com:443/", Dw), "explicit default port");
|
||||
string puny = new System.Globalization.IdnMapping().GetAscii("b\u00fccher.example");
|
||||
Check(WebPanelUrlPolicy.IsAllowed("https://b\u00fccher.example/", new[] { puny }), "unicode host matches its punycode entry");
|
||||
Check(WebPanelUrlPolicy.IsAllowed("https://" + puny + "/", new[] { puny }), "punycode host matches itself");
|
||||
Check(!WebPanelUrlPolicy.IsAllowed("https://bucher.example/", new[] { puny }), "ascii lookalike does not match punycode entry");
|
||||
Check(!WebPanelUrlPolicy.IsAllowed("https://xn--bcher-kva.example/", new[] { "bucher.example" }), "punycode host does not match other entry");
|
||||
}
|
||||
|
||||
private static void Allowlist_BadEntries()
|
||||
{
|
||||
Case("Allowlist_BadEntries");
|
||||
foreach (string bad in new[] { "*", "*.com", "https://x.com", "x.com/path", "x.com:443", "a.*.com", "", " ", "x.com.", "*.x.*.com", "a b.com", "*x.com" })
|
||||
Check(!WebPanelUrlPolicy.TryParseAllowlist(bad, out _), "rejects entry list '" + bad + "'");
|
||||
Check(!WebPanelUrlPolicy.TryParseAllowlist(null, out _), "null rejected");
|
||||
string seventeen = string.Join("\n", Enumerable.Range(0, 17).Select(i => $"h{i}.example.com"));
|
||||
Check(!WebPanelUrlPolicy.TryParseAllowlist(seventeen, out _), "17 entries rejected");
|
||||
string sixteen = string.Join("\n", Enumerable.Range(0, 16).Select(i => $"h{i}.example.com"));
|
||||
Check(WebPanelUrlPolicy.TryParseAllowlist(sixteen, out var e16) && e16.Length == 16, "16 entries accepted");
|
||||
Check(!WebPanelUrlPolicy.TryParseAllowlist("good.example.com\nbad/entry", out _), "one bad entry fails the list");
|
||||
Check(!WebPanelUrlPolicy.TryParseAllowlist(new string('a', 254) + ".com", out _), "over-long entry rejected");
|
||||
}
|
||||
|
||||
private static void Allowlist_ParseGood()
|
||||
{
|
||||
Case("Allowlist_ParseGood");
|
||||
Check(WebPanelUrlPolicy.TryParseAllowlist("*.AcDreamWeave.com\r\nlocalhost\n", out var e), "parses");
|
||||
Eq(e.Length, 2, "entry count");
|
||||
Eq(e[0], "*.acdreamweave.com", "lower-cased wildcard");
|
||||
Eq(e[1], "localhost", "single-label host ok");
|
||||
}
|
||||
|
||||
private static void BrowserSafe()
|
||||
{
|
||||
Case("BrowserSafe");
|
||||
Check(WebPanelUrlPolicy.IsBrowserSafe("https://example.com/a?b=c"), "https");
|
||||
Check(WebPanelUrlPolicy.IsBrowserSafe("http://example.com/"), "http");
|
||||
foreach (string u in new[] { "file:///C:/Windows/notepad.exe", "javascript:alert(1)", "ms-settings:network", "mailto:a@b.c", "/relative/path", "example.com", "https://u:p@example.com/", "https://u@example.com/", "", " " })
|
||||
Check(!WebPanelUrlPolicy.IsBrowserSafe(u), "not safe: " + u);
|
||||
Check(!WebPanelUrlPolicy.IsBrowserSafe(null), "null not safe");
|
||||
}
|
||||
|
||||
private static void Redact()
|
||||
{
|
||||
Case("Redact");
|
||||
string r = WebPanelUrlPolicy.Redact("https://www.acdreamweave.com/launch?code=SECRET&to=/Browse#f");
|
||||
Eq(r, "https://www.acdreamweave.com/launch", "query and fragment stripped");
|
||||
Check(!r.Contains("SECRET"), "secret absent");
|
||||
Eq(WebPanelUrlPolicy.Redact("https://user:pw@host.example:8443/p/q?x=1"), "https://host.example:8443/p/q", "userinfo dropped, port kept");
|
||||
Check(!WebPanelUrlPolicy.Redact("https://user:pw@host.example/p").Contains("pw"), "password absent");
|
||||
Eq(WebPanelUrlPolicy.Redact("not a url"), "<invalid url>", "garbage");
|
||||
Eq(WebPanelUrlPolicy.Redact(null), "<invalid url>", "null");
|
||||
Eq(WebPanelUrlPolicy.Redact(""), "<invalid url>", "empty");
|
||||
Check(!WebPanelUrlPolicy.Redact("mailto:secret@x.com").Contains("secret"), "non-hierarchical leaks nothing");
|
||||
}
|
||||
|
||||
private static void Visibility()
|
||||
{
|
||||
Case("Visibility");
|
||||
// open, alive, !iconic, visible, !fgNull, fgPid, ourPid
|
||||
Eq(WebPanelWindowPolicy.Visible(true, true, false, true, false, 5, 5), (bool?)true, "same process shows");
|
||||
Eq(WebPanelWindowPolicy.Visible(true, true, false, true, false, 6, 5), (bool?)false, "other process hides");
|
||||
Eq(WebPanelWindowPolicy.Visible(true, true, false, true, true, 0, 5), (bool?)null, "null foreground keeps current");
|
||||
Eq(WebPanelWindowPolicy.Visible(true, true, true, true, false, 5, 5), (bool?)false, "iconic hides");
|
||||
Eq(WebPanelWindowPolicy.Visible(false, true, false, true, false, 5, 5), (bool?)false, "closed hides");
|
||||
Eq(WebPanelWindowPolicy.Visible(true, false, false, true, false, 5, 5), (bool?)false, "dead game hides");
|
||||
Eq(WebPanelWindowPolicy.Visible(true, true, false, false, false, 5, 5), (bool?)false, "invisible game hides");
|
||||
Eq(WebPanelWindowPolicy.Visible(false, true, false, true, true, 0, 5), (bool?)false, "closed beats null foreground");
|
||||
Eq(WebPanelWindowPolicy.Visible(true, true, true, true, true, 0, 5), (bool?)false, "iconic beats null foreground");
|
||||
}
|
||||
|
||||
private static void Layout()
|
||||
{
|
||||
Case("Layout");
|
||||
var client = new PanelRect(100, 50, 1200, 800); // 1100 x 750
|
||||
var f = WebPanelWindowPolicy.Default(0, 0);
|
||||
Check(Math.Abs(f.W - 0.88) < 1e-9 && Math.Abs(f.H - 0.88) < 1e-9, "0 means 88 %");
|
||||
Check(Math.Abs(f.X - 0.06) < 1e-9 && Math.Abs(f.Y - 0.06) < 1e-9, "centered");
|
||||
var r = WebPanelWindowPolicy.Place(client, f);
|
||||
Eq(r.Width, 968, "88 % of 1100");
|
||||
Eq(r.Height, 660, "88 % of 750");
|
||||
Eq(r.Left - client.Left, client.Right - r.Right, "horizontally centered");
|
||||
Eq(r.Top - client.Top, client.Bottom - r.Bottom, "vertically centered");
|
||||
|
||||
var full = WebPanelWindowPolicy.Default(120, 120);
|
||||
Check(full.W == 1.0 && full.H == 1.0 && full.X == 0 && full.Y == 0, "120 clamps to 100");
|
||||
var small = WebPanelWindowPolicy.Default(5, 5);
|
||||
Check(Math.Abs(small.W - 0.30) < 1e-9, "5 clamps to 30");
|
||||
Eq(WebPanelWindowPolicy.Place(client, full), client, "100 % fills the client");
|
||||
|
||||
var tiny = new PanelRect(0, 0, 300, 200);
|
||||
Eq(WebPanelWindowPolicy.Place(tiny, WebPanelWindowPolicy.Default(0, 0)), tiny, "tiny client clamps to client");
|
||||
var mid = new PanelRect(0, 0, 600, 400);
|
||||
var m = WebPanelWindowPolicy.Place(mid, WebPanelWindowPolicy.Default(30, 30));
|
||||
Check(m.Width >= 480 && m.Height >= 320, "minimum size enforced");
|
||||
Check(m.Left >= mid.Left && m.Top >= mid.Top && m.Right <= mid.Right && m.Bottom <= mid.Bottom, "min size stays inside client");
|
||||
|
||||
// Off-screen fractions stay inside.
|
||||
var off = WebPanelWindowPolicy.Place(client, new PanelFractions(0.95, 0.95, 0.5, 0.5));
|
||||
Check(off.Left >= client.Left && off.Top >= client.Top && off.Right <= client.Right && off.Bottom <= client.Bottom, "never outside client");
|
||||
|
||||
// Round trip.
|
||||
var user = new PanelRect(300, 200, 1000, 700);
|
||||
var rt = WebPanelWindowPolicy.Place(client, WebPanelWindowPolicy.FromUserRect(client, user));
|
||||
Check(Math.Abs(rt.Left - user.Left) <= 1 && Math.Abs(rt.Top - user.Top) <= 1 &&
|
||||
Math.Abs(rt.Right - user.Right) <= 1 && Math.Abs(rt.Bottom - user.Bottom) <= 1, $"round trip within 1 px: {rt}");
|
||||
var wild = WebPanelWindowPolicy.FromUserRect(client, new PanelRect(-500, -500, 5000, 5000));
|
||||
Check(wild.X >= 0 && wild.Y >= 0 && wild.W <= 1 && wild.H <= 1 && wild.X + wild.W <= 1.000001, "FromUserRect clamps to 0..1");
|
||||
}
|
||||
|
||||
private static void Profile()
|
||||
{
|
||||
Case("Profile");
|
||||
const string root = @"C:\Users\x\AppData\Local";
|
||||
string a = WebPanelProfile.Folder(root, "DreamWeave", "Alice");
|
||||
Eq(a, WebPanelProfile.Folder(root, "DreamWeave", " ALICE "), "case/space-insensitive key");
|
||||
Check(a != WebPanelProfile.Folder(root, "DreamWeave", "Bob"), "different keys differ");
|
||||
Check(a != WebPanelProfile.Folder(root, "Other", "Alice"), "different worlds differ");
|
||||
string leaf = Path.GetFileName(a);
|
||||
Eq(leaf.Length, 32, "hex is 32 chars");
|
||||
Check(leaf.All(c => (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')), "lowercase hex");
|
||||
// SHA-256("alice") first 16 bytes.
|
||||
Eq(leaf, Convert.ToHexString(System.Security.Cryptography.SHA256.HashData(System.Text.Encoding.UTF8.GetBytes("alice")), 0, 16).ToLowerInvariant(), "hash of lowered key");
|
||||
Eq(Path.GetFileName(WebPanelProfile.Folder(root, "W", "")), "default", "empty key");
|
||||
Eq(Path.GetFileName(WebPanelProfile.Folder(root, "W", null)), "default", "null key");
|
||||
|
||||
string evil = WebPanelProfile.Folder(root, "Dream/Weave..", "k");
|
||||
Check(!evil.Contains(".."), "no dot-dot");
|
||||
Eq(Path.GetFileName(Path.GetDirectoryName(evil)!), "Dream_Weave__", "world sanitized");
|
||||
Eq(Path.GetFileName(Path.GetDirectoryName(WebPanelProfile.Folder(root, null, "k"))!), "_default", "null world");
|
||||
Eq(Path.GetFileName(Path.GetDirectoryName(WebPanelProfile.Folder(root, " ", "k"))!), "_default", "blank world");
|
||||
Eq(Path.GetFileName(Path.GetDirectoryName(WebPanelProfile.Folder(root, new string('w', 100), "k"))!).Length, 64, "world capped at 64");
|
||||
Eq(Path.GetFileName(Path.GetDirectoryName(WebPanelProfile.Folder(root, @"..\..\x", "k"))!), "______x", "traversal neutralised");
|
||||
string full = Path.GetFullPath(evil);
|
||||
Check(full.StartsWith(Path.GetFullPath(root), StringComparison.OrdinalIgnoreCase), "under localAppData");
|
||||
Check(evil.Contains(Path.Combine("RynthCore", "WebPanel")), "RynthCore\\WebPanel layout");
|
||||
}
|
||||
}
|
||||
24
tools/WebPanelTests/WebPanelTests.csproj
Normal file
24
tools/WebPanelTests/WebPanelTests.csproj
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<!--
|
||||
Offline checks for the web panel's pure policies (URL allowlist and redaction, window
|
||||
visibility and layout, profile folder): src\RynthCore.Engine\WebPanel\*.cs, compiled in.
|
||||
No AC, no Win32, no WebView2, no files.
|
||||
Run: dotnet run -c Release (exit 0 = pass, 1 = fail)
|
||||
-->
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<InvariantGlobalization>true</InvariantGlobalization>
|
||||
<RootNamespace>WebPanelTests</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="..\..\src\RynthCore.Engine\WebPanel\WebPanelUrlPolicy.cs" Link="Engine\WebPanelUrlPolicy.cs" />
|
||||
<Compile Include="..\..\src\RynthCore.Engine\WebPanel\WebPanelWindowPolicy.cs" Link="Engine\WebPanelWindowPolicy.cs" />
|
||||
<Compile Include="..\..\src\RynthCore.Engine\WebPanel\WebPanelProfile.cs" Link="Engine\WebPanelProfile.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
Reference in a new issue