跨域传输信息postMessage

widnow.postMessage()方法允许安全的跨域传输。

Syntax

otherWindow.postMessage(message, targetOrigin, [transfer]);
otherWindow
指向另一个窗口的引用:。
message
传输给另一个窗口的信息
targetOrigin
一个字符串,指定消息来源(URL形式)。记住总是提供一个特定的URL地址如果你知道的话,不要总是使用“*”(针对所有的URL),因为指定一个特定的URL可以防止恶意的网站来攻击。

The dispatched event

其他的窗口可以通过以下代码来监听被发送的信息:

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

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

  // ...
}

被发送的信息的属性如下:

data
从其他窗口传递的data
origin
当postMessage调用的时候,发送信息窗口的origin。这个字符串是协议和"://"和主机名和后面用":"连接了一个接口名称,如果这个接口名和默认的接口名不一样的话。比如http://example.org(默认的接口是443),http://example.net(默认的接口是80)和http://example.com:8080。注意,这个origin并不一定要是当前亦或未来的那个窗口的origin,所以这将有可能当调用postMessage的时候,导致跳转到另一个完全不同的地址。
source
发送信息的window对象。你可以使用这个在不同origin之间在两个窗口之间建立两个通信。

Security concerns

如果你不想接受到其他网站的信息,不要在message对象上增加任何监听。

使用origin(有可能也会使用source)属性来确定发送者的身份,如果你不想接受到其他网站的信息的话。

