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

Commit

Permalink
Throw if UseUrls specifies HTTPS or path base (#1519).
Browse files Browse the repository at this point in the history
  • Loading branch information
Cesar Blum Silveira authored Mar 30, 2017
1 parent e64bffd commit 1be31ae
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 65 deletions.
10 changes: 10 additions & 0 deletions src/Microsoft.AspNetCore.Server.Kestrel.Core/KestrelServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ public void Start<TContext>(IHttpApplication<TContext> application)
{
var parsedAddress = ServerAddress.FromUrl(address);

if (parsedAddress.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException($"HTTPS endpoints can only be configured using {nameof(KestrelServerOptions)}.{nameof(KestrelServerOptions.Listen)}().");
}

if (!string.IsNullOrEmpty(parsedAddress.PathBase))
{
throw new InvalidOperationException($"A path base can only be configured using {nameof(IApplicationBuilder)}.UsePathBase().");
}

if (!string.IsNullOrEmpty(parsedAddress.PathBase))
{
_logger.LogWarning($"Path base in address {address} is not supported and will be ignored. To specify a path base, use {nameof(IApplicationBuilder)}.UsePathBase().");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,6 @@ public static TheoryData<string, Func<IServerAddressesFeature, string[]>> Addres
dataset.Add($"http://*:{port}/", _ => new[] { $"http://127.0.0.1:{port}/" });
dataset.Add($"http://+:{port}/", _ => new[] { $"http://127.0.0.1:{port}/" });

// Path after port
dataset.Add($"http://127.0.0.1:{port}/base/path", _ => new[] { $"http://127.0.0.1:{port}/base/path" });

// Dynamic port and non-loopback addresses
dataset.Add("http://127.0.0.1:0/", GetTestUrls);
dataset.Add($"http://{Dns.GetHostName()}:0/", GetTestUrls);
Expand Down Expand Up @@ -411,10 +408,6 @@ public static TheoryData<string, Func<IServerAddressesFeature, string[]>> Addres
dataset.Add($"http://127.0.0.1:{port}/;http://[::1]:{port}/",
_ => new[] { $"http://127.0.0.1:{port}/", $"http://[::1]:{port}/" });

// Path after port
dataset.Add($"http://[::1]:{port}/base/path",
_ => new[] { $"http://[::1]:{port}/base/path" });

// Dynamic port and non-loopback addresses
var ipv6Addresses = GetIPAddresses()
.Where(ip => ip.AddressFamily == AddressFamily.InterNetworkV6)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,64 +566,6 @@ await connection.Receive($"HTTP/1.1 200 OK",
}
}

[Theory]
[InlineData("/base", "/base")]
[InlineData("/base", "/base/")]
[InlineData("/base", "/base/something")]
[InlineData("/base", "/base/something/")]
[InlineData("/base/something", "/base/something")]
[InlineData("/base/something", "/base/something/")]
[InlineData("/base/something", "/base/something/more")]
[InlineData("/base/something", "/base/something/more/")]
public async Task DoesNotSplitPathBase(string registerPathBase, string requestPath)
{
var testLogger = new TestApplicationErrorLogger();

string contextPathBase = null;
string contextPath = null;

var builder = new WebHostBuilder()
.UseKestrel()
.UseUrls($"http://127.0.0.1:0{registerPathBase}")
.ConfigureServices(services =>
{
services.AddSingleton<ILoggerFactory>(new KestrelTestLoggerFactory(testLogger));
})
.Configure(app =>
{
app.Run(context =>
{
contextPathBase = context.Request.PathBase;
contextPath = context.Request.Path;

return TaskCache.CompletedTask;
});
});

using (var host = builder.Build())
{
host.Start();

using (var connection = new TestConnection(host.GetPort()))
{
await connection.Send($"GET {requestPath} HTTP/1.1\r\n\r\n");
await connection.Receive("HTTP/1.1 200 OK");
}

Assert.Single(testLogger.Messages, log =>
log.LogLevel == LogLevel.Warning &&
string.Equals(
$"Path base in address http://127.0.0.1:0{registerPathBase} is not supported and will be ignored. To specify a path base, use {nameof(IApplicationBuilder)}.UsePathBase().",
log.Message,
StringComparison.Ordinal));
}

Assert.Equal("", contextPathBase);
Assert.Equal(requestPath, contextPath);


}

private async Task TestRemoteIPAddress(string registerAddress, string requestAddress, string expectAddress)
{
var builder = new WebHostBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.Extensions.Options;
using Moq;
using Xunit;
using Microsoft.AspNetCore.Builder;

namespace Microsoft.AspNetCore.Server.KestrelTests
{
Expand All @@ -35,6 +36,42 @@ public void StartWithInvalidAddressThrows()
}
}

[Fact]
public void StartWithHttpsAddressThrows()
{
var testLogger = new TestApplicationErrorLogger { ThrowOnCriticalErrors = false };

using (var server = CreateServer(new KestrelServerOptions(), testLogger))
{
server.Features.Get<IServerAddressesFeature>().Addresses.Add("https://127.0.0.1:0");

var exception = Assert.Throws<InvalidOperationException>(() => StartDummyApplication(server));

Assert.Equal(
$"HTTPS endpoints can only be configured using {nameof(KestrelServerOptions)}.{nameof(KestrelServerOptions.Listen)}().",
exception.Message);
Assert.Equal(1, testLogger.CriticalErrorsLogged);
}
}

[Fact]
public void StartWithPathBaseInAddressThrows()
{
var testLogger = new TestApplicationErrorLogger { ThrowOnCriticalErrors = false };

using (var server = CreateServer(new KestrelServerOptions(), testLogger))
{
server.Features.Get<IServerAddressesFeature>().Addresses.Add("http://127.0.0.1:0/base");

var exception = Assert.Throws<InvalidOperationException>(() => StartDummyApplication(server));

Assert.Equal(
$"A path base can only be configured using {nameof(IApplicationBuilder)}.UsePathBase().",
exception.Message);
Assert.Equal(1, testLogger.CriticalErrorsLogged);
}
}

[Theory]
[InlineData("http://localhost:5000")]
[InlineData("The value of the string shouldn't matter.")]
Expand Down

0 comments on commit 1be31ae

Please sign in to comment.