Add explanation rules for catches, editable over MCP
Build and deploy / build (push) Successful in 1m29s
Build and deploy / deploy (push) Successful in 21s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
TomandClaude Opus 5.5 committed 2026-11-21 11:46:22 +01:00
1 parent 4086fa3da3
commit 2437880013
37 files changed
+1867 -37

No files matched your search

+58 -2
View File
@@ -23,6 +23,9 @@ public sealed class McpEndpointTests : IDisposable
});
}
private static readonly string[] ReadTools =
["get_stats", "list_catches", "longest_catches", "lookup_ip", "top", "list_explanation_rules", "list_unexplained", "test_explanation_rule"];
[Fact]
public async Task Rejects_requests_without_token()
{
@@ -62,7 +65,7 @@ public sealed class McpEndpointTests : IDisposable
var tools = await ReadAsync(await client.SendAsync(Rpc("tools/list", token)));
var listed = tools.GetProperty("result").GetProperty("tools").EnumerateArray().ToList();
listed.Select(t => t.GetProperty("name").GetString())
.ShouldBe(["get_stats", "list_catches", "longest_catches", "lookup_ip", "top"], ignoreOrder: true);
.ShouldBe(ReadTools, ignoreOrder: true);
listed.ShouldAllBe(t => t.GetProperty("annotations").GetProperty("readOnlyHint").GetBoolean());
listed.Where(t => !t.TryGetProperty("outputSchema", out var schema) || schema.ValueKind != JsonValueKind.Object).ShouldBeEmpty();
@@ -87,7 +90,13 @@ public sealed class McpEndpointTests : IDisposable
{ "top", """{"dimension":"user_agents","period":"all"}""" },
{ "longest_catches", """{"period":"all"}""" },
{ "lookup_ip", """{"ip":"203.0.113.9"}""" },
{ "lookup_ip", """{"ip":"192.0.2.1"}""" }
{ "lookup_ip", """{"ip":"192.0.2.1"}""" },
{ "list_explanation_rules", """{}""" },
{ "list_unexplained", """{}""" },
{ "list_unexplained", """{"period":"7d","group_by":"path","limit":5}""" },
{ "test_explanation_rule", """{"kind":"target","pattern":"^/\\.env"}""" },
{ "test_explanation_rule", """{"kind":"client_info","pattern":"curl","priority":5}""" },
{ "test_explanation_rule", """{"kind":"outcome","pattern":"^Rejected$"}""" }
};
[Theory]
@@ -134,6 +143,53 @@ public sealed class McpEndpointTests : IDisposable
result.GetProperty("content")[0].GetProperty("text").GetString().ShouldNotBeNull().ShouldContain(expected, Case.Sensitive);
}
[Fact]
public async Task Write_tools_are_only_offered_to_and_usable_by_tokens_with_write_permission()
{
using var client = _factory.CreateClient();
var tokens = _factory.Services.GetRequiredService<McpTokenService>();
var readOnly = await tokens.CreateAsync("read");
var writer = await tokens.CreateAsync("Claude Code", canWrite: true);
var writerTools = (await ReadAsync(await client.SendAsync(Rpc("tools/list", writer)))).GetProperty("result").GetProperty("tools")
.EnumerateArray().Select(t => t.GetProperty("name").GetString()).ToList();
writerTools.ShouldBe(
[.. ReadTools, "create_explanation_rule", "update_explanation_rule", "set_explanation_rule_enabled", "delete_explanation_rule"],
ignoreOrder: true);
var denied = (await ReadAsync(await client.SendAsync(Rpc("tools/call", readOnly, new
{
name = "create_explanation_rule",
arguments = new { kind = "target", pattern = "^/\\.env$", text = "Sucht nach einer Konfigurationsdatei." }
})))).GetProperty("result");
denied.GetProperty("isError").GetBoolean().ShouldBeTrue();
denied.GetProperty("content")[0].GetProperty("text").GetString().ShouldNotBeNull().ShouldContain("write permission");
var created = (await ReadAsync(await client.SendAsync(Rpc("tools/call", writer, new
{
name = "create_explanation_rule",
arguments = new { kind = "target", pattern = "^/\\.env$", text = "Sucht nach einer Konfigurationsdatei.", priority = 5, note = "CVE-0000-0000" }
})))).GetProperty("result");
(created.TryGetProperty("isError", out var isError) && isError.GetBoolean()).ShouldBeFalse(created.ToString());
var rule = created.GetProperty("structuredContent");
rule.GetProperty("kind").GetString().ShouldBe("target");
rule.GetProperty("createdBy").GetString().ShouldBe("Claude Code");
rule.GetProperty("enabled").GetBoolean().ShouldBeTrue();
var listed = (await ReadAsync(await client.SendAsync(Rpc("tools/call", readOnly, new { name = "list_explanation_rules", arguments = new { } }))))
.GetProperty("result").GetProperty("structuredContent");
listed.GetProperty("count").GetInt32().ShouldBe(1);
listed.GetProperty("rules")[0].GetProperty("catches").GetInt32().ShouldBe(2);
var invalid = (await ReadAsync(await client.SendAsync(Rpc("tools/call", writer, new
{
name = "create_explanation_rule",
arguments = new { kind = "target", pattern = "(unclosed", text = "Text" }
})))).GetProperty("result");
invalid.GetProperty("isError").GetBoolean().ShouldBeTrue();
invalid.GetProperty("content")[0].GetProperty("text").GetString().ShouldNotBeNull().ShouldContain("regular expression");
}
private static async Task<Dictionary<string, JsonElement>> OutputSchemasAsync(HttpClient client, string token)
{
var tools = await ReadAsync(await client.SendAsync(Rpc("tools/list", token)));