内容安全策略(CSP)阻止eval方法调用

时间:2016-07-12 11:15:51

标签: javascript node.js content-security-policy

我正在使用nicEditor而在nicEditor中有一个名为eval的方法被CSP阻止。当我注释掉CSP代码时,它运行正常。

Error: call to eval() blocked by CSP     nicEdit.js:779:36

我的CSP代码:

scriptSrc: ["'self'", "'unsafe-inline'"]

我还阅读了https://developer.chrome.com/extensions/contentSecurityPolicy

提前致谢

2 个答案:

答案 0 :(得分:2)

如果您确实需要使用包含nicEditor的{​​{1}}(这可能不是一个好主意),您可以添加以下指令:'unsafe-eval'

我真的,真的,建议您只使用不依赖eval的不同编辑器。在大多数情况下,这确实是一种安全风险。

如果您需要替代方案,请查看ProseMirror例如。

答案 1 :(得分:0)

有问题的行位于(' who ', 'what ', ' hello from the other side ', ' this is slim shady ') (' who ', 'what ', 'side', ' hello from the other ', ' this is slim shady ', 'd', 'w', 'a', 'g', 'oh my ') 的{​​{1}}函数中(对我来说是682行):

addButton

将其替换为:

nicEditorPanel

我刚刚遇到了制作Google Chrome扩展程序的问题。 Chrome扩展程序加载程序不允许我使用' unsafe-eval'在CSP权限。无论如何,如上所述,出于安全原因,最好不要这样做。看一下代码看起来好像是使用var type = (button['type']) ? eval('(typeof('+button['type']+') == "undefined") ? null : '+button['type']+';') : nicEditorButton; 根据String的名称来调用构造函数。

由于所有全局函数都是var type = (button['type']) ? (typeof(button['type']) == "undefined") ? null : window[button['type']] : nicEditorButton; 对象的属性,因此我的更改以String形式(eval())调用构造函数的名称作为窗口的属性,而不是简单地对其进行评估。

相关问题