【问题标题】:Make Angular working with restrictive Content Security Policy (CSP)让 Angular 使用限制性内容安全策略 (CSP)
【发布时间】: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);

【问题讨论】:

  • 您是否偶然在模板中使用生成的值?例如&lt;img [src]="function(input)"/&gt; 或&lt;object data="{{fieldValue}}"&gt;&lt;/object&gt;? Angular 不允许使用不安全的值来防止 XSS

标签: angular content-security-policy angular-cli


【解决方案1】:

@angular/cli>=8.2 的编辑答案

从this Github thread,可以使用angular.json中的index属性来控制应用程序的HTML索引的生成:

build: {
  ...
  "configurations": {
    "production": {
      "index": {
        "input": "src/index.production.html",
         "output": "index.html"
       },
      ...
    }
  }
}

原答案

我找到了一种方法,可以在我的生产环境中使用限制性 CSP,同时仍然能够使用 JTI 编译器进行开发。

  • 将第二个文件:index.production.html 添加到 src 文件夹。
  • 将index.html 的内容复制到该文件,并添加限制性CSP 标头。
<meta http-equiv="Content-Security-Policy" 
content="default-src 'none';
  frame-src 'self';
  script-src 'self';
  style-src 'self' 'unsafe-inline';
  font-src 'self';
  img-src 'self' data:;
  connect-src 'self'">
  • 然后,将以下内容添加到您的 angular.json:
build: {
  ...
  "configurations": {
    "production": {
      "fileReplacements": [
        {
          "replace": "src/index.html",
          "with": "src/index.production.html"
        }
      ],
      ...
    }
  }
}

这确保当您运行生产构建时,它将使用带有限制性 CSP 的 index.production.html,并且当您在本地运行它时,您可以使用 JTI 编译器。

【讨论】:

  • 这是一个很好的解决方案,但不再有效。 fileReplacements 属性不考虑像 index.html 这样的非捆绑文件。更新后的解决方案是github.com/angular/angular-cli/issues/…
  • 这与上述评论中的更新解决方案相结合,对我有用。我能够将 CSP 元数据添加到我的产品构建中,但 securityheaders.com 仍然说我缺少它们,我的许多(不是全部)woff2 文件现在也被拒绝了?
  • 我必须将 'unsafe-inline' 添加到 script-src 才能使其工作(Angular 12)
【解决方案2】:

使用提前编译可以解决问题。以下命令可用于构建使用限制性 CSP 的应用程序。

ng build --prod

要在本地进行测试,您可以使用

ng serve --prod

【讨论】:

  • 运行 ng serve 时:这是一个简单的服务器,用于在本地测试或调试 Angular 应用程序。尚未针对安全问题对其进行审查。 请勿将其用于生产!
  • 即使使用 ng build --prod 即生成 AOT 构建,也无法解决 CSP 问题。
【解决方案3】:

【讨论】:

  • 第一个链接也使用 zone.js 会触发相同的错误。在 GitHub 问题中,最后一条评论说它不适用于所有应用程序,只是简单的应用程序。
  • 离线模板编译器是相当新的。如果它不起作用,请使用最小的 repo 项目报告错误。
【解决方案4】:

我在 Angular 9 部署在 WildFly 服务器 时遇到了这个问题。该错误位于 WildFly 服务器 配置中,位于 standalone.xml 中设置的 response-header 中,而它不在 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=:reload

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

【讨论】:

    【解决方案5】:

    注意,我是从 Angular 10 文档中得到的,这里是:https://angular.io/guide/security

    当我在服务器响应中添加此标头时,我解决了这个问题。

    Content-Security-Policy: trusted-types angular; require-trusted-types-for 'script';
    

    【讨论】:

      猜你喜欢
      • 2021-12-26
      • 1970-01-01
      • 2023-01-07
      • 2020-04-08
      • 2016-03-25
      • 2013-10-09
      • 2015-07-28
      • 1970-01-01
      • 2023-03-26
      相关资源
      最近更新 更多