dotnet / dotnet/runtime

.NET 10 importmap - incompatible with secure Content Security Policy (CSP)

Open
#121,885 5 comments 0 reactions 1 assignee Claimed by @maraf View on GitHub
arch-wasm area-System.Runtime.InteropServices.JavaScript needs-author-action os-browser
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

### Description

This is an existing, mature project, using .NET 9 that has just been migrated to .NET 10. After struggling with #121849 I'm now facing an issue with the new importmap.

Our Content Security Policy is strict, as recommended in many security best practices as protection against CSS and clickjacking:

```
"object-src 'none'; upgrade-insecure-requests; img-src https: data:; form-action 'none'; font-src 'self'; base-uri 'self'; frame-ancestors 'none'; script-src 'self' https://static.cloudflareinsights.com/ 'unsafe-eval'"
```

Notice that unsafe-eval is considered a weakness but seems to be required by the Blazor Mono runtime, with #68374 unresolved. I haven't been able to find any other mention of Content Security Policy in this GitHub project (or elsewhere).

However, it now looks like the new importmap used for fingerprinted files requires an even weaker 'unsafe-inline', which is considered the weakest of all the options. We get this error from the browser:

```
Executing inline script violates the following Content Security Policy directive 'script-src 'self' https://static.cloudflareinsights.com/ 'unsafe-eval''. Either the 'unsafe-inline' keyword, a hash ('sha256-4K2dkMg8Na5SjWlB6DRBmGhpIg6nIQjasnFdQLS8WRc='), or a nonce ('nonce-...') is required to enable inline execution. The action has been blocked.
```

and more importantly, any attempt to download fingerprinted files, e.g., dotnet.js fails because the fingerprint is not included in the URL (as expected when the importmap is not loaded).

Are there any options to generate this importmap as an independent .json file instead of embedding it into the index.html page? Or any other alternative solution that doesn't required weakening our Content Security Policy even more?

### Reproduction Steps

Include a secure CSP header when serving Blazor WASM assets.

### Expected behavior

The importmap is compatible with secure CSP headers and the runtime is loaded.

### Actual behavior

Browser error that prevents the importmap and the runtime from being loaded:

```
Executing inline script violates the following Content Security Policy directive 'script-src 'self' https://static.cloudflareinsights.com/ 'unsafe-eval''. Either the 'unsafe-inline' keyword, a hash ('sha256-4K2dkMg8Na5SjWlB6DRBmGhpIg6nIQjasnFdQLS8WRc='), or a nonce ('nonce-...') is required to enable inline execution. The action has been blocked.
```

### Regression?

Yes? This feature didn't exist in .NET 9 but our system used to work in .NET 9.

### Known Workarounds

None

### Configuration

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.