Separate dashboard port, first-run password setup and default trusted proxies
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a34ff96490
commit
c22d452720
25 files changed
+654
-109
No files matched your search
@@ -50,13 +50,38 @@ public sealed class ForwardedHeadersTests
|
||||
ip.ShouldBe(sender);
|
||||
}
|
||||
|
||||
private static async Task<IPAddress?> SendAsync(IPAddress remoteIp, params (string Name, string Value)[] headers)
|
||||
[Theory]
|
||||
[InlineData("172.19.0.3")]
|
||||
[InlineData("10.1.2.3")]
|
||||
[InlineData("192.168.1.2")]
|
||||
[InlineData("127.0.0.1")]
|
||||
[InlineData("fd00::2")]
|
||||
public async Task Trusts_private_networks_by_default(string proxy)
|
||||
{
|
||||
var ip = await SendAsync(IPAddress.Parse(proxy), trustedProxy: null, ("X-Real-IP", Client));
|
||||
|
||||
ip.ShouldBe(IPAddress.Parse(Client));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Configured_proxies_replace_the_default_networks()
|
||||
{
|
||||
var sender = IPAddress.Parse("192.168.1.2");
|
||||
|
||||
var ip = await SendAsync(sender, ("X-Real-IP", Client));
|
||||
|
||||
ip.ShouldBe(sender);
|
||||
}
|
||||
|
||||
private static Task<IPAddress?> SendAsync(IPAddress remoteIp, params (string Name, string Value)[] headers) =>
|
||||
SendAsync(remoteIp, "172.18.0.0/16", headers);
|
||||
|
||||
private static async Task<IPAddress?> SendAsync(IPAddress remoteIp, string? trustedProxy, 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"
|
||||
["Flypaper:Http:TrustedProxies:0"] = trustedProxy
|
||||
})
|
||||
.Build();
|
||||
|
||||
@@ -78,6 +103,7 @@ public sealed class ForwardedHeadersTests
|
||||
|
||||
var httpContext = new DefaultHttpContext { RequestServices = services };
|
||||
httpContext.Connection.RemoteIpAddress = remoteIp;
|
||||
httpContext.Connection.LocalPort = 8081;
|
||||
httpContext.Request.Host = new HostString("dashboard.lan");
|
||||
httpContext.Request.Path = "/";
|
||||
foreach (var (name, value) in headers)
|
||||
|
||||
@@ -71,6 +71,7 @@ public sealed class HealthProbeTests
|
||||
|
||||
var httpContext = new DefaultHttpContext { RequestServices = services };
|
||||
httpContext.Connection.RemoteIpAddress = remoteIp;
|
||||
httpContext.Connection.LocalPort = 8080;
|
||||
httpContext.Request.Host = new HostString("flypaper:8080");
|
||||
httpContext.Request.Path = path;
|
||||
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
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 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", "/.env")]
|
||||
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")]
|
||||
public async Task Trap_port_answers_other_paths_of_known_hosts_with_plain_404(string path)
|
||||
{
|
||||
var sink = Substitute.For<ITrapEventSink>();
|
||||
|
||||
var (reachedApp, status) = await SendAsync(TrapPort, "example.com", path, sink);
|
||||
|
||||
reachedApp.ShouldBeFalse();
|
||||
status.ShouldBe(StatusCodes.Status404NotFound);
|
||||
sink.DidNotReceiveWithAnyArgs().Publish(default!);
|
||||
}
|
||||
|
||||
private static async Task<(bool ReachedApp, int Status)> SendAsync(int localPort, string host, string path, ITrapEventSink? sink = null)
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(new Dictionary<string, string?>
|
||||
{
|
||||
["Flypaper:MaxConnections"] = "0",
|
||||
["Flypaper:Http:KnownHosts:0"] = "example.com",
|
||||
["Flypaper:Http:TrapPaths:0"] = "/.env"
|
||||
})
|
||||
.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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user