【问题标题】:Why isn't "script.text = ..." considered unsafe under strict CSP?为什么在严格的 CSP 下“script.text = ...”不被认为是不安全的?
【发布时间】:2018-05-21 15:36:07
【问题描述】:

根据 MDN 在 script-src 上的文档,以下内容被认为是“不安全的”,并在严格的 CSP 下被阻止:

  • eval()
  • Function()
  • window.setTimeout("bad()", 0) 等方法中使用的字符串文字
  • window.execScript(仅限 IE

但为什么以下内容不在此列表中?和eval(badCode)有什么区别?

var script = document.createElement('script');
script.text = badCode;
document.head.appendChild(script);

【问题讨论】:

    标签: content-security-policy


    【解决方案1】:

    使用 script-src 将阻止内联脚本执行,因此设置脚本文本或 textContent 将不起作用。要检查它,请加载一个带有以下元标记的 html 文件:

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

    并尝试再次执行您的代码。

    来源:

    https://developer.chrome.com/extensions/contentSecurityPolicy#JSExecution https://developers.google.com/web/fundamentals/security/csp/?hl=en

    【讨论】:

      猜你喜欢
      • 2011-08-25
      • 1970-01-01
      • 2021-02-04
      • 2014-03-08
      • 2014-03-26
      • 2011-08-13
      • 2012-11-19
      • 2020-01-03
      • 1970-01-01
      相关资源
      最近更新 更多