Remove trap paths and known hosts: catch everything on the trap port
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b9f2b0443c
commit
9a56fc5b48
31 files changed
+335
-462
No files matched your search
@@ -48,8 +48,7 @@ public sealed class HealthProbeTests
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(new Dictionary<string, string?>
|
||||
{
|
||||
["Flypaper:MaxConnections"] = "0",
|
||||
["Flypaper:Http:KnownHosts:0"] = "dashboard.lan"
|
||||
["Flypaper:MaxConnections"] = "0"
|
||||
})
|
||||
.Build();
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Net;
|
||||
using Flypaper.Core;
|
||||
using Flypaper.Core.Abstractions;
|
||||
using Flypaper.Core.Models;
|
||||
using Flypaper.Http;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
@@ -31,42 +32,49 @@ public sealed class PortSeparationTests
|
||||
[Theory]
|
||||
[InlineData("unknown.example", "/")]
|
||||
[InlineData("203.0.113.7", "/settings")]
|
||||
[InlineData("example.com", "/")]
|
||||
[InlineData("example.com", "/.env")]
|
||||
[InlineData("example.com", "/login")]
|
||||
[InlineData("", "/wp-login.php")]
|
||||
[InlineData("unknown.example", "/mcp")]
|
||||
[InlineData("203.0.113.7", "/mcp")]
|
||||
public async Task Trap_port_traps_unknown_hosts_and_trap_paths(string host, string path)
|
||||
{
|
||||
var (reachedApp, status) = await SendAsync(TrapPort, host, path);
|
||||
|
||||
reachedApp.ShouldBeFalse();
|
||||
status.ShouldBe(StatusCodes.Status404NotFound);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("/")]
|
||||
[InlineData("/settings")]
|
||||
[InlineData("/login")]
|
||||
[InlineData("/mcp")]
|
||||
public async Task Trap_port_answers_other_paths_of_known_hosts_with_plain_404(string path)
|
||||
[InlineData("203.0.113.7", "/health")]
|
||||
public async Task Trap_port_catches_every_host_and_path(string host, string path)
|
||||
{
|
||||
var sink = Substitute.For<ITrapEventSink>();
|
||||
|
||||
var (reachedApp, status) = await SendAsync(TrapPort, "example.com", path, sink);
|
||||
var (reachedApp, status) = await SendAsync(TrapPort, host, path, sink);
|
||||
|
||||
reachedApp.ShouldBeFalse();
|
||||
status.ShouldBe(StatusCodes.Status404NotFound);
|
||||
sink.DidNotReceiveWithAnyArgs().Publish(default!);
|
||||
sink.Received(1).Publish(Arg.Is<TrapEvent>(e => e.Outcome == TrapOutcome.Rejected));
|
||||
}
|
||||
|
||||
private static async Task<(bool ReachedApp, int Status)> SendAsync(int localPort, string host, string path, ITrapEventSink? sink = null)
|
||||
[Fact]
|
||||
public async Task Removed_settings_change_nothing()
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(new Dictionary<string, string?>
|
||||
var (reachedApp, _) = await SendAsync(
|
||||
TrapPort,
|
||||
"example.com",
|
||||
"/",
|
||||
extraSettings: new Dictionary<string, string?>
|
||||
{
|
||||
["Flypaper:MaxConnections"] = "0",
|
||||
["Flypaper:Http:KnownHosts:0"] = "example.com",
|
||||
["Flypaper:Http:TrapPaths:0"] = "/.env"
|
||||
})
|
||||
});
|
||||
|
||||
reachedApp.ShouldBeFalse();
|
||||
}
|
||||
|
||||
private static async Task<(bool ReachedApp, int Status)> SendAsync(
|
||||
int localPort,
|
||||
string host,
|
||||
string path,
|
||||
ITrapEventSink? sink = null,
|
||||
Dictionary<string, string?>? extraSettings = null)
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(new Dictionary<string, string?> { ["Flypaper:MaxConnections"] = "0" })
|
||||
.AddInMemoryCollection(extraSettings ?? [])
|
||||
.Build();
|
||||
|
||||
var services = new ServiceCollection()
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
using Flypaper.Http;
|
||||
using Flypaper.Tests.Fakes;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Shouldly;
|
||||
|
||||
namespace Flypaper.Tests.Http;
|
||||
|
||||
public sealed class RemovedSettingsTests
|
||||
{
|
||||
[Fact]
|
||||
public void Warns_about_each_removed_setting_that_is_still_set()
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(new Dictionary<string, string?>
|
||||
{
|
||||
["Flypaper:Http:KnownHosts:0"] = "example.com",
|
||||
["Flypaper:Http:TrapPaths:0"] = "/.env",
|
||||
["Flypaper:Http:TrustedProxies:0"] = "172.19.0.0/16"
|
||||
})
|
||||
.Build();
|
||||
var logger = new ListLogger();
|
||||
|
||||
FlypaperHttpExtensions.WarnAboutRemovedSettings(configuration, logger);
|
||||
|
||||
logger.Entries.Count.ShouldBe(2);
|
||||
logger.Entries.ShouldAllBe(e => e.Level == LogLevel.Warning);
|
||||
logger.Entries.ShouldContain(e => e.Message.Contains("Flypaper__Http__KnownHosts"));
|
||||
logger.Entries.ShouldContain(e => e.Message.Contains("Flypaper__Http__TrapPaths"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Stays_silent_without_removed_settings()
|
||||
{
|
||||
var logger = new ListLogger();
|
||||
|
||||
FlypaperHttpExtensions.WarnAboutRemovedSettings(new ConfigurationBuilder().Build(), logger);
|
||||
|
||||
logger.Entries.ShouldBeEmpty();
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
using Flypaper.Http;
|
||||
using Flypaper.Http.Matching;
|
||||
using Flypaper.Tests.Fakes;
|
||||
using Shouldly;
|
||||
|
||||
namespace Flypaper.Tests.Http;
|
||||
|
||||
public sealed class TrapMatcherTests
|
||||
{
|
||||
private readonly TrapMatcher _matcher = new(TestOptions.Monitor(new HttpTrapOptions
|
||||
{
|
||||
KnownHosts = ["example.com", "*.example.org"],
|
||||
TrapPaths = ["/.env", "/.git/*", "/wp-login.php"]
|
||||
}));
|
||||
|
||||
[Theory]
|
||||
[InlineData("example.com", "/")]
|
||||
[InlineData("EXAMPLE.com", "/index.html")]
|
||||
[InlineData("app.example.org", "/api/items")]
|
||||
[InlineData("example.com", "/.envelope")]
|
||||
[InlineData("example.com", "/.git")]
|
||||
public void Lets_normal_requests_pass(string host, string path) =>
|
||||
_matcher.Match(host, path).ShouldBe(TrapMatch.None);
|
||||
|
||||
[Theory]
|
||||
[InlineData("example.com", "/.env")]
|
||||
[InlineData("example.com", "/.ENV")]
|
||||
[InlineData("example.com", "/.env/")]
|
||||
[InlineData("example.com", "/.git/config")]
|
||||
[InlineData("app.example.org", "/wp-login.php")]
|
||||
public void Traps_configured_paths(string host, string path) =>
|
||||
_matcher.Match(host, path).ShouldBe(TrapMatch.TrapPath);
|
||||
|
||||
[Theory]
|
||||
[InlineData("unknown.net", "/")]
|
||||
[InlineData("203.0.113.7", "/")]
|
||||
[InlineData("example.org", "/")]
|
||||
[InlineData("", "/")]
|
||||
[InlineData(null, "/")]
|
||||
public void Traps_unknown_hosts(string? host, string path) =>
|
||||
_matcher.Match(host, path).ShouldBe(TrapMatch.UnknownHost);
|
||||
}
|
||||
Reference in new issue
Block a user