From 2522534979a85fbfd9fe7ab87c9d61ccf3c0c2a1 Mon Sep 17 00:00:00 2001 From: Daniel Wallace Date: Sat, 7 Nov 2026 15:01:35 -0800 Subject: [PATCH] RynthDreamWeave: per-account web profile key WebProfile.Key(serverId, account) = serverId/ + 32 lowercase hex of SHA-256 over the server id and the trimmed, lower-cased account; null without an account. Keys pass ServerWebOpen.IsValidProfileKey. Co-Authored-By: Claude Opus 5.5 --- .../DreamWeave/WebProfile.cs | 23 +++++++++++++++++ .../DreamWeaveWebTests.cs | 25 +++++++++++++++++++ .../RynthServerPack.Tests.csproj | 1 + 3 files changed, 49 insertions(+) create mode 100644 Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/WebProfile.cs diff --git a/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/WebProfile.cs b/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/WebProfile.cs new file mode 100644 index 0000000..d799222 --- /dev/null +++ b/Plugins/RynthCore.Plugin.RynthDreamWeave/DreamWeave/WebProfile.cs @@ -0,0 +1,23 @@ +// The per-account web profile key: each account gets its own WebView2 profile folder, so website +// cookies never cross accounts. Pure, so RynthServerPack.Tests compiles it directly. +using System; +using System.Security.Cryptography; +using System.Text; + +namespace RynthCore.Plugin.RynthDreamWeave; + +internal static class WebProfile +{ + /// + /// "serverId/" plus the first 32 lowercase hex chars of SHA-256(UTF-8(serverId + "\n" + the trimmed, + /// lower-cased account)); null when there is no account. The server id (not the world name) keeps + /// one site's cookies together. The hash keeps the account name out of the folder name. The result + /// passes ServerWebOpen.IsValidProfileKey when serverId matches ^[a-z0-9-]{1,32}$. + /// + public static string? Key(string serverId, string? account) + { + if (string.IsNullOrWhiteSpace(account)) return null; + byte[] hash = SHA256.HashData(Encoding.UTF8.GetBytes(serverId + "\n" + account.Trim().ToLowerInvariant())); + return serverId + "/" + Convert.ToHexString(hash).ToLowerInvariant().Substring(0, 32); + } +} diff --git a/Tools/RynthServerPack.Tests/DreamWeaveWebTests.cs b/Tools/RynthServerPack.Tests/DreamWeaveWebTests.cs index 3fb1378..dfbc215 100644 --- a/Tools/RynthServerPack.Tests/DreamWeaveWebTests.cs +++ b/Tools/RynthServerPack.Tests/DreamWeaveWebTests.cs @@ -27,6 +27,7 @@ internal static class DreamWeaveWebTests r.Add("dreamweave web: reset keeps the throttle", ResetKeepsThrottle); r.Add("dreamweave web: a late code is eaten while it could still be valid", LateCodeIsStale); r.Add("dreamweave web: overlapping requests never leak a code to chat", OverlappingRequests); + r.Add("dreamweave web: profile keys separate accounts", ProfileKeys); } private const string Code = "AbCdEfGhIjKlMnOpQrStUvWxYz0123456789-_AbCde"; // 43 chars @@ -354,6 +355,30 @@ internal static class DreamWeaveWebTests Check.True(u.OnChatLine(Line, 0x05, 70_000, out _, out _, out _) == LaunchLine.NotOurs, "+70,000 not ours"); } + private static void ProfileKeys() + { + string? a = WebProfile.Key("dreamweave", "Alice"); + string? b = WebProfile.Key("dreamweave", "Bob"); + Check.True(a != null && b != null, "keys exist"); + Check.True(a != b, "different accounts -> different keys"); + Check.Eq(WebProfile.Key("dreamweave", " alice "), a, "trimmed and case-folded"); + Check.Eq(WebProfile.Key("dreamweave", "ALICE"), a, "case-folded"); + Check.Eq(WebProfile.Key("dreamweave", "Alice"), a, "stable across calls"); + Check.True(WebProfile.Key("coldeve", "Alice") != a, "same account, other server -> different key"); + Check.True(a!.StartsWith("dreamweave/", StringComparison.Ordinal), "prefixed by the server id"); + string[] accounts = { "Alice", "Bob", "a", "x y", "caf" + (char)0xE9, "A-very-long-account-name-0123456789-0123456789-0123456789" }; + foreach (string acct in accounts) + { + string? k = WebProfile.Key("dreamweave", acct); + Check.True(ServerWebOpen.IsValidProfileKey(k), "valid key for " + acct.Length + " chars"); + // A 1-2 char account could appear in the server id or the hex by chance; the rest cannot. + if (acct.Length >= 3) Check.True(!k!.Contains(acct.Trim().ToLowerInvariant()), "key never contains the account text"); + } + Check.True(WebProfile.Key("dreamweave", null) == null, "null account -> null"); + Check.True(WebProfile.Key("dreamweave", "") == null, "empty account -> null"); + Check.True(WebProfile.Key("dreamweave", " ") == null, "blank account -> null"); + } + private static void OverlappingRequests() { // Scenario 1: request 1 times out, request 2 begins, request 1's late code arrives. diff --git a/Tools/RynthServerPack.Tests/RynthServerPack.Tests.csproj b/Tools/RynthServerPack.Tests/RynthServerPack.Tests.csproj index 1fb19fa..68c8d8a 100644 --- a/Tools/RynthServerPack.Tests/RynthServerPack.Tests.csproj +++ b/Tools/RynthServerPack.Tests/RynthServerPack.Tests.csproj @@ -38,6 +38,7 @@ +