Webkit的跨域安全问题说明
更新时间:2011年09月13日 22:17:02 作者:
在使用try catch处理iframe跨域产生的异常时,chrome和safari浏览器似乎不能正常运作:他们直接抛出了错误而没有抛出可供JS截获的异常。
这里有个简单的测试页面:IE、火狐弹出"hello world",而chrome,safari,opera毫无反应。
以下是小段测试代码(刻意修改domain,让父页面和子页面为不同域页面):
1.父页面代码:
<script>
document.domain = "nunumick.me";
function doTest(){
alert('hello world');
}
</script>
<iframe src="http://www.nunumick.me/lab/x-domain/webkit-test.html">
</iframe>
2.子页面代码:
<script>
try{
top.name;
}catch(e){
document.domain = 'nunumick.me';
top.doTest();
}
</script>
以上代码目的是尝试在访问异常时动态修改domain达到顺利访问,但webkit内核浏览器粗暴地报错而非抛出可截获的异常,其他浏览器均如期运行。
chrome错误信息:
try{
//see if we can access the iframe's location
//without a permission denied error
var iframeSearch = _getSegment(iframeLoc.href, "?");
//good, the iframe is same origin (no thrown exception)
if(document.title != docTitle){
//sync title of main window with title of iframe.
docTitle = this.iframe.document.title = document.title;
}
}catch(e){
//permission denied - server cannot be reached.
ifrOffline = true;
console.error("dojo.hash: Error adding history
entry. Server unreachable.");
}
再如FCKeditor
try{
if ( (/fcksource=true/i).test( window.top.location.search ) )
sFile = 'fckeditor.original.html' ;
}
catch (e) { /* Ignore it. Much probably we are insi
de a FRAME where the "top" is in another domain (security error). */ }
以下是小段测试代码(刻意修改domain,让父页面和子页面为不同域页面):
1.父页面代码:
复制代码 代码如下:
<script>
document.domain = "nunumick.me";
function doTest(){
alert('hello world');
}
</script>
<iframe src="http://www.nunumick.me/lab/x-domain/webkit-test.html">
</iframe>
2.子页面代码:
复制代码 代码如下:
<script>
try{
top.name;
}catch(e){
document.domain = 'nunumick.me';
top.doTest();
}
</script>
以上代码目的是尝试在访问异常时动态修改domain达到顺利访问,但webkit内核浏览器粗暴地报错而非抛出可截获的异常,其他浏览器均如期运行。
chrome错误信息:
据了解,采用此类try catch方式做安全可行性判断的并不只是个别现象,如DOJO
复制代码 代码如下:
try{
//see if we can access the iframe's location
//without a permission denied error
var iframeSearch = _getSegment(iframeLoc.href, "?");
//good, the iframe is same origin (no thrown exception)
if(document.title != docTitle){
//sync title of main window with title of iframe.
docTitle = this.iframe.document.title = document.title;
}
}catch(e){
//permission denied - server cannot be reached.
ifrOffline = true;
console.error("dojo.hash: Error adding history
entry. Server unreachable.");
}
再如FCKeditor
复制代码 代码如下:
try{
if ( (/fcksource=true/i).test( window.top.location.search ) )
sFile = 'fckeditor.original.html' ;
}
catch (e) { /* Ignore it. Much probably we are insi
de a FRAME where the "top" is in another domain (security error). */ }
还有很多网友的反馈:chrome bug report
以上代码在chrome,safari,opera均不适用。翻了一些资料,记录在此:
1.html5 security location
2.webkit dev lists
从webkit开发人员的讨论消息中看到,他们承认这个问题但并不情愿去改正,holly shit!
您可能感兴趣的文章:
- jquery下利用jsonp跨域访问实现方法
- jquery下异步提交表单 异步跨域提交表单
- 基于Jquery的跨域传输数据(JSONP)
- Ajax跨域代理访问网络资源的实现代码
- 使用JSON实现数据的跨域传输的php代码
- jquery $.getJSON()跨域请求
- JS跨域总结
- js iframe跨域访问(同主域/非同主域)分别深入介绍
- 借助script进行Http跨域请求:JSONP实现原理及代码
- JQuery的Ajax跨域请求原理概述及实例
- UEditor 编辑器跨域上传解决方法
- AJAX的跨域访问-两种有效的解决方法介绍
- Ajax跨域查询完美解决通过$.getJSON()实现
- PHP如何利用P3P实现跨域
- 如何设置iframe高度自适应在跨域情况下的可用方法
- 完美解决AJAX跨域问题
- AJAX跨域请求json数据的实现方法
- 用iframe设置代理解决ajax跨域请求问题
- jquery ajax jsonp跨域调用实例代码
- jquery ajax跨域解决方法(json方式)
- jquery的ajax和getJson跨域获取json数据的实现方法
- 用jQuery与JSONP轻松解决跨域访问的问题
- php跨域cookie共享使用方法
- JavaScript使用HTML5的window.postMessage实现跨域通信例子
- 关于JavaScript跨域问题及实时刷新解决方案
- IE9版本以下ajax 跨域问题可行解决方法
- js跨域问题浅析及解决方法优缺点对比
- 使用jsonp完美解决跨域问题
- 5种处理js跨域问题方法汇总
- js同源策略详解
- 深入浅析同源策略和跨域访问
相关文章
JavaScript跨平台的开源框架NativeScript
本文给大家分享的是一款使用javascript来构建跨平台原生移动应用的开源框架--NativeScript,可以使用JavaScript开发跨平台、真正原生的iOS, Android 和 Windows 移动App。开发人员使用NativeScript提供的库来构建应用UI,其抽象了各种原生平台之间的不同。2015-03-03javascript设计模式 – 备忘录模式原理与用法实例分析
这篇文章主要介绍了javascript设计模式 – 备忘录模式,结合实例形式分析了javascript备忘录模式相关概念、原理、用法及操作注意事项,需要的朋友可以参考下2020-04-04
最新评论