ServerPack: open a web page in the engine's panel, else the browser
Pure allowlist/route rules (ServerWebOpen) plus the host opener (ServerPackWeb): ShellExecuteW on a background STA thread; panel seams stubbed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
5c6af0bbe5
commit
dd80216933
6 changed files with 215 additions and 0 deletions
81
Plugins/Shared/ServerPack/ServerPackWeb.cs
Normal file
81
Plugins/Shared/ServerPack/ServerPackWeb.cs
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
// ServerPackWeb.cs - the host side of ServerWebOpen: open a server's web page in the engine's web
|
||||
// panel when it has one, else in the system browser. A server's own code calls OpenWeb on the pump
|
||||
// (ServerChatCommand, a RunOnTick action); the answer goes to `done`, which may run on a worker
|
||||
// thread (Reply is any-thread). The answer never contains the URL.
|
||||
//
|
||||
// Never Process.Start inside acclient: the browser opens with ShellExecuteW, as the engine does
|
||||
// (ProblemReport.cs), on its own short-lived STA thread so the shell call never blocks the pump.
|
||||
// The panel seams (PanelAvailable, TryOpenPanel) are stubs until the engine API is wired.
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace RynthCore.Plugin.ServerPack;
|
||||
|
||||
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.
|
||||
private bool PanelAvailable => false;
|
||||
|
||||
private bool TryOpenPanel(WebOpenRequest r, out string error)
|
||||
{
|
||||
error = "The web panel is not available.";
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>Opens the page (pump thread). `done` gets the route taken and a message without the URL.</summary>
|
||||
internal void OpenWeb(WebOpenRequest r, Action<WebOpenOutcome> done)
|
||||
{
|
||||
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."));
|
||||
return;
|
||||
}
|
||||
|
||||
if (route == WebOpenRoute.Panel)
|
||||
{
|
||||
if (TryOpenPanel(r, out string error))
|
||||
{
|
||||
done(new WebOpenOutcome(route, 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;
|
||||
}
|
||||
|
||||
string url = r.Url;
|
||||
var thread = new Thread(() =>
|
||||
{
|
||||
string message;
|
||||
bool ok;
|
||||
try
|
||||
{
|
||||
long code = ShellExecuteW(IntPtr.Zero, "open", url, null, null, 1).ToInt64();
|
||||
ok = code > 32;
|
||||
message = ok ? "Opened in your browser." : $"Couldn't open your browser (code {code}).";
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
ok = false;
|
||||
message = "Couldn't open your browser (code 0).";
|
||||
}
|
||||
done(new WebOpenOutcome(WebOpenRoute.Browser, ok, message));
|
||||
})
|
||||
{
|
||||
IsBackground = true,
|
||||
Name = "ServerPack web open",
|
||||
};
|
||||
thread.SetApartmentState(ApartmentState.STA);
|
||||
thread.Start();
|
||||
}
|
||||
|
||||
/// <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()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -67,6 +67,9 @@
|
|||
<Compile Include="$(RynthSuiteRoot)Plugins\Shared\RynthServerQuestPackApi.cs" Link="Shared\RynthServerQuestPackApi.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)ServerPackQuestPack.cs" Link="ServerPack\ServerPackQuestPack.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)ServerPackInterface.cs" Link="ServerPack\ServerPackInterface.cs" />
|
||||
<!-- Open a web page in the engine's panel, else the browser (allowlisted https only). -->
|
||||
<Compile Include="$(MSBuildThisFileDirectory)ServerWebOpen.cs" Link="ServerPack\ServerWebOpen.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)ServerPackWeb.cs" Link="ServerPack\ServerPackWeb.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
79
Plugins/Shared/ServerPack/ServerWebOpen.cs
Normal file
79
Plugins/Shared/ServerPack/ServerWebOpen.cs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
// ServerWebOpen.cs - the rules for opening a server's web page from a server plugin: which URLs are
|
||||
// allowed and where the page goes (the engine's web panel when it has one, else the system browser).
|
||||
// Pure (no engine, no threads): RynthServerPack.Tests covers it. The host side is ServerPackWeb.cs.
|
||||
// No Regex: the plugins are NativeAOT and Regex costs size, so the key is parsed by hand.
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace RynthCore.Plugin.ServerPack;
|
||||
|
||||
/// <summary>A page to open. The URL comes from constants in the plugin, never from chat.</summary>
|
||||
internal sealed record WebOpenRequest(string Url, IReadOnlyList<string> AllowedHosts, string ProfileKey, string Title);
|
||||
|
||||
internal enum WebOpenRoute { Panel, Browser, Refused }
|
||||
|
||||
/// <summary>What happened. The message never contains the URL.</summary>
|
||||
internal sealed record WebOpenOutcome(WebOpenRoute Route, bool Ok, string Message);
|
||||
|
||||
internal static class ServerWebOpen
|
||||
{
|
||||
/// <summary>
|
||||
/// Absolute https, no userinfo, the default port, and a host equal to an allowed entry
|
||||
/// (OrdinalIgnoreCase). An entry "*.x" matches subdomains of x only, never x itself.
|
||||
/// </summary>
|
||||
public static bool IsAllowedUrl(string? url, IReadOnlyList<string> allowedHosts)
|
||||
{
|
||||
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;
|
||||
|
||||
foreach (string entry in allowedHosts)
|
||||
{
|
||||
if (string.IsNullOrEmpty(entry)) continue;
|
||||
if (entry.StartsWith("*.", StringComparison.Ordinal))
|
||||
{
|
||||
string suffix = entry.Substring(1); // ".x"
|
||||
if (suffix.Length > 1
|
||||
&& host.Length > suffix.Length
|
||||
&& host.EndsWith(suffix, StringComparison.OrdinalIgnoreCase))
|
||||
return true;
|
||||
}
|
||||
else if (string.Equals(host, entry, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
if (key == null) return false;
|
||||
int slash = key.IndexOf('/');
|
||||
if (slash < 1 || slash > 32) return false;
|
||||
if (key.Length - slash - 1 != 32) return false;
|
||||
for (int i = 0; i < slash; i++)
|
||||
{
|
||||
char c = key[i];
|
||||
if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-')) return false;
|
||||
}
|
||||
for (int i = slash + 1; i < key.Length; i++)
|
||||
{
|
||||
char c = key[i];
|
||||
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>Refused if the URL is not allowed; Browser if there is no panel or no valid key; else Panel.</summary>
|
||||
public static WebOpenRoute Route(WebOpenRequest r, bool panelAvailable)
|
||||
{
|
||||
if (!IsAllowedUrl(r.Url, r.AllowedHosts)) return WebOpenRoute.Refused;
|
||||
if (!panelAvailable || !IsValidProfileKey(r.ProfileKey)) return WebOpenRoute.Browser;
|
||||
return WebOpenRoute.Panel;
|
||||
}
|
||||
}
|
||||
|
|
@ -22,6 +22,7 @@ internal static class Program
|
|||
ServerPackCommandsTests.Register(runner);
|
||||
DreamWeaveSkillsTests.Register(runner);
|
||||
DreamWeaveTests.Register(runner);
|
||||
ServerWebOpenTests.Register(runner);
|
||||
return runner.Run("RynthServerPack tests", args.Length > 0 ? args[0] : null);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,6 +32,8 @@
|
|||
<!-- "/rserver" words and answers (2026-10-28) and the shared reply queue. -->
|
||||
<Compile Include="..\..\Plugins\Shared\ServerPack\ServerPackCommands.cs" Link="Plugin\ServerPackCommands.cs" />
|
||||
<Compile Include="$(RynthSuiteRoot)Plugins\Shared\CommandReply.cs" Link="Shared\CommandReply.cs" />
|
||||
<!-- The web opener's pure rules (allowlist, route). -->
|
||||
<Compile Include="..\..\Plugins\Shared\ServerPack\ServerWebOpen.cs" Link="Plugin\ServerWebOpen.cs" />
|
||||
<!-- RynthConquest's own features (pure parts) and the RynthOracle sources it links. -->
|
||||
<Compile Include="..\..\Plugins\RynthCore.Plugin.RynthConquest\Conquest\ConquestData.cs" Link="Conquest\ConquestData.cs" />
|
||||
<Compile Include="..\..\Plugins\RynthCore.Plugin.RynthConquest\Conquest\ConquestFeature.cs" Link="Conquest\ConquestFeature.cs" />
|
||||
|
|
|
|||
49
Tools/RynthServerPack.Tests/ServerWebOpenTests.cs
Normal file
49
Tools/RynthServerPack.Tests/ServerWebOpenTests.cs
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
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");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue