大家好,我是tiantian。前几天,Chrome开发者博客发表了这样一篇文章:一般来说,Chrome以后会禁止修改document.domain。如果你的网站依赖于设置document.domain来解决跨域问题,那么你可能需要注意了。并且Chrome计划从Chrome101开始让document.domain不可变,这意味着如果你依赖这种方式放宽同源策略,你的网站可能会受到影响。MDN对domain的定义如下:Document接口的domain属性获取/设置当前文档的原始domain部分,常用于同源策略。要了解有关设置document.domain的安全影响的更多信息,请阅读MDN:https://developer.mozilla.org/en-US/docs/Web/API/Document/domain#setter同源策略:保证一个网页无法访问(修改或提取数据)另一个页面,除非这些页面托管在同一来源。为了解决很多跨域通信问题,很多网站都会通过设置document.domain来达到目的。例如,user1.example.com和user2.example.com不能相互访问。为了实现这种跨域访问,他们可以通过将document.domain设置为他们的公共域后缀来授予彼此访问权限。跨域使用的方案有很多种。有多少站点使用这种方法来放宽同源策略?在2020-12-01HTTPArchive语料库中,我们看到7038个页面(共7,849,064个:0.09%)的行为受到document.domain的影响。例如,阿里巴巴在*.alibaba.com域(即https://baominhjsc.trustpass.alibaba.com/)上运行店面,它依赖于document.domain与来自onetalk.alibaba.com的域进行通信。另一个例子是qq.com有大约110个子域,它们依赖document.domain与apps.game.qq.com等框架通信登录状态。那么对于我们开发者来说,以后真的禁用了document.domain,怎么办呢?新提案Origin-Agent-Clusterhttp标头(规范)允许按来源(而不是站点)隔离页面请求。如果设置为true(Origin-Agent-Cluster:?1),要求浏览器按来源分离页面。如果为假,则按站点。(AgentCluster是隔离组的规范,由于底层隔离在API层是不可见的,所以规范只是粗略的涉及到这个话题。)详细解读:Origin-Agent-Cluster:header将继续发挥作用当前在场时执行。当标题不存在时,将更改的是默认值。当页面分配给document.domain但未设置Origin-Agent-Cluster:?0标头时,我们将实施控制台警告。因此,将来有可能通过document.domain放宽同源策略的唯一方法是发送Origin-Agent-Cluster:?0标头。可能会有一个比较迷惑的问题,谁需要设置Origin-Agent-Clusterheader?个人认为,任何想要设置document.domain以实现跨域通信的页面都需要发送Origin-Agent-Cluster:?0以选择加入该功能。浏览器兼容性对这项工作持开放态度,Origin规范声明应删除此功能。主要意思是强调避免使用document.domain,这样会破坏同源策略提供的安全保护。由于这些安全问题,此功能已从Web平台中删除。(这是一个漫长的过程,需要很多年。)WebKit表示他们对弃用document.domain持中等程度的积极态度。如果需要看他们的讨论,可以阅读以下内容:https://github.com/w3ctag/design-reviews/issues/564#issuecomment-768450217总结document.domain以后是否会被禁用放宽一样-originpolicy,这个过程应该很长。对于开发者,我们可以通过设置Origin-Agent-Clusterhttpheader来进行过渡。至于最终的方案,值得我们期待。参考https://developer.mozilla.org/zh-CN/docs/Web/API/Document/domain#setterhttps://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policyhttps://developer.chrome.com/blog/immutable-document-domain/https://developer.mozilla.org/zh-CN/docs/Web/API/Window/postMessagehttps://github.com/mikewest/deprecating-文档域/https://github.com/mikewest/deprecating-document-domain/https://github.com/mikewest/deprecating-document-domain/blob/main/2020-12-document-domain-usage。csvhttps://html.spec.whatwg.org/multipage/origin.htmlhttps://github.com/w3ctag/design-reviews/issues/564#issuecomment-768450217
