Electron中Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self'".


不只是Electron頁面,CSP(Content Security Policy)對於普通瀏覽器一樣生效。

方法一: 去掉Content Security Policy

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

刪除

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
    <meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'">
    <title></title>
  </head>
  <body>
    <script>
(省略號)
    </script>
  </body>
</html>

中的

    <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">

即可解決。但是會弱化應用的安全性:

Electron Security Warning (Insecure Content-Security-Policy) This renderer process has either no Content Security
    Policy set or a policy with "unsafe-eval" enabled. This exposes users of
    this app to unnecessary security risks.

For more information and help, consult
https://electronjs.org/docs/tutorial/security.
This warning will not show up
once the app is packaged.

方法二:不采用內聯

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
    <meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'">
    <title></title>
  </head>
  <body>
    <script src="render.js"></script>
  </body>
</html>

CSP為什么禁止執行inline script?

參考:
https://stackoverflow.com/questions/46256983/script-causes-refused-to-execute-inline-script-either-the-unsafe-inline-keyw

https://developers.google.com/web/fundamentals/security/csp/#if_you_absolutely_must_use_it

淺談XSS攻擊的那些事(附常用繞過姿勢)https://zhuanlan.zhihu.com/p/26177815

內聯代碼被視為是有害的。

不過,基於來源的白名單無法解決 XSS 攻擊帶來的最大威脅: 內聯腳本注入。如果攻擊者可以注入一個 script 標記,在標記中直接包含一些惡意的負載(<script>sendMyDataToEvilDotCom();</script>),則瀏覽器將無法將它與合法內聯腳本標記區分開來。CSP 可通過完全禁止內聯腳本來解決此問題: 這是唯一確定有效的方式。(……此處省略N段示例代碼……)除了能夠更好地配合 CSP 外,重寫的代碼還具有許多優勢;無論您是否使用 CSP,這都是最佳做法。 內聯 JavaScript 混合結構和行為的方式正是您不應采用的方式。使用外部資源,瀏覽器更容易緩存,開發者也更容易理解,並有助於編譯和壓縮。如果您將代碼移入外部資源,那么您可以編寫更好的代碼。 以相同方式處理內聯樣式: style 屬性和 style 標記都應合並到外部樣式表,以防范可通過 CSS 實現的各種極其狡猾的 數據滲漏方法。

例如,你的網站有一個實時顯示用戶留言的功能,如果有個用戶留言為

<script>一段惡意的代碼,將可能被運行在其它客戶端上,瀏覽器無法分辨</script>

這樣子,“瀏覽器將無法將它與合法內聯腳本標記區分開來”。

方法三:修改CSP,列出允許執行的腳本代碼的Hash值

參考:http://www.ruanyifeng.com/blog/2016/09/csp.html

nonce值的例子就不試了,因為貌似需要服務端的配合。。。。。。。。。。。。。

修改CSP,添加對應腳本的Hash值:

script-src 'self' 'sha256-7+h+PlvW2r7C/54Jfw3CKKxxm09qDfh5XfqJDZKp1t0='

Hash值對應的代碼如下:

    <script>
      console.log("dasdas");
    </script>

這樣這段inline script就可以順利執行了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 Refused to load the script xxxxxx because it violates the following Content Security Policy directive:"script-src 'self' xxxxxxxxxxxxx" 瀏覽器安全問題 vscode electron error (Refused to execute inline event handler because it violates the following Content Security Policy directive ... 和 Uncaught ReferenceError: process is not defined) 解決 Jenkins 中無法展示 HTML 樣式的問題,csp問題,Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self'". 開發chrome插件報錯:Refused to execute inline event handler because it violates the following Content Security Policy directive because it violates the following Content Security Policy directive: "default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'". Note that 'script-src' was not explicitly set, so 'default-s because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'style-src' was not explicitly set, so 'default-src' is used as a fallback.錯誤應該怎么解決? iframe because an ancestor violates the following Content Security Policy directive: "frame-ancestors 'self' it violates the following Content Security Policy directive: "default-src 'self' http://example.com". Note that 'img-src' was not explicitly set, so 'default-src' is used as a fallback. Electron中提示:Refused to execute inline event handler because it violates Refused to display because an ancestor violates the following Content Security問題簡單解決
 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM