Skip to content

[blazor] Set Cross-Origin-Policy headers for multi-threaded runtime #47855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Components/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

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

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
38 changes: 37 additions & 1 deletion src/Components/WebAssembly/DevServer/src/Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ public static void Configure(IApplicationBuilder app, IConfiguration configurati

app.UseWebAssemblyDebugging();

bool applyCopHeaders = configuration.GetValue<bool>("ApplyCopHeaders");

if (applyCopHeaders)
{
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
{
Expand All @@ -41,7 +61,17 @@ public static void Configure(IApplicationBuilder app, IConfiguration configurati

app.UseEndpoints(endpoints =>
{
endpoints.MapFallbackToFile("index.html");
endpoints.MapFallbackToFile("index.html", new StaticFileOptions
{
OnPrepareResponse = fileContext =>
{
if (applyCopHeaders)
{
// Browser multi-threaded runtime requires cross-origin policy headers to enable SharedArrayBuffer.
ApplyCrossOriginPolicyHeaders(fileContext.Context);
}
}
});
});
}

Expand Down Expand Up @@ -69,4 +99,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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<_BlazorDevServerDll>$(MSBuildThisFileDirectory)../tools/blazor-devserver.dll</_BlazorDevServerDll>
<RunCommand>dotnet</RunCommand>
<RunArguments>&quot;$(_BlazorDevServerDll)&quot; --applicationpath &quot;$(TargetPath)&quot;</RunArguments>
<_RunExtraArguments Condition="'$(WasmEnableThreads)' == 'true'">--apply-cop-headers</_RunExtraArguments>
<RunArguments>&quot;$(_BlazorDevServerDll)&quot; --applicationpath &quot;$(TargetPath)&quot; $(_RunExtraArguments)</RunArguments>
</PropertyGroup>
</Project>