Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Extension - Content Security Policy - executing inline code

I am using an external JavaScript lib in my chrome extension. I has inline execution, so I get following kind of error

(The error I get on console)

Refused to execute JavaScript URL because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension://". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.

The error message clearly says there is a work-around possible.

Chrome-Content Security Policy says not possible. Many related question cited this link.

Blog This blogger says it is possible, but probably this is applicable to only older chrome extension.

Any work around possible?

PS: don't wanna/can't change the entire library I am using.

EDIT: how to use hash or nonce to enable inline execution.

like image 201
Amit G Avatar asked Sep 02 '14 14:09

Amit G


People also ask

How do I enable an inline script in CSP?

Other methods. The unsafe-inline source list value can be used to allow inline scripts, but this also defeats much of the purpose of CSP. CSP Level 3 (newest browsers) support a source list value: unsafe-hashes which can be used to allow inline script in javascript event handlers (eg onclick or onmouseover , etc).

What is 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.).

How do I enable Content-Security-Policy in Chrome?

To edit the configuration, go to chrome://extensions and click Options under Content Security Policy Override. The text area in the Options automatically saves as you edit.

How do I disable Content-Security-Policy in Chrome?

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

No, this is not possible to relax this policy. unsafe-inline is specifically ignored by Chrome Extensions since manifest version 2.

Documentation (emphasis mine):

There is no mechanism for relaxing the restriction against executing inline JavaScript. In particular, setting a script policy that includes 'unsafe-inline' will have no effect.

The error message mentions several possible ways, but the docs are clear that no CSP will allow inline scripting, and ignoring unsafe-inline is but one of the measures.

Update

As of Chrome 46, inline scripts can be whitelisted by specifying the base64-encoded hash of the source code in the policy. This hash must be prefixed by the used hash algorithm (sha256, sha384 or sha512). See Hash usage for elements for an example.

See this answer for more in-depth look at whitelisting.

like image 145
Xan Avatar answered Nov 12 '22 22:11

Xan