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); } private static readonly string[] Hosts = { "www.acdreamweave.com" }; private static readonly string[] Wild = { "*.acdreamweave.com" }; private const string Key = "dreamweave/0123456789abcdef0123456789abcdef"; 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"); } }