Chrome Extension - Content Security Policy - executing inline code

Ask Question

Asked 6 years, 3 months ago

Active 3 months ago

Viewed 40k times

This question shows research effort; it is useful and clear

32

This question does not show any research effort; it is unclear or not useful

9

Bookmark this question.

Show activity on this post.

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.

google-chrome google-chrome-extension content-security-policy

share

Share a link to this question

Copy linkCC BY-SA 3.0

| improve this question | follow

Follow this question to receive notifications

|

edited Sep 3 ‘14 at 5:00

Amit G

asked Sep 2 ‘14 at 14:06

[

Chrome Extension - Content Security Policy - executing inline code - Stack Overflow - 图1

](/users/2724125/amit-g)

Amit GAmit G

2,03522 gold badges2020 silver badges4242 bronze badges

add a comment |

6 Answers 6

Active Oldest Votes

This answer is useful

23

This answer is not useful

Show activity on this post.

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

share

Share a link to this answer

Copy linkCC BY-SA 3.0

| improve this answer | follow

Follow this answer to receive notifications

|

edited May 23 ‘17 at 12:00

[

Chrome Extension - Content Security Policy - executing inline code - Stack Overflow - 图2

](/users/-1/community)

Community

111 silver badge

answered Sep 2 ‘14 at 14:58

[

Chrome Extension - Content Security Policy - executing inline code - Stack Overflow - 图3

](/users/934239/xan)

XanXan

64.8k1313 gold badges140140 silver badges167167 bronze badges

add a comment |

This answer is useful

16

This answer is not useful

Show activity on this post.

Copied from my answer to a similar question here. For recent versions of Chrome (46+) the current answer is no longer true. unsafe-inline still has no effect (in both the manifest and in meta header tags), but per the documentation, you can use the technique described here to relax the restriction.

Hash usage for <script> elements

The script-src directive lets developers whitelist a particular inline script by specifying its hash as an allowed source of script.

Usage is straightforward. The server computes the hash of a particular script block’s contents, and includes the base64 encoding of that value in the Content-Security-Policy header:

  1. Content-Security-Policy: default-src 'self';
  2. script-src 'self' https://example.com 'sha256-base64 encoded hash'

As an example, consider:

manifest.json:

  1. {
  2. "manifest_version": 2,
  3. "name": "csp test",
  4. "version": "1.0.0",
  5. "minimum_chrome_version": "46",
  6. "content_security_policy": "script-src 'self' 'sha256-WOdSzz11/3cpqOdrm89LBL2UPwEU9EhbDtMy2OciEhs='",
  7. "background": {
  8. "page": "background.html"
  9. }
  10. }

background.html:

  1. <!DOCTYPE html>
  2. <html>
  3. <head></head>
  4. <body>
  5. <script>alert('foo');</script>
  6. </body>
  7. </html>

Result:
Chrome Extension - Content Security Policy - executing inline code - Stack Overflow - 图4

I also tested putting the applicable directive in a meta tag instead of the manifest. While the CSP indicated in the console message did include the content of the tag, it would not execute the inline script (in Chrome 53).

new background.html:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'sha256-WOdSzz11/3cpqOdrm89LBL2UPwEU9EhbDtMy2OciEhs='">
  5. </head>
  6. <body>
  7. <script>alert('foo');</script>
  8. </body>
  9. </html>

Result:
Chrome Extension - Content Security Policy - executing inline code - Stack Overflow - 图5

share

Share a link to this answer

Copy linkCC BY-SA 3.0

| improve this answer | follow

Follow this answer to receive notifications

|

edited May 23 ‘17 at 12:32

[

Chrome Extension - Content Security Policy - executing inline code - Stack Overflow - 图6

](/users/-1/community)

Community

111 silver badge

answered Jul 24 ‘16 at 18:25

[

Chrome Extension - Content Security Policy - executing inline code - Stack Overflow - 图7

](/users/1698058/chris-hunt)

Chris HuntChris Hunt

