Skip to content

Commit 4d3eccc

Browse files
authored
Set new HTTPS environment variable when using out of process (#16713)
1 parent 83a09f6 commit 4d3eccc

File tree

9 files changed

+80
-12
lines changed

9 files changed

+80
-12
lines changed

src/Middleware/HttpsPolicy/src/HttpsRedirectionMiddleware.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ private int TryGetHttpsPort()
122122
// 1. Set in the HttpsRedirectionOptions
123123
// 2. HTTPS_PORT environment variable
124124
// 3. IServerAddressesFeature
125-
// 4. Fail if not set
126-
var nullablePort = _config.GetValue<int?>("HTTPS_PORT");
125+
// 4. Fail if not sets
126+
127+
var nullablePort = _config.GetValue<int?>("HTTPS_PORT") ?? _config.GetValue<int?>("ANCM_HTTPS_PORT");
127128
if (nullablePort.HasValue)
128129
{
129130
var port = nullablePort.Value;

src/Servers/IIS/AspNetCoreModuleV2/OutOfProcessRequestHandler/serverprocess.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
#define STARTUP_TIME_LIMIT_INCREMENT_IN_MILLISECONDS 5000
1515

16-
1716
HRESULT
1817
SERVER_PROCESS::Initialize(
1918
PROCESS_MANAGER *pProcessManager,

src/Servers/IIS/AspNetCoreModuleV2/RequestHandlerLib/environmentvariablehash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#define ASPNETCORE_IIS_AUTH_ENV_STR L"ASPNETCORE_IIS_HTTPAUTH"
99
#define ASPNETCORE_IIS_WEBSOCKETS_SUPPORTED_ENV_STR L"ASPNETCORE_IIS_WEBSOCKETS_SUPPORTED"
1010
#define ASPNETCORE_IIS_PHYSICAL_PATH_ENV_STR L"ASPNETCORE_IIS_PHYSICAL_PATH"
11-
#define ASPNETCORE_HTTPS_PORT_ENV_STR L"ASPNETCORE_HTTPS_PORT"
11+
#define ASPNETCORE_ANCM_HTTPS_PORT_ENV_STR L"ASPNETCORE_ANCM_HTTPS_PORT"
1212
#define ASPNETCORE_IIS_AUTH_WINDOWS L"windows;"
1313
#define ASPNETCORE_IIS_AUTH_BASIC L"basic;"
1414
#define ASPNETCORE_IIS_AUTH_ANONYMOUS L"anonymous;"

src/Servers/IIS/AspNetCoreModuleV2/RequestHandlerLib/environmentvariablehelpers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ENVIRONMENT_VAR_HELPERS
4343
environmentVariables.insert_or_assign(ASPNETCORE_IIS_PHYSICAL_PATH_ENV_STR, pApplicationPhysicalPath);
4444
if (pHttpsPort)
4545
{
46-
environmentVariables.try_emplace(ASPNETCORE_HTTPS_PORT_ENV_STR, pHttpsPort);
46+
environmentVariables.try_emplace(ASPNETCORE_ANCM_HTTPS_PORT_ENV_STR, pHttpsPort);
4747
}
4848

4949
std::wstring strIisAuthEnvValue;

src/Servers/IIS/IIS/test/Common.FunctionalTests/HttpsTests.cs

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Linq;
56
using System.Net;
67
using System.Net.Http;
@@ -56,14 +57,14 @@ public async Task HttpsHelloWorld(TestVariant variant)
5657
if (DeployerSelector.HasNewHandler &&
5758
DeployerSelector.HasNewShim)
5859
{
59-
// We expect ServerAddress to be set for InProcess and HTTPS_PORT for OutOfProcess
60+
// We expect ServerAddress to be set for InProcess and ANCM_HTTPS_PORT for OutOfProcess
6061
if (variant.HostingModel == HostingModel.InProcess)
6162
{
6263
Assert.Equal(deploymentParameters.ApplicationBaseUriHint, await client.GetStringAsync("/ServerAddresses"));
6364
}
6465
else
6566
{
66-
Assert.Equal(port.ToString(), await client.GetStringAsync("/HTTPS_PORT"));
67+
Assert.Equal(port.ToString(), await client.GetStringAsync("/ANCM_HTTPS_PORT"));
6768
}
6869
}
6970
}
@@ -92,9 +93,8 @@ public async Task ServerAddressesIncludesBaseAddress()
9293
}
9394

9495
[ConditionalFact]
95-
[RequiresNewHandler]
9696
[RequiresNewShim]
97-
public async Task HttpsPortCanBeOverriden()
97+
public async Task AncmHttpsPortCanBeOverriden()
9898
{
9999
var deploymentParameters = Fixture.GetBaseDeploymentParameters(HostingModel.OutOfProcess);
100100

@@ -106,12 +106,57 @@ public async Task HttpsPortCanBeOverriden()
106106
.SetAttributeValue("bindingInformation", $":{TestPortHelper.GetNextSSLPort()}:localhost");
107107
});
108108

109-
deploymentParameters.WebConfigBasedEnvironmentVariables["ASPNETCORE_HTTPS_PORT"] = "123";
109+
deploymentParameters.WebConfigBasedEnvironmentVariables["ASPNETCORE_ANCM_HTTPS_PORT"] = "123";
110110

111111
var deploymentResult = await DeployAsync(deploymentParameters);
112112
var client = CreateNonValidatingClient(deploymentResult);
113113

