首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用限制内容安全策略(CSP)使角工作

使用限制内容安全策略(CSP)使角工作
EN

Stack Overflow用户
提问于 2016-08-03 04:58:04
回答 4查看 59.9K关注 0票数 42

我无法使基本Angular2 (最终)应用程序使用以下限制性CSP。

代码语言:javascript
复制
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和下面的说明创建的新应用程序。

代码语言:javascript
复制
ng new csp-test

index.html中插入定义以下限制性内容安全策略的元标记。

代码语言:javascript
复制
<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'">

然后送达应用程序。

代码语言:javascript
复制
ng serve

访问http://localhost:4200/时,该页面不会加载,因为脚本被CSP阻塞。

错误

lang.js

代码语言:javascript
复制
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'".

用源代码。

代码语言:javascript
复制
335: return new (Function.bind.apply(Function, [void 0].concat(fnArgNames.concat(fnBody))))().apply(void 0, fnArgValues);

zone.js

代码语言:javascript
复制
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'".(…)

用源代码。

代码语言:javascript
复制
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);
EN

Stack Overflow用户

发布于 2021-02-05 01:16:09

这个问题发生在部署在一个服务器中的WildFly 9中。这个错误在WildFly服务器配置中,在standalone.xml中设置的响应头中,而不是像我想的那样在index.html中。

standalone.xml

代码语言:javascript
复制
<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

代码语言:javascript
复制
./jboss-cli.sh --connect command=:reload

jboss-cli位于WildFly服务器的bin文件夹中

票数 0
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38734708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档