Content security policy headers (CSP) #6752
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Create a possibility to generate Content security policy (csp) headers .
Content Security Policy is a crucial security standard that helps protect your web applications from various types of attacks, including Cross-Site Scripting (XSS), clickjacking, and other code injection attacks. It works by allowing you to specify which resources (scripts, styles, images, etc.) your browser should be allowed to load.
more info about Content security policy
Description of solution
The intend is to manage CSP for WebForms and MVC pipeline.
Webforms can not be very struct in the policy that can be used. Here script-src 'unsafe-inline' and 'unsafe-eval' will be automatically added to csp. It is not added in the settings because it's specific for webforms.
In the future MVC pipeline can be very strict. Here no js evaluation will be used and all inline javascript will be marked with a nonce.
This is the default csp for the setting
default-src 'self'; script-src 'self' 'report-sample'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'; object-src 'none'; base-uri 'none'; form-action 'self'; frame-ancestors 'none'; frame-src 'self'; connect-src 'self';
3 http headers will be managed : Content-Security-Policy, Content-Security-Policy-Report-Only and Reporting-Endpoints
Persona bar - Security settings
Implementation
It commes in a new project for csp management and a test project.
The IContentSecurityPolicy service that can be used with DI had all the stuff for skin and module developers to contribute to the policy.
For webforms skin developers actually the way to contribute is
Details of DotNetNuke.ContentSecurityPolicy library
The
DotNetNuke.ContentSecurityPolicy
library provides a fluent API for building and emitting Content Security Policy (CSP) headers in DNN. TheIContentSecurityPolicy
interface is the main entry point to compose directives, manage sources, configure reporting, and generate final header strings.Interface:
IContentSecurityPolicy
Namespace:
DotNetNuke.ContentSecurityPolicy
Properties
SourceCspContributor
fordefault-src
.SourceCspContributor
forscript-src
.SourceCspContributor
forstyle-src
.SourceCspContributor
forimg-src
.SourceCspContributor
forconnect-src
.SourceCspContributor
forfont-src
.SourceCspContributor
forobject-src
.SourceCspContributor
formedia-src
.SourceCspContributor
forframe-src
.SourceCspContributor
forframe-ancestors
.SourceCspContributor
forform-action
.SourceCspContributor
forbase-uri
.Methods
Inline
,Self
,Nonce
).plugin-types
(e.g.,application/pdf
).sandbox
options (e.g.,allow-scripts allow-same-origin
).form-action
source.frame-ancestors
source.report-to
group name to the policy.IContentSecurityPolicy
for chaining.Content-Security-Policy
header value.upgrade-insecure-requests
directive.Working with sources
Directive properties expose a
SourceCspContributor
, which supports adding/removing sources such as:AddSelf()
→'self'
AddNone()
→'none'
AddInline()
→'unsafe-inline'
AddEval()
→'unsafe-eval'
AddStrictDynamic()
→'strict-dynamic'
AddNonce(string)
→'nonce-<value>'
AddHash(string)
→'sha256-...'
,'sha384-...'
,'sha512-...'
AddHost(string)
→example.com
,https://cdn.example.com
AddScheme(string)
→https:
,data:
,blob:
RemoveSources(CspSourceType)
to remove by typeSee:
CspSourceType.cs
,CspSource.cs
,SourceCspContributor.cs
.Usage examples
Configure a baseline policy with a nonce
Parse and merge an existing CSP header
Remove an unsafe source
Notes
Nonce
in your inline tags:<script nonce="{policy.Nonce}">
.AddHeaders
is useful to import settings from configuration and extend them programmatically.Fixes #6720