Assert min and maxrange of port
MODIFY
WalletWasabi.Tests/ParserTests.cs
WalletWasabi.Tests/ParserTests.cs
@@ -51,6 +51,19 @@ namespace WalletWasabi.Tests
$" {host}/ : 5000/ "
};
+ var inputsWithInvalidPorts = new[]
+ {
+ $"{host}:-1",
+ $"{host}:999999999999999999999",
+ $"{host}:-999999999999999999999",
+ $"{host}:{int.MaxValue}",
+ $"{host}:{uint.MaxValue}",
+ $"{host}:{long.MaxValue}",
+ $"{host}:0.1",
+ $"{host}:{int.MinValue}",
+ $"{host}:{long.MinValue}",
+ };
+
// Default port is used.
foreach (var inputString in inputsWithoutPorts)
{
@@ -77,6 +90,18 @@ namespace WalletWasabi.Tests
{
Assert.False(EndPointParser.TryParse(inputString, -1, out EndPoint ep));
}
+
+ // Defaultport corrects invalid inputs.
+ foreach (var inputString in inputsWithInvalidPorts)
+ {
+ Assert.True(EndPointParser.TryParse(inputString, 5000, out EndPoint ep));
+ }
+
+ // Invalid ports -1 default.
+ foreach (var inputString in inputsWithInvalidPorts)
+ {
+ Assert.False(EndPointParser.TryParse(inputString, -1, out EndPoint ep));
+ }
}
private static void AssertEndPointParserOutputs(bool isSuccess, EndPoint endPoint, string expectedHost, int expectedPort)

MODIFY
WalletWasabi/Helpers/EndPointParser.cs
WalletWasabi/Helpers/EndPointParser.cs
@@ -92,9 +92,23 @@ namespace System.Net
string host = parts[0];
if (host == "localhost")
{
- endPoint = new IPEndPoint(IPAddress.Loopback, port);
+ host = IPAddress.Loopback.ToString();
}
- else if (IPAddress.TryParse(host, out IPAddress addr))
+
+ bool isPortInValidRange = port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort;
+ if (isPortInValidRange)
+ {
+ if (defaultPort == -1)
+ {
+ return false;
+ }
+ else
+ {
+ port = defaultPort;
+ }
+ }
+
+ if (IPAddress.TryParse(host, out IPAddress addr))
{
endPoint = new IPEndPoint(addr, port);
}

Assert min and maxrange of port