diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/ServerAddress.cs b/src/Microsoft.AspNetCore.Server.Kestrel/ServerAddress.cs index 8f7b19c634..4dd7ffb0cf 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/ServerAddress.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/ServerAddress.cs @@ -141,6 +141,11 @@ namespace Microsoft.AspNetCore.Server.Kestrel serverAddress.Host = url.Substring(schemeDelimiterEnd, pathDelimiterStart - schemeDelimiterEnd); } + if (string.IsNullOrEmpty(serverAddress.Host)) + { + throw new FormatException($"Invalid URL: {url}"); + } + // Path should not end with a / since it will be used as PathBase later if (url[url.Length - 1] == '/') { diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/ServerAddressTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/ServerAddressTests.cs index 9e113d7978..f38ec1d367 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/ServerAddressTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/ServerAddressTests.cs @@ -14,13 +14,29 @@ namespace Microsoft.AspNetCore.Server.KestrelTests [InlineData("")] [InlineData("5000")] [InlineData("//noscheme")] - public void FromUriThrowsForSchemelessUrls(string url) + public void FromUriThrowsForUrlsWithoutSchemeDelimiter(string url) + { + Assert.Throws(() => ServerAddress.FromUrl(url)); + } + + [Theory] + [InlineData("://")] + [InlineData("://:5000")] + [InlineData("http://")] + [InlineData("http://:5000")] + [InlineData("http:///")] + [InlineData("http:///:5000")] + [InlineData("http:////")] + [InlineData("http:////:5000")] + public void FromUriThrowsForUrlsWithoutHost(string url) { Assert.Throws(() => ServerAddress.FromUrl(url)); } [Theory] [InlineData("://emptyscheme", "", "emptyscheme", 0, "", "://emptyscheme:0")] + [InlineData("http://+", "http", "+", 80, "", "http://+:80")] + [InlineData("http://*", "http", "*", 80, "", "http://*:80")] [InlineData("http://localhost", "http", "localhost", 80, "", "http://localhost:80")] [InlineData("http://www.example.com", "http", "www.example.com", 80, "", "http://www.example.com:80")] [InlineData("https://www.example.com", "https", "www.example.com", 443, "", "https://www.example.com:443")]