gpt4 book ai didi

content-security-policy - 内容安全策略 'frame ancestors' 指令在 <meta> 元素中不起作用

转载 作者:行者123 更新时间:2023-12-01 12:09:31 30 4
gpt4 key购买 nike

作为点击劫持的一部分,我们正尝试将 CSP header 作为元标记添加到我们的 angular 项目中。下面是html

<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Security-Policy" content="frame-ancestors 'self'">
<meta charset="utf-8">
<title>QA Eval Webapp</title>
<base href="/">

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
</body>
</html>

但是,当我们尝试将页面作为 iframe 加载时,它会加载。我们已经在谷歌浏览器中进行了测试。

为了解决这个问题,我们构建了 angular 项目,将 dist 下的文件移动到了一个 Web 应用程序,添加了一个过滤器来为每个请求的响应添加 CSP header 。下面是代码
package com.web.beginner;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;

@WebFilter(urlPatterns="/*", filterName = "cspfilter")
public class CSPFilter implements Filter {

@Override
public void destroy() {
// TODO Auto-generated method stub

}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.setHeader("Content-Security-Policy", "frame-ancestors 'self'");
chain.doFilter(request, response);
}

@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub

}

}

这有效。

为什么添加到元标记中时 CSP header 不起作用?我什至查过 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy他们提到在元标记中添加 CSP。

我知道 X-FRAME-OPTIONS 不支持 html 元标记。内容安全策略也一样吗?还是 chrome 忽略了元标记中的 CSP?

最佳答案

规范要求浏览器忽略 frame-ancestors如果在 meta 中指定-元素政策。

所以申请一个frame-ancestors策略,您必须使用 Content-Security-Policy标题。

规范引用

https://w3c.github.io/webappsec-csp/#meta-element在 CSP 规范中,特别是:

Note: The Content-Security-Policy-Report-Only header is not supported inside a meta element. Neither are the report-uri, frame-ancestors, and sandbox directives.



https://html.spec.whatwg.org/multipage/semantics.html#attr-meta-http-equiv-content-security-policy的第4步HTML 规范中的内容安全策略状态 ( http-equiv="content-security-policy ") 算法(“此 pragma 在 Document 上强制执行内容安全策略”),要求:

Remove all occurrences of the report-uri, frame-ancestors, and sandbox directives

关于content-security-policy - 内容安全策略 'frame ancestors' 指令在 &lt;meta&gt; 元素中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53094845/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com