我在使用webpack编译后重新加载我的Chrome扩展时遇到以下错误:
Uncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:".
at new Function (<anonymous>)
at evalExpression (compiler.js:33919)
at jitStatements (compiler.js:33937)
at JitCompiler.webpackJsonp.../../../compiler/esm5/compiler.js.JitCompiler._interpretOrJit (compiler.js:34520)
at JitCompiler.webpackJsonp.../../../compiler/esm5/compiler.js.JitCompiler._compileTemplate (compiler.js:34448)
at compiler.js:34347
at Set.forEach (<anonymous>)
at JitCompiler.webpackJsonp.../../../compiler/esm5/compiler.js.JitCompiler._compileComponents (compiler.js:34347)
at compiler.js:34217
at Object.then (compiler.js:474)
我的CSP授予unsafe-eval
权限。
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
我如何才能允许在我的代码中使用eval()
(因为webpack使用它来生成源Map)?
9条答案
按热度按时间lstz6jyr1#
我花了几个小时,但您可能想要做的是改变webpack使用的源Map的风格。默认情况下,它使用val。
https://webpack.js.org/configuration/devtool/
我将此代码添加到我的webpack中:
devtool: 'cheap-module-source-map'
解决这个问题的诀窍是找出为什么
webpack --mode development
有错误,而webpack --mode production
没有。另外,我使用的是Reaction,而不是聚合物,但我非常确定这一点仍然适用。
fcy6dtqo2#
通过Manifest阅读有趣的内容
https://developer.chrome.com/extensions/contentSecurityPolicy
已评估的Java脚本
通过在您的策略中添加‘unSafe-val’,可以放松针对val()及其相关对象(如setTimeout(字符串)、setInterval(字符串)和new函数(字符串)的策略:
但是,我们强烈建议不要这样做。这些函数是臭名昭著的XSS攻击载体。
1szpjjfi3#
感谢@兰迪的回复。但是,对于Vue CLI生成的VUE项目,没有
webpack.config.js
,因此解决方案将向vue.config.js
中添加以下配置:13z8s7eq4#
Chrome扩展实际上不允许使用
unsafe-eval
或eval
。https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Content_Security_Policy
在制作Chrome扩展时,要明白它受到内容安全策略的严格限制。请确保您已阅读并理解WebExpanies内容安全策略。如果您希望拥有如下所示的内联脚本:
您必须将脚本标记内容计算到其SHA256值中,并将其添加到您的清单中,以便允许其执行。
v7pvogib5#
Webpack V5
使用
--no-devtool
可以快速摆脱麻烦。没有评估代码,没有
.map
文件。Webpack CLI https://webpack.js.org/api/cli#negated-flags
neekobn86#
仅供参考,我遇到这个问题是因为我在Babel.config.js中添加了
istanbul
插件。生成结果包含
new Function('return this')
zlwx9yxi7#
清单3可以使用
3zwjbxry8#
Mv3中不允许使用来自外部域的脚本,所有脚本都必须包含在扩展包中。
请参阅Migrating to Manifest V3.
gpnt7bae9#
在我使用MVC5应用程序的情况下,我所要做的就是在Visual Studio中安装Nuget包:‘NWebsec.Mvc’,应用程序运行得很好。