mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 15:42:48 -05:00
UseForwardedHeaders with known proxies
This commit is contained in:
@ -5,6 +5,7 @@ namespace Bit.Core
|
||||
public class GlobalSettings
|
||||
{
|
||||
public bool SelfHosted { get; set; }
|
||||
public virtual string KnownProxies { get; set; }
|
||||
public virtual string SiteName { get; set; }
|
||||
public virtual string StripeApiKey { get; set; }
|
||||
public virtual string ProjectName { get; set; }
|
||||
|
@ -27,6 +27,8 @@ using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using IdentityServer4.AccessTokenValidation;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using System.Linq;
|
||||
|
||||
namespace Bit.Core.Utilities
|
||||
{
|
||||
@ -390,5 +392,29 @@ namespace Bit.Core.Utilities
|
||||
await next.Invoke();
|
||||
});
|
||||
}
|
||||
|
||||
public static void UseForwardedHeaders(this IApplicationBuilder app, GlobalSettings globalSettings)
|
||||
{
|
||||
var options = new ForwardedHeadersOptions
|
||||
{
|
||||
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
||||
};
|
||||
if(!string.IsNullOrWhiteSpace(globalSettings.KnownProxies))
|
||||
{
|
||||
var proxies = globalSettings.KnownProxies.Split(',');
|
||||
foreach(var proxy in proxies)
|
||||
{
|
||||
if(System.Net.IPAddress.TryParse(proxy, out var ip))
|
||||
{
|
||||
options.KnownProxies.Add(ip);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(options.KnownProxies.Count > 1)
|
||||
{
|
||||
options.ForwardLimit = null;
|
||||
}
|
||||
app.UseForwardedHeaders(options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user