10.跨域解决方案postmessage


所谓跨域无外乎有几种:

  • 浏览器与服务器之间的跨域;
  • 页面和其打开的新窗口的数据传递
  • 多窗口间的消息传递
  • 页面和嵌套的iframe的消息传递

这里我们只讲一种跨域解决方案postMessage,并讨论它的利弊。

HTML5的出现,引入了很多新东西,比如前面介绍的web Worker,localstorage,sesionStorage,canvas.....但是这些的引入却也引入了大量的安全问题。

如何通过postMessage实现跨域

127.0.0.0:8101/a.html

<body>
    <div style="width:200px; float:left; margin-right:200px;border:solid 1px #333;">
        <div id="color">Frame Color</div>
    </div>
    <div>
        <iframe id="child" src="http://127.0.0.1:8102/b.html"></iframe>
    </div>
</body>

<script>
    window.onload = function() {
        window.frames[0].postMessage('getcolor', 'http://127.0.0.1:8102/b.html');
    }
    window.addEventListener('message', function(e) {
        var color = e.data;
        document.getElementById('color').style.backgroundColor = color;
    }, false);
</script>

在a.html中跨域访问了b.html,这种跨域实际上通过iframe就能做到,但是a.html并不能获取b.html中的信息。 上面的例子中,通过postMessage()跨域传递信息。

postMessage(data,origin)方法接受两个参数

1.data:要传递的数据,html5规范中提到该参数可以是JavaScript的任意基本类型或可复制的对象,然而并不是所有浏览器都做到了这点儿,部分浏览器只能处理字符串参数,所以我们在传递参数的时候需要使用JSON.stringify()方法对对象参数序列化,在低版本IE中引用json2.js可以实现类似效果。

2.origin:字符串参数,指明目标窗口的源,协议+主机+端口号[+URL],URL会被忽略,所以可以不写,这个参数是为了安全考虑,postMessage()方法只会将message传递给指定窗口,当然如果愿意也可以建参数设置为"*",这样可以传递给任意窗口,如果要指定和当前窗口同源的话设置为"/"。

127.0.0.1:8102/b.html

<body>
    <div id="container" onclick="changeColor();" style="width:100%; height:100%; background-color:rgb(204, 102, 0);">
        13 click to change color 14 </div>
</body>
<script>
    var container = document.getElementById('container');
    window.addEventListener('message', function(e) {
        if (e.source != window.parent) return;
        var color = container.style.backgroundColor;
        window.parent.postMessage(color, '*');
    }, false);

    function changeColor() {
        var color = container.style.backgroundColor;
        if (color == 'rgb(204, 102, 0)') {
            color = 'rgb(204, 204, 0)';
        } else {
            color = 'rgb(204,102,0)';
        }
        container.style.backgroundColor = color;
        window.parent.postMessage(color, '*');
    }
</script>

b.html写了一个changeColor函数,每当颜色变化的时候,就将color传递给'*'(指所有监听message的事件都能收到这个消息) 这里写图片描述

postMessage的安全问题

其实和iframe一样,权限越大漏洞越大 看看postMesage的定义

otherWindow.postMessage(message, targetOrigin, [transfer]);

说明:

  • otherWindow
    • 其他窗口的一个引用,比如iframe的contentWindow属性、执行window.open返回的窗口对象、或者是命名过或数值索引的window.frames。
  • message
    • 将要发送到其他 window的数据。它将会被结构化克隆算法序列化。这意味着你可以不受什么限制的将数据对象安全的传送给目标窗口而无需自己序列化。[1]
  • targetOrigin
    • 通过窗口的origin属性来指定哪些窗口能接收到消息事件,其值可以是字符串""(表示无限制)或者一个URI。在发送消息的时候,如果目标窗口的协议、主机地址或端口这三者的任意一项不匹配targetOrigin提供的值,那么消息就不会被发送;只有三者完全匹配,消息才会被发送。这个机制用来控制消息可以发送到哪些窗口;例如,当用postMessage传送密码时,这个参数就显得尤为重要,必须保证它的值与这条包含密码的信息的预期接受者的orign属性完全一致,来防止密码被恶意的第三方截获。如果你明确的知道消息应该发送到哪个窗口,那么请始终提供一个有确切值的targetOrigin,而不是。不提供确切的目标将导致数据泄露到任何对数据感兴趣的恶意站点。
  • transfer 可选
    • 是一串和message 同时传递的 Transferable 对象. 这些对象的所有权将被转移给消息的接收方,而发送一方将不再保有所有权。

message事件

window.addEventListener("message", receiveMessage, false);

function receiveMessage(event)
{
  // For Chrome, the origin property is in the event.originalEvent
  // object.
  var origin = event.origin || event.originalEvent.origin; 
  if (origin !== "http://example.org:8080")
    return;

  // ...
}

postMessage是一种实现跨域访问的方法,如果postMessage(message, targetOrigin, [transfer]); 的targetOrigin设置为"*",那么有可能被其他网页监听传送的信息。

如果在addEventListener("message", receiveMessage, false);不限制origin,那么可能收到来自其他恶意网页的信息对页面进行恶意篡改。

因此要从以下几点注意postMessage的安全:

  1. 不要设置targetOrigin为"*"
  2. 严格检查origin
  3. 验证message的合法性

results matching ""

    No results matching ""