Skip to content

Commit

Permalink
fix(threads): Don't enable secure mode unless threading is enabled fo…
Browse files Browse the repository at this point in the history
…r local server

(cherry picked from commit 74311cd)
  • Loading branch information
jeromelaban authored and mergify[bot] committed Nov 1, 2022
1 parent efda6a5 commit 2c6f229
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/Uno.Wasm.Bootstrap.Cli/Server/Startup.cs
Expand Up @@ -57,16 +57,23 @@ public void Configure(IApplicationBuilder app, IConfiguration configuration)
var env = app.ApplicationServices.GetRequiredService<IWebHostEnvironment>();
var contentRoot = env.ContentRootPath;

var webHostEnvironment = app.ApplicationServices.GetRequiredService<IWebHostEnvironment>();

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();
});

Expand All @@ -79,8 +86,6 @@ public void Configure(IApplicationBuilder app, IConfiguration configuration)

app.UseWebAssemblyDebugging(configuration);

var webHostEnvironment = app.ApplicationServices.GetRequiredService<IWebHostEnvironment>();

// foreach(DictionaryEntry entry in Environment.GetEnvironmentVariables())
// {
// Console.WriteLine($"Env: {entry.Key}={entry.Value}");
Expand Down Expand Up @@ -137,6 +142,24 @@ public void Configure(IApplicationBuilder app, IConfiguration configuration)
});
}

private static bool ShouldUseSecureMode(IWebHostEnvironment environment, IConfiguration configuration)
{
var buildConfiguration = configuration.GetValue<string>("configuration") ?? "";
var targetFramework = configuration.GetValue<string>("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)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Uno.Wasm.Bootstrap/ShellTask.cs
Expand Up @@ -253,6 +253,7 @@ public override bool Execute()
CopyRuntime();
RunPackager();
TryDeployDebuggerProxy();
GenerateServerAppFeaturesInfo();
ExtractAdditionalJS();
ExtractAdditionalCSS();
CleanupDist();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 2c6f229

Please sign in to comment.