Skip to content

Commit 49732b0

Browse files
authored
Adding an app setting to allow CORS configuration (#9846)
1 parent a77bf00 commit 49732b0

File tree

6 files changed

+31
-13
lines changed

6 files changed

+31
-13
lines changed

release_notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
- Customers can opt-out of this behavior by setting `SendCanceledInvocationsToWorker` to `false` in host.json
1414
- If a worker does not support CancellationTokens, cancelled invocations will not be sent to the worker
1515
- Warn when `FUNCTIONS_WORKER_RUNTIME` is not set (#9799)
16+
- Add an app setting to allow CORS configuration (#9846)

src/WebJobs.Script.WebHost/Configuration/CorsOptionsSetup.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.Linq;
7-
using System.Threading.Tasks;
86
using Microsoft.AspNetCore.Cors.Infrastructure;
97
using Microsoft.Extensions.Options;
108

@@ -23,7 +21,7 @@ public CorsOptionsSetup(IEnvironment env, IOptions<HostCorsOptions> hostCorsOpti
2321

2422
public void Configure(CorsOptions options)
2523
{
26-
if (_env.IsAnyLinuxConsumption())
24+
if (_env.IsAnyLinuxConsumption() || _env.IsCorsConfigurationEnabled())
2725
{
2826
string[] allowedOrigins = _hostCorsOptions.Value.AllowedOrigins?.ToArray() ?? Array.Empty<string>();
2927
var policyBuilder = new CorsPolicyBuilder(allowedOrigins);

src/WebJobs.Script.WebHost/WebScriptHostBuilderExtension.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Net.Http;
6+
using System.Runtime.InteropServices;
67
using Microsoft.AspNetCore.Mvc.ApplicationParts;
78
using Microsoft.Azure.WebJobs.Host.Config;
89
using Microsoft.Azure.WebJobs.Host.Executors;
@@ -125,11 +126,17 @@ public static IHostBuilder AddWebScriptHost(this IHostBuilder builder, IServiceP
125126
services.TryAddSingleton<IJobHostMiddlewarePipeline, DefaultMiddlewarePipeline>();
126127
services.TryAddEnumerable(ServiceDescriptor.Singleton<IJobHostHttpMiddleware, CustomHttpHeadersMiddleware>());
127128
services.TryAddEnumerable(ServiceDescriptor.Singleton<IJobHostHttpMiddleware, HstsConfigurationMiddleware>());
128-
if (environment.IsAnyLinuxConsumption())
129+
130+
bool isAnyLinuxConsumption = environment.IsAnyLinuxConsumption();
131+
132+
if (isAnyLinuxConsumption || environment.IsCorsConfigurationEnabled())
129133
{
130134
services.AddSingleton<ICorsMiddlewareFactory, CorsMiddlewareFactory>();
131135
services.TryAddEnumerable(ServiceDescriptor.Singleton<IJobHostHttpMiddleware, JobHostCorsMiddleware>());
136+
}
132137

138+
if (isAnyLinuxConsumption)
139+
{
133140
// EasyAuth must go after CORS, as CORS preflight requests can happen before authentication
134141
services.TryAddEnumerable(ServiceDescriptor.Singleton<IJobHostHttpMiddleware, JobHostEasyAuthMiddleware>());
135142
}

src/WebJobs.Script/Environment/EnvironmentExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ public static bool IsContainer(this IEnvironment environment)
165165
&& runningInContainerValue;
166166
}
167167

168+
public static bool IsCorsConfigurationEnabled(this IEnvironment environment)
169+
{
170+
return environment.GetEnvironmentVariable(EnableCorsConfiguration) == "1";
171+
}
172+
168173
public static bool IsPersistentFileSystemAvailable(this IEnvironment environment)
169174
{
170175
return environment.IsWindowsAzureManagedHosting()

src/WebJobs.Script/Environment/EnvironmentSettingNames.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public static class EnvironmentSettingNames
113113
public const string LinuxAzureAppServiceStorage = "WEBSITES_ENABLE_APP_SERVICE_STORAGE";
114114
public const string CoreToolsEnvironment = "FUNCTIONS_CORETOOLS_ENVIRONMENT";
115115
public const string RunningInContainer = "DOTNET_RUNNING_IN_CONTAINER";
116+
public const string EnableCorsConfiguration = "FUNCTIONS_ENABLE_CORS_CONFIGURATION";
116117

117118
public const string ExtensionBundleSourceUri = "FUNCTIONS_EXTENSIONBUNDLE_SOURCE_URI";
118119

test/WebJobs.Script.Tests/Middleware/CorsConfigurationMiddlewareTests.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ public async Task Invoke_HasCorsConfig_InvokesNext()
5454
Assert.True(nextInvoked);
5555
}
5656

57-
[Fact]
58-
public async Task Invoke_OriginAllowed_AddsExpectedHeaders()
57+
[Theory]
58+
[InlineData(EnvironmentSettingNames.ContainerName, "foo")]
59+
[InlineData(EnvironmentSettingNames.EnableCorsConfiguration, "1")]
60+
public async Task Invoke_OriginAllowed_AddsExpectedHeaders(string appSettingName, string appSettingValue)
5961
{
6062
var envars = new Dictionary<string, string>()
6163
{
62-
{ EnvironmentSettingNames.ContainerName, "foo" },
64+
{ appSettingName, appSettingValue }
6365
};
6466
var testEnv = new TestEnvironment(envars);
6567
var testOrigin = "https://functions.azure.com";
@@ -107,12 +109,14 @@ public async Task Invoke_OriginAllowed_AddsExpectedHeaders()
107109
Assert.Equal("true", allowCredsHeaderValues.FirstOrDefault());
108110
}
109111

110-
[Fact]
111-
public async Task Invoke_OriginNotAllowed_DoesNotAddHeaders()
112+
[Theory]
113+
[InlineData(EnvironmentSettingNames.ContainerName, "foo")]
114+
[InlineData(EnvironmentSettingNames.EnableCorsConfiguration, "1")]
115+
public async Task Invoke_OriginNotAllowed_DoesNotAddHeaders(string appSettingName, string appSettingValue)
112116
{
113117
var envars = new Dictionary<string, string>()
114118
{
115-
{ EnvironmentSettingNames.ContainerName, "foo" },
119+
{ appSettingName, appSettingValue }
116120
};
117121
var testEnv = new TestEnvironment(envars);
118122
var badOrigin = "http://badorigin.com";
@@ -161,12 +165,14 @@ public async Task Invoke_OriginNotAllowed_DoesNotAddHeaders()
161165
Assert.False(response.Headers.TryGetValues("Access-Control-Allow-Methods", out allowMethods));
162166
}
163167

164-
[Fact]
165-
public async Task Invoke_Adds_AccessControlAllowMethods()
168+
[Theory]
169+
[InlineData(EnvironmentSettingNames.ContainerName, "foo")]
170+
[InlineData(EnvironmentSettingNames.EnableCorsConfiguration, "1")]
171+
public async Task Invoke_Adds_AccessControlAllowMethods(string appSettingName, string appSettingValue)
166172
{
167173
var envars = new Dictionary<string, string>()
168174
{
169-
{ EnvironmentSettingNames.ContainerName, "foo" },
175+
{ appSettingName, appSettingValue }
170176
};
171177
var testEnv = new TestEnvironment(envars);
172178
var testOrigin = "https://functions.azure.com";

0 commit comments

Comments
 (0)