Content-Security-Policy:拒绝执行内联脚本
Content-Security-Policy: Refused to execute inline script
我正在尝试实施内容安全策略。
我的 HTML 文件不包含任何 JavaScript 代码,除了包含外部 js 文件。但控制台仍然显示:
Refused to execute inline script because it violates the following Content Security Policy directive:
所以我的问题是:
包含外部 JavaScript 文件,例如 <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
被视为 "inline-script" ?
如果是这样,我该怎么做才能通过 CSP 允许这些脚本?我已经尝试在我的脚本中使用 nonce
但它总是说:
Undefined attribute name (nonce)
开发工具(例如 Google Chrome)是否提供查看哪个内联脚本导致错误的功能?
谢谢
- 在此上下文中,包含外部 JS 文件不被视为 "inline-script"。在
script-src
属性 中指定外部源就足够了,例如 script-src 'self' https://code.jquery.com/jquery-1.12.4.js
- 因为外部文件不被视为内联脚本,所以我不需要使用 nonce 或 hash。但是提供了信息here
- 在Google Chrome的开发工具中,我没有找到任何信息是哪一行或哪个外部JS文件导致错误。相反,我使用了 Firebug。至少提到了导致错误的行。有了这个帮助,您可以轻松消除 DOM 个被忽略的元素。
但真正对我有帮助的是here。
It’s very important to always define default-src. Otherwise, the directives will default to allowing all resources
在我的例子中,将 default-src 'self'
添加到 CSP 消除了错误!
我正在尝试实施内容安全策略。
我的 HTML 文件不包含任何 JavaScript 代码,除了包含外部 js 文件。但控制台仍然显示:
Refused to execute inline script because it violates the following Content Security Policy directive:
所以我的问题是:
包含外部 JavaScript 文件,例如
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
被视为 "inline-script" ?如果是这样,我该怎么做才能通过 CSP 允许这些脚本?我已经尝试在我的脚本中使用
nonce
但它总是说:Undefined attribute name (nonce)
开发工具(例如 Google Chrome)是否提供查看哪个内联脚本导致错误的功能?
谢谢
- 在此上下文中,包含外部 JS 文件不被视为 "inline-script"。在
script-src
属性 中指定外部源就足够了,例如script-src 'self' https://code.jquery.com/jquery-1.12.4.js
- 因为外部文件不被视为内联脚本,所以我不需要使用 nonce 或 hash。但是提供了信息here
- 在Google Chrome的开发工具中,我没有找到任何信息是哪一行或哪个外部JS文件导致错误。相反,我使用了 Firebug。至少提到了导致错误的行。有了这个帮助,您可以轻松消除 DOM 个被忽略的元素。
但真正对我有帮助的是here。
It’s very important to always define default-src. Otherwise, the directives will default to allowing all resources
在我的例子中,将 default-src 'self'
添加到 CSP 消除了错误!