拒绝框架,因为祖先违反了以下内容安全策略指令:"frame-ancestors"。

3

我为Tomcat实现了CSP过滤器。我正在尝试展示一个在我的Tomcat服务器上本地运行的iframe。

public class CSPFilter implements Filter {

    public static final String POLICY = "frame-ancestors https://10.10.11.172";

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) {
        if (response instanceof HttpServletResponse) {
            ((HttpServletResponse)response).setHeader("Content-Security-Policy", CSPFilter.POLICY);
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException { }

    @Override
    public void destroy() { }

}

并包含在web.xml中

<filter>
  <filter-name>CSPFilter</filter-name>
  <filter-class>com.vocera.reportservice.CSPFilter</filter-class>
;
</filter>
<filter-mapping>
  <filter-name>CSPFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

但是我仍然遇到以下错误:
由于祖先违反了以下内容安全策略指令:“frame-ancestors https://10.10.11.172”,因此拒绝了对“https://10.10.11.172:8443/”进行框架设置。
尝试使用“frame-src 'self' https://10.10.11.172;”后,该错误消失了,但页面变成了空白。
1个回答

1
如果端口不是默认端口,则需要在CSP源代码中包含它。根据https://www.w3.org/TR/CSP2/#match-source-expression
  1. 如果源表达式不包含端口部分且url-port不是url-scheme的默认端口,则返回不匹配。

  2. 如果源表达式包含端口部分,则当以下两个条件都为真时返回不匹配:

    1. port-part不包含U+002A ASTERISK字符(*)
    2. port-part不表示与url-port相同的数字

2
我也尝试了这两个带有端口的选项:"frame-ancestors https://10.10.11.172:8443";和"frame-src 'self' https://10.10.11.172:8443;" - VKS
你尝试过除了自定义端口之外还指定方案吗? - Halvor Sakshaug
1
是的,我尝试过使用http://和https://。 - VKS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接