using System; using RynthCore.Plugin.ServerPack; using RynthCore.RynthAiTests.Harness; namespace RynthServerPack.Tests; // The web opener's pure part: which URLs a server plugin may open, and where the page goes. internal static class ServerWebOpenTests { public static void Register(Runner r) { r.Add("web open: only https on an allowed host, and the route follows the engine", Rules); r.Add("web open: a request never prints its URL, and the browser's error says Windows", NoUrlInText); } private static readonly string[] Hosts = { "www.acdreamweave.com" }; private static readonly string[] Wild = { "*.acdreamweave.com" }; private const string Key = "dreamweave/0123456789abcdef0123456789abcdef"; private static void NoUrlInText() { const string secret = "AbCdEfGhIjKlMnOpQrStUvWxYz0123456789-_AbCde"; var r = new WebOpenRequest("https://www.acdreamweave.com/launch?code=" + secret + "&to=%2Fmarket", Hosts, Key, "DreamWeave"); Check.Eq(r.ToString(), "WebOpenRequest(DreamWeave)", "ToString names the title only"); Check.False(r.ToString().Contains(secret, StringComparison.Ordinal), "ToString never carries the code"); Check.False(("" + r).Contains("acdreamweave", StringComparison.Ordinal), "nor the URL"); Check.False((r with { Title = "x" }).ToString().Contains(secret, StringComparison.Ordinal), "a copy neither"); string web = System.IO.File.ReadAllText(System.IO.Path.Combine(ServerProjectsTests.PluginsDir(), "Shared", "ServerPack", "ServerPackWeb.cs")); Check.True(web.Contains("$\"Couldn't open your browser (Windows error {code}).\"", StringComparison.Ordinal), "the browser failure names the Windows error"); Check.True(web.Contains("\"Couldn't open your browser (Windows error 0).\"", StringComparison.Ordinal), "and its exception path"); Check.False(web.Contains("(code ", StringComparison.Ordinal), "the old \"(code N)\" wording is gone"); } private static void Rules() { Check.True(ServerWebOpen.IsAllowedUrl("https://www.acdreamweave.com/x", Hosts), "https on the host"); Check.True(ServerWebOpen.IsAllowedUrl("https://WWW.AcDreamWeave.com/x", Hosts), "host case-insensitive"); Check.True(!ServerWebOpen.IsAllowedUrl("http://www.acdreamweave.com/x", Hosts), "http refused"); Check.True(!ServerWebOpen.IsAllowedUrl("https://user@www.acdreamweave.com/x", Hosts), "userinfo refused"); Check.True(!ServerWebOpen.IsAllowedUrl("https://www.acdreamweave.com:444/x", Hosts), "other port refused"); Check.True(!ServerWebOpen.IsAllowedUrl("https://www.acdreamweave.com.evil.com/x", Hosts), "suffix host refused"); Check.True(!ServerWebOpen.IsAllowedUrl("javascript:alert(1)", Hosts), "javascript refused"); Check.True(!ServerWebOpen.IsAllowedUrl(null, Hosts), "null refused"); Check.True(!ServerWebOpen.IsAllowedUrl("/relative", Hosts), "relative refused"); Check.True(ServerWebOpen.IsAllowedUrl("https://wiki.acdreamweave.com/", Wild), "wildcard matches a subdomain"); Check.True(!ServerWebOpen.IsAllowedUrl("https://acdreamweave.com/", Wild), "wildcard excludes the bare domain"); Check.True(!ServerWebOpen.IsAllowedUrl("https://evilacdreamweave.com/", Wild), "wildcard needs the dot"); Check.True(ServerWebOpen.IsValidProfileKey(Key), "dreamweave/ + 32 hex"); Check.True(!ServerWebOpen.IsValidProfileKey("dreamweave/0123456789ABCDEF0123456789abcdef"), "uppercase refused"); Check.True(!ServerWebOpen.IsValidProfileKey("../0123456789abcdef0123456789abcdef"), "dotdot refused"); Check.True(!ServerWebOpen.IsValidProfileKey(Key + "0"), "longer refused"); Check.True(!ServerWebOpen.IsValidProfileKey(""), "empty refused"); Check.True(!ServerWebOpen.IsValidProfileKey(null), "null refused"); var ok = new WebOpenRequest("https://www.acdreamweave.com/x", Hosts, Key, "t"); Check.Eq(ServerWebOpen.Route(ok, true), WebOpenRoute.Panel, "panel"); Check.Eq(ServerWebOpen.Route(ok, false), WebOpenRoute.Browser, "no panel -> browser"); Check.Eq(ServerWebOpen.Route(ok with { ProfileKey = "" }, true), WebOpenRoute.Browser, "empty key -> browser"); Check.Eq(ServerWebOpen.Route(ok with { ProfileKey = "Bad/Key" }, true), WebOpenRoute.Browser, "bad key -> browser"); Check.Eq(ServerWebOpen.Route(ok with { Url = "http://www.acdreamweave.com/x" }, true), WebOpenRoute.Refused, "bad url refused"); Check.True(ServerWebOpen.FallbackToBrowser(PanelResult.RefusedBeforeNavigate), "refused before navigate -> browser"); Check.True(!ServerWebOpen.FallbackToBrowser(PanelResult.Opened), "opened -> no browser"); Check.True(!ServerWebOpen.FallbackToBrowser(PanelResult.UrlNotAllowed), "engine allowlist is not bypassed"); Check.True(!ServerWebOpen.FallbackToBrowser(PanelResult.FailedAfterNavigate), "spent code -> no browser"); Check.True(ServerWebOpen.TryGetAllowedUri("https://www.acdreamweave.com/x?a=1", Hosts, out string abs) && abs == "https://www.acdreamweave.com/x?a=1", "validated absolute uri"); Check.True(!ServerWebOpen.TryGetAllowedUri("http://www.acdreamweave.com/", Hosts, out string none) && none.Length == 0, "refused gives no uri"); } }