Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does CSP protect us if allowing unsafe-inline

Currently I'm defining Content Security Policy (CSP) as below;

Header set Content-Security-Policy: "default-src 'self' data:; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:;" 

Considering the CSP definition above, I have a challenge with inline JavaScript as it can be over-ridden at any time.

What is the use of unsafe-inline if it virtually does not protect?

like image 705
Zernel Avatar asked Oct 20 '16 03:10

Zernel


People also ask

What is the use of unsafe-inline in CSP?

The unsafe-inline option is to be used when moving or rewriting inline code in your current site is not an immediate option but you still want to use CSP to control other aspects (such as object-src, preventing injection of third-party js etc.).

What is CSP unsafe eval?

'unsafe-eval' allows the application to use the eval() JavaScript function. This reduces the protection against certain types of DOM-based XSS bugs, but makes it easier to adopt CSP. If your application doesn't use eval() , you can remove this keyword and have a safer policy.

Does CSP prevent XSS?

CSP is a browser security mechanism that aims to mitigate XSS and some other attacks. It works by restricting the resources (such as scripts and images) that a page can load and restricting whether a page can be framed by other pages.

What is Content-Security-Policy CSP used for?

Content Security Policy (CSP) is an added layer of security that helps to detect and mitigate certain types of attacks, including Cross-Site Scripting (XSS) and data injection attacks. These attacks are used for everything from data theft, to site defacement, to malware distribution.


1 Answers

The unsafe-inline option is to be used when moving or rewriting inline code in your current site is not an immediate option but you still want to use CSP to control other aspects (such as object-src, preventing injection of third-party js etc.). You are correct in that unsafe-inline does not offer much security as it allows execution of unsafe in-page scripts and event handlers.

Google's CSP Evaluator is a nifty tool to determine if your policy is strong.

A use case where the unsafe-inline option is used can be found in Google's Web Developer documentation on Content Security Policy:

A wedding-ring discussion forum admin wants to ensure that all resources are only loaded via secure channels, but doesn't really write much code; rewriting large chunks of the third-party forum software that's filled to the brim with inline script and style is beyond his abilities. The following policy would be effective:

Content-Security-Policy: default-src https:; script-src https: 'unsafe-inline'; style-src https: 'unsafe-inline' 

Even though https: is specified in default-src, the script and style directives don't automatically inherit that source. Each directive completely overwrites the default for that specific type of resource.

like image 165
Anand Bhat Avatar answered Sep 20 '22 14:09

Anand Bhat