default-src 'none';
script-src 'self';
style-src 'self';
font-src 'self';
img-src 'self' data:;
connect-src 'self'
在lang.js中有一个unsafe-eval错误,在zone.js中有两个。您能提供解决方案吗?
使用Angular CLI重现步骤
我创建了一个GitHub存储库。您也可以按照以下说明进行操作。
使用最新的Angular CLI和Webpack 6.0.8以及以下说明创建的新应用程序。
ng new csp-test
请在
index.html
中插入定义以下限制性内容安全策略的元标签。<meta
http-equiv="Content-Security-Policy"
content="default-src 'none';script-src 'self';style-src 'self';font-src 'self';img-src 'self' data:;connect-src 'self'">
然后启动应用程序。
ng serve
访问 http://localhost:4200/ 时,页面因为脚本被 CSP 阻止而无法加载。
错误
lang.js
lang.js:335 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'".
使用源代码。
335: return new (Function.bind.apply(Function, [void 0].concat(fnArgNames.concat(fnBody))))().apply(void 0, fnArgValues);
zone.js
zone.js:344 Unhandled Promise rejection: 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'".
; Zone: <root> ; Task: Promise.then ; Value: 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'".
zone.js:346 Error: Uncaught (in promise): 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'".(…)
使用源代码。
343: if (rejection) {
344: console.error('Unhandled Promise rejection:', rejection instanceof Error ? rejection.message : rejection, '; Zone:', e.zone.name, '; Task:', e.task && e.task.source, '; Value:', rejection, rejection instanceof Error ? rejection.stack : undefined);
345: }
346: console.error(e);
<img [src]="function(input)"/>
或<object data="{{fieldValue}}"></object>
这样的内容?Angular 为了防止跨站脚本攻击,不允许使用不安全的值。 - Scrambo