Example #1
0
        public async Task XForwardedProtoOverrideChangesRequestProtocol()
        {
            var assertsExecuted = false;
            var options         = new OverrideHeaderMiddlewareOptions();

            options.ForwardedOptions = ForwardedHeaders.XForwardedProto;

            var server = TestServer.Create(app =>
            {
                app.UseOverrideHeaders(options);
                app.Run(context =>
                {
                    Assert.Equal("TestProtocol", context.Request.Scheme);
                    assertsExecuted = true;
                    return(Task.FromResult(0));
                });
            });

            var req = new HttpRequestMessage(HttpMethod.Get, "");

            req.Headers.Add("X-Forwarded-Proto", "TestProtocol");
            await server.CreateClient().SendAsync(req);

            Assert.True(assertsExecuted);
        }
Example #2
0
        public async Task PartiallyEnabledForwardsPartiallyChangesRequest()
        {
            var assertsExecuted       = false;
            var XForwardedForAndProto = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
            var options = new OverrideHeaderMiddlewareOptions();

            options.ForwardedOptions = XForwardedForAndProto;

            var server = TestServer.Create(app =>
            {
                app.UseOverrideHeaders(options);
                app.Run(context =>
                {
                    Assert.Equal("11.111.111.11", context.Connection.RemoteIpAddress.ToString());
                    Assert.Equal("localhost", context.Request.Host.ToString());
                    Assert.Equal("Protocol", context.Request.Scheme);
                    assertsExecuted = true;
                    return(Task.FromResult(0));
                });
            });

            var req = new HttpRequestMessage(HttpMethod.Get, "");

            req.Headers.Add("X-Forwarded-For", "11.111.111.11");
            req.Headers.Add("X-Forwarded-Proto", "Protocol");
            await server.CreateClient().SendAsync(req);

            Assert.True(assertsExecuted);
        }
Example #3
0
        public async Task XForwardedForOverrideBadIpDoesntChangeRemoteIp()
        {
            var assertsExecuted = false;
            var options         = new OverrideHeaderMiddlewareOptions();

            options.ForwardedOptions = ForwardedHeaders.XForwardedFor;

            var server = TestServer.Create(app =>
            {
                app.UseOverrideHeaders(options);
                app.Run(context =>
                {
                    Assert.Null(context.Connection.RemoteIpAddress);
                    assertsExecuted = true;
                    return(Task.FromResult(0));
                });
            });

            var req = new HttpRequestMessage(HttpMethod.Get, "");

            req.Headers.Add("X-Forwarded-For", "BAD-IP");
            await server.CreateClient().SendAsync(req);

            Assert.True(assertsExecuted);
        }
Example #4
0
        public async Task AllOptionsDisabledRequestDoesntChange()
        {
            var assertsExecuted = false;
            var options         = new OverrideHeaderMiddlewareOptions();

            options.ForwardedOptions = ForwardedHeaders.None;

            var server = TestServer.Create(app =>
            {
                app.UseOverrideHeaders(options);
                app.Run(context =>
                {
                    Assert.Null(context.Connection.RemoteIpAddress);
                    Assert.Equal("localhost", context.Request.Host.ToString());
                    Assert.Equal("http", context.Request.Scheme);
                    assertsExecuted = true;
                    return(Task.FromResult(0));
                });
            });

            var req = new HttpRequestMessage(HttpMethod.Get, "");

            req.Headers.Add("X-Forwarded-For", "11.111.111.11");
            req.Headers.Add("X-Forwarded-Host", "otherhost");
            req.Headers.Add("X-Forwarded-Proto", "Protocol");
            await server.CreateClient().SendAsync(req);

            Assert.True(assertsExecuted);
        }
