106 lines
3.4 KiB
C#
106 lines
3.4 KiB
C#
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;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using NSubstitute;
|
|
using Shouldly;
|
|
|
|
namespace Flypaper.Tests.Http;
|
|
|
|
public sealed class PortSeparationTests
|
|
{
|
|
private const int TrapPort = 8080;
|
|
private const int DashboardPort = 8081;
|
|
private static readonly IPAddress PublicIp = IPAddress.Parse("203.0.113.9");
|
|
|
|
[Theory]
|
|
[InlineData("unknown.example", "/")]
|
|
[InlineData("unknown.example", "/.env")]
|
|
[InlineData("203.0.113.7", "/wp-login.php")]
|
|
public async Task Dashboard_port_never_traps(string host, string path)
|
|
{
|
|
var (reachedApp, _) = await SendAsync(DashboardPort, host, path);
|
|
|
|
reachedApp.ShouldBeTrue();
|
|
}
|
|
|
|
[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", "/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, host, path, sink);
|
|
|
|
reachedApp.ShouldBeFalse();
|
|
status.ShouldBe(StatusCodes.Status404NotFound);
|
|
sink.Received(1).Publish(Arg.Is<TrapEvent>(e => e.Outcome == TrapOutcome.Rejected));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Removed_settings_change_nothing()
|
|
{
|
|
var (reachedApp, _) = await SendAsync(
|
|
TrapPort,
|
|
"example.com",
|
|
"/",
|
|
extraSettings: new Dictionary<string, string?>
|
|
{
|
|
["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()
|
|
.AddLogging()
|
|
.AddSingleton(sink ?? Substitute.For<ITrapEventSink>())
|
|
.AddFlypaperCore(configuration)
|
|
.AddFlypaperHttp(configuration)
|
|
.BuildServiceProvider();
|
|
|
|
var reachedApp = false;
|
|
var app = new ApplicationBuilder(services);
|
|
app.UseFlypaper();
|
|
app.Run(_ =>
|
|
{
|
|
reachedApp = true;
|
|
return Task.CompletedTask;
|
|
});
|
|
|
|
var httpContext = new DefaultHttpContext { RequestServices = services };
|
|
httpContext.Connection.RemoteIpAddress = PublicIp;
|
|
httpContext.Connection.LocalPort = localPort;
|
|
httpContext.Request.Host = new HostString(host);
|
|
httpContext.Request.Path = path;
|
|
|
|
await app.Build()(httpContext);
|
|
return (reachedApp, httpContext.Response.StatusCode);
|
|
}
|
|
}
|