document.domain与三级域名的问题
以前如果要使iframe里面的脚本能访问parent的内容,但iframe和parent的二级域名相同,那一般都会在两者都写上document.domain="xxx.com" 以放宽访问权限。
今天发现,如果iframe和parent在同一个三级域名下,比如都是aa.bb.com,那设了document.domain反而会造成访问拒绝。
查了下MSDN,有如下解释:
Remarks
The property initially returns the host name of the server from which the page is served. The property can be assigned the domain suffix to allow sharing of pages across frames. For example, a page in one frame from home.microsoft.com and a page from www.microsoft.com initially would not be able to communicate with each other. However, by setting the domain property of both pages to the suffix "microsoft.com", you ensure that both pages are considered secure and access is available between the pages.
When you set the domain property, use the domain name determined by the server rather than by the client browser.
All the pages on different hosts must have the domain property explicitly set to the same value to communicate successfully with each other. For example, the value of the domain property of a page on the host microsoft.com would be "microsoft.com" by default. It might seem logical that if you set the domain property of a page on another host named msdn.microsoft.com to "microsoft.com," that the two pages could communicate with each other. However, this is not the case unless you have also explicitly set the domain property of the page on microsoft.com to "microsoft.com".
Furthermore, this property cannot be used to allow cross-frame communication among frames with different domain suffixes. For example, a page in one frame from www.microsoft.com and a page in another frame from www.msn.com would not be able to communicate with each other even if the domain property of both pages was set to the suffix "microsoft.com".
Security Alert Using this property incorrectly can compromise the security of your Web site. Set the domain property only if you must allow cross-domain scripting. Use a value determined on the server. Setting this property to a value determined on the client (like through the location object) could expose your site to attack from another site through Domain Name System (DNS) manipulation. For more information, see Security Considerations: Dynamic HTML.
For more information on domain security, see About Cross-Frame Scripting and Security.
中文翻译:
在DHTML中,不同的窗口和框架(frames)中的内容可以通过对象模式用脚本相互交互。但是,由于可以在一个浏览器中通过多个窗口和框架同时显示多个毫不相干的内容,因此制定一定的限制性规则用于确保数据的真实性和保护个人隐私信息就显得很有必要的。
这片文章描述了为什么要添加这些限制性规则,以及这些规则在是如何应用在DHML模型上的。所有这些关于脚本交互的规则在windows, dialog boxes, frameSets, frames和iframes这些对象上都是一律适用的。
对于绝大多数内容而言,只有来至于同一个Web域的内容之间的相互交互操作才是允许的。例如,在www.studyez.com 上的一个页面可以用脚本自由的操作任何在www.studyez.com 这个站点的任何其他页面,但是不能通过脚本去操作在其他Web网站上的页面,即使这个页面通过iframe嵌入在www.studyez.com 的页面中。DHTML对象模型使用document.domain属性来判定这个限制规则:只有在同一个Web域里的页面才能自由交互。判断是否属于同一个Web域,要看域名主机、协议以及端口三者是否一致,只有三者完全相同,才能称之为同一个域。比如http://www.studyez.com/ 的页面与 https://www.studyez.com 的页面之间不能用脚本交互操作(因为协议不同),http://www.studyez.com 的页面与http://community.studyez.com 的页面之间也不能用脚本相互交互(因为域名主机不同),http://www.studyez.com:888/ 与 http://www.studyez.com/ 的页面之间一样也不能交互操作(因为端口不同)。
一个页面的脚本许可操作范围可以通过设置这个页面的document.domain属性而扩大到具有相同父域名的所有站点中。例如,在http://www.studyez.com/ 中的一个页面,通过脚本设置document.domain属性(初始默认 www.studyez.com)为studyez.com,从而使得在http://gongwuyuan.studyez.com , http://community.studyez.com, 以及所有其他studyez.com所属的字域名中的页面都可以与这个页面进行交互操作,只要这些页面也相应的设置document.domain属性为studyez.com即可。由于只有那些来至于同一个父域studyez.com所属的站点的页面才可以设置document.domain=studyez.com,所以这也间接的保证了这些内容都是来自于同一个提供者并且双方都已经明确地作了允许互操作的设置。这个设置限制为父域名必须是有效的二级域名,对于.com, .net, 是如studyez.com,对于国际域名,如.cn,则是studyez.com.cn 或 studyez.cn,而不能是com.cn。
为了确保在windows和frames中可以加载任何网站的内容,所以导航操作是一直允许的,不受前面所述的规则限制。只有读取或者修改内容才是被限制的。如可以设置href属性以导航到新的页面,但是如果加载的页面不在同一个Web域里,则不能读取href属性,因为如果可以读取href属性的话,就可以知道用户正在浏览什么页面,而这是违反用户隐私策略的。对于处于不同Web域情况下,与导航相关属性的操作限制如下:window.location.href 可以设置用于导航到新的页面,但是不可以读取
window.location除href之外的属性 禁止
document.location.href 可写不可读
document.location除href之外的属性 禁止
iframe.src 可写不可读当脚本尝试受限的操作时,都会抛出一个"拒绝访问"的错误。
从安全的角度理解跨域脚本操作的限制是非常重要的,因为否则的话,一个恶意站点的页面可以通过嵌入iframe加载别的网站的内容从而通过脚本和DHTML模型为所欲为。
