Blazor Stop Requiring unsafe-inline in CSP#36771
Conversation
unsafe-inlineunsafe-inline
unsafe-inlineunsafe-inline in CSP
|
Note: We'll need to update the docs to reflect this change (todo after merge): |
|
In terms of testing, tried out some local scenarios, and didn't encounter any issues. Grep'd through the codebase to find offending functionality and made the fixes in this PR. I looked into CSP'ifying the E2E tests, but that looks like a much more involved change (refactoring all the inline JS - or providing hashes, both of which may be cumbersome in terms of maintainability of the tests). Additionally, all the inline css would need to be stripped out of the test files as well. I'm open to exploring this path if people think it's worthwhile, personally I'm not totally sure if it's worth the time investment. |
|
@TanayParikh thanks for the thorough investigation. The fixes for Blazor server look good to me. Do you think we could get an issue filed on the runtime repo to better understand the need for As per the unsafe inline, I'm ok if we document this for the time being to make sure folks include the hash in their csp policy. That said, I think we should consider removing the inline script in 7.0 (not clear how). Can you file a bug to track it? |
|
src/Components/Web.JS/src/Platform/Circuits/DefaultReconnectDisplay.ts
Outdated
Show resolved
Hide resolved
|
@TanayParikh This is a great investigation! It's awesome that we now fully understand the gap between where we are and supporting arbitrary CSPs. I agree with @javiercn that the Server changes look great (with a possible simplification as per comment above). About the WebAssembly side:
There's no value in taking the risk of fixing our What do you think? |
|
Actually I don't even know if Chromium does have a problem with |
I agree! I've created #36805 to track removing |
Reflects changes in dotnet/aspnetcore#36771 / dotnet/aspnetcore#34428. Created dotnet/aspnetcore#36805 to track removal of the inline script which requires the `sha256-v8v3RKRPmN4odZ1CWM5gw80QKPCCWMcpNeOmimNL2AA=` hash.
|
Documentation updates: dotnet/AspNetCore.Docs#23365 |
|
/backport to release/6.0 |
|
Started backporting to release/6.0: https://github.com/dotnet/aspnetcore/actions/runs/1258896840 |
|
@TanayParikh an error occurred while backporting to release/6.0, please check the run log for details! Error: @TanayParikh is not a repo collaborator, backporting is not allowed. |
|
@dotnet/aspnet-build has something changed with the backport bot? Using the same command as before.
Perhaps a new requirement? Do I need to be manually added somewhere? |
|
Started backporting to release/6.0: https://github.com/dotnet/aspnetcore/actions/runs/1258896840 |
|
@TanayParikh an error occurred while backporting to release/6.0, please check the run log for details! Error: @TanayParikh is not a repo collaborator, backporting is not allowed. |
|
/backport to release/6.0 |
|
Started backporting to release/6.0: https://github.com/dotnet/aspnetcore/actions/runs/1262562948 |
|
@TanayParikh an error occurred while backporting to release/6.0, please check the run log for details! Error: @TanayParikh is not a repo collaborator, backporting is not allowed. |
* Blazor Server Allow Unsafe Inline For: #34428 * Update MonoPlatform.ts * Fix DefaultReconnectDisplay.test * PR Feedback
|
Hi, is this completed? I'm having security complains in a corporate environment because my Blazor WASM App in .NET 8 doesn't work with |
Blazor Server
The
innerHtmlwas triggering the CSP, thereby requiringunsafe-inline. The reconnection logic has been updated to no longer requireinnerHtml. This is the only place I could seeinnerHtmlbeing used, aside from the browser renderer, however that doesn't appear to cause an issue as the nodes don't appear to actually be added to the page.aspnetcore/src/Components/Web.JS/src/Rendering/BrowserRenderer.ts
Lines 489 to 492 in da06d1b
Per the Blazor docs
Note, from what I found
unsafe-evaldoesn't appear to be an issue for Blazor Server (only WASM due toevalutilization in mono).Validated using:
Blazor WASM:
Issue was with the global module script:
aspnetcore/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts
Line 212 in 5750125
unsafe-inlinecan be removed via adding the script hash to the CSP as per below:Validated using:
where
sha256-v8v3RKRPmN4odZ1CWM5gw80QKPCCWMcpNeOmimNL2AA=is the hash of the global module script.Note:
unsafe-evalis still required due to a dependency inmono. Currently the instantiate wasm call is failing without it.aspnetcore/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts
Line 272 in 5750125
Note,
unsafe-inlinealso requires inlinejsscript blocks to either have an associated hash or nonce. This means something like aBlazor.start(...)script would need to be put into a separate file. Not a big deal, just something worth noting.Fixes: #34428