using System.Net; using System.Net.Http.Json; using Titano.Protocol; using Titano.Server.Configuration; using Titano.Testing; namespace Titano.Server.Tests.Hosting; /// TLS mode proxy (BRIEF ยง14.2): forwarded headers count only from trusted proxies, and the admin warns otherwise. [Category("server")] public sealed class ProxyModeTests(TestContext context) { /// A documentation network (RFC 5737) the test client never connects from. private const string ElsewhereOnly = "192.0.2.0/24"; [Test] public async Task AClientOutsideTheTrustedProxiesCannotChooseItsAddress() { await using var server = await TestServer.StartAsync(context, o => { o.TlsMode = TlsMode.Proxy; o.TrustedProxies = ElsewhereOnly; o.LoginAttemptsPerMinute = 5; }); for (var i = 0; i < 5; i++) { using var failed = await LoginAsync(server, "user" + i, $"203.0.113.{i + 1}"); await failed.ExpectErrorAsync(HttpStatusCode.Unauthorized, "invalid_credentials"); } // A new X-Forwarded-For per attempt does not buy a new budget: the limit stays on the real address. using var limited = await LoginAsync(server, "admin", "203.0.113.99"); await limited.ExpectErrorAsync(HttpStatusCode.TooManyRequests, "rate_limited"); var audit = await server.Database.Audit.ListAsync(null, "login_failed", 50, null, context.Cancellation); Assert.Count(5, audit.Items); Assert.True(audit.Items.All(e => IsLoopback(e.RemoteAddress)), string.Join(", ", audit.Items.Select(e => e.RemoteAddress))); } [Test] public async Task ATrustedProxyReportsTheClientAddress() { // The default trusts loopback and the private networks a compose or Unraid proxy container connects from. await using var server = await TestServer.StartAsync(context, o => { o.TlsMode = TlsMode.Proxy; o.LoginAttemptsPerMinute = 5; }); for (var i = 0; i < 8; i++) { using var failed = await LoginAsync(server, "user" + i, $"203.0.113.{i + 1}"); await failed.ExpectErrorAsync(HttpStatusCode.Unauthorized, "invalid_credentials"); } var audit = await server.Database.Audit.ListAsync(null, "login_failed", 50, null, context.Cancellation); Assert.Contains("203.0.113.8", audit.Items.Select(e => e.RemoteAddress)); } [Test] public async Task TheAdminWarnsWhenAPageDidNotComeThroughATrustedProxyOverHttps() { await using var server = await TestServer.StartAsync(context, o => o.TlsMode = TlsMode.Proxy); var admin = await server.AdminAsync(); Assert.True((await DashboardAsync(admin, forwardedProto: null)).ProxyWarning, "plain HTTP to the server's own port"); Assert.False((await DashboardAsync(admin, "https")).ProxyWarning, "HTTPS through a trusted proxy"); Assert.True((await DashboardAsync(admin, "http")).ProxyWarning, "plain HTTP through the proxy"); Assert.True((await SettingsAsync(admin, forwardedProto: null)).ProxyWarning); Assert.False((await SettingsAsync(admin, "https")).ProxyWarning); await using var untrusted = await TestServer.StartAsync(context, o => { o.TlsMode = TlsMode.Proxy; o.TrustedProxies = ElsewhereOnly; }); var other = await untrusted.AdminAsync(); Assert.True((await DashboardAsync(other, "https")).ProxyWarning, "a claim of HTTPS from a peer that is no trusted proxy"); } [Test] public async Task OtherTlsModesNeverWarn() { await using var server = await TestServer.StartAsync(context); var admin = await server.AdminAsync(); Assert.False((await DashboardAsync(admin, forwardedProto: null)).ProxyWarning); Assert.False((await SettingsAsync(admin, forwardedProto: null)).ProxyWarning); } private static bool IsLoopback(string? address) => IPAddress.TryParse(address, out var ip) && IPAddress.IsLoopback(ip.IsIPv4MappedToIPv6 ? ip.MapToIPv4() : ip); private static async Task LoginAsync(TestServer server, string username, string forwardedFor) { using var request = new HttpRequestMessage(HttpMethod.Post, "/api/v1/auth/login") { Content = JsonContent.Create(new LoginRequest(username, "wrong-password-x", "tests"), ProtocolJson.Default.LoginRequest), }; request.Headers.Add("X-Forwarded-For", forwardedFor); return await server.Client.SendAsync(request); } private static async Task DashboardAsync(HttpClient admin, string? forwardedProto) { using var response = await SendAsync(admin, "/api/v1/server/dashboard", forwardedProto); return await response.ReadAsync(ProtocolJson.Default.DashboardDto); } private static async Task SettingsAsync(HttpClient admin, string? forwardedProto) { using var response = await SendAsync(admin, "/api/v1/server/settings", forwardedProto); return await response.ReadAsync(ProtocolJson.Default.ServerSettingsDto); } private static async Task SendAsync(HttpClient client, string path, string? forwardedProto) { using var request = new HttpRequestMessage(HttpMethod.Get, path); if (forwardedProto is not null) request.Headers.Add("X-Forwarded-Proto", forwardedProto); return await client.SendAsync(request); } }