From 7b0e87362fa83a53ad00feadb77bd564bb502975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Mon, 24 Apr 2023 15:11:08 +0200 Subject: [PATCH 1/4] Apply COP headers on .js and fallback index.html --- .../WebAssembly/DevServer/src/Server/Startup.cs | 10 +++++++++- ...ponentsWebAssemblyApplicationBuilderExtensions.cs | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Components/WebAssembly/DevServer/src/Server/Startup.cs b/src/Components/WebAssembly/DevServer/src/Server/Startup.cs index 7ce4af4f5adb..aaf25c1c584d 100644 --- a/src/Components/WebAssembly/DevServer/src/Server/Startup.cs +++ b/src/Components/WebAssembly/DevServer/src/Server/Startup.cs @@ -41,7 +41,15 @@ public static void Configure(IApplicationBuilder app, IConfiguration configurati app.UseEndpoints(endpoints => { - endpoints.MapFallbackToFile("index.html"); + endpoints.MapFallbackToFile("index.html", new StaticFileOptions + { + OnPrepareResponse = fileContext => + { + // Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer. + fileContext.Context.Response.Headers["Cross-Origin-Embedder-Policy"] = "require-corp"; + fileContext.Context.Response.Headers["Cross-Origin-Opener-Policy"] = "same-origin"; + } + }); }); } diff --git a/src/Components/WebAssembly/Server/src/ComponentsWebAssemblyApplicationBuilderExtensions.cs b/src/Components/WebAssembly/Server/src/ComponentsWebAssemblyApplicationBuilderExtensions.cs index fda8564a2fc8..39abed773654 100644 --- a/src/Components/WebAssembly/Server/src/ComponentsWebAssemblyApplicationBuilderExtensions.cs +++ b/src/Components/WebAssembly/Server/src/ComponentsWebAssemblyApplicationBuilderExtensions.cs @@ -61,6 +61,8 @@ public static IApplicationBuilder UseBlazorFrameworkFiles(this IApplicationBuild } } + // Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer. + ApplyCrossOriginPolicyHeadersOnJavaScriptFiles(context); await next(context); }); @@ -126,6 +128,16 @@ private static StaticFileOptions CreateStaticFilesOptions(IFileProvider webRootF return options; } + private static void ApplyCrossOriginPolicyHeadersOnJavaScriptFiles(HttpContext httpContext) + { + string fileExtension = Path.GetExtension(httpContext.Request.Path); + if (string.Equals(fileExtension, ".js")) + { + httpContext.Response.Headers["Cross-Origin-Embedder-Policy"] = "require-corp"; + httpContext.Response.Headers["Cross-Origin-Opener-Policy"] = "same-origin"; + } + } + private static void AddMapping(FileExtensionContentTypeProvider provider, string name, string mimeType) { if (!provider.Mappings.ContainsKey(name)) From a967aa9451d06fbb65b7b12bd8e51200ef98b79d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Mon, 24 Apr 2023 21:42:45 +0200 Subject: [PATCH 2/4] Move change to DevServer only --- .../DevServer/src/Server/Startup.cs | 24 +++++++++++++++++-- ...WebAssemblyApplicationBuilderExtensions.cs | 12 ---------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/Components/WebAssembly/DevServer/src/Server/Startup.cs b/src/Components/WebAssembly/DevServer/src/Server/Startup.cs index aaf25c1c584d..f336af527739 100644 --- a/src/Components/WebAssembly/DevServer/src/Server/Startup.cs +++ b/src/Components/WebAssembly/DevServer/src/Server/Startup.cs @@ -29,6 +29,21 @@ public static void Configure(IApplicationBuilder app, IConfiguration configurati app.UseWebAssemblyDebugging(); + app.Use(async (ctx, next) => + { + if (ctx.Request.Path.StartsWithSegments("/_framework") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.server.js") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.web.js")) + { + string fileExtension = Path.GetExtension(ctx.Request.Path); + if (string.Equals(fileExtension, ".js")) + { + // Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer. + ApplyCrossOriginPolicyHeaders(ctx); + } + } + + await next(ctx); + }); + app.UseBlazorFrameworkFiles(); app.UseStaticFiles(new StaticFileOptions { @@ -46,8 +61,7 @@ public static void Configure(IApplicationBuilder app, IConfiguration configurati OnPrepareResponse = fileContext => { // Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer. - fileContext.Context.Response.Headers["Cross-Origin-Embedder-Policy"] = "require-corp"; - fileContext.Context.Response.Headers["Cross-Origin-Opener-Policy"] = "same-origin"; + ApplyCrossOriginPolicyHeaders(fileContext.Context); } }); }); @@ -77,4 +91,10 @@ private static void EnableConfiguredPathbase(IApplicationBuilder app, IConfigura }); } } + + private static void ApplyCrossOriginPolicyHeaders(HttpContext httpContext) + { + httpContext.Response.Headers["Cross-Origin-Embedder-Policy"] = "require-corp"; + httpContext.Response.Headers["Cross-Origin-Opener-Policy"] = "same-origin"; + } } diff --git a/src/Components/WebAssembly/Server/src/ComponentsWebAssemblyApplicationBuilderExtensions.cs b/src/Components/WebAssembly/Server/src/ComponentsWebAssemblyApplicationBuilderExtensions.cs index 39abed773654..fda8564a2fc8 100644 --- a/src/Components/WebAssembly/Server/src/ComponentsWebAssemblyApplicationBuilderExtensions.cs +++ b/src/Components/WebAssembly/Server/src/ComponentsWebAssemblyApplicationBuilderExtensions.cs @@ -61,8 +61,6 @@ public static IApplicationBuilder UseBlazorFrameworkFiles(this IApplicationBuild } } - // Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer. - ApplyCrossOriginPolicyHeadersOnJavaScriptFiles(context); await next(context); }); @@ -128,16 +126,6 @@ private static StaticFileOptions CreateStaticFilesOptions(IFileProvider webRootF return options; } - private static void ApplyCrossOriginPolicyHeadersOnJavaScriptFiles(HttpContext httpContext) - { - string fileExtension = Path.GetExtension(httpContext.Request.Path); - if (string.Equals(fileExtension, ".js")) - { - httpContext.Response.Headers["Cross-Origin-Embedder-Policy"] = "require-corp"; - httpContext.Response.Headers["Cross-Origin-Opener-Policy"] = "same-origin"; - } - } - private static void AddMapping(FileExtensionContentTypeProvider provider, string name, string mimeType) { if (!provider.Mappings.ContainsKey(name)) From 4f000c137f6d82bbacb00174288dedc9cb126a19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Tue, 25 Apr 2023 10:21:43 +0200 Subject: [PATCH 3/4] Apply COP headers conditionally based on WasmEnableThreads --- .../DevServer/src/Server/Program.cs | 1 + .../DevServer/src/Server/Startup.cs | 30 ++++++++++++------- ...e.Components.WebAssembly.DevServer.targets | 3 +- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/Components/WebAssembly/DevServer/src/Server/Program.cs b/src/Components/WebAssembly/DevServer/src/Server/Program.cs index e06070daea4e..21eedddef02f 100644 --- a/src/Components/WebAssembly/DevServer/src/Server/Program.cs +++ b/src/Components/WebAssembly/DevServer/src/Server/Program.cs @@ -35,6 +35,7 @@ public static IHost BuildWebHost(string[] args) => ["Logging:LogLevel:Microsoft"] = "Warning", ["Logging:LogLevel:Microsoft.Hosting.Lifetime"] = "Information", [WebHostDefaults.StaticWebAssetsKey] = name, + ["ApplyCopHeaders"] = args.Contains("--apply-cop-headers").ToString() }; config.AddInMemoryCollection(inMemoryConfiguration); diff --git a/src/Components/WebAssembly/DevServer/src/Server/Startup.cs b/src/Components/WebAssembly/DevServer/src/Server/Startup.cs index f336af527739..5125c59a74af 100644 --- a/src/Components/WebAssembly/DevServer/src/Server/Startup.cs +++ b/src/Components/WebAssembly/DevServer/src/Server/Startup.cs @@ -29,20 +29,25 @@ public static void Configure(IApplicationBuilder app, IConfiguration configurati app.UseWebAssemblyDebugging(); - app.Use(async (ctx, next) => + bool applyCopHeaders = configuration.GetValue("ApplyCopHeaders"); + + if (applyCopHeaders) { - if (ctx.Request.Path.StartsWithSegments("/_framework") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.server.js") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.web.js")) + app.Use(async (ctx, next) => { - string fileExtension = Path.GetExtension(ctx.Request.Path); - if (string.Equals(fileExtension, ".js")) + if (ctx.Request.Path.StartsWithSegments("/_framework") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.server.js") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.web.js")) { - // Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer. - ApplyCrossOriginPolicyHeaders(ctx); + string fileExtension = Path.GetExtension(ctx.Request.Path); + if (string.Equals(fileExtension, ".js")) + { + // Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer. + ApplyCrossOriginPolicyHeaders(ctx); + } } - } - await next(ctx); - }); + await next(ctx); + }); + } app.UseBlazorFrameworkFiles(); app.UseStaticFiles(new StaticFileOptions @@ -60,8 +65,11 @@ public static void Configure(IApplicationBuilder app, IConfiguration configurati { OnPrepareResponse = fileContext => { - // Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer. - ApplyCrossOriginPolicyHeaders(fileContext.Context); + if (applyCopHeaders) + { + // Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer. + ApplyCrossOriginPolicyHeaders(fileContext.Context); + } } }); }); diff --git a/src/Components/WebAssembly/DevServer/src/build/Microsoft.AspNetCore.Components.WebAssembly.DevServer.targets b/src/Components/WebAssembly/DevServer/src/build/Microsoft.AspNetCore.Components.WebAssembly.DevServer.targets index 1b98a9920cf7..754d5271e341 100644 --- a/src/Components/WebAssembly/DevServer/src/build/Microsoft.AspNetCore.Components.WebAssembly.DevServer.targets +++ b/src/Components/WebAssembly/DevServer/src/build/Microsoft.AspNetCore.Components.WebAssembly.DevServer.targets @@ -2,6 +2,7 @@ <_BlazorDevServerDll>$(MSBuildThisFileDirectory)../tools/blazor-devserver.dll dotnet - "$(_BlazorDevServerDll)" --applicationpath "$(TargetPath)" + <_RunExtraArguments Condition="'$(WasmEnableThreads)' == 'true'">--apply-cop-headers + "$(_BlazorDevServerDll)" --applicationpath "$(TargetPath)" $(_RunExtraArguments) From 69cae311847396ede3c11464ccc08f77344d89d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Tue, 27 Jun 2023 13:57:21 +0200 Subject: [PATCH 4/4] Copy the --apply-cop-headers to the inner loop targets --- src/Components/Directory.Build.targets | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Components/Directory.Build.targets b/src/Components/Directory.Build.targets index 9ccefdc7fc6d..616d89e64dc0 100644 --- a/src/Components/Directory.Build.targets +++ b/src/Components/Directory.Build.targets @@ -9,7 +9,8 @@ <_BlazorDevServerPath>$(ArtifactsDir)bin/Microsoft.AspNetCore.Components.WebAssembly.DevServer/$(Configuration)/$(DefaultNetCoreTargetFramework)/blazor-devserver.dll dotnet - exec "$(_BlazorDevServerPath)" --applicationpath "$(TargetPath)" $(AdditionalRunArguments) + <_RunExtraArguments Condition="'$(WasmEnableThreads)' == 'true'">--apply-cop-headers + exec "$(_BlazorDevServerPath)" --applicationpath "$(TargetPath)" $(_RunExtraArguments) $(AdditionalRunArguments)