Skip to content

Commit 529e844

Browse files
authored
[blazor] Set Cross-Origin-Policy headers for multi-threaded runtime (#47855)
* Apply COP headers on .js and fallback index.html on the DevServer * Copy the --apply-cop-headers to the inner loop targets
1 parent fca06e7 commit 529e844

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

src/Components/Directory.Build.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
<_BlazorDevServerPath>$(ArtifactsDir)bin/Microsoft.AspNetCore.Components.WebAssembly.DevServer/$(Configuration)/$(DefaultNetCoreTargetFramework)/blazor-devserver.dll</_BlazorDevServerPath>
1111
<RunCommand>dotnet</RunCommand>
12-
<RunArguments>exec &quot;$(_BlazorDevServerPath)&quot; --applicationpath &quot;$(TargetPath)&quot; $(AdditionalRunArguments)</RunArguments>
12+
<_RunExtraArguments Condition="'$(WasmEnableThreads)' == 'true'">--apply-cop-headers</_RunExtraArguments>
13+
<RunArguments>exec &quot;$(_BlazorDevServerPath)&quot; --applicationpath &quot;$(TargetPath)&quot; $(_RunExtraArguments) $(AdditionalRunArguments)</RunArguments>
1314
</PropertyGroup>
1415

1516
<ItemGroup>

src/Components/WebAssembly/DevServer/src/Server/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static IHost BuildWebHost(string[] args) =>
3535
["Logging:LogLevel:Microsoft"] = "Warning",
3636
["Logging:LogLevel:Microsoft.Hosting.Lifetime"] = "Information",
3737
[WebHostDefaults.StaticWebAssetsKey] = name,
38+
["ApplyCopHeaders"] = args.Contains("--apply-cop-headers").ToString()
3839
};
3940

4041
config.AddInMemoryCollection(inMemoryConfiguration);

src/Components/WebAssembly/DevServer/src/Server/Startup.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ public static void Configure(IApplicationBuilder app, IConfiguration configurati
2929

3030
app.UseWebAssemblyDebugging();
3131

32+
bool applyCopHeaders = configuration.GetValue<bool>("ApplyCopHeaders");
33+
34+
if (applyCopHeaders)
35+
{
36+
app.Use(async (ctx, next) =>
37+
{
38+
if (ctx.Request.Path.StartsWithSegments("/_framework") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.server.js") && !ctx.Request.Path.StartsWithSegments("/_framework/blazor.web.js"))
39+
{
40+
string fileExtension = Path.GetExtension(ctx.Request.Path);
41+
if (string.Equals(fileExtension, ".js"))
42+
{
43+
// Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer.
44+
ApplyCrossOriginPolicyHeaders(ctx);
45+
}
46+
}
47+
48+
await next(ctx);
49+
});
50+
}
51+
3252
app.UseBlazorFrameworkFiles();
3353
app.UseStaticFiles(new StaticFileOptions
3454
{
@@ -41,7 +61,17 @@ public static void Configure(IApplicationBuilder app, IConfiguration configurati
4161

4262
app.UseEndpoints(endpoints =>
4363
{
44-
endpoints.MapFallbackToFile("index.html");
64+
endpoints.MapFallbackToFile("index.html", new StaticFileOptions
65+
{
66+
OnPrepareResponse = fileContext =>
67+
{
68+
if (applyCopHeaders)
69+
{
70+
// Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer.
71+
ApplyCrossOriginPolicyHeaders(fileContext.Context);
72+
}
73+
}
74+
});
4575
});
4676
}
4777

@@ -69,4 +99,10 @@ private static void EnableConfiguredPathbase(IApplicationBuilder app, IConfigura
6999
});
70100
}
71101
}
102+
103+
private static void ApplyCrossOriginPolicyHeaders(HttpContext httpContext)
104+
{
105+
httpContext.Response.Headers["Cross-Origin-Embedder-Policy"] = "require-corp";
106+
httpContext.Response.Headers["Cross-Origin-Opener-Policy"] = "same-origin";
107+
}
72108
}

src/Components/WebAssembly/DevServer/src/build/Microsoft.AspNetCore.Components.WebAssembly.DevServer.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<PropertyGroup>
33
<_BlazorDevServerDll>$(MSBuildThisFileDirectory)../tools/blazor-devserver.dll</_BlazorDevServerDll>
44
<RunCommand>dotnet</RunCommand>
5-
<RunArguments>&quot;$(_BlazorDevServerDll)&quot; --applicationpath &quot;$(TargetPath)&quot;</RunArguments>
5+
<_RunExtraArguments Condition="'$(WasmEnableThreads)' == 'true'">--apply-cop-headers</_RunExtraArguments>
6+
<RunArguments>&quot;$(_BlazorDevServerDll)&quot; --applicationpath &quot;$(TargetPath)&quot; $(_RunExtraArguments)</RunArguments>
67
</PropertyGroup>
78
</Project>

0 commit comments

Comments
 (0)