[EDIT by guardrex to add the topic metadata]
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
Some notes
As part of the security model for blazor server-side we are exploring how feasible is to use CSP to protect against XSS. CSP is a general security mechanism that informs the browser of what are valid sources for different content items loaded on a page or actions taken by a component on a page. For example, CSP can limit the the sources for scripts, stylesheets, images, etc. or limit the origins to which outgoing requests can be sent through different methods.
CSP allows a developer to specify one or more policies that get applied to the document. These policies can be specified either on the Content-Security-Policy header or on the meta tag. A document needs to pass all specified policies to be correct/valid, this means policies don't combine, in any form, but that aspects of a policy can subsume aspects of other policies.
Policies are evaluated while the document is loading. The user-agent (browser) inspect every candiate source and determines if it meets the policy. When it does not, it simply blocks loading that element. For example, a policy might not allow third party scripts in a document. When a document contains a script (for example in the src attribute of a script tag) to a third party origin, the browser sees that it is not included on the policy and prevents it from loading.
block-all-mixed-content<meta http-equiv="Content-Security-Policy" content="block-all-mixed-content; upgrade-insecure-requests;">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; img-src data: https://*; script-src https://stackpath.bootstrapcdn.com/ 'sha256-34WLX60Tw3aG6hylk0plKbZZFXCuepeQ6Hu7OqRf8PI=' 'self'; style-src https://stackpath.bootstrapcdn.com/ 'unsafe-inline' 'self';">
There are actually two policies (the document needs to comply with both):
default-src 'self': Unless specified otherwise, the only source valid for content is our same origin.img-src: Images can be loaded from data urls and any https source. This is required for bootstrap to work.script-src 'self' https://stackpath.bootstrapcdn.com/ 'sha256-34WLX60Tw3aG6hylk0plKbZZFXCuepeQ6Hu7OqRf8PI=';: Scripts can be loaded from our origin, the Bootstrap CDN or be an accepted inline script. The hash corresponds to the script inlined to perform fallback detection.style-src https://stackpath.bootstrapcdn.com/ 'unsafe-inline' 'self': Stylesheets can be loaded from our origin, the Bootrap CDN or inlined. The inline piece is the most troublesome and its caused by how we craft the UI for the reconnect case. We should consider changing it to something else that doesn't require inline styling.block-all-mixed-content: All but Edge/IEupgrade-insecure-requests: All but Edge/IEimg-src: All but IEscript-src: All but IEstyle-src: All but IECSP is a good fit for blazor to protect the source and target origins for content, so it is good to prevent the leackage of circuit ids. That said, CSP is complex to handle to understand and requires browser support, which is spotty across browsers. For that reason, I think we should enable it in our templates but not use it as the sole mechanism to protect circuit ids, as we want to allow users to remove the policy if its causing issues with their apps.
On the off-chance you haven’t seen it before, it might be worth running the suggested policies though Google’s CSP validator. It complains about use of a host whitelist and a missing object-src. I understand that neither of those would be a high risk in this case but I just wanted to make the suggestion. Don’t feel obligated to respond if this is already something that’s been considered. 😄
@serpent5 Thanks that's a neat tool. The above policy is just an example and the result of me tinkering a bit. There is something more tightened that can likely be crafted.
Plz correct the labels if I guessed wrong (e.g., if this is for Pre6 and will be worked by the PU).
@javiercn can you add the missing content here, so @guardrex can start working on this. I see you've got a lot covered here but also mentioned that some more need to be done here.
I can probably work out the draft from what's here.
@javiercn Recommend that u add anything else that you think would be helpful. If your remarks ☝️ cover it, then I'll take it from there.
EDIT Upon review, CSP is applicable to Blazor WebAssembly apps :point_right: they can leverage CSP for their assets, too.