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:
Claude committed 2026-10-12 09:27:31 +00:00
1 parent 047cddfe7b
commit 5ec59a2e96
17 files changed
+340 -22

No files matched your search

@@ -1,4 +1,5 @@
using System.Globalization;
using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;
using Titano.Data;
@@ -45,15 +46,27 @@ public sealed class ServerOptions
("TITANO_DB_SSLMODE", nameof(DbSslMode), (o, v) => o.DbSslMode = v),
("TITANO_HTTP_PORT", nameof(HttpPort), (o, v) => o.HttpPort = Int("TITANO_HTTP_PORT", v)),
("TITANO_HTTPS_PORT", nameof(HttpsPort), (o, v) => o.HttpsPort = Int("TITANO_HTTPS_PORT", v)),
("TITANO_PUBLIC_HTTP_PORT", nameof(PublicHttpPort), (o, v) => o.PublicHttpPort = Int("TITANO_PUBLIC_HTTP_PORT", v)),
("TITANO_PUBLIC_HTTPS_PORT", nameof(PublicHttpsPort), (o, v) => o.PublicHttpsPort = Int("TITANO_PUBLIC_HTTPS_PORT", v)),
("TITANO_TLS_MODE", nameof(TlsMode), (o, v) => o.TlsMode = Enum.Parse<TlsMode>(v, ignoreCase: true)),
("TITANO_TLS_CERT", nameof(TlsCert), (o, v) => o.TlsCert = v),
("TITANO_TLS_KEY", nameof(TlsKey), (o, v) => o.TlsKey = v),
("TITANO_TRUSTED_PROXIES", nameof(TrustedProxies), (o, v) => o.TrustedProxies = v),
("TITANO_LANG", nameof(Language), (o, v) => o.Language = v),
("TITANO_WORKERS", nameof(Workers), (o, v) => o.Workers = Math.Max(1, Int("TITANO_WORKERS", v))),
("TITANO_DISCOVERY", nameof(Discovery), (o, v) => o.Discovery = bool.Parse(v)),
("TITANO_LOG_LEVEL", nameof(LogLevel), (o, v) => o.LogLevel = Enum.Parse<LogLevel>(v, ignoreCase: true)),
];
/// <summary>
/// Reverse proxies whose <c>X-Forwarded-*</c> headers count in TLS mode proxy: loopback and the private networks a
/// compose or Unraid proxy container connects from (RFC 1918, IPv6 unique local).
/// </summary>
public const string DefaultTrustedProxies = "127.0.0.0/8, ::1/128, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fc00::/7";
/// <summary>The environment variables the server reads (<c>--help</c> lists them all).</summary>
public static IEnumerable<string> EnvironmentVariables => Variables.Select(v => v.Variable).Prepend("TITANO_DATA_PATH");
public string ServerName { get; set; } = "Titano Server";
public string ArchivePath { get; set; } = OperatingSystem.IsWindows() ? @"C:\TitanoArchive" : "/archive";
public string DataPath { get; set; } = OperatingSystem.IsWindows() ? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Titano Server") : "/data";
@@ -72,9 +85,26 @@ public sealed class ServerOptions
public string DbSslMode { get; set; } = "prefer";
public int HttpPort { get; set; } = 8080;
public int HttpsPort { get; set; } = 8443;
/// <summary>
/// Port clients reach the HTTP listener on, when it differs from <see cref="HttpPort"/> (a container port published
/// as another host port); advertised by LAN discovery. Null: <see cref="HttpPort"/>.
/// </summary>
public int? PublicHttpPort { get; set; }
/// <summary>Port clients reach the HTTPS listener on (admin redirect, LAN discovery). Null: <see cref="HttpsPort"/>.</summary>
public int? PublicHttpsPort { get; set; }
public TlsMode TlsMode { get; set; } = TlsMode.Auto;
public string? TlsCert { get; set; }
public string? TlsKey { get; set; }
/// <summary>
/// Addresses or networks (CIDR), separated by commas or spaces, of the reverse proxies trusted in TLS mode proxy.
/// Forwarded headers from any other peer are ignored, so a client that reaches the HTTP port directly cannot choose
/// the address the sign-in rate limit and the audit log see.
/// </summary>
public string TrustedProxies { get; set; } = DefaultTrustedProxies;
public string Language { get; set; } = "en";
public int Workers { get; set; } = Math.Max(1, Environment.ProcessorCount / 2);
public bool Discovery { get; set; } = true;
@@ -192,12 +222,38 @@ public sealed class ServerOptions
// Port 0 asks the OS for a free port (used by the in-process test host).
if (HttpPort is < 0 or > 65535 || HttpsPort is < 0 or > 65535)
throw new InvalidOperationException("Ports must be between 0 and 65535.");
if (PublicHttpPort is < 1 or > 65535 || PublicHttpsPort is < 1 or > 65535)
throw new InvalidOperationException("TITANO_PUBLIC_HTTP_PORT and TITANO_PUBLIC_HTTPS_PORT must be between 1 and 65535.");
try
{
TrustedProxyNetworks();
}
catch (FormatException ex)
{
throw new InvalidOperationException($"TITANO_TRUSTED_PROXIES must list IP addresses or networks such as 172.16.0.0/12: {ex.Message}", ex);
}
if (ChunkSizeBytes is < 64 * 1024 or > 64 * 1024 * 1024)
throw new InvalidOperationException("The chunk size must be between 64 KiB and 64 MiB.");
if (MaxUploadBytes < 1 || TokenLifetimeDays < 1 || JobHistoryDays < 1 || UploadRetentionHours < 1)
throw new InvalidOperationException("Upload size, token lifetime, job history and upload retention must be positive.");
}
/// <summary>The parsed <see cref="TrustedProxies"/>; a single address is a network of one.</summary>
/// <exception cref="FormatException">An entry is neither an IP address nor a network in CIDR notation.</exception>
public IReadOnlyList<IPNetwork> TrustedProxyNetworks()
{
var networks = new List<IPNetwork>();
foreach (var entry in TrustedProxies.Split([',', ';', ' ', '\t'], StringSplitOptions.RemoveEmptyEntries))
{
if (IPNetwork.TryParse(entry, out var network)) networks.Add(network);
else if (!entry.Contains('/', StringComparison.Ordinal) && IPAddress.TryParse(entry, out var address)
&& (address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6 || address.ScopeId == 0))
networks.Add(new IPNetwork(address, address.GetAddressBytes().Length * 8));
else throw new FormatException($"'{entry}' is neither an address nor a network.");
}
return networks;
}
/// <summary>TITANO_DB is <c>postgres</c>, <c>sqlite</c> or a complete connection string (<c>Host=…</c> / <c>Data Source=…</c>).</summary>
private void ApplyDatabase(string value)
{