// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1 using System.Net; using Microsoft.AspNetCore.HttpOverrides; namespace Progmasoft.Progmaweb.Api.Security; internal static class ForwardedHeadersPolicy { internal const int MaximumForwardedHops = 1; internal static readonly IPAddress[] TrustedProxies = [IPAddress.Loopback, IPAddress.IPv6Loopback]; public static void Configure(ForwardedHeadersOptions options) { options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedHost | ForwardedHeaders.XForwardedProto; options.ForwardLimit = MaximumForwardedHops; // The API is bound to loopback and only receives forwarded headers from the local Nginx/Next.js proxy. // Trusting every peer would let a direct caller forge the IP used by the authentication rate limiter. options.KnownIPNetworks.Clear(); options.KnownProxies.Clear(); foreach (IPAddress trustedProxy in TrustedProxies) { options.KnownProxies.Add(trustedProxy); } } }