Skip to content

Commit

Permalink
fix(aspnetcore): wrong ASPNETCORE_URLS lead to wrong ip
Browse files Browse the repository at this point in the history
  • Loading branch information
catcherwong committed Aug 7, 2022
1 parent 9597bec commit 22537b7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/Nacos.AspNetCore/UriTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,19 @@ public static IEnumerable<Uri> GetUri(IFeatureCollection features, string ip, in
address = Environment.GetEnvironmentVariable("ASPNETCORE_URLS");
if (!string.IsNullOrWhiteSpace(address))
{
var url = ReplaceAddress(address, preferredNetworks);
var url = ReplaceAddress(address, preferredNetworks);

var uris = url.Split(splitChars).Select(x => new Uri(x));

foreach (var item in uris)
{
if (!IPAddress.TryParse(item.Host, out _))
{
throw new Nacos.V2.Exceptions.NacosException("Invalid ip address from ASPNETCORE_URLS");
}
}

return url.Split(splitChars).Select(x => new Uri(x));
return uris;
}

// 4. --urls
Expand All @@ -74,7 +84,17 @@ public static IEnumerable<Uri> GetUri(IFeatureCollection features, string ip, in

var url = ReplaceAddress(address, preferredNetworks);

return url.Split(splitChars).Select(x => new Uri(x));
var uris = url.Split(splitChars).Select(x => new Uri(x));

foreach (var item in uris)
{
if (!IPAddress.TryParse(item.Host, out _))
{
throw new Nacos.V2.Exceptions.NacosException("Invalid ip address from --urls");
}
}

return uris;
}
}

Expand Down
23 changes: 23 additions & 0 deletions tests/Nacos.AspNetCore.Tests/UriToolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,28 @@ public void GetUri_Should_Return_Multi_Uris()

Assert.Equal(2, uris.Count());
}

[Theory]
[InlineData("http://+80")]
[InlineData("http://*80")]
public void GetUrl_With_ASPNETCORE_URLS_Should_ThrowExceptions(string url)
{
System.Environment.SetEnvironmentVariable("ASPNETCORE_URLS", url);
var ex = Assert.Throws<Nacos.V2.Exceptions.NacosException>(() => UriTool.GetUri(null, "", 0, ""));
Assert.Equal("Invalid ip address from ASPNETCORE_URLS", ex.ErrorMsg);
}

[Theory]
[InlineData("http://+:80")]
[InlineData("http://*:80")]
public void GetUrl_With_ASPNETCORE_URLS_Should_Succeed(string url)
{
System.Environment.SetEnvironmentVariable("ASPNETCORE_URLS", url);
var uris = UriTool.GetUri(null, "", 0, "");
Assert.Single(uris);
var uri = uris.First();
Assert.True(System.Net.IPAddress.TryParse(uri.Host, out _));
Assert.Equal(80, uri.Port);
}
}
}

0 comments on commit 22537b7

Please sign in to comment.