Skip to content

support ForwardedHeaders configuration #61419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/DefaultBuilder/src/ForwardedHeadersOptionsSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,24 @@ public void Configure(ForwardedHeadersOptions options)
return;
}

options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
var forwardedHeaders = _configuration["ForwardedHeaders_Headers"];
if (string.IsNullOrEmpty(forwardedHeaders))
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
}
else
{
var headers = ForwardedHeaders.None;
foreach (var headerName in forwardedHeaders.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
{
if (Enum.TryParse<ForwardedHeaders>(headerName, true, out var headerValue))
{
headers |= headerValue;
}
}
options.ForwardedHeaders = headers;
}

// Only loopback proxies are allowed by default. Clear that restriction because forwarders are
// being enabled by explicit configuration.
options.KnownNetworks.Clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,38 @@ public async Task WebHostConfiguration_EnablesForwardedHeadersFromConfig()
result.EnsureSuccessStatusCode();
}

[Fact]
public async Task WebHostConfiguration_EnablesForwardedHeaders_CustomHeaders_FromConfig()
{
using var host = WebHost.CreateDefaultBuilder()
.ConfigureAppConfiguration(configBuilder =>
{
configBuilder.AddInMemoryCollection(new[]
{
new KeyValuePair<string, string>("FORWARDEDHEADERS_ENABLED", "true" ),
new KeyValuePair<string, string>("FORWARDEDHEADERS_HEADERS", "All" ),
});
})
.UseTestServer()
.Configure(app =>
{
Assert.True(app.Properties.ContainsKey("ForwardedHeadersAdded"), "Forwarded Headers");
app.Run(context =>
{
Assert.Equal("https", context.Request.Scheme);
Assert.Equal("/test", context.Request.PathBase.Value);
return Task.CompletedTask;
});
}).Build();

await host.StartAsync();
var client = host.GetTestClient();
client.DefaultRequestHeaders.Add("x-forwarded-proto", "https");
client.DefaultRequestHeaders.Add("x-forwarded-prefix", "/test");
var result = await client.GetAsync("http://localhost/");
result.EnsureSuccessStatusCode();
}

[Fact]
public void CreateDefaultBuilder_RegistersRouting()
{
Expand Down
Loading