Example #5
0
        public OverrideHeaderMiddleware(RequestDelegate next, OverrideHeaderMiddlewareOptions options)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _options = options;
            _next    = next;
        }
        public OverrideHeaderMiddleware(RequestDelegate next, OverrideHeaderMiddlewareOptions options)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _options = options;
            _next = next;
        }
        public async Task XForwardedHostOverrideChangesRequestHost()
        {
            var assertsExecuted = false;
            var options = new OverrideHeaderMiddlewareOptions();
            options.ForwardedOptions = ForwardedHeaders.XForwardedHost;

            var server = TestServer.Create(app =>
            {
                app.UseOverrideHeaders(options);
                app.Run(context =>
                {
                    Assert.Equal("testhost", context.Request.Host.ToString());
                    assertsExecuted = true;
                    return Task.FromResult(0);
                });
            });

            var req = new HttpRequestMessage(HttpMethod.Get, "");
            req.Headers.Add("X-Forwarded-Host", "testhost");
            await server.CreateClient().SendAsync(req);
            Assert.True(assertsExecuted);
        }
        public async Task XForwardedForOverrideBadIpDoesntChangeRemoteIp()
        {
            var assertsExecuted = false;
            var options = new OverrideHeaderMiddlewareOptions();
            options.ForwardedOptions = ForwardedHeaders.XForwardedFor;

            var server = TestServer.Create(app =>
            {
                app.UseOverrideHeaders(options);
                app.Run(context =>
                {
                    Assert.Null(context.Connection.RemoteIpAddress);
                    assertsExecuted = true;
                    return Task.FromResult(0);
                });
            });

            var req = new HttpRequestMessage(HttpMethod.Get, "");
            req.Headers.Add("X-Forwarded-For", "BAD-IP");
            await server.CreateClient().SendAsync(req);
            Assert.True(assertsExecuted);
        }
 /// <summary>
 /// Forwards proxied headers onto current request
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="options">Enables the different override options.</param>
 /// <returns></returns>
 public static IApplicationBuilder UseOverrideHeaders(this IApplicationBuilder builder, OverrideHeaderMiddlewareOptions options)
 {
     return builder.Use(next => new OverrideHeaderMiddleware(next, options).Invoke);
 }
Example #10
0
        public void AllForwardsDisabledByDefault()
        {
            var options = new OverrideHeaderMiddlewareOptions();

            Assert.True(options.ForwardedOptions == 0);
        }
        public async Task PartiallyEnabledForwardsPartiallyChangesRequest()
        {
            var assertsExecuted = false;
            var XForwardedForAndProto = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
            var options = new OverrideHeaderMiddlewareOptions();
            options.ForwardedOptions = XForwardedForAndProto;

            var server = TestServer.Create(app =>
            {
                app.UseOverrideHeaders(options);
                app.Run(context =>
                {
                    Assert.Equal("11.111.111.11", context.Connection.RemoteIpAddress.ToString());
                    Assert.Equal("localhost", context.Request.Host.ToString());
                    Assert.Equal("Protocol", context.Request.Scheme);
                    assertsExecuted = true;
                    return Task.FromResult(0);

                });
            });

            var req = new HttpRequestMessage(HttpMethod.Get, "");
            req.Headers.Add("X-Forwarded-For", "11.111.111.11");
            req.Headers.Add("X-Forwarded-Proto", "Protocol");
            await server.CreateClient().SendAsync(req);
            Assert.True(assertsExecuted);
        }
        public async Task AllOptionsDisabledRequestDoesntChange()
        {
            var assertsExecuted = false;
            var options = new OverrideHeaderMiddlewareOptions();
            options.ForwardedOptions = ForwardedHeaders.None;

            var server = TestServer.Create(app =>
            {
                app.UseOverrideHeaders(options);
                app.Run(context =>
                {
                    Assert.Null(context.Connection.RemoteIpAddress);
                    Assert.Equal("localhost", context.Request.Host.ToString());
                    Assert.Equal("http", context.Request.Scheme);
                    assertsExecuted = true;
                    return Task.FromResult(0);
                });
            });

            var req = new HttpRequestMessage(HttpMethod.Get, "");
            req.Headers.Add("X-Forwarded-For", "11.111.111.11");
            req.Headers.Add("X-Forwarded-Host", "otherhost");
            req.Headers.Add("X-Forwarded-Proto", "Protocol");
            await server.CreateClient().SendAsync(req);
            Assert.True(assertsExecuted);
        }
 public void AllForwardsDisabledByDefault()
 {
     var options = new OverrideHeaderMiddlewareOptions();
     Assert.True(options.ForwardedOptions == 0);
 }