|
2 | 2 | // The .NET Foundation licenses this file to you under the Apache 2.0 License. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
| 5 | +using System.Net; |
5 | 6 | using FluentAssertions.Extensions; |
| 7 | +using Microsoft.AspNetCore.Builder; |
| 8 | +using Microsoft.AspNetCore.Hosting; |
| 9 | +using Microsoft.AspNetCore.HttpOverrides; |
6 | 10 | using Microsoft.Extensions.Configuration; |
| 11 | +using Microsoft.Extensions.DependencyInjection; |
| 12 | +using Microsoft.Extensions.Options; |
7 | 13 | using Steeltoe.Common.TestResources; |
| 14 | +using IPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork; |
8 | 15 |
|
9 | 16 | namespace Steeltoe.Configuration.CloudFoundry.Test; |
10 | 17 |
|
@@ -216,6 +223,102 @@ public void Load_VCAP_APPLICATION_Allows_Reload_Without_Throwing_Exception() |
216 | 223 | Assert.Equal("fb8fbcc6-8d58-479e-bcc7-3b4ce5a7f0ca", options.Version); |
217 | 224 | } |
218 | 225 |
|
| 226 | + [Theory] |
| 227 | + [InlineData(true)] |
| 228 | + [InlineData(false)] |
| 229 | + public async Task ForwardedHeadersOptions_unrestricted_when_running_on_CloudFoundry(bool isRunningOnCloudFoundry) |
| 230 | + { |
| 231 | + using IDisposable? scope = isRunningOnCloudFoundry ? new EnvironmentVariableScope("VCAP_APPLICATION", "{}") : null; |
| 232 | + |
| 233 | + WebApplicationBuilder builder = TestWebApplicationBuilderFactory.CreateDefault(); |
| 234 | + builder.AddCloudFoundryConfiguration(); |
| 235 | + await using WebApplication host = builder.Build(); |
| 236 | + |
| 237 | + ForwardedHeadersOptions options = host.Services.GetRequiredService<IOptions<ForwardedHeadersOptions>>().Value; |
| 238 | + |
| 239 | + if (isRunningOnCloudFoundry) |
| 240 | + { |
| 241 | + options.ForwardedHeaders.Should().HaveFlag(ForwardedHeaders.XForwardedFor); |
| 242 | + options.ForwardedHeaders.Should().HaveFlag(ForwardedHeaders.XForwardedProto); |
| 243 | + options.KnownNetworks.Should().BeEmpty(); |
| 244 | + options.KnownProxies.Should().BeEmpty(); |
| 245 | + } |
| 246 | + else |
| 247 | + { |
| 248 | + options.ForwardedHeaders.Should().NotHaveFlag(ForwardedHeaders.XForwardedFor); |
| 249 | + options.ForwardedHeaders.Should().NotHaveFlag(ForwardedHeaders.XForwardedProto); |
| 250 | + options.KnownNetworks.Should().ContainSingle().Which.Should().BeEquivalentTo(IPNetwork.Parse("127.0.0.1/8")); |
| 251 | + options.KnownProxies.Should().ContainSingle().Which.Should().Be(IPAddress.Parse("::1")); |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + [Theory] |
| 256 | + [InlineData(true)] |
| 257 | + [InlineData(false)] |
| 258 | + public async Task ForwardedHeadersMiddleware_updates_connection_details_when_running_on_CloudFoundry(bool isRunningOnCloudFoundry) |
| 259 | + { |
| 260 | + using IDisposable? scope = isRunningOnCloudFoundry ? new EnvironmentVariableScope("VCAP_APPLICATION", "{}") : null; |
| 261 | + |
| 262 | + WebApplicationBuilder builder = WebApplication.CreateBuilder(); |
| 263 | + builder.WebHost.UseKestrel().UseUrls("http://127.0.0.1:0"); |
| 264 | + builder.AddCloudFoundryConfiguration(); |
| 265 | + await using WebApplication host = builder.Build(); |
| 266 | + bool? forwardedHeadersWereEvaluated = null; |
| 267 | + |
| 268 | + host.Map("/", context => |
| 269 | + { |
| 270 | + forwardedHeadersWereEvaluated = context.Request.IsHttps; |
| 271 | + return Task.CompletedTask; |
| 272 | + }); |
| 273 | + |
| 274 | + await host.StartAsync(TestContext.Current.CancellationToken); |
| 275 | + string address = host.Urls.First(url => url.StartsWith("http://", StringComparison.OrdinalIgnoreCase)); |
| 276 | + |
| 277 | + var client = new HttpClient(); |
| 278 | + client.DefaultRequestHeaders.Add("X-Forwarded-Proto", "https"); |
| 279 | + client.DefaultRequestHeaders.Add("X-Forwarded-For", "1.2.3.4"); |
| 280 | + |
| 281 | + HttpResponseMessage response = await client.GetAsync(new Uri(address), TestContext.Current.CancellationToken); |
| 282 | + response.StatusCode.Should().Be(HttpStatusCode.OK); |
| 283 | + |
| 284 | + forwardedHeadersWereEvaluated.Should() |
| 285 | + .Be(isRunningOnCloudFoundry, $"X-Forwarded-Proto should {(isRunningOnCloudFoundry ? string.Empty : "not ")}be evaluated"); |
| 286 | + |
| 287 | + await host.StopAsync(TestContext.Current.CancellationToken); |
| 288 | + } |
| 289 | + |
| 290 | + [Fact] |
| 291 | + public async Task ForwardedHeadersMiddleware_uses_customized_options_when_running_on_CloudFoundry() |
| 292 | + { |
| 293 | + using var vcapScope = new EnvironmentVariableScope("VCAP_APPLICATION", "{}"); |
| 294 | + WebApplicationBuilder builder = WebApplication.CreateBuilder(); |
| 295 | + builder.WebHost.UseKestrel().UseUrls("http://127.0.0.1:0"); |
| 296 | + builder.AddCloudFoundryConfiguration(); |
| 297 | + builder.Services.Configure<ForwardedHeadersOptions>(options => options.KnownProxies.Add(IPAddress.Parse("192.168.1.20"))); |
| 298 | + await using WebApplication host = builder.Build(); |
| 299 | + bool? forwardedHeadersWereEvaluated = null; |
| 300 | + |
| 301 | + host.Map("/", context => |
| 302 | + { |
| 303 | + forwardedHeadersWereEvaluated = context.Request.IsHttps; |
| 304 | + return Task.CompletedTask; |
| 305 | + }); |
| 306 | + |
| 307 | + await host.StartAsync(TestContext.Current.CancellationToken); |
| 308 | + string address = host.Urls.First(url => url.StartsWith("http://", StringComparison.OrdinalIgnoreCase)); |
| 309 | + |
| 310 | + var client = new HttpClient(); |
| 311 | + client.DefaultRequestHeaders.Add("X-Forwarded-Proto", "https"); |
| 312 | + client.DefaultRequestHeaders.Add("X-Forwarded-For", "1.2.3.4"); |
| 313 | + |
| 314 | + HttpResponseMessage response = await client.GetAsync(new Uri(address), TestContext.Current.CancellationToken); |
| 315 | + |
| 316 | + response.StatusCode.Should().Be(HttpStatusCode.OK); |
| 317 | + forwardedHeadersWereEvaluated.Should().BeFalse("X-Forwarded-Proto should not be evaluated for unknown proxies"); |
| 318 | + |
| 319 | + await host.StopAsync(TestContext.Current.CancellationToken); |
| 320 | + } |
| 321 | + |
219 | 322 | private sealed class VcapApp |
220 | 323 | { |
221 | 324 | #pragma warning disable S3459 // Unassigned members should be removed |
|
0 commit comments