Restructure solution into folders and fix persistence issues

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
TomandClaude Opus 5.5 committed 2026-11-14 14:10:24 +01:00
1 parent 37987a394a
commit 7c39cb5562
62 files changed
+219 -151

No files matched your search

@@ -0,0 +1,91 @@
using System.Net;
using Flypaper.Core;
using Flypaper.Core.Abstractions;
using Flypaper.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Shouldly;
namespace Flypaper.Tests.Http;
public sealed class ForwardedHeadersTests
{
private static readonly IPAddress Npm = IPAddress.Parse("172.18.0.5");
private const string Client = "198.51.100.7";
[Fact]
public async Task Uses_x_forwarded_for_from_proxy_in_trusted_network()
{
var ip = await SendAsync(Npm, ("X-Forwarded-For", $"10.9.9.9, {Client}"));
ip.ShouldBe(IPAddress.Parse(Client));
}
[Fact]
public async Task Accepts_ipv4_mapped_proxy_address()
{
var ip = await SendAsync(Npm.MapToIPv6(), ("X-Forwarded-For", Client));
ip.ShouldBe(IPAddress.Parse(Client));
}
[Fact]
public async Task Falls_back_to_x_real_ip()
{
var ip = await SendAsync(Npm.MapToIPv6(), ("X-Real-IP", Client));
ip.ShouldBe(IPAddress.Parse(Client));
}
[Fact]
public async Task Ignores_headers_from_untrusted_sender()
{
var sender = IPAddress.Parse("203.0.113.50");
var ip = await SendAsync(sender, ("X-Forwarded-For", Client), ("X-Real-IP", Client));
ip.ShouldBe(sender);
}
private static async Task<IPAddress?> SendAsync(IPAddress remoteIp, params (string Name, string Value)[] headers)
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
["Flypaper:Http:KnownHosts:0"] = "dashboard.lan",
["Flypaper:Http:TrustedProxies:0"] = "172.18.0.0/16"
})
.Build();
var services = new ServiceCollection()
.AddLogging()
.AddSingleton(Substitute.For<ITrapEventSink>())
.AddFlypaperCore(configuration)
.AddFlypaperHttp(configuration)
.BuildServiceProvider();
IPAddress? seen = null;
var app = new ApplicationBuilder(services);
app.UseFlypaper();
app.Run(context =>
{
seen = context.Connection.RemoteIpAddress;
return Task.CompletedTask;
});
var httpContext = new DefaultHttpContext { RequestServices = services };
httpContext.Connection.RemoteIpAddress = remoteIp;
httpContext.Request.Host = new HostString("dashboard.lan");
httpContext.Request.Path = "/";
foreach (var (name, value) in headers)
{
httpContext.Request.Headers[name] = value;
}
await app.Build()(httpContext);
return seen;
}
}
@@ -0,0 +1,42 @@
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);
}