简体   繁体   English

使用带有 nonces 脚本的 CSP 时,是否有办法在非异步资产上记录资产加载错误?

[英]Is there a way to log asset load errors on non-async assets when using a CSP with nonces for scripts?

Because enabling use of nonces in CSP disables event handlers, it's not possible to add an onerror property to script tags to handle/report asset load issues.因为在 CSP 中启用随机数会禁用事件处理程序,所以不可能将onerror属性添加到脚本标记来处理/报告资产加载问题。

Is there a recommended way to handle this?有推荐的方法来处理这个吗? Particularly for script tags which are not loaded async.特别是对于未异步加载的脚本标签。

Thanks!谢谢!

If you dynamically load an external script, you can hang CSP-safe event listeners:如果动态加载外部脚本,则可以挂起 CSP 安全事件侦听器:

let script = document.createElement('script');

script.src = "https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.3.0/lodash.js"
document.head.append(script);

script.addEventListener('error', function(e) {
    // handle onerror event here
    });

try creating a separate service that only purpose to monitor, handle, and report/display errors.尝试创建一个仅用于监视、处理和报告/显示错误的单独服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM