Skip to content

[Blazor] Fix multiple CSP policies #55200

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 6 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,20 @@ public static RazorComponentsEndpointConventionBuilder AddInteractiveServerRende
var original = b.RequestDelegate;
b.RequestDelegate = async context =>
{
context.Response.Headers.Add("Content-Security-Policy", $"frame-ancestors {options.ContentSecurityFrameAncestorsPolicy}");
if (context.Response.Headers.ContentSecurityPolicy.Count == 0)
{
context.Response.Headers.ContentSecurityPolicy = $"frame-ancestors {options.ContentSecurityFrameAncestorsPolicy}";
}
else
{
var result = new string[context.Response.Headers.ContentSecurityPolicy.Count + 1];
for (var i = 0; i < result.Length - 1; i++)
{
result[i] = context.Response.Headers.ContentSecurityPolicy[i];
}
result[^1] = $"frame-ancestors {options.ContentSecurityFrameAncestorsPolicy}";
context.Response.Headers.ContentSecurityPolicy = result;
}
await original(context);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,39 @@ public async Task EmbeddingServerAppInsideIframe_WorksAsync()
Assert.DoesNotContain("Content-Security-Policy", response.Headers.Select(h => h.Key));
}
}

[Fact]
public async Task EmbeddingServerAppInsideIframe_WorksWithMultipleCspHeaders()
{
Navigate("/subdir/iframe?add-csp");

var logs = Browser.GetBrowserLogs(LogLevel.Severe);

Assert.Empty(logs);

// Get the iframe element from the page, and inspect its contents for a p element with id inside-iframe
var iframe = Browser.FindElement(By.TagName("iframe"));
Browser.SwitchTo().Frame(iframe);
Browser.Exists(By.Id("inside-iframe"));

using var client = new HttpClient() { BaseAddress = _serverFixture.RootUri };
var response = await client.GetAsync("/subdir/iframe?add-csp");
response.EnsureSuccessStatusCode();

if (ExpectedPolicy != null)
{
Assert.Equal(
response.Headers.GetValues("Content-Security-Policy"),
[
"script-src 'self' 'unsafe-inline'",
$"frame-ancestors {ExpectedPolicy}"
]);
}
else
{
Assert.DoesNotContain("Content-Security-Policy", response.Headers.Select(h => h.Key));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this assertion really correct? If you're requesting the ?add-csp response, that should add at least one CSP in all cases, wouldn't it?

Or is it possible this test code never actually runs? It's hard to reason about with the subclasses and ExpectedPolicy etc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's hard to write these tests. I copy pasted it from another test but validated that it works (that section doesn't run).

I'll clean that up.

}
}
}

public abstract partial class BlockedWebSocketCompressionTests(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseRouting();
UseFakeAuthState(app);
app.UseAntiforgery();

app.Use((ctx, nxt) =>
{
if (ctx.Request.Query.ContainsKey("add-csp"))
{
ctx.Response.Headers.Add("Content-Security-Policy", "script-src 'self' 'unsafe-inline'");
}
return nxt();
});

_ = app.UseEndpoints(endpoints =>
{
_ = endpoints.MapRazorComponents<TRootComponent>()
Expand Down