我无法使基本的Angular2(最终)应用程序与以下限制性CSP一起使用。
default-src 'none';
script-src 'self';
style-src 'self';
font-src 'self';
img-src 'self' data:;
connect-src 'self'
有一个不安全的-EVAL错误lang.js和两个zone.js。您能提供解决方案吗?
我已经创建了一个GitHub存储库。您也可以按照以下说明进行操作。
将最后一个Angular CLI与Webpack 6.0.8一起使用,并使用以下说明创建新的应用程序。
ng new csp-test
在index.html
meta标记中插入,以定义以下限制性内容安全策略。
<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: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: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);
使用最新的Angular CLI版本(从1.0.0-beta.17开始)已解决了该问题。以下命令可为正在运行的应用程序提供服务,因为它包含了提前编译。
ng serve --prod
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句