从具有沙箱属性的 <iframe> 发出的 XHR 请求的原始标头为 null

amb*_*ber 4 javascript google-chrome xmlhttprequest cors d3.js

我有一个项目,我正在尝试从数据处理程序下载一些制表符分隔值格式的数据,但是,Google Chrome 正在发送 Origin 标头值的空值。

\n\n

当我导航到http://server.corp.somebiz.com/reportpages/Report_Page_Requests_Over_Time.aspx?app=76ac42b7-ba6f-4be4-b297-758ebc9fe615时,我看到了这一点

\n\n
var url = \'http://server.corp.somebiz.com/DataHandlers/ReportSets.ashx?task=pagerequestsovertime&app=188d1956-c4a7-42f7-9bdd-38f54c14e125&format=tsv\';\n\nd3.tsv(url, function(d) {\n  d.date = parseTime(d.date);\n  d.close = +d.close;\n  return d;\n}, function(error, data) {\n  if (error) throw error;\n\n  console.log(\'Do stuff\');\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n

以下是请求的原始标头:

\n\n
GET /DataHandlers/ReportSets.ashx?task=pagerequestsovertime&app=786b5ef3-1389-4890-8004-533fd1f66f16&format=tsv HTTP/1.1\nHost: server.corp.somebiz.com\nConnection: keep-alive\naccept: text/tab-separated-values,*/*\nOrigin: null\nUser-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36\nAccept-Encoding: gzip, deflate, sdch\nAccept-Language: en-US,en;q=0.8\n
Run Code Online (Sandbox Code Playgroud)\n\n

这最终会在控制台上出现错误:

\n\n
XMLHttpRequest cannot load http://server.corp.somebiz.com/DataHandlers/ReportSets.ashx?task=pagere\xe2\x80\xa66ac42b7-ba6f-4be4-b297-758ebc9fe615&start=2/1/2017&end=3/2/2017&format=tsv. The \'Access-Control-Allow-Origin\' header has a value \'http://server.corp.somebiz.com\' that is not equal to the supplied origin. Origin \'null\' is therefore not allowed access.\n
Run Code Online (Sandbox Code Playgroud)\n\n

我不仅要查找发生这种情况的原因,还要查找导致 Chrome 向服务器发送 null Origin 标头的条件是什么。

\n\n

这似乎是 Chrome 特有的问题,因为 Internet Explorer 11 正在向服务器发送正确的 Origin 值。

\n\n

更新:添加另一个问题,这可能是也可能不是一个促成因素。

\n\n

我将调用页面加载到<iframe>元素中以隔离脚本元素。在 iframe 之外调用页面会导致不同的行为,Chrome 上的 Origin 标头完全丢失。

\n

sid*_*ker 6

如果iframe您\xe2\x80\x99正在加载的调用页面具有sandbox不\xe2\x80\x99t包含值的属性allow-same-origin,浏览器会给它一个\xe2\x80\x9cunique\xe2\x80\x9d origin:

\n\n
\n

设置[ sandbox] 属性后,内容将被视为来自唯一的来源,表单、脚本和各种可能令人烦恼的 API 将被禁用,链接将被阻止定位其他浏览上下文,并且插件将受到保护。关键字allow-same-origin导致内容被视为来自其真实来源,而不是强制其具有唯一来源

\n
\n\n

Origin\xe2\x80\xa6 并且在确定要在跨源请求中发送的标头的值时,浏览器会将任何唯一的来源序列化为null并为标头提供Origin该值。

\n