DreamWeave: website links, logged in by launch code #2
3 changed files with 51 additions and 13 deletions
ServerPack web open: fall back only before the panel navigates
PanelResult + pure FallbackToBrowser; guard TryOpenPanel throws; guard done callbacks; hand ShellExecuteW the validated absolute URI. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
commit
c085c16e15
|
|
@ -17,13 +17,14 @@ public sealed partial class ServerPackPlugin
|
||||||
[DllImport("shell32.dll", CharSet = CharSet.Unicode, EntryPoint = "ShellExecuteW")]
|
[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);
|
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 PanelAvailable => false;
|
||||||
|
|
||||||
private bool TryOpenPanel(WebOpenRequest r, out string error)
|
private PanelResult TryOpenPanel(WebOpenRequest r) => PanelResult.RefusedBeforeNavigate;
|
||||||
|
|
||||||
|
private static void Finish(Action<WebOpenOutcome> done, WebOpenOutcome outcome)
|
||||||
{
|
{
|
||||||
error = "The web panel is not available.";
|
try { done(outcome); } catch { }
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Opens the page (pump thread). `done` gets the route taken and a message without the URL.</summary>
|
/// <summary>Opens the page (pump thread). `done` gets the route taken and a message without the URL.</summary>
|
||||||
|
|
@ -32,23 +33,35 @@ public sealed partial class ServerPackPlugin
|
||||||
WebOpenRoute route = ServerWebOpen.Route(r, PanelAvailable);
|
WebOpenRoute route = ServerWebOpen.Route(r, PanelAvailable);
|
||||||
if (route == WebOpenRoute.Refused)
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (route == WebOpenRoute.Panel)
|
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;
|
return;
|
||||||
}
|
}
|
||||||
// The panel failed: the browser is the fallback, and the report below says it was the browser.
|
if (!ServerWebOpen.FallbackToBrowser(result))
|
||||||
LogTagged("web panel did not open: " + error);
|
{
|
||||||
route = WebOpenRoute.Browser;
|
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(() =>
|
var thread = new Thread(() =>
|
||||||
{
|
{
|
||||||
string message;
|
string message;
|
||||||
|
|
@ -64,7 +77,7 @@ public sealed partial class ServerPackPlugin
|
||||||
ok = false;
|
ok = false;
|
||||||
message = "Couldn't open your browser (code 0).";
|
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,
|
IsBackground = true,
|
||||||
|
|
@ -73,7 +86,6 @@ public sealed partial class ServerPackPlugin
|
||||||
thread.SetApartmentState(ApartmentState.STA);
|
thread.SetApartmentState(ApartmentState.STA);
|
||||||
thread.Start();
|
thread.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Closes the engine's web panel if this plugin opened one. Nothing to close until the panel API is wired.</summary>
|
/// <summary>Closes the engine's web panel if this plugin opened one. Nothing to close until the panel API is wired.</summary>
|
||||||
internal void CloseWeb()
|
internal void CloseWeb()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,9 @@ internal sealed record WebOpenRequest(string Url, IReadOnlyList<string> AllowedH
|
||||||
|
|
||||||
internal enum WebOpenRoute { Panel, Browser, Refused }
|
internal enum WebOpenRoute { Panel, Browser, Refused }
|
||||||
|
|
||||||
|
/// <summary>What the engine's panel did. Only a refusal before navigating may fall back to the browser.</summary>
|
||||||
|
internal enum PanelResult { Opened, RefusedBeforeNavigate, UrlNotAllowed, FailedAfterNavigate }
|
||||||
|
|
||||||
/// <summary>What happened. The message never contains the URL.</summary>
|
/// <summary>What happened. The message never contains the URL.</summary>
|
||||||
internal sealed record WebOpenOutcome(WebOpenRoute Route, bool Ok, string Message);
|
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.
|
/// (OrdinalIgnoreCase). An entry "*.x" matches subdomains of x only, never x itself.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static bool IsAllowedUrl(string? url, IReadOnlyList<string> allowedHosts)
|
public static bool IsAllowedUrl(string? url, IReadOnlyList<string> allowedHosts)
|
||||||
|
=> TryGetAllowedUri(url, allowedHosts, out _);
|
||||||
|
|
||||||
|
/// <summary>The same check, giving the validated absolute URI (what the browser should be handed).</summary>
|
||||||
|
public static bool TryGetAllowedUri(string? url, IReadOnlyList<string> allowedHosts, out string absoluteUri)
|
||||||
{
|
{
|
||||||
|
absoluteUri = string.Empty;
|
||||||
if (string.IsNullOrEmpty(url) || allowedHosts == null) return false;
|
if (string.IsNullOrEmpty(url) || allowedHosts == null) return false;
|
||||||
if (!Uri.TryCreate(url, UriKind.Absolute, out Uri? uri)) return false;
|
if (!Uri.TryCreate(url, UriKind.Absolute, out Uri? uri)) return false;
|
||||||
if (!string.Equals(uri.Scheme, "https", StringComparison.Ordinal)) return false;
|
if (!string.Equals(uri.Scheme, "https", StringComparison.Ordinal)) return false;
|
||||||
if (uri.UserInfo.Length != 0 || !uri.IsDefaultPort) return false;
|
if (uri.UserInfo.Length != 0 || !uri.IsDefaultPort) return false;
|
||||||
string host = uri.Host;
|
string host = uri.Host;
|
||||||
if (host.Length == 0) return false;
|
if (host.Length == 0) return false;
|
||||||
|
if (!HostAllowed(host, allowedHosts)) return false;
|
||||||
|
absoluteUri = uri.AbsoluteUri;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool HostAllowed(string host, IReadOnlyList<string> allowedHosts)
|
||||||
|
{
|
||||||
|
|
||||||
foreach (string entry in allowedHosts)
|
foreach (string entry in allowedHosts)
|
||||||
{
|
{
|
||||||
|
|
@ -49,6 +64,10 @@ internal static class ServerWebOpen
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>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.</summary>
|
||||||
|
public static bool FallbackToBrowser(PanelResult r) => r == PanelResult.RefusedBeforeNavigate;
|
||||||
|
|
||||||
/// <summary>^[a-z0-9-]{1,32}/[0-9a-f]{32}$ - the engine's panel profile key (server/token).</summary>
|
/// <summary>^[a-z0-9-]{1,32}/[0-9a-f]{32}$ - the engine's panel profile key (server/token).</summary>
|
||||||
public static bool IsValidProfileKey(string? key)
|
public static bool IsValidProfileKey(string? key)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -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 = "" }, 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 { 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.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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue