fix: restrict browser origins, honor ListenAddress, tighten JSON-RPC handling (1.4.2)
- OriginPolicy: only localhost, private/loopback IP origins, ServerUrl origin and the new AllowedOrigins setting may call the MCP port from a browser; others get 403 (blocks cross-site reads and DNS rebinding). No more reflected origin with Allow-Credentials. - ListenAddress is now used for the HttpListener prefix; warn when the server listens on all interfaces without an API key. - JSON-RPC: implement ping, never answer notifications, acknowledge client responses, negotiate protocolVersion, return 405 for GET /mcp and 404 for paths other than /mcp. - Tests for origin policy, listen prefix and protocol handling. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
37eb8b8a4e
commit
5b3dbc2d04
10 files changed
+256
-30
No files matched your search
@@ -132,8 +132,9 @@ foreach(var t in tools.EnumerateArray()) if(t.GetProperty("inputSchema").GetProp
|
||||
rows.Clear();
|
||||
var emptySeries=Run(new {seriesId="100"});
|
||||
Check(emptySeries.GetProperty("total").GetInt32()==0 && emptySeries.GetProperty("returned").GetInt32()==0 && !emptySeries.GetProperty("hasMore").GetBoolean(), "Empty series");
|
||||
Check(typeof(SeriesInventory).Assembly.GetName().Version.ToString(3)=="1.4.1", "Release version");
|
||||
Check(typeof(SeriesInventory).Assembly.GetName().Version.ToString(3)=="1.4.2", "Release version");
|
||||
AdvancedTests.Run();
|
||||
ProtocolTests.Run();
|
||||
InventoryReplay.Run();
|
||||
Console.WriteLine($"PASS: {passed} assertions; 35 episodes in 5 pages, no per-episode detail lookup.");
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using eMCPy.Mcp;
|
||||
|
||||
public static class ProtocolTests
|
||||
{
|
||||
public static void Run()
|
||||
{
|
||||
int assertions = 0;
|
||||
void Check(bool ok, string msg) { if (!ok) throw new Exception(msg); assertions++; }
|
||||
void Throws(Action f, string msg) { bool t = false; try { f(); } catch (ArgumentException) { t = true; } Check(t, msg); }
|
||||
|
||||
// ---- Origin-Policy ----
|
||||
Check(OriginPolicy.IsAllowed(null, null, null), "Kein Origin (Agent/curl) erlaubt");
|
||||
Check(OriginPolicy.IsAllowed("", null, null), "Leerer Origin erlaubt");
|
||||
Check(OriginPolicy.IsAllowed("http://192.168.1.2:8096", null, null), "Dashboard per LAN-IP");
|
||||
Check(OriginPolicy.IsAllowed("http://10.0.0.5:8096", null, null), "10/8 erlaubt");
|
||||
Check(OriginPolicy.IsAllowed("http://172.20.1.1:8096", null, null), "172.16/12 erlaubt");
|
||||
Check(OriginPolicy.IsAllowed("http://localhost:8096", null, null), "localhost erlaubt");
|
||||
Check(OriginPolicy.IsAllowed("http://127.0.0.1:8096", null, null), "Loopback erlaubt");
|
||||
Check(OriginPolicy.IsAllowed("http://[::1]:8096", null, null), "IPv6-Loopback erlaubt");
|
||||
Check(OriginPolicy.IsAllowed("http://[fd00::12]:8096", null, null), "IPv6-ULA erlaubt");
|
||||
Check(!OriginPolicy.IsAllowed("https://evil.example", null, null), "Fremde Domain abgewiesen");
|
||||
Check(!OriginPolicy.IsAllowed("http://evil.example:8097", null, null), "DNS-Rebinding-Domain abgewiesen");
|
||||
Check(!OriginPolicy.IsAllowed("http://8.8.8.8", null, null), "Öffentliche IP abgewiesen");
|
||||
Check(!OriginPolicy.IsAllowed("http://172.32.0.1", null, null), "172.32 ist nicht privat");
|
||||
Check(!OriginPolicy.IsAllowed("null", null, null), "Origin 'null' (Sandbox/Datei) abgewiesen");
|
||||
Check(!OriginPolicy.IsAllowed("file://x", null, null), "file-Schema abgewiesen");
|
||||
Check(!OriginPolicy.IsAllowed("http://192.168.1.2.evil.example", null, null), "IP-ähnliche Domain abgewiesen");
|
||||
Check(OriginPolicy.IsAllowed("https://emby.domain.de", "https://emby.domain.de/emby/", null), "ServerUrl-Origin erlaubt");
|
||||
Check(OriginPolicy.IsAllowed("https://emby.domain.de", null, "https://a.de, https://EMBY.domain.de/"), "AllowedOrigins erlaubt");
|
||||
Check(!OriginPolicy.IsAllowed("https://emby.domain.de:8443", null, "https://emby.domain.de"), "Port muss passen");
|
||||
Check(!OriginPolicy.IsAllowed("http://emby.domain.de", null, "https://emby.domain.de"), "Schema muss passen");
|
||||
|
||||
// ---- Listen-Prefix ----
|
||||
Check(OriginPolicy.ListenerPrefix("", 8097) == "http://*:8097/", "Leer = alle");
|
||||
Check(OriginPolicy.ListenerPrefix("0.0.0.0", 8097) == "http://*:8097/", "0.0.0.0 = alle");
|
||||
Check(OriginPolicy.ListenerPrefix("::", 8097) == "http://*:8097/", ":: = alle");
|
||||
Check(OriginPolicy.ListenerPrefix("127.0.0.1", 8097) == "http://127.0.0.1:8097/", "Loopback-Bindung");
|
||||
Check(OriginPolicy.ListenerPrefix(" 192.168.1.2 ", 9000) == "http://192.168.1.2:9000/", "Konkrete IP");
|
||||
Check(OriginPolicy.ListenerPrefix("::1", 8097) == "http://[::1]:8097/", "IPv6 in Klammern");
|
||||
Check(OriginPolicy.ListenerPrefix("emby.local", 8097) == "http://emby.local:8097/", "Hostname");
|
||||
Throws(() => OriginPolicy.ListenerPrefix("bad host/x", 8097), "Ungültige Adresse abgewiesen");
|
||||
|
||||
// ---- JSON-RPC / MCP ----
|
||||
var registry = new McpToolRegistry(null, null);
|
||||
JsonElement Call(string body) => JsonSerializer.SerializeToElement(registry.HandleRequest(body));
|
||||
|
||||
var ping = Call("{\"jsonrpc\":\"2.0\",\"id\":7,\"method\":\"ping\"}");
|
||||
Check(ping.GetProperty("id").GetInt32() == 7 && ping.GetProperty("result").ValueKind == JsonValueKind.Object, "ping liefert leeres Ergebnis");
|
||||
|
||||
Check(registry.HandleRequest("{\"jsonrpc\":\"2.0\",\"method\":\"notifications/initialized\"}") == null, "initialized ohne Antwort");
|
||||
Check(registry.HandleRequest("{\"jsonrpc\":\"2.0\",\"method\":\"notifications/cancelled\",\"params\":{\"requestId\":1}}") == null, "cancelled ohne Antwort");
|
||||
Check(registry.HandleRequest("{\"jsonrpc\":\"2.0\",\"method\":\"tools/list\"}") == null, "Jede Notification ohne Antwort");
|
||||
Check(registry.HandleRequest("{\"jsonrpc\":\"2.0\",\"id\":3,\"result\":{}}") == null, "Client-Antwort wird nur quittiert");
|
||||
|
||||
var noMethod = Call("{\"jsonrpc\":\"2.0\",\"id\":4}");
|
||||
Check(noMethod.GetProperty("error").GetProperty("code").GetInt32() == -32600, "Fehlende method = Invalid Request");
|
||||
|
||||
var unknown = Call("{\"jsonrpc\":\"2.0\",\"id\":5,\"method\":\"resources/list\"}");
|
||||
Check(unknown.GetProperty("error").GetProperty("code").GetInt32() == -32601, "Unbekannte Methode");
|
||||
|
||||
string Version(string requested)
|
||||
{
|
||||
var p = requested == null ? "{}" : "{\"protocolVersion\":\"" + requested + "\"}";
|
||||
return Call("{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":" + p + "}")
|
||||
.GetProperty("result").GetProperty("protocolVersion").GetString();
|
||||
}
|
||||
Check(Version("2024-11-05") == "2024-11-05", "Alte Version wird übernommen");
|
||||
Check(Version("2025-03-26") == "2025-03-26", "2025-03-26 wird übernommen");
|
||||
Check(Version("2025-06-18") == "2025-06-18", "2025-06-18 wird übernommen");
|
||||
Check(Version("2099-01-01") == McpToolRegistry.SupportedProtocolVersions[0], "Unbekannt → neueste");
|
||||
Check(Version(null) == McpToolRegistry.SupportedProtocolVersions[0], "Ohne Angabe → neueste");
|
||||
|
||||
Console.WriteLine($"PASS protocol: {assertions} assertions.");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user