diff --git a/aspnetcore/blazor/security/content-security-policy.md b/aspnetcore/blazor/security/content-security-policy.md index 17402dbc46..c8a41b6f5a 100644 --- a/aspnetcore/blazor/security/content-security-policy.md +++ b/aspnetcore/blazor/security/content-security-policy.md @@ -5,15 +5,22 @@ description: Learn how to use a Content Security Policy (CSP) with ASP.NET Core monikerRange: '>= aspnetcore-3.1' ms.author: riande ms.custom: mvc -ms.date: 11/09/2021 +ms.date: 03/25/2022 no-loc: ["Blazor Hybrid", Home, Privacy, Kestrel, appsettings.json, "ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR] uid: blazor/security/content-security-policy --- # Enforce a Content Security Policy for ASP.NET Core Blazor + + This article explains how to use a [Content Security Policy (CSP)](https://developer.mozilla.org/docs/Web/HTTP/CSP) with ASP.NET Core Blazor apps to help protect against [Cross-Site Scripting (XSS)](xref:security/cross-site-scripting) attacks. -:::moniker range=">= aspnetcore-6.0" +:::moniker range=">= aspnetcore-6.0 < aspnetcore-7.0" [Cross-Site Scripting (XSS)](xref:security/cross-site-scripting) is a security vulnerability where an attacker places one or more malicious client-side scripts into an app's rendered content. A CSP helps protect against XSS attacks by informing the browser of valid: @@ -42,8 +49,8 @@ Minimally, specify the following directives and sources for Blazor apps. Add add * Specify the `https://stackpath.bootstrapcdn.com/` host source for Bootstrap scripts. * Specify `self` to indicate that the app's origin, including the scheme and port number, is a valid source. * In a Blazor WebAssembly app: - * Specify hashes to permit required scripts to load. * Specify `unsafe-eval` to permit the Blazor WebAssembly Mono runtime to function. + * Specify any additional hashes to permit your required *non-framework scripts* to load. * In a Blazor Server app, specify hashes to permit required scripts to load. * [style-src](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/style-src): Indicates valid sources for stylesheets. * Specify the `https://stackpath.bootstrapcdn.com/` host source for Bootstrap stylesheets. @@ -468,3 +475,146 @@ Test and update an app's policy every release. * [Google CSP Evaluator](https://csp-evaluator.withgoogle.com/) :::moniker-end + +:::moniker range=">= aspnetcore-7.0" + +[Cross-Site Scripting (XSS)](xref:security/cross-site-scripting) is a security vulnerability where an attacker places one or more malicious client-side scripts into an app's rendered content. A CSP helps protect against XSS attacks by informing the browser of valid: + +* Sources for loaded content, including scripts, stylesheets, and images. +* Actions taken by a page, specifying permitted URL targets of forms. +* Plugins that can be loaded. + +To apply a CSP to an app, the developer specifies several CSP content security *directives* in one or more `Content-Security-Policy` headers or `` tags. For guidance on applying a CSP to an app in C# code at startup, see . + +Policies are evaluated by the browser while a page is loading. The browser inspects the page's sources and determines if they meet the requirements of the content security directives. When policy directives aren't met for a resource, the browser doesn't load the resource. For example, consider a policy that doesn't allow third-party scripts. When a page contains a `