Skip to content

Make HttpsRedirectionMiddleware throw if there are multiple ports #29166

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

Merged
merged 1 commit into from
Jan 11, 2021
Merged
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
12 changes: 0 additions & 12 deletions src/Middleware/HttpsPolicy/src/HttpsLoggingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ internal static class HttpsLoggingExtensions
private static readonly Action<ILogger, string, Exception?> _redirectingToHttps;
private static readonly Action<ILogger, int, Exception?> _portLoadedFromConfig;
private static readonly Action<ILogger, Exception?> _failedToDeterminePort;
private static readonly Action<ILogger, Exception?> _failedMultiplePorts;
private static readonly Action<ILogger, int, Exception?> _portFromServer;

static HttpsLoggingExtensions()
Expand All @@ -31,12 +30,6 @@ static HttpsLoggingExtensions()
new EventId(3, "FailedToDeterminePort"),
"Failed to determine the https port for redirect.");

_failedMultiplePorts = LoggerMessage.Define(
LogLevel.Warning,
new EventId(4, "FailedMultiplePorts"),
"Cannot determine the https port from IServerAddressesFeature, multiple values were found. " +
"Please set the desired port explicitly on HttpsRedirectionOptions.HttpsPort.");

_portFromServer = LoggerMessage.Define<int>(
LogLevel.Debug,
new EventId(5, "PortFromServer"),
Expand All @@ -58,11 +51,6 @@ public static void FailedToDeterminePort(this ILogger logger)
_failedToDeterminePort(logger, null);
}

public static void FailedMultiplePorts(this ILogger logger)
{
_failedMultiplePorts(logger, null);
}

public static void PortFromServer(this ILogger logger, int port)
{
_portFromServer(logger, port, null);
Expand Down
7 changes: 4 additions & 3 deletions src/Middleware/HttpsPolicy/src/HttpsRedirectionMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public HttpsRedirectionMiddleware(RequestDelegate next, IOptions<HttpsRedirectio
var httpsRedirectionOptions = options.Value;
if (httpsRedirectionOptions.HttpsPort.HasValue)
{
_httpsPort = new Lazy<int>(() => httpsRedirectionOptions.HttpsPort.Value);
_httpsPort = new Lazy<int>(httpsRedirectionOptions.HttpsPort.Value);
}
else
{
Expand Down Expand Up @@ -149,8 +149,9 @@ private int TryGetHttpsPort()
// If we find multiple different https ports specified, throw
if (nullablePort.HasValue && nullablePort != bindingAddress.Port)
{
_logger.FailedMultiplePorts();
return PortNotFound;
throw new InvalidOperationException(
"Cannot determine the https port from IServerAddressesFeature, multiple values were found. " +
"Set the desired port explicitly on HttpsRedirectionOptions.HttpsPort.");
}
else
{
Expand Down
21 changes: 3 additions & 18 deletions src/Middleware/HttpsPolicy/test/HttpsRedirectionMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,21 +286,13 @@ public async Task SetServerAddressesFeature_SingleHttpsAddress_Success()
}

[Fact]
public async Task SetServerAddressesFeature_MultipleHttpsAddresses_LogsAndFailsToRedirect()
public async Task SetServerAddressesFeature_MultipleHttpsAddresses_Throws()
{
var sink = new TestSink(
TestSink.EnableWithTypeName<HttpsRedirectionMiddleware>,
TestSink.EnableWithTypeName<HttpsRedirectionMiddleware>);
var loggerFactory = new TestLoggerFactory(sink, enabled: true);
using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseTestServer()
.ConfigureServices(services =>
{
services.AddSingleton<ILoggerFactory>(loggerFactory);
})
.Configure(app =>
{
app.UseHttpsRedirection();
Expand All @@ -323,16 +315,9 @@ public async Task SetServerAddressesFeature_MultipleHttpsAddresses_LogsAndFailsT

var request = new HttpRequestMessage(HttpMethod.Get, "");

var response = await client.SendAsync(request);
Assert.Equal(200, (int)response.StatusCode);

var logMessages = sink.Writes.ToList();

Assert.Single(logMessages);
var message = logMessages.First();
Assert.Equal(LogLevel.Warning, message.LogLevel);
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => client.SendAsync(request));
Assert.Equal("Cannot determine the https port from IServerAddressesFeature, multiple values were found. " +
"Please set the desired port explicitly on HttpsRedirectionOptions.HttpsPort.", message.State.ToString());
"Set the desired port explicitly on HttpsRedirectionOptions.HttpsPort.", ex.Message);
}

[Fact]
Expand Down