Skip to content
This repository has been archived by the owner on Dec 20, 2018. It is now read-only.

OverrideHeaderMiddleware doesn't handle X-Forwarded-For with ports #19

Closed
Tratcher opened this issue Dec 3, 2015 · 0 comments
Closed
Assignees
Milestone

Comments

@Tratcher
Copy link
Member

Tratcher commented Dec 3, 2015

Code from IISPlatformHandler that should be moved to OverrideHeaderMiddleware to correctly handle ports.

        private static void UpdateRemoteIp(HttpContext httpContext)
        {
            var xForwardedForHeaderValue = httpContext.Request.Headers.GetCommaSeparatedValues(XForwardedForHeaderName);
            if (xForwardedForHeaderValue != null && xForwardedForHeaderValue.Length > 0)
            {
                IPAddress ipFromHeader;
                int? port;
                if (IPAddressWithPortParser.TryParse(xForwardedForHeaderValue[0], out ipFromHeader, out port))
                {
                    var connection = httpContext.Connection;
                    var remoteIPString = connection.RemoteIpAddress?.ToString();
                    if (!string.IsNullOrEmpty(remoteIPString))
                    {
                        httpContext.Request.Headers[XOriginalIPName] = remoteIPString;
                    }
                    if (port.HasValue)
                    {
                        if (connection.RemotePort != 0)
                        {
                            httpContext.Request.Headers[XOriginalPortName] = connection.RemotePort.ToString(CultureInfo.InvariantCulture);
                        }
                        connection.RemotePort = port.Value;
                    }
                    connection.RemoteIpAddress = ipFromHeader;
                }
            }
        }
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Net;
using Microsoft.AspNet.IISPlatformHandler;
using Xunit;

namespace Microsoft.AspNet.PipelineHandler.Tests
{
    public class IPAddressWithPortParserTests
    {
        [Theory]
        [InlineData("127.0.0.1", "127.0.0.1", null)]
        [InlineData("127.0.0.1:1", "127.0.0.1", 1)]
        [InlineData("1", "0.0.0.1", null)]
        [InlineData("1:1", "0.0.0.1", 1)]
        [InlineData("::1", "::1", null)]
        [InlineData("[::1]", "::1", null)]
        [InlineData("[::1]:1", "::1", 1)]
        public void ParsesCorrectly(string input, string expectedAddress, int? expectedPort)
        {
            IPAddress address;
            int? port;
            var success = IPAddressWithPortParser.TryParse(input, out address, out port);
            Assert.True(success);
            Assert.Equal(expectedAddress, address?.ToString());
            Assert.Equal(expectedPort, port);
        }

        [InlineData("[::1]:")]
        [InlineData("[::1:")]
        [InlineData("::1:")]
        [InlineData("127:")]
        [InlineData("127.0.0.1:")]
        [InlineData("")]
        [InlineData("[]")]
        [InlineData("]")]
        [InlineData("]:1")]
        public void ShouldNotParse(string input)
        {
            IPAddress address;
            int? port;
            var success = IPAddressWithPortParser.TryParse(input, out address, out port);
            Assert.False(success);
            Assert.Equal(null, address);
            Assert.Equal(null, port);
        }
    }
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants