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 committed Mar 22, 2017
1 parent fffb823 commit 1b93bcb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 60 deletions.
7 changes: 6 additions & 1 deletion src/Microsoft.AspNetCore.Server.Kestrel/KestrelServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,14 @@ public void Start<TContext>(IHttpApplication<TContext> application)
{
var parsedAddress = ServerAddress.FromUrl(address);

if (parsedAddress.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException($"HTTPS addresses are not supported. Use {nameof(KestrelServerOptions)}.{nameof(KestrelServerOptions.Listen)}() to configure an HTTPS endpoint.");
}

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().");
throw new InvalidOperationException($"Addresses containing a path base are not supported. Use {nameof(IApplicationBuilder)}.UsePathBase() to configure a path base in your application.");
}

if (parsedAddress.IsUnixPipe)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
using Microsoft.AspNetCore.Testing;
using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;
using Moq;
using Microsoft.Extensions.Logging.Testing;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Xunit;
using Microsoft.Extensions.Internal;

namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
{
Expand Down Expand Up @@ -531,64 +532,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 @@ -4,6 +4,7 @@
using System;
using System.Linq;
using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Server.Kestrel;
Expand Down Expand Up @@ -50,6 +51,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 addresses are not supported. Use {nameof(KestrelServerOptions)}.{nameof(KestrelServerOptions.Listen)}() to configure an HTTPS endpoint.",
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(
$"Addresses containing a path base are not supported. Use {nameof(IApplicationBuilder)}.UsePathBase() to configure a path base in your application.",
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 1b93bcb

Please sign in to comment.