Skip to content

Add exception on failed port string parsing in BindingAddress.cs Resolves #24384 #63135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Http/Http/src/BindingAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,11 @@ public static BindingAddress Parse(string address)
var port = 0;

var hasSpecifiedPort = false;
if (!isUnixPipe)
if (!isUnixPipe && !isNamedPipe)
{
// Verify not a loopback uri.
var portDelimiterStart = address.LastIndexOf(':', pathDelimiterStart - 1, pathDelimiterStart - schemeDelimiterEnd);
if (portDelimiterStart >= 0)
if (portDelimiterStart >= 0 && address.Substring(pathDelimiterStart - 1, 1) != "]")
{
var portDelimiterEnd = portDelimiterStart + ":".Length;

Expand All @@ -218,6 +219,10 @@ public static BindingAddress Parse(string address)
host = address.Substring(schemeDelimiterEnd, portDelimiterStart - schemeDelimiterEnd);
port = portNumber;
}
else
{
throw new FormatException($"Invalid port: '{portString}', only numbers are allowed.");
}
}

if (!hasSpecifiedPort)
Expand Down
23 changes: 18 additions & 5 deletions src/Http/Http/test/BindingAddressTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ public void FromUriThrowsForUrlsWithoutHost(string url)
Assert.Throws<FormatException>(() => BindingAddress.Parse(url));
}

[Theory]
[InlineData("http://localhost:NOTAPORT")]
[InlineData("http://localhost:NOTAPORT/")]
[InlineData("http://localhost:NOTAPORT/random/url")]
[InlineData("https://localhost:NOTAPORT")]
[InlineData("https://localhost:NOTAPORT/")]
[InlineData("https://localhost:NOTAPORT/random/url")]
[InlineData("http://www.example.com:NOTAPORT")]
[InlineData("http://www.example.com:NOTAPORT/")]
[InlineData("http://www.example.com:NOTAPORT/foo?bar=baz")]
[InlineData("https://www.example.com:NOTAPORT")]
[InlineData("https://www.example.com:NOTAPORT/")]
[InlineData("https://www.example.com:NOTAPORT/foo?bar=baz")]
public void FromUriThrowsForUrlsWithInvalidPortNumbers(string url)
{
Assert.Throws<FormatException>(() => BindingAddress.Parse(url));
}

[ConditionalTheory]
[InlineData("http://unix:/")]
[InlineData("http://unix:/c")]
Expand All @@ -52,11 +70,6 @@ public void FromUriThrowsForUrlsWithWrongFilePathOnWindows(string url)
[InlineData("http://www.example.com:5000", "http", "www.example.com", 5000, "", null)]
[InlineData("https://www.example.com:5000", "https", "www.example.com", 5000, "", null)]
[InlineData("http://www.example.com:5000/", "http", "www.example.com", 5000, "", "http://www.example.com:5000")]
[InlineData("http://www.example.com:NOTAPORT", "http", "www.example.com:NOTAPORT", 80, "", "http://www.example.com:notaport:80")]
[InlineData("https://www.example.com:NOTAPORT", "https", "www.example.com:NOTAPORT", 443, "", "https://www.example.com:notaport:443")]
[InlineData("http://www.example.com:NOTAPORT/", "http", "www.example.com:NOTAPORT", 80, "", "http://www.example.com:notaport:80")]
[InlineData("http://foo:/tmp/kestrel-test.sock:5000/doesn't/matter", "http", "foo:", 80, "/tmp/kestrel-test.sock:5000/doesn't/matter", "http://foo::80/tmp/kestrel-test.sock:5000/doesn't/matter")]
[InlineData("http://unix:foo/tmp/kestrel-test.sock", "http", "unix:foo", 80, "/tmp/kestrel-test.sock", "http://unix:foo:80/tmp/kestrel-test.sock")]
[InlineData("http://unix:5000/tmp/kestrel-test.sock", "http", "unix", 5000, "/tmp/kestrel-test.sock", "http://unix:5000/tmp/kestrel-test.sock")]
public void UrlsAreParsedCorrectly(string url, string scheme, string host, int port, string pathBase, string toString)
{
Expand Down
Loading