/// <summary>
        ///     Copies X-Forwarded-For, X-Forwarded-Host, X-Forwarded-Proto and
        ///     X-Forwarded-PathBase headers to the forward request context from
        ///     the incoming request. This should only be performed if this. If
        ///     the headers already exist, they will be appended.
        /// </summary>
        /// <param name="forwardContext">The forward context.</param>
        /// <returns>The forward context.</returns>
        public static ForwardContext CopyXForwardedHeaders(this ForwardContext forwardContext)
        {
            var headers = forwardContext.UpstreamRequest.Headers;

            if (forwardContext.HttpContext.Request.Headers.TryGetValue(XForwardedExtensions.XForwardedFor, out var forValues))
            {
                headers.Remove(XForwardedExtensions.XForwardedFor);
                headers.TryAddWithoutValidation(XForwardedExtensions.XForwardedFor, forValues.ToArray());
            }

            if (forwardContext.HttpContext.Request.Headers.TryGetValue(XForwardedExtensions.XForwardedHost, out var hostValues))
            {
                headers.Remove(XForwardedExtensions.XForwardedHost);
                headers.TryAddWithoutValidation(XForwardedExtensions.XForwardedHost, hostValues.ToArray());
            }

            if (forwardContext.HttpContext.Request.Headers.TryGetValue(XForwardedExtensions.XForwardedProto, out var protoValues))
            {
                headers.Remove(XForwardedExtensions.XForwardedProto);
                headers.TryAddWithoutValidation(XForwardedExtensions.XForwardedProto, protoValues.ToArray());
            }

            if (forwardContext.HttpContext.Request.Headers.TryGetValue(XForwardedExtensions.XForwardedPathBase, out var pathBaseValues))
            {
                headers.Remove(XForwardedExtensions.XForwardedPathBase);
                headers.TryAddWithoutValidation(XForwardedExtensions.XForwardedPathBase, pathBaseValues.ToArray());
            }

            return(forwardContext);
        }
Exemple #2
0
        public async Task X_Forwarded_Headers_should_be_removed_by_default()
        {
            ForwardContext forwardContext = null;

            _builder.Configure(app => app.RunProxy(
                                   context => context
                                   .ForwardTo("http://localhost:5000/bar/")
                                   .Send()));
            var server = new TestServer(_builder);
            var client = server.CreateClient();

            var requestMessage = new HttpRequestMessage(HttpMethod.Get, "http://mydomain.example")
            {
                Content = new StringContent("Request Body")
            };

            requestMessage.Headers.TryAddWithoutValidation(XForwardedExtensions.XForwardedFor, "127.0.0.1");
            requestMessage.Headers.TryAddWithoutValidation(XForwardedExtensions.XForwardedProto, "http");
            requestMessage.Headers.TryAddWithoutValidation(XForwardedExtensions.XForwardedHost, "localhost");
            requestMessage.Headers.TryAddWithoutValidation(XForwardedExtensions.XForwardedPathBase, "bar");
            await client.SendAsync(requestMessage);

            var sentRequest = _testMessageHandler.SentRequestMessages.Single();

            sentRequest.Headers.Contains(XForwardedExtensions.XForwardedHost).ShouldBeFalse();
            sentRequest.Headers.Contains(XForwardedExtensions.XForwardedProto).ShouldBeFalse();
            sentRequest.Headers.Contains(XForwardedExtensions.XForwardedFor).ShouldBeFalse();
            sentRequest.Headers.Contains(XForwardedExtensions.XForwardedPathBase).ShouldBeFalse();
        }
        public static ForwardContext ApplyXForwardedHeaders(this ForwardContext forwardContext)
        {
            var headers    = forwardContext.UpstreamRequest.Headers;
            var protocol   = forwardContext.HttpContext.Request.Scheme;
            var @for       = forwardContext.HttpContext.Connection.RemoteIpAddress;
            var host       = forwardContext.HttpContext.Request.Headers["Host"];
            var hostString = HostString.FromUriComponent(host);
            var pathBase   = forwardContext.HttpContext.Request.PathBase.Value;

            headers.ApplyXForwardedHeaders(@for, hostString, protocol, pathBase);

            return(forwardContext);
        }
 public static ForwardContext ApplyXForwardedHeaders(this ForwardContext forwardContext) =>
 forwardContext.AddXForwardedHeaders();