From 2c6f2299238714c881c6b1bb7d719f4fd54f0f42 Mon Sep 17 00:00:00 2001 From: Jerome Laban Date: Mon, 31 Oct 2022 23:47:25 -0400 Subject: [PATCH] fix(threads): Don't enable secure mode unless threading is enabled for local server (cherry picked from commit 74311cda9f8d06a2c27cd080129c20e4aef15178) --- src/Uno.Wasm.Bootstrap.Cli/Server/Startup.cs | 33 +++++++++++++++++--- src/Uno.Wasm.Bootstrap/ShellTask.cs | 5 +++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/Uno.Wasm.Bootstrap.Cli/Server/Startup.cs b/src/Uno.Wasm.Bootstrap.Cli/Server/Startup.cs index 8770d95c..5858672e 100644 --- a/src/Uno.Wasm.Bootstrap.Cli/Server/Startup.cs +++ b/src/Uno.Wasm.Bootstrap.Cli/Server/Startup.cs @@ -57,16 +57,23 @@ public void Configure(IApplicationBuilder app, IConfiguration configuration) var env = app.ApplicationServices.GetRequiredService(); var contentRoot = env.ContentRootPath; + var webHostEnvironment = app.ApplicationServices.GetRequiredService(); + + var useSecureMode = ShouldUseSecureMode(webHostEnvironment, configuration); + app.Use(async (context, next) => { context.Response.Headers.Add("Access-Control-Allow-Origin", "*"); context.Response.Headers.Add("Access-Control-Allow-Methods", "*"); context.Response.Headers.Add("Access-Control-Allow-Headers", "*"); + if (useSecureMode) + { + // Required for SharedArrayBuffer: https://developer.chrome.com/blog/enabling-shared-array-buffer/ + context.Response.Headers.Add("Cross-Origin-Embedder-Policy", "require-corp"); + context.Response.Headers.Add("Cross-Origin-Opener-Policy", "same-origin"); + } - // Required for SharedArrayBuffer: https://developer.chrome.com/blog/enabling-shared-array-buffer/ - context.Response.Headers.Add("Cross-Origin-Embedder-Policy", "require-corp"); - context.Response.Headers.Add("Cross-Origin-Opener-Policy", "same-origin"); await next(); }); @@ -79,8 +86,6 @@ public void Configure(IApplicationBuilder app, IConfiguration configuration) app.UseWebAssemblyDebugging(configuration); - var webHostEnvironment = app.ApplicationServices.GetRequiredService(); - // foreach(DictionaryEntry entry in Environment.GetEnvironmentVariables()) // { // Console.WriteLine($"Env: {entry.Key}={entry.Value}"); @@ -137,6 +142,24 @@ public void Configure(IApplicationBuilder app, IConfiguration configuration) }); } + private static bool ShouldUseSecureMode(IWebHostEnvironment environment, IConfiguration configuration) + { + var buildConfiguration = configuration.GetValue("configuration") ?? ""; + var targetFramework = configuration.GetValue("targetframework") ?? ""; + + var contentRoot = environment.ContentRootPath; + var debuggerInfoRoot = Path.Combine(contentRoot, "obj", buildConfiguration, targetFramework); + + var featuresInfoFile = Path.Combine(debuggerInfoRoot, ".unoappfeatures"); + if (File.Exists(featuresInfoFile)) + { + var featuresRaw = File.ReadAllText(featuresInfoFile); + + return featuresRaw.Contains("threads", StringComparison.Ordinal); + } + + return false; + } private static string FindIndexHtmlFile(string basePath) { diff --git a/src/Uno.Wasm.Bootstrap/ShellTask.cs b/src/Uno.Wasm.Bootstrap/ShellTask.cs index e7b51bbe..a0167f5e 100644 --- a/src/Uno.Wasm.Bootstrap/ShellTask.cs +++ b/src/Uno.Wasm.Bootstrap/ShellTask.cs @@ -253,6 +253,7 @@ public override bool Execute() CopyRuntime(); RunPackager(); TryDeployDebuggerProxy(); + GenerateServerAppFeaturesInfo(); ExtractAdditionalJS(); ExtractAdditionalCSS(); CleanupDist(); @@ -713,6 +714,10 @@ private void TryDeployDebuggerProxy() File.WriteAllText(Path.Combine(wasmDebuggerRootPath, ".debuggerinfo"), proxyBasePath); } + private void GenerateServerAppFeaturesInfo() + // Write down an info file so the static file server can find it + => File.WriteAllText(Path.Combine(IntermediateOutputPath, ".unoappfeatures"), BuildRuntimeFeatures()); + private void RunPackager() { BuildReferencedAssembliesList();