Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content security unsafe-eval policy only for one url

How is it possible to add a directive 'unsafe-eval' only for one source ?

I'm developing a cordova application and as I need to allow script-src from multiple source (external script like twitter, etc..) I set in meta http-equiv="Content-Security-Policy : script-src *

<meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline' data: gap: * 'unsafe-eval'; style-src 'self' 'unsafe-inline'; script-src * 'self' 'unsafe-inline' 'unsafe-eval';">

This works but it is dangerous because it's wide open.

So i would like to add 'unsafe-eval' only for the script that needs it (https://maps.googleapis.com in my example)

Is it possible ?

like image 563
Nicolas Avatar asked Aug 10 '16 15:08

Nicolas


People also ask

What is unsafe eval Content-Security-Policy?

'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.

Can you bypass CSP?

If the application is using angular JS and scripts are loaded from a whitelisted domain. It is possible to bypass this CSP policy by calling callback functions and vulnerable class.

How do I ignore Content-Security-Policy?

Click the extension icon to disable Content-Security-Policy header for the tab. Click the extension icon again to re-enable Content-Security-Policy header. Use this only as a last resort. Disabling Content-Security-Policy means disabling features designed to protect you from cross-site scripting.


1 Answers

You can't.

'unsafe-eval' in a policy isn't some kind of flag or attribute that is applied to some particular script source. Instead, it is a script source in and of itself, as you can see in this excerpt from the CSP spec:

source-expression = scheme-source / host-source / keyword-source / nonce-source / hash-source
scheme-source     = scheme-part ":"
host-source       = [ scheme-part "://" ] host-part [ port-part ] [ path-part ]
keyword-source    = "'self'" / "'unsafe-inline'" / "'unsafe-eval'"

Also, even if you could, this wouldn't protect you from some other script triggering the unsafe eval within the maps api code with its own string to evaluate.

Have a look at CSP unsafe-eval using Google Maps API though, maybe you can get rid of 'unsafe-eval' altogether.

like image 178
aferber Avatar answered Oct 13 '22 00:10

aferber