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
This commit is contained in:
17 files changed
+340
-22
No files matched your search
@@ -1,3 +1,4 @@
|
||||
using System.Globalization;
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Hosting.Server;
|
||||
using Microsoft.AspNetCore.Hosting.Server.Features;
|
||||
@@ -7,7 +8,9 @@ namespace Titano.Server.Hosting;
|
||||
|
||||
/// <summary>
|
||||
/// Serves the embedded admin panel at <c>/admin</c>. Requests over plain HTTP from another machine are redirected to
|
||||
/// the HTTPS listener, because the session cookie is <c>Secure</c> (BRIEF §14.2) and would not survive plain HTTP.
|
||||
/// the HTTPS listener, on the port clients reach it on (<see cref="ServerOptions.PublicHttpsPort"/> when a container
|
||||
/// port is published as another host port), because the session cookie is <c>Secure</c> (BRIEF §14.2) and would not
|
||||
/// survive plain HTTP.
|
||||
/// </summary>
|
||||
public static class AdminPanel
|
||||
{
|
||||
@@ -33,14 +36,22 @@ public static class AdminPanel
|
||||
});
|
||||
}
|
||||
|
||||
private static string? HttpsLocation(HttpContext context, ServerOptions options, IServer server)
|
||||
/// <summary>
|
||||
/// True when TLS mode is proxy but this request did not reach the server through a trusted proxy over HTTPS (BRIEF
|
||||
/// §14.2: the admin warns). The server has no HTTPS listener in that mode, so a request is HTTPS only when a proxy
|
||||
/// listed in <see cref="ServerOptions.TrustedProxies"/> forwarded it as such.
|
||||
/// </summary>
|
||||
public static bool BypassesProxy(HttpContext context, ServerOptions options) =>
|
||||
options.TlsMode == TlsMode.Proxy && !context.Request.IsHttps;
|
||||
|
||||
internal static string? HttpsLocation(HttpContext context, ServerOptions options, IServer server)
|
||||
{
|
||||
if (context.Request.IsHttps || options.TlsMode == TlsMode.Proxy) return null;
|
||||
if (context.Connection.RemoteIpAddress is { } remote && IPAddress.IsLoopback(remote)) return null;
|
||||
foreach (var address in server.Features.Get<IServerAddressesFeature>()?.Addresses ?? [])
|
||||
{
|
||||
if (!address.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) continue;
|
||||
var port = address[(address.LastIndexOf(':') + 1)..].TrimEnd('/');
|
||||
var port = options.PublicHttpsPort?.ToString(CultureInfo.InvariantCulture) ?? address[(address.LastIndexOf(':') + 1)..].TrimEnd('/');
|
||||
return $"https://{context.Request.Host.Host}:{port}{context.Request.PathBase}{context.Request.Path}{context.Request.QueryString}";
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -12,7 +12,8 @@ namespace Titano.Server.Hosting;
|
||||
/// <summary>
|
||||
/// LAN discovery (BRIEF §14.9, <c>TITANO_DISCOVERY</c>): every few seconds a <see cref="DiscoveryBeacon"/> is broadcast on
|
||||
/// UDP port <see cref="DiscoveryBeacon.Port"/> of every IPv4 network, so clients can offer this server on first run.
|
||||
/// Switching discovery off in the settings stops it without a restart.
|
||||
/// Switching discovery off in the settings stops it without a restart. In a container the broadcasts reach the LAN
|
||||
/// only with host networking; on a bridge network they stay on the bridge.
|
||||
/// </summary>
|
||||
public sealed class DiscoveryBeaconService(ServerOptions options, ServerState state, IServer server, ILogger<DiscoveryBeaconService> logger) : BackgroundService
|
||||
{
|
||||
@@ -51,12 +52,15 @@ public sealed class DiscoveryBeaconService(ServerOptions options, ServerState st
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The beacon for the ports Kestrel actually listens on (HTTPS 0 when TLS is terminated elsewhere).</summary>
|
||||
/// <summary>
|
||||
/// The beacon for the ports clients reach: the public ports when set, else the ports Kestrel actually listens on
|
||||
/// (HTTPS 0 when TLS is terminated elsewhere).
|
||||
/// </summary>
|
||||
public DiscoveryBeacon CreateBeacon() => new(
|
||||
options.ServerName,
|
||||
SystemEndpoints.Version,
|
||||
BoundPort(Uri.UriSchemeHttp) ?? options.HttpPort,
|
||||
state.TlsFingerprint is null ? 0 : BoundPort(Uri.UriSchemeHttps) ?? options.HttpsPort,
|
||||
options.PublicHttpPort ?? BoundPort(Uri.UriSchemeHttp) ?? options.HttpPort,
|
||||
state.TlsFingerprint is null ? 0 : options.PublicHttpsPort ?? BoundPort(Uri.UriSchemeHttps) ?? options.HttpsPort,
|
||||
state.TlsFingerprint);
|
||||
|
||||
public async Task SendOnceAsync(UdpClient udp, IEnumerable<IPEndPoint> targets, CancellationToken cancellationToken)
|
||||
|
||||
@@ -36,7 +36,16 @@ public sealed record ServerCommandLine(ServerCommand Command, IReadOnlyList<stri
|
||||
TITANO_ARCHIVE_PATH TITANO_DATA_PATH TITANO_SERVER_NAME TITANO_ADMIN_USER TITANO_ADMIN_PASSWORD
|
||||
TITANO_DB (postgres | sqlite | connection string) TITANO_DB_HOST TITANO_DB_PORT TITANO_DB_NAME TITANO_DB_USER
|
||||
TITANO_DB_PASSWORD TITANO_DB_SSLMODE TITANO_HTTP_PORT TITANO_HTTPS_PORT TITANO_TLS_MODE (auto | pem | proxy)
|
||||
TITANO_TLS_CERT TITANO_TLS_KEY TITANO_LANG TITANO_WORKERS TITANO_DISCOVERY
|
||||
TITANO_TLS_CERT TITANO_TLS_KEY TITANO_LANG TITANO_WORKERS
|
||||
TITANO_LOG_LEVEL (Trace | Debug | Information | Warning | Error)
|
||||
TITANO_DISCOVERY LAN beacon (true | false); in a container it needs host networking
|
||||
TITANO_PUBLIC_HTTP_PORT, TITANO_PUBLIC_HTTPS_PORT
|
||||
ports clients reach when the container's ports are published as other host ports
|
||||
(admin redirect to HTTPS, LAN beacon)
|
||||
TITANO_TRUSTED_PROXIES proxy mode: addresses or networks (CIDR) whose X-Forwarded-* headers count
|
||||
(default: loopback, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fc00::/7)
|
||||
|
||||
Container image only: PUID, PGID (user and group the server runs as, owners of /data), TZ (local time zone)
|
||||
""";
|
||||
|
||||
public static ServerCommandLine Parse(IReadOnlyList<string> args)
|
||||
|
||||
@@ -106,11 +106,15 @@ public static class ServerHost
|
||||
services.Configure<HostOptions>(o => o.ShutdownTimeout = TimeSpan.FromSeconds(30));
|
||||
if (options.TlsMode == TlsMode.Proxy)
|
||||
{
|
||||
// Only the configured proxies may say who the client is: a client that reaches the HTTP port directly would
|
||||
// otherwise pick its own address for the sign-in rate limit and the audit log, and claim HTTPS.
|
||||
var trusted = options.TrustedProxyNetworks();
|
||||
services.Configure<ForwardedHeadersOptions>(f =>
|
||||
{
|
||||
f.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost;
|
||||
f.KnownIPNetworks.Clear();
|
||||
f.KnownProxies.Clear();
|
||||
foreach (var network in trusted) f.KnownIPNetworks.Add(network);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user