diff --git a/Plugins/Shared/ServerPack/ServerPackWeb.cs b/Plugins/Shared/ServerPack/ServerPackWeb.cs index af3c95e..cabecd7 100644 --- a/Plugins/Shared/ServerPack/ServerPackWeb.cs +++ b/Plugins/Shared/ServerPack/ServerPackWeb.cs @@ -17,13 +17,14 @@ public sealed partial class ServerPackPlugin [DllImport("shell32.dll", CharSet = CharSet.Unicode, EntryPoint = "ShellExecuteW")] private static extern IntPtr ShellExecuteW(IntPtr hwnd, string? operation, string file, string? parameters, string? directory, int showCmd); - // Seams for the engine's web panel. + // Seam for the engine's web panel (a stub until the engine API is wired). private bool PanelAvailable => false; - private bool TryOpenPanel(WebOpenRequest r, out string error) + private PanelResult TryOpenPanel(WebOpenRequest r) => PanelResult.RefusedBeforeNavigate; + + private static void Finish(Action done, WebOpenOutcome outcome) { - error = "The web panel is not available."; - return false; + try { done(outcome); } catch { } } /// Opens the page (pump thread). `done` gets the route taken and a message without the URL. @@ -32,23 +33,35 @@ public sealed partial class ServerPackPlugin WebOpenRoute route = ServerWebOpen.Route(r, PanelAvailable); if (route == WebOpenRoute.Refused) { - done(new WebOpenOutcome(route, false, "That page is not on this server's list of pages, so it was not opened.")); + Finish(done, new WebOpenOutcome(route, false, "That page is not on this server's list of pages, so it was not opened.")); return; } if (route == WebOpenRoute.Panel) { - if (TryOpenPanel(r, out string error)) + PanelResult result; + try { result = TryOpenPanel(r); } + catch (Exception) { result = PanelResult.RefusedBeforeNavigate; } + + if (result == PanelResult.Opened) { - done(new WebOpenOutcome(route, true, "Opened in the web panel.")); + Finish(done, new WebOpenOutcome(WebOpenRoute.Panel, true, "Opened in the web panel.")); return; } - // The panel failed: the browser is the fallback, and the report below says it was the browser. - LogTagged("web panel did not open: " + error); - route = WebOpenRoute.Browser; + if (!ServerWebOpen.FallbackToBrowser(result)) + { + LogTagged("web panel: " + result); + Finish(done, new WebOpenOutcome(WebOpenRoute.Panel, false, "The web panel couldn't open that page.")); + return; + } + LogTagged("web panel: " + result + ", using the browser"); } - string url = r.Url; + if (!ServerWebOpen.TryGetAllowedUri(r.Url, r.AllowedHosts, out string url)) + { + Finish(done, new WebOpenOutcome(WebOpenRoute.Refused, false, "That page is not on this server's list of pages, so it was not opened.")); + return; + } var thread = new Thread(() => { string message; @@ -64,7 +77,7 @@ public sealed partial class ServerPackPlugin ok = false; message = "Couldn't open your browser (code 0)."; } - done(new WebOpenOutcome(WebOpenRoute.Browser, ok, message)); + Finish(done, new WebOpenOutcome(WebOpenRoute.Browser, ok, message)); }) { IsBackground = true, @@ -73,7 +86,6 @@ public sealed partial class ServerPackPlugin thread.SetApartmentState(ApartmentState.STA); thread.Start(); } - /// Closes the engine's web panel if this plugin opened one. Nothing to close until the panel API is wired. internal void CloseWeb() { diff --git a/Plugins/Shared/ServerPack/ServerWebOpen.cs b/Plugins/Shared/ServerPack/ServerWebOpen.cs index 2100a4d..7b0392c 100644 --- a/Plugins/Shared/ServerPack/ServerWebOpen.cs +++ b/Plugins/Shared/ServerPack/ServerWebOpen.cs @@ -12,6 +12,9 @@ internal sealed record WebOpenRequest(string Url, IReadOnlyList AllowedH internal enum WebOpenRoute { Panel, Browser, Refused } +/// What the engine's panel did. Only a refusal before navigating may fall back to the browser. +internal enum PanelResult { Opened, RefusedBeforeNavigate, UrlNotAllowed, FailedAfterNavigate } + /// What happened. The message never contains the URL. internal sealed record WebOpenOutcome(WebOpenRoute Route, bool Ok, string Message); @@ -22,13 +25,25 @@ internal static class ServerWebOpen /// (OrdinalIgnoreCase). An entry "*.x" matches subdomains of x only, never x itself. /// public static bool IsAllowedUrl(string? url, IReadOnlyList allowedHosts) + => TryGetAllowedUri(url, allowedHosts, out _); + + /// The same check, giving the validated absolute URI (what the browser should be handed). + public static bool TryGetAllowedUri(string? url, IReadOnlyList allowedHosts, out string absoluteUri) { + absoluteUri = string.Empty; if (string.IsNullOrEmpty(url) || allowedHosts == null) return false; if (!Uri.TryCreate(url, UriKind.Absolute, out Uri? uri)) return false; if (!string.Equals(uri.Scheme, "https", StringComparison.Ordinal)) return false; if (uri.UserInfo.Length != 0 || !uri.IsDefaultPort) return false; string host = uri.Host; if (host.Length == 0) return false; + if (!HostAllowed(host, allowedHosts)) return false; + absoluteUri = uri.AbsoluteUri; + return true; + } + + private static bool HostAllowed(string host, IReadOnlyList allowedHosts) + { foreach (string entry in allowedHosts) { @@ -49,6 +64,10 @@ internal static class ServerWebOpen return false; } + /// Fall back to the browser only when the panel refused before navigating. After a + /// navigate the one-time code is spent (a dead link), and UrlNotAllowed must not bypass the engine's list. + public static bool FallbackToBrowser(PanelResult r) => r == PanelResult.RefusedBeforeNavigate; + /// ^[a-z0-9-]{1,32}/[0-9a-f]{32}$ - the engine's panel profile key (server/token). public static bool IsValidProfileKey(string? key) { diff --git a/Tools/RynthServerPack.Tests/ServerWebOpenTests.cs b/Tools/RynthServerPack.Tests/ServerWebOpenTests.cs index acbeb10..25ffd9e 100644 --- a/Tools/RynthServerPack.Tests/ServerWebOpenTests.cs +++ b/Tools/RynthServerPack.Tests/ServerWebOpenTests.cs @@ -45,5 +45,12 @@ internal static class ServerWebOpenTests 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"); } }