Skip to content
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

Updated Dashboard to accept/map forwarded headers #6969

Open
wants to merge 6 commits 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
9 changes: 9 additions & 0 deletions src/Aspire.Dashboard/DashboardWebApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization.Policy;
using Microsoft.AspNetCore.Cors.Infrastructure;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Server.Kestrel;
using Microsoft.AspNetCore.Server.Kestrel.Core;
Expand Down Expand Up @@ -267,6 +268,14 @@ public DashboardWebApplication(
options.Cookie.Name = DashboardAntiForgeryCookieName;
});

if (builder.Configuration.GetBool("ASPNETCORE_FORWARDEDHEADERS_ENABLED") ?? false)
{
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedHost | ForwardedHeaders.XForwardedProto;
});
}

_app = builder.Build();

_dashboardOptionsMonitor = _app.Services.GetRequiredService<IOptionsMonitor<DashboardOptions>>();
Expand Down
20 changes: 20 additions & 0 deletions tests/Aspire.Dashboard.Tests/Integration/StartupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Aspire.Hosting;
using Google.Protobuf;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.InternalTesting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -713,6 +714,25 @@ public async Task Configuration_CorsNoOtlpHttpEndpoint_Error()
s => Assert.Contains(DashboardConfigNames.DashboardOtlpHttpUrlName.ConfigKey, s));
}

[Fact]
public async Task Configuration_ForwardedHeaders_OverrideDefaults()
{
// Arrange & Act
await using var app = IntegrationTestHelpers.CreateDashboardWebApplication(testOutputHelper,
additionalConfiguration: data =>
{
data["ASPNETCORE_FORWARDEDHEADERS_ENABLED"] = "true";
},
clearLogFilterRules: false);

// Assert
await app.StartAsync().DefaultTimeout();

var options = app.Services.GetRequiredService<IOptions<ForwardedHeadersOptions>>();

Assert.Equal(ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost, options.Value.ForwardedHeaders);
}

private static void AssertIPv4OrIPv6Endpoint(Func<EndpointInfo> endPointAccessor)
{
// Check that the address is IPv4 or IPv6 any.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;

namespace Aspire.Dashboard.Tests.Middleware;

public class ForwardedHeadersMiddlewareTests
{
[Fact]
public async Task Validate_ForwardedHeaders_MapToHttpRequest()
{
using var host = await SetUpHostAsync(true);
var client = host.GetTestClient();
client.DefaultRequestHeaders.Add("X-Forwarded-Host", "example.com");
client.DefaultRequestHeaders.Add("X-Forwarded-Proto", "https");

var response = await client.GetAsync("/");
var responseContent = await response.Content.ReadAsStringAsync();

Assert.NotNull(responseContent);
Assert.Equal("https://example.com", responseContent);
}

// Create a test that doesn't use the ForwardedHeaders middleware to ensure that the test fails.
[Fact]
public async Task Validate_ForwardedHeaders_NotMapToHttpRequest()
{
using var host = await SetUpHostAsync();
var client = host.GetTestClient();
client.DefaultRequestHeaders.Add("X-Forwarded-Host", "example.com");
client.DefaultRequestHeaders.Add("X-Forwarded-Proto", "https");
var response = await client.GetAsync("/");
var responseContent = await response.Content.ReadAsStringAsync();

Assert.NotNull(responseContent);
Assert.Equal("http://localhost", responseContent);
}

private static async Task<IHost> SetUpHostAsync(bool mapForwardedHeaders = false)
{
return await new HostBuilder()
.ConfigureWebHost(webBuilder =>
{
webBuilder
.UseTestServer()
.ConfigureServices(services =>
{
services.AddRouting();
if (mapForwardedHeaders)
{
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedHost | ForwardedHeaders.XForwardedProto;
});
}
})
.Configure(app =>
{
app.UseRouting();
if (mapForwardedHeaders)
{
app.UseForwardedHeaders();
}

app.Run(async context =>
{
await context.Response.WriteAsync($"{context.Request.Scheme}://{context.Request.Host}");
});
});
})
.StartAsync();
}
}
Loading