firefox 13.0跨域localStorage访问:错误:操作不安全 [英] firefox 13.0 cross domain localStorage access: Error: The operation is insecure

查看:160
本文介绍了firefox 13.0跨域localStorage访问:错误:操作不安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我把Firefox升级到13.0。但是我的javascript代码出了问题。



有一个网页A(www.xx.com)和网页B(webim.xx.com)。我使用 iframe 标签将B嵌入到A中。

网页A

首先将域名设置为'xx.com'。

 < script> document.domain ='xx。 com公司;< /脚本> 

然后创建一个iframe加载网页B。

 <脚本> 
var iframe = document.createElement('iframe');
document.body.insertBefore(iframe,document.body.firstChild)
iframe.src ='http://webim.xx.com';
< / script>

网页B
将网域设定为「xx.com '

 < script> document.domain ='xx.com';< / script> 

然后我访问网页B的localStorage。



在网页A上执行代码:

  window.iframe.contentWindow.localStorage.setItem('a','a ')

然后会发生错误:

 错误:操作不安全。 

在以前的版本或其他浏览器中,代码可以正常执行。 b

有人知道为什么吗?



这是一个错误?

以及..如何解决这个问题? Thx。





刚才我找到了解决这个问题的方法。

我无法直接访问localStorage,但可以调用iframe的功能 ,它可以调用自己网页的localStroage。

  ///网页B 
< script>
document.domain ='xx.com';
var ls = {///< ls是localStorage的缩写。
setItem:function(k,v){
return localStorage.setItem(k,v);
},
getItem:function(k){
return localStorage.getItem(k);
},
removeItem:function(k){
return localStorage.removeItem(k);
},
clear:function(){
return localStorage.clear();
}
}
< / script>

然后我调用 ls.setItem 等。

  ///网页A 
< script> iframe.ls。 setItem('a','b');< / script>

即使我可以解决这个问题,为什么firefox 13.0会导致这个问题?旧的Firefox的行为是越野车,并修复了错误。根据规范,设置document.domain应该对localStorage的行为完全没有影响,所以在你的情况下,你试图设置localStorage为不同的域,这是不允许的。



请参阅 https://bugzilla.mozilla.org/show_bug.cgi?id= 495337 和localStorage规范的详细信息。


Today I upgrade my Firefox to 13.0. But something goes wrong with my javascript code.

There's a webpage A(www.xx.com) and webpage B(webim.xx.com). I embed B in A using iframe tag.

webpage A

first set the domain as 'xx.com'

<script>document.domain = 'xx.com';</script>

then create an iframe to load webpage B.

<script>
var iframe = document.createElement('iframe');
document.body.insertBefore(iframe, document.body.firstChild)
iframe.src = 'http://webim.xx.com';
</script>

webpage B set the domain as 'xx.com'

<script>document.domain = 'xx.com';</script>

Then I access the localStorage of webpage B.

On webpage A, execute code:

window.iframe.contentWindow.localStorage.setItem('a', 'a')

Then an error will be given:

Error: The operation is insecure.

In the previous versions or other browser, the code can execute normally.

Anybody knows why?

It's a bug?

And.. How to solve this problem? Thx.


Just now I found a way to fix this problem.

I can't access the localStorage directly, but I can call the function of the iframe which can call the localStroage of its own webpage.

/// webpage B
<script>
document.domain = 'xx.com';
var ls = { ///< ls is short for localStorage.
    setItem: function(k, v) {
        return localStorage.setItem(k, v);
    },
    getItem: function(k) {
        return localStorage.getItem(k);
    },
    removeItem: function(k) {
        return localStorage.removeItem(k);
    },
    clear: function(){
        return localStorage.clear();
    }
}
</script>

Then I call ls.setItem etc. to access the localStorage of the iframe.

/// webpage A
<script>iframe.ls.setItem('a', 'b');</script>

Even though I can solve this problem, why firefox 13.0 cause this problem?

解决方案

The old Firefox behavior was buggy, and the bug got fixed. Per spec, setting document.domain should have absolutely no effect on the behavior of localStorage, so in your case you're trying to set localStorage for a different domain, which is not allowed.

See https://bugzilla.mozilla.org/show_bug.cgi?id=495337 and the localStorage spec for details.

这篇关于firefox 13.0跨域localStorage访问:错误:操作不安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