Skip to content

Commit 5281367

Browse files
authored
Make HttpsRedirectionMiddleware throw if there are multiple ports #27951 (#29166)
1 parent 58610a8 commit 5281367

File tree

3 files changed

+7
-33
lines changed

3 files changed

+7
-33
lines changed

src/Middleware/HttpsPolicy/src/HttpsLoggingExtensions.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ internal static class HttpsLoggingExtensions
1111
private static readonly Action<ILogger, string, Exception?> _redirectingToHttps;
1212
private static readonly Action<ILogger, int, Exception?> _portLoadedFromConfig;
1313
private static readonly Action<ILogger, Exception?> _failedToDeterminePort;
14-
private static readonly Action<ILogger, Exception?> _failedMultiplePorts;
1514
private static readonly Action<ILogger, int, Exception?> _portFromServer;
1615

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

34-
_failedMultiplePorts = LoggerMessage.Define(
35-
LogLevel.Warning,
36-
new EventId(4, "FailedMultiplePorts"),
37-
"Cannot determine the https port from IServerAddressesFeature, multiple values were found. " +
38-
"Please set the desired port explicitly on HttpsRedirectionOptions.HttpsPort.");
39-
4033
_portFromServer = LoggerMessage.Define<int>(
4134
LogLevel.Debug,
4235
new EventId(5, "PortFromServer"),
@@ -58,11 +51,6 @@ public static void FailedToDeterminePort(this ILogger logger)
5851
_failedToDeterminePort(logger, null);
5952
}
6053

61-
public static void FailedMultiplePorts(this ILogger logger)
62-
{
63-
_failedMultiplePorts(logger, null);
64-
}
65-
6654
public static void PortFromServer(this ILogger logger, int port)
6755
{
6856
_portFromServer(logger, port, null);

src/Middleware/HttpsPolicy/src/HttpsRedirectionMiddleware.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public HttpsRedirectionMiddleware(RequestDelegate next, IOptions<HttpsRedirectio
4848
var httpsRedirectionOptions = options.Value;
4949
if (httpsRedirectionOptions.HttpsPort.HasValue)
5050
{
51-
_httpsPort = new Lazy<int>(() => httpsRedirectionOptions.HttpsPort.Value);
51+
_httpsPort = new Lazy<int>(httpsRedirectionOptions.HttpsPort.Value);
5252
}
5353
else
5454
{
@@ -149,8 +149,9 @@ private int TryGetHttpsPort()
149149
// If we find multiple different https ports specified, throw
150150
if (nullablePort.HasValue && nullablePort != bindingAddress.Port)
151151
{
152-
_logger.FailedMultiplePorts();
153-
return PortNotFound;
152+
throw new InvalidOperationException(
153+
"Cannot determine the https port from IServerAddressesFeature, multiple values were found. " +
154+
"Set the desired port explicitly on HttpsRedirectionOptions.HttpsPort.");
154155
}
155156
else
156157
{

src/Middleware/HttpsPolicy/test/HttpsRedirectionMiddlewareTests.cs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -286,21 +286,13 @@ public async Task SetServerAddressesFeature_SingleHttpsAddress_Success()
286286
}
287287

288288
[Fact]
289-
public async Task SetServerAddressesFeature_MultipleHttpsAddresses_LogsAndFailsToRedirect()
289+
public async Task SetServerAddressesFeature_MultipleHttpsAddresses_Throws()
290290
{
291-
var sink = new TestSink(
292-
TestSink.EnableWithTypeName<HttpsRedirectionMiddleware>,
293-
TestSink.EnableWithTypeName<HttpsRedirectionMiddleware>);
294-
var loggerFactory = new TestLoggerFactory(sink, enabled: true);
295291
using var host = new HostBuilder()
296292
.ConfigureWebHost(webHostBuilder =>
297293
{
298294
webHostBuilder
299295
.UseTestServer()
300-
.ConfigureServices(services =>
301-
{
302-
services.AddSingleton<ILoggerFactory>(loggerFactory);
303-
})
304296
.Configure(app =>
305297
{
306298
app.UseHttpsRedirection();
@@ -323,16 +315,9 @@ public async Task SetServerAddressesFeature_MultipleHttpsAddresses_LogsAndFailsT
323315

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

326-
var response = await client.SendAsync(request);
327-
Assert.Equal(200, (int)response.StatusCode);
328-
329-
var logMessages = sink.Writes.ToList();
330-
331-
Assert.Single(logMessages);
332-
var message = logMessages.First();
333-
Assert.Equal(LogLevel.Warning, message.LogLevel);
318+
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => client.SendAsync(request));
334319
Assert.Equal("Cannot determine the https port from IServerAddressesFeature, multiple values were found. " +
335-
"Please set the desired port explicitly on HttpsRedirectionOptions.HttpsPort.", message.State.ToString());
320+
"Set the desired port explicitly on HttpsRedirectionOptions.HttpsPort.", ex.Message);
336321
}
337322

338323
[Fact]

0 commit comments

Comments
 (0)