我无法使基本Angular2 (最终)应用程序使用以下限制性CSP。
default-src 'none';
script-src 'self';
style-src 'self';
font-src 'self';
img-src 'self' data:;
connect-src 'self'在lang.js中有一个不安全级别的错误,在zone.js中有两个。你能提供一个解决方案吗?
用角CLI复制的步骤
我创建了一个GitHub存储库。您还可以按照下面的说明操作。
使用Webpack 6.0.8的最后一个角CLI和下面的说明创建的新应用程序。
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);发布于 2021-02-05 01:16:09
这个问题发生在部署在一个服务器中的WildFly 9中。这个错误在WildFly服务器配置中,在standalone.xml中设置的响应头中,而不是像我想的那样在index.html中。
standalone.xml
<filters>
<response-header name="server-header" header-name="Server" header-value="JBoss-EAP/7"/>
<response-header name="x-powered-by-header" header-name="X-Powered-By" header-value="Undertow/1"/>
>>> Here >>> <response-header name="Content-Security-Policy" header-name="Content-Security-Policy" header-value="default-src 'self'; style-src 'self' 'unsafe-inline'"/>
<response-header name="x-frame-options" header-name="X-Frame-Options" header-value="SAMEORIGIN"/>
<response-header name="x-xss-protection" header-name="X-XSS-Protection" header-value="1; mode=block"/>
<response-header name="x-content-type-options" header-name="X-Content-Type-Options" header-value="nosniff"/>
<response-header name="strict-transport-security" header-name="Strict-Transport-Security" header-value="max-age=31536000; includeSubDomains;"/>
<response-header name="my-custom-header" header-name="my-custom-header" header-value="my-custom-value"/>
</filters>注:重新启动WildFly Server
./jboss-cli.sh --connect command=:reloadjboss-cli位于WildFly服务器的bin文件夹中
https://stackoverflow.com/questions/38734708
复制相似问题