| layout | post |
|---|---|
| title | Content Security Policy (CSP) - Syncfusion |
| description | Learn how to use Syncfusion Blazor components with a strict Content Security Policy (CSP) in Blazor Web App, Blazor WebAssembly (WASM), and Blazor Server. |
| platform | Blazor |
| control | Common |
| documentation | ug |
Content Security Policy (CSP) is a browser security feature that protects your application against malicious attacks like cross-site scripting (XSS) and data injection. It works by controlling where your application can load scripts, styles, images, fonts, and other resources from.
Add CSP directives to the <head> tag of your application's main HTML file:
- For .NET 8, 9, or 10 Blazor Web Apps (Server, WebAssembly, or Auto modes): Add to
~/Components/App.razor - For Blazor WebAssembly Standalone Apps: Add to
wwwroot/index.html
Syncfusion now provides strict CSP compatibility for over 80 components. This means most of your application's core functionality can work securely without needing unsafe directives like 'unsafe-eval' or 'unsafe-inline'.
This makes it easier for you to enforce strong security policies while still having access to all component features.
The following CSP configurations are recommended for Syncfusion® Blazor components that support strict CSP (Refer Supported list below).
{% tabs %}
{% highlight html tabtitle="Blazor Server App" %} {% endhighlight %}
{% highlight html tabtitle="Blazor WebAssembly" %} {% endhighlight %}
{% endtabs %}
Why 'wasm-unsafe-eval' for WebAssembly ?
WebAssembly requires the 'wasm-unsafe-eval' directive to compile and run. This is different from 'unsafe-eval' and is necessary for client-side WebAssembly applications.
Most Syncfusion components support strict CSP. However, some components or features still need the style-src 'unsafe-inline' directive. Read the sections below to determine if your application needs it.
The following components need inline styles to work and always require 'unsafe-inline':
| Category | Components | Reason for unsafe-inline Requirement |
| Data Visualization |
|
|
| File Viewers & Editors |
|
|
| Interactive Chat |
|
|
| File Management |
|
|
| Layout |
|
|
| Diagrams and Maps |
|
|
| Kanban |
|
|
These components work under strict CSP for most features, but specific advanced features need 'unsafe-inline':
| Category | Components |
|---|---|
| Data Management | Pivot Table - Click here for feature details |
| Scheduling | Gantt Chart - Click here for feature details |
| Charts | Circular Gauge - Click here for feature details Heatmap Chart - Click here for feature details |
| Navigation | TreeView - Click here for feature details |
| Maps | Maps -Click here for feature details |
If you add style attributes directly through InputAttributes or HtmlAttributes, strict CSP will block them:
@* This won't work under strict CSP *@
<SfTextBox InputAttributes='@(new Dictionary<string, object> { { "style", "width:200px;" } })' />Better approach: Use component properties instead:
@* Use built-in properties like Width instead *@
<SfTextBox Width="200px" />
@* Or apply CSS classes to style the component *@
<style>
.my-textbox { width: 200px; }
</style>
<SfTextBox CssClass="my-textbox" />This keeps your CSP strict while still achieving your styling goals.
If your application needs any of the above scenarios, use this configuration:
<meta http-equiv="Content-Security-Policy"
content="base-uri 'self';
default-src 'self';
connect-src 'self' https: ws: wss:;
img-src 'self' data: https:;
object-src 'none';
script-src 'self';
style-src 'self' 'unsafe-inline';
font-src 'self' data:;
upgrade-insecure-requests;">This allows inline styles while keeping the rest of your security policy strict.