3,24611 gold badge2222 silver badges4444 bronze badges

  • 2
    What about executing JS inline with a anchor href like <a href="javascript:void(0)">Hello</a>. I’ve tried rRMdkshZyJlCmDX27XnL7g3zXaxv7ei6Sg+yt4R3svU= and 97l24HYIWEdSIQ8PoMHzpxiGCZuyBDXtN19RPKFsOgk= with no luck. – kspearrin Sep 21 ‘16 at 20:27

  • the solution using manifest.json worked for me; the meta tag version did not work. – Yvonne Aburrow Nov 16 ‘17 at 14:32

  • 1
    I get a ‘content_security_policy’: Ignored insecure CSP value”sha256-TTV2e1hDY8O7+uUJbANScTuJ3ibjGZ9SqN6LdxfzDCs=”in directive’script-src’. – john ktejik Jun 28 at 19:26

add a comment |

This answer is useful

6

This answer is not useful

Show activity on this post.

You have something in your code like this:

  1. <button onclick="myFunction()"> Show password</button>

In a nutshell this is not allowed in chrome apps and extensions.

Change this to the following and it will work:

html:

  1. <button id="myButton"> Show password</button>
  2. <script src="script.js"></script>

script.js:

document.getElementById("myButton").addEventListener("click", myFunction); function myFunction(){ console.log('asd'); }

Long story:

In chrome apps, Content Security Policy does not allow inline javascript. So you have to put your javascript in a .js file and include it in your HTML.

Further reading: https://developer.chrome.com/extensions/contentSecurityPolicy

share

Share a link to this answer

Copy linkCC BY-SA 4.0

| improve this answer | follow

Follow this answer to receive notifications

|

answered Oct 31 ‘19 at 12:11

[

Chrome Extension - Content Security Policy - executing inline code - Stack Overflow - 图8

](/users/10333904/gtm)

GtmGtm

13511 silver badge1010 bronze badges

add a comment |

This answer is useful

1

This answer is not useful

Show activity on this post.

I’ve got this problem after I added a simple checkbox on the login page to toggle password visibility and here is how I have fixed my problem, hope it helps;

  • I’ve stopped using my checkbox’s onclick event, which was causing this problem in incognito mode of chrome and instead gave an id to my checkbox.

Before

  1. <div class="form__row">
  2. <div class="form__controls">
  3. <label class="checkbox"><input type="checkbox" onclick="togglePasswordVisibility()"> Show password</label>
  4. </div>
  5. </div>

After

  1. <div class="form__row">
  2. <div class="form__controls">
  3. <label class="checkbox"><input type="checkbox" id="checkboxTogglePasswordVisibility"> Show password</label>
  4. </div>
  5. </div>
  • I’ve created a Script.js file and added an event listener method into it to handle onclick event of my checkbox to do the job.

Remember to reference your js file, if you haven’t, yet. You can simply reference it like this.

  1. <script src="../Scripts/Script.js"></script>

And here is the event listener that I’ve added into my Script.js.

  1. $(document).ready(function () {
  2. addEventListenerForCheckboxTogglePasswordVisibility()
  3. });
  4. function addEventListenerForCheckboxTogglePasswordVisibility() {
  5. var checkbox = document.getElementById("checkboxTogglePasswordVisibility");
  6. if (checkbox !== null) {
  7. checkbox.addEventListener('click', togglePasswordVisibility);
  8. }
  9. }
  10. function togglePasswordVisibility() {
  11. var passwordElement = document.getElementById("password");
  12. if (passwordElement.type === "password") {
  13. passwordElement.type = "text";
  14. } else {
  15. passwordElement.type = "password";
  16. }
  17. }

Error before the fix;

Chrome Extension - Content Security Policy - executing inline code - Stack Overflow - 图9

share

Share a link to this answer

Copy linkCC BY-SA 4.0

| improve this answer | follow

Follow this answer to receive notifications

|

https://stackoverflow.com/questions/25625412/chrome-extension-content-security-policy-executing-inline-code/38555325#38555325