【发布时间】:2016-08-03 04:58:04
【问题描述】:
我无法使基本 Angular2(最终)应用程序与以下限制性 CSP 一起工作。
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 repository。您也可以按照以下说明进行操作。
将最后一个 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 不允许使用不安全的值来防止 XSS
标签: angular content-security-policy angular-cli