任何的window(包括,例如http://evil.example.com)可以向任何的其他window发送信息,你没有任何的保障来保证未知的发送者不会发送恶意的信息。确保了发送信息者的身份之后,你还需要确验证接收到的内容的语法。否则,发送信任信息的受信网站可能在你的网站上创建一个跨域的脚本漏洞。

不要使用“*”,而是确定的目标origin,当你使用postMessage来发送信息给其他的windows的时候,防止其他网站在你postMessage的时候拦截发送的信息。

Example

/*
 * In window A‘s scripts, with A being on <http://example.com:8080>:
 */

var popup = window.open(...popup details...);

// When the popup has fully loaded, if not blocked by a popup blocker:

// This does nothing, assuming the window hasn‘t changed its location.
popup.postMessage("The user is ‘bob‘ and the password is ‘secret‘",
                  "https://secure.example.net");

// This will successfully queue a message to be sent to the popup, assuming
// the window hasn‘t changed its location.
popup.postMessage("hello there!", "http://example.org");

function receiveMessage(event)
{
  // Do we trust the sender of this message?  (might be
  // different from what we originally opened, for example).
  if (event.origin !== "http://example.org")
    return;

  // event.source is popup
  // event.data is "hi there yourself!  the secret response is: rheeeeet!"
}
window.addEventListener("message", receiveMessage, false);
/*
 * In the popup‘s scripts, running on <http://example.org>:
 */

// Called sometime after postMessage is called
function receiveMessage(event)
{
  // Do we trust the sender of this message?
  if (event.origin !== "http://example.com:8080")
    return;

  // event.source is window.opener
  // event.data is "hello there!"

  // Assuming you‘ve verified the origin of the received message (which
  // you must do in any case), a convenient idiom for replying to a
  // message is to call postMessage on event.source and provide
  // event.origin as the targetOrigin.
  event.source.postMessage("hi there yourself!  the secret response " +
                           "is: rheeeeet!",
                           event.origin);
}

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

Notes

任何window可以在任何其他的window上使用这个方法,在任何的时候,不管当前页面在window中的location,来发送信息。

所以,任何的对象监听被使用来接受信息时必须先检查信息发送着的身份,使用origin和可能使用的属性source来判断。

这个必须再三声明:不检查origin和source可能会导致跨站点脚本攻击。

和任何的异步执行的脚本(timeout,用户生成的脚本),调用postMessage来监听何时事件处理函数监听postMessage发送的事件对象是不可能的,将会抛出错误。

发送对象的origin属性是不会受到当前在调用窗口的document.domain值的影响。

For IDN host names only, the value of the origin property is not consistently Unicode or punycode; for greatest compatibility check for both the IDN and punycode values when using this property if you expect messages from IDN sites. This value will eventually be consistently IDN, but for now you should handle both IDN and punycode forms.

The value of the origin property when the sending window contains a javascript: or data:URL is the origin of the script that loaded the URL.

Using window.postMessage in extensions

window.postMessage is available to JavaScript running in chrome code (e.g., in extensions and privileged code), but the source property of the dispatched event is always null as a security restriction. (The other properties have their expected values.) The targetOrigin argument for a message sent to a window located at a chrome: URL is currently misinterpreted such that the only value which will result in a message being sent is "*". Since this value is unsafe when the target window can be navigated elsewhere by a malicious site, it is recommended thatpostMessage not be used to communicate with chrome: pages for now; use a different method (such as a query string when the window is opened) to communicate with chrome windows. Lastly, posting a message to a page at a file: URL currently requires that the targetOriginargument be "*"file:// cannot be used as a security restriction; this restriction may be modified in the future.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1.0 6.0 (6.0)[1]
8.0 (8.0)[2]
8.0[3]
10.0[4]
9.5 4.0
transferargument ? 20.0 (20.0) Not supported ? ?

[1] Prior to Gecko 6.0 (Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3), the message parameter must be a string. Starting in Gecko 6.0 (Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3), themessage parameter is serialized using the structured clone algorithm. This means you can pass a broad variety of data objects safely to the destination window without having to serialize them yourself.

[2] Gecko 8.0 introduced support for sending File and FileList objects between windows. This is only allowed if the recipient‘s principal is contained within the sender‘s principal for security reasons.

[3] IE8 and IE9 only support it for <frame> and <iframe>.

[4] IE10 has important limitations: see this article for details.

时间: 2024-08-26 03:29:21

跨域传输信息postMessage的相关文章

【h5】h5数据跨域交换postMessage用法

h5数据跨域交换postMessage用法 来源 1.与通过window.open()打开的新窗口跨域数据交换,代码如下: (1)源窗口 1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title>源窗口</title> 6 </head> 7 <body> 8 <b

前端跨域的那些事

这一节,我们来讲一讲,前端跨域的那些事,主要分成这样的几部分来讲解, 一.为什么要跨域? 二.常见的几种跨域与使用场景 2.1 JSONP跨域 2.2 iframe跨域 2.3 window.name 跨域 2.4 document.domain 跨域 2.5 cookie跨域 2.6 postMessage跨域 三.总结 一.为什么要跨域 跨域,通常情况下是说在两个不通过的域名下面无法进行正常的通信,或者说是无法获取其他域名下面的数据,这个主要的原因是,浏览器出于安全问题的考虑,采用了同源策略

跨域通信方法总结

本文总结了5种较常见的跨域通信方法,如下: 1)jsonp 2)CORS(Cross OriginResource Sharing,跨源资源共享) 3)主域相同可以设置document.domain 4)利用window.name实现跨域 5)利用window.name实现跨域 jsonp 讲解jsonp之前先看一个例子:假设域A.com上有一个页面a.html,代码如下: var dosomething= function(data){ alert('我是A.com域上的页面,可以被跨域的re

详解js跨域

什么是跨域? 概念:只要协议.域名.端口有任何一个不同,都被当作是不同的域. 对于端口和协议的不同,只能通过后台来解决.URL 说明 是否允许通信 http://www.a.com/a.js http://www.a.com/b.js 同一域名下 允许 http://www.a.com/lab/a.js http://www.a.com/script/b.js 同一域名下不同文件夹 允许 http://www.a.com:8000/a.js http://www.a.com/b.js 同一域名,

详解JS跨域问题

什么是跨域? 概念:只要协议.域名.端口有任何一个不同,都被当作是不同的域. URL 说明 是否允许通信 http://www.a.com/a.js http://www.a.com/b.js 同一域名下 允许 http://www.a.com/lab/a.js http://www.a.com/script/b.js 同一域名下不同文件夹 允许 http://www.a.com:8000/a.js http://www.a.com/b.js 同一域名,不同端口 不允许 http://www.a

跨域问题总结

先看概念,什么是跨域:协议.域名.端口中有任何一个不同,它们之间要通信就叫跨域. 情形 是否允许通信 同一域名下 是 同一域名下不同文件夹 是 同一域名.不同端口 否 同一域名.不同协议 否 域名和域名对应ip 否 主域名相同.子域名不同 否 不同域名 否 常用的跨域方式有2种:CROS(跨域资源共享Cross-Origin Resource Sharing).JSONP CROS:服务器端对于CORS的支持,主要就是通过设置Access-Control-Allow-Origin来进行的.如果浏

JavaScript跨域问题总结

什么是跨域? 概念:只要协议.域名.端口有任何一个不同,都被当作是不同的域. URL 说明 是否允许通信 http://www.a.com/a.js http://www.a.com/b.js 同一域名下 允许 http://www.a.com/lab/a.js http://www.a.com/script/b.js 同一域名下不同文件夹 允许 http://www.a.com:8000/a.js http://www.a.com/b.js 同一域名,不同端口 不允许 http://www.a

js跨域问题解决方案

1.JSON-P跨域 动态脚本注入的方法,通过script标签引入一个js文件,这个js文件载入成功后会执行我们在url参数中指定的函数,并且会把我们需要的json数据作为参数传入.所以jsonp是需要服务器端的页面进行相应的配合的. 2.window.name跨域 indow对象有个name属性,该属性有个特征:即在一个窗口(window)的生命周期内,窗口载入的所有的页面都是共享一个window.name的,每个页面对window.name都有读写的权限,window.name是持久存在一个

跨域请求的常用方式及解释

同源策略 首先基于安全的原因,浏览器是存在同源策略这个机制的,同源策略阻止从一个域加载的脚本去获取另一个域上的文档属性.也就是说,受到请求的 URL 的域必须与当前 Web 页面的域相同.这意味着浏览器隔离来自不同源的内容,以防止它们之间的操作. js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据. 只要协议.域名.端口有任何一个不同,都被当作是不同的域. 下面介绍几种常用的跨域请求方式 默认端口