为什么iframe请求不发送Cookie?

时间:2018-12-18 22:29:44

标签: google-chrome security iframe cross-domain content-security-policy

同级部门已创建一个HTML文件,该文件实际上是少数iframe的支架。每个iframe调用一个报表,该报表托管在Web服务器上,参数略有不同。调用的报告将向未经身份验证的用户显示登录表单,或向已认证的用户显示报告内容。

scaffold.html:

<html>
   <head>
      <title>I just show the output from a bunch of report calls</title>
   </head>
   <body>
      <iframe src="https://somesite.com/useful_report.html?parameter1=a&parameter2=1" id="iframe1"></iframe>
      <iframe src="https://somesite.com/useful_report.html?parameter1=b&parameter2=2" id="iframe2"></iframe>
      <iframe src="https://somesite.com/useful_report.html?parameter1=c&parameter2=3" id="iframe3"></iframe>
      <iframe src="https://somesite.com/useful_report.html?parameter1=d&parameter2=4" id="iframe4"></iframe>
   </body>
</html>

兄弟姐妹组织向我们解释说,如果用户登录到https://somesite.com,则上述设置的效果很好-每个iframe都将显示几天内的valid_report.html内容。

当我

  1. 登录https://somesite.com,然后
  2. 将文件:/// C:/Users/me/Desktop/scaffold.html加载到Chrome

每个iframe都返回https://somesite.com登录表单。然后,如果我在单独的选项卡中打开utility_report.html,则会加载报告内容(证明somesite.com知道我仍在登录‡)。

使用开发人员工具,我可以看到对有用的_report.html的请求标头不包含“ Cookie:”属性,因此这解释了为什么有用的_report.html返回表单登录的原因。

我的问题是为什么iframe请求不发送Cookie?什么Chrome和/或服务器设置/策略/指令阻止了它?

‡-现在它知道我知道了。

2 个答案:

答案 0 :(得分:1)

如果您希望使用本机ajax或jquery ajax,请删除async:false。它对我有用。

为进一步兼容旧版浏览器,我建议使用http://easyxdm.net/wp/。 EasyXDM方法是使用iframe hack,该iframe hack要求您将html文件放置在要进行ajax调用的主机上。这将被强制异步,是的。但是,使用此easyXDM的好处在于,您不必担心cors标头。

答案 1 :(得分:0)

这是因为Chrome defaults to Lax SameSite cookie策略,这意味着除非用户看到不包含iframe的URL,否则不会发送cookie。

如果您拥有 somesite.com ,则可以通过将SameSite策略设置为“无”来退出此策略,并通过进行Double Submit Cookie处理CSRF攻击的风险。