出于安全目的,我已从我的 CSP header 中删除了 script-src: 'unsafe-eval'
。我注意到这现在打破了谷歌图表。图表现在无法呈现并显示错误:
Invalid JSON string: {}
有什么想法,或者 Google 只是吹嘘它并允许在他们的库中进行不安全评估?我在他们的 map 上遇到了同样的问题,不得不使用不同的图书馆。
最佳答案
Google Visualization 确实在使用 eval()
内部
Currently Google Visualization API is using:
1. eval on corechart.I.js (3 times)
2. eval on jsapi
取自here ,关于 GV 和 Chrome App Manifest v2 兼容性的错误报告,大约 1 个月大。 Google Visualization 本身不符合 Manifest V2 的规则:
Is your code or library using eval(), new Function(), innerHTML, setTimeout(), or otherwise passing strings of JS code that are dynamically evaluated?
- Use JSON.parse() if you’re parsing JSON code into an object.
- Use a CSP-friendly library, for example, AngularJS.
- Create a sandbox entry in your manifest and run the affected code in the sandbox, using postMessage() to communicate with the sandboxed page.
所以 google 自己非常清楚这个问题。
我的印象是 GV 内部正在使用 eval(jsonString)
而不是 JSON.parse()
由于浏览器兼容性。 JSON.parse()
在 IE8、FF 3.1 等之前不可用(请参阅 compatibility list ),这就是为什么在阻止 eval()
时会出现无效 JSON 错误的原因出去。
因为这也会影响 GV 如何遵守 Manifest 规则,我想这个问题很快就会消失。
关于javascript - 谷歌图表不安全评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30744615/