9

我阅读了有关配置/实施 Content-Security-Policy 标头的信息,并且遇到了两种方法:

  1. 使用实现链接中给出的过滤器的自定义过滤器
  2. 使用元标记

请注意,此问题与此问题不重复,我正在寻找比链接中给出的更好的解决方案

我看到(1)中的缺点是它通过代码驱动,而不是通过配置文件,选项(2)中的缺点是如果我说 100 个 html 文件,我需要把这个标签放在每个 HTML 中?(如果我错了,请纠正我)我正在寻找的解决方案是我可以在 web.xml 中配置并适用于所有 html 文件的解决方案。我们在 web.xml 中配置 X-Frame-Options 的方式就像在这里给出的那样,难道我们没有在 web.xml 中配置 Content-Security-Policy 的类似方式吗?

4

2 回答 2

7

在 web.xml 中配置 content-security-policy

您可以使用OWASP 此处提供的建议。它是一个可以在后端实现的 Web 过滤器。

然后必须在您的web.xml文件中定义以下过滤器。这会在您的应用程序中的每个请求上调用。在 java 中,您可以通过创建适当的类来做到这一点。

    <filter>
        <filter-name>ContentSecurityPolicy</filter-name>
        <filter-class>YourPackagePath.ContentSecurityPolicyFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>ContentSecurityPolicy</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

以上将在您的 HTTP 标头中为 content-security-policy 实现以下值

默认源代码“无”;style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src '自我'; 框架源“自我”;连接源代码“自我”;形式动作“自我”;反射-xss 块

于 2019-05-31T18:42:43.043 回答
3

您是否尝试过使用https://github.com/sourceclear/headlines(死链接,这是我能找到的全部:https ://github.com/stevespringett/headlines )?它的目标是使与安全相关的标头成为配置问题,而不是您要求的代码。

{
  "XContentTypeConfig": {
    "enabled": true
  },

  "XFrameOptionsConfig": {
    "enabled": true,
    "value":"DENY"
  },

  "XssProtectionConfig": {
    "enabled": true
  },

  "HstsConfig": {
    "enabled": true,
    "includeSubdomains":true,
    "maxAge":31536000
  },

  "CspConfig": {
    "csp": {
      "default-src":["'self'"]
    },
    "cspReportOnly":{}
  },

  ... snip
}
于 2016-08-31T19:37:22.727 回答