Files
Chronos/tests/Titano.Server.Tests/Hosting/DiscoveryTests.cs
T
Claude 5ec59a2e96 fix(server): trust forwarded headers only from known proxies
In TLS mode proxy the server accepted X-Forwarded-For/Proto/Host from any
peer, so a client reaching the HTTP port directly could pick a new address
per sign-in (defeating the 5/min/IP limit), forge audit addresses and claim
HTTPS; the admin never warned (SRS-03, SRV-3). Forwarded headers now count
only from TITANO_TRUSTED_PROXIES (addresses or CIDR networks; default
loopback, 10/8, 172.16/12, 192.168/16, fc00::/7), and the Dashboard and
Settings show a "Not via proxy" chip with a tooltip when a page did not
come through a trusted proxy over HTTPS (ProxyWarning in DashboardDto and
ServerSettingsDto).

TITANO_PUBLIC_HTTP_PORT and TITANO_PUBLIC_HTTPS_PORT name the ports clients
reach when a container's ports are published as other host ports: the
admin's HTTP-to-HTTPS redirect and the LAN beacon use them instead of the
bound container ports (DOCKER-4, DOCKER-5). --help lists TITANO_LOG_LEVEL,
the new variables and the container's PUID, PGID and TZ (DOCKER-7); a test
keeps it in step with the variables the server reads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-12 09:27:31 +00:00

55 lines
2.3 KiB
C#

using System.Net;
using System.Net.Sockets;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Titano.Protocol;
using Titano.Server.Api;
using Titano.Server.Hosting;
using Titano.Testing;
namespace Titano.Server.Tests.Hosting;
[Category("server")]
public sealed class DiscoveryTests(TestContext context)
{
[Test]
public async Task TheBeaconTravelsOverUdpAndParses()
{
await using var server = await TestServer.StartAsync(context, o => o.ServerName = "Studio NAS");
var service = server.Services.GetRequiredService<DiscoveryBeaconService>();
using var listener = new UdpClient(new IPEndPoint(IPAddress.Loopback, 0));
using var sender = new UdpClient(AddressFamily.InterNetwork);
await service.SendOnceAsync(sender, [(IPEndPoint)listener.Client.LocalEndPoint!], context.Cancellation);
var datagram = await listener.ReceiveAsync(context.Cancellation).AsTask().WaitAsync(TimeSpan.FromSeconds(5));
var beacon = Assert.NotNull(DiscoveryBeacon.TryParse(datagram.Buffer));
Assert.Equal("Studio NAS", beacon.Name);
Assert.Equal(SystemEndpoints.Version, beacon.Version);
Assert.Equal(server.HttpBase.Port, beacon.HttpPort);
Assert.Equal(server.HttpsBase!.Port, beacon.HttpsPort);
using var info = await server.Client.GetAsync("/api/v1/server/info");
Assert.Equal((await info.ReadAsync(ProtocolJson.Default.ServerInfo)).TlsFingerprint, beacon.TlsFingerprint);
}
[Test]
public async Task TheBeaconAdvertisesThePortsClientsReach()
{
// Container ports published as other host ports: a client on the LAN must use the host's.
await using var server = await TestServer.StartAsync(context, o =>
{
o.PublicHttpPort = 18080;
o.PublicHttpsPort = 18443;
});
var beacon = server.Services.GetRequiredService<DiscoveryBeaconService>().CreateBeacon();
Assert.Equal((18080, 18443), (beacon.HttpPort, beacon.HttpsPort));
}
[Test]
public void ForeignDatagramsAreIgnored()
{
Assert.Null(DiscoveryBeacon.TryParse(Encoding.UTF8.GetBytes("HELLO {}")));
Assert.Null(DiscoveryBeacon.TryParse(Encoding.UTF8.GetBytes("TITANO1 {not json")));
Assert.Null(DiscoveryBeacon.TryParse(new byte[4096]));
}
}