114-
Assert.Equal("123", await client.GetStringAsync("/HTTPS_PORT"));
114+
Assert.Equal("123", await client.GetStringAsync("/ANCM_HTTPS_PORT"));
115+
Assert.Equal("NOVALUE", await client.GetStringAsync("/HTTPS_PORT"));
116+
}
117+
118+
[ConditionalFact]
119+
[RequiresNewShim]
120+
public async Task HttpsRedirectionWorksIn30AndNot22()
121+
{
122+
var port = TestPortHelper.GetNextSSLPort();
123+
var deploymentParameters = Fixture.GetBaseDeploymentParameters(HostingModel.OutOfProcess);
124+
deploymentParameters.WebConfigBasedEnvironmentVariables["ENABLE_HTTPS_REDIRECTION"] = "true";
125+
deploymentParameters.ApplicationBaseUriHint = $"http://localhost:{TestPortHelper.GetNextPort()}/";
126+
127+
deploymentParameters.AddServerConfigAction(
128+
element => {
129+
element.Descendants("bindings")
130+
.Single()
131+
.AddAndGetInnerElement("binding", "protocol", "https")
132+
.SetAttributeValue("bindingInformation", $":{port}:localhost");
133+
134+
element.Descendants("access")
135+
.Single()
136+
.SetAttributeValue("sslFlags", "None");
137+
});
138+
139+
var deploymentResult = await DeployAsync(deploymentParameters);
140+
var handler = new HttpClientHandler
141+
{
142+
ServerCertificateCustomValidationCallback = (a, b, c, d) => true,
143+
AllowAutoRedirect = false
144+
};
145+
var client = new HttpClient(handler)
146+
{
147+
BaseAddress = new Uri(deploymentParameters.ApplicationBaseUriHint)
148+
};
149+
150+
if (DeployerSelector.HasNewHandler)
151+
{
152+
var response = await client.GetAsync("/ANCM_HTTPS_PORT");
153+
Assert.Equal(307, (int)response.StatusCode);
154+
}
155+
else
156+
{
157+
var response = await client.GetAsync("/ANCM_HTTPS_PORT");
158+
Assert.Equal(200, (int)response.StatusCode);
159+
}
115160
}
116161

117162
[ConditionalFact]
@@ -140,7 +185,7 @@ public async Task MultipleHttpsPortsProduceNoEnvVar()
140185
var deploymentResult = await DeployAsync(deploymentParameters);
141186
var client = CreateNonValidatingClient(deploymentResult);
142187

143-
Assert.Equal("NOVALUE", await client.GetStringAsync("/HTTPS_PORT"));
188+
Assert.Equal("NOVALUE", await client.GetStringAsync("/ANCM_HTTPS_PORT"));
144189
}
145190

146191
private static HttpClient CreateNonValidatingClient(IISDeploymentResult deploymentResult)

src/Servers/IIS/IIS/test/testassets/InProcessNewShimWebSite/InProcessNewShimWebSite.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" >
4343
<AllowExplicitReference>true</AllowExplicitReference>
4444
</PackageReference>
45+
<PackageReference Include="Microsoft.AspNetCore.HttpsPolicy" Version="2.2.0" >
46+
<AllowExplicitReference>true</AllowExplicitReference>
47+
</PackageReference>
4548
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.0" >
4649
<AllowExplicitReference>true</AllowExplicitReference>
4750
</PackageReference>

src/Servers/IIS/IIS/test/testassets/InProcessWebSite/InProcessWebSite.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<Reference Include="Microsoft.AspNetCore.Server.IIS" />
2424
<Reference Include="Microsoft.AspNetCore.Server.IISIntegration" />
2525
<Reference Include="Microsoft.AspNetCore.Server.Kestrel" />
26+
<Reference Include="Microsoft.AspNetCore.HttpsPolicy" />
2627
<Reference Include="Microsoft.AspNetCore.WebUtilities" />
2728
<Reference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
2829
<Reference Include="Microsoft.Extensions.Configuration.Json" />

src/Servers/IIS/IIS/test/testassets/InProcessWebSite/Startup.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public partial class Startup
3434
{
3535
public void Configure(IApplicationBuilder app)
3636
{
37+
if (Environment.GetEnvironmentVariable("ENABLE_HTTPS_REDIRECTION") != null)
38+
{
39+
app.UseHttpsRedirection();
40+
}
3741
TestStartup.Register(app, this);
3842
}
3943

@@ -981,6 +985,13 @@ private async Task ProcessId(HttpContext context)
981985
await context.Response.WriteAsync(Process.GetCurrentProcess().Id.ToString());
982986
}
983987

988+
public async Task ANCM_HTTPS_PORT(HttpContext context)
989+
{
990+
var httpsPort = context.RequestServices.GetService<IConfiguration>().GetValue<int?>("ANCM_HTTPS_PORT");
991+
992+
await context.Response.WriteAsync(httpsPort.HasValue ? httpsPort.Value.ToString() : "NOVALUE");
993+
}
994+
984995
public async Task HTTPS_PORT(HttpContext context)
985996
{
986997
var httpsPort = context.RequestServices.GetService<IConfiguration>().GetValue<int?>("HTTPS_PORT");

src/Servers/IIS/IntegrationTesting.IIS/src/XElementExtensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,13 @@ public static XElement GetOrAdd(this XElement element, string name, string attri
4343

4444
return existing;
4545
}
46+
47+
public static XElement AddAndGetInnerElement(this XElement element, string name, string attribute, string attributeValue)
48+
{
49+
var innerElement = new XElement(name, new XAttribute(attribute, attributeValue));
50+
element.Add(innerElement);
51+
52+
return innerElement;
53+
}
4654
}
4755
}

0 commit comments

Comments
 (0)