Why am I getting this Content Policy error?

David Thielen 3,226 Reputation points
2024-09-15T14:37:02.18+00:00

Hi all;

I have added the following to my Blazor Interactive Server app using NWebSec:

app.UseHsts(options => options.MaxAge(days: 30));
app.UseXContentTypeOptions();
app.UseXXssProtection(options => options.EnabledWithBlockMode());
app.UseXfo(options => options.SameOrigin());
app.UseReferrerPolicy(opts => opts.NoReferrerWhenDowngrade());

app.UseCsp(options => options
    .DefaultSources(s => s.Self()
        .CustomSources("data:", "https:"))
    .StyleSources(s => s.Self()
        .CustomSources("*.microsoft.com", "*.windows.net", "*.azurewebsites.net", "www.google.com",
            "fonts.googleapis.com")
        .UnsafeInline()
    )
    .ImageSources(s => s.Self()
        .CustomSources("data:", "https:"))
    .FontSources(s => s.Self()
        .CustomSources("fonts.googleapis.com"))
    .ScriptSources(s => s.Self()
        .CustomSources("*.microsoft.com", "*.windows.net", "*.azurewebsites.net", "www.google.com",
            "cse.google.com")
        .UnsafeInline()
        .UnsafeEval()
    )
    .WorkerSources(s => s.Self()
        .CustomSources("louishowe-dev.azurewebsites.net", "*.microsoft.com", "*.windows.net", "*.azurewebsites.net"))
);

// NWebSec does not handle this (no updates to that library in 4 years)
app.Use(async (context, next) =>
{
    context.Response.Headers.Add("Permissions-Policy", "geolocation=*, camera=(), microphone=()");
    await next.Invoke();
});

And I am getting the errors:

Refused to load the font '<URL>' because it violates the following Content Security Policy directive: "font-src 'self' fonts.googleapis.com".
Understand this error

Refused to create a worker from 'blob:https://louishowe-dev.azurewebsites.net/02bff816-5188-44d1-9395-484a2964920e' because it violates the following Content Security Policy directive: "worker-src 'self' louishowe-dev.azurewebsites.net *.microsoft.com *.windows.net *.azurewebsites.net".

Uncaught SecurityError: Failed to construct 'Worker': Access to the script at 'blob:https://louishowe-dev.azurewebsites.net/02bff816-5188-44d1-9395-484a2964920e' is denied by the document's Content Security Policy.

I believe both of those urls are explicitly set as allowed. I event set louishowe-dev.azurewebsites.net along with *.azurewebsites.net. And yet it won't create the worker. What am I doing wrong?

And for the font, not sure what I need to enable to allow <URL>.

??? - thanks - dave

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | .NET | Blazor
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Bruce (SqlWork.com) 82,606 Reputation points Volunteer Moderator
    2024-09-15T15:44:43.31+00:00

    I don’t believe your code is generating the urls you expect. Use the browser debug tools to see network requests and page source.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.