跨域传输信息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
[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.
跨域传输信息postMessage的更多相关文章
- JavaScript 跨域:window.postMessage 实现跨域通信
JavaScript 跨域方式实现方式有很多,之前,一篇文章中提到了 JSONP 形式实现跨域.本文将介绍 HTML5 新增的 api 实现跨域:window.postMessage . 1 othe ...
- 详解JS跨域问题
什么是跨域? 概念:只要协议.域名.端口有任何一个不同,都被当作是不同的域. JavaScript 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- 【前端】【转】JS跨域问题总结
详情见原博客:详解js跨域问题 概念:只要协议.域名.端口有任何一个不同,都被当作是不同的域. 跨域资源共享(CORS) CORS(Cross-Origin Resource Sharing)跨域资源 ...
- js跨域问题解决方案
跨域:当协议.域名.端口号任何一个不相同时,叫称为跨域. HTML5 CORS(cross-origin-resource-sharing)跨域资源共享: 原理:当需要访问跨域的资源时,可以通 ...
- 详解js跨域
什么是跨域? 概念:只要协议.域名.端口有任何一个不同,都被当作是不同的域. 对于端口和协议的不同,只能通过后台来解决.URL 说明 是否允许通信 http://www.a.com/a.js http ...
- JS中的跨域问题
一.什么是跨域? 1.定义:跨域是指从一个域名的网页去请求另一个域名的资源.比如从www.baidu.com 页面去请求 www.google.com 的资源.但是一般情况下不能这么做,它是由浏览器的 ...
- js跨域解决方式
什么是跨域? 概念:仅仅要协议.域名.port有不论什么一个不同,都被当作是不同的域.(所谓同源是指,域名.协议,port同样.),对于port和协议的不同,仅仅能通过后台来解决. URL 说明 是否 ...
- 在javascript中的跨域解决
跨域产生的原因 跨域是由浏览器的同源策略引起的,即不同源(协议,域名,端口中其中有一个不同)的js是不能读取对方的资源的.当要网站中的js要请求其他网站的数据时就会产生跨域问题,就像下面这样,浏览器会 ...
- 一个iOS程序员眼中的跨域问题
摘要: 跨域问题是web开发领域一个常见的问题,相信每个web开发者都遇到"跨域"的问题 最近公司的iOS开发任务比较少,所以自己最近开始了Web开发的任务,在用H5做了很多页面, ...
随机推荐
- uitableview使用reloaddata不管用
原因在于决定row number得array变动后没有再次将其count赋值给numberOfRowsInSection中返回的成员变量.致使没有其作用
- iOS Mobile Development: Using Xcode Targets to Reuse the Code 使用xcode targets来实现代码复用
In the context of iOS mobile app development, a clone is simply an app that is based off another mob ...
- Direct2D教程(三)简单几何图形
从本章开始,我们介绍D2D几何图形. D2D图形分类 Direct2D支持多种类型的几何图形,包括Simple Geometry(简单几何图形) 矩形 圆角矩形 椭圆 Path Geometry(路径 ...
- [反汇编练习] 160个CrackMe之033
[反汇编练习] 160个CrackMe之033. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- 3D空间中射线与轴向包围盒AABB的交叉检测算法 【转】
http://blog.csdn.net/i_dovelemon/article/details/38342739 引言 在上一节中,我讲述了如何实现射线与三角形的交叉检测算法. 但是,我们应该知道, ...
- PS 抠图如何使用通道法处理头发
通道抠图法抠出美女飘逸头发-PS抠图实例教程 抠图更换背景后效果图 通道抠图法抠出美女飘逸头发-PS抠图实例教程 教程步骤: 1 打开原图,进入通道面板. 通道抠图法抠出美女飘逸头发-PS抠图实 ...
- Unix系统介绍
一.基础知识 操作系统 用户与计算机硬件之间的界面,是控制.管理计算机内各种硬件与软件资源.它是一个多用户.多任务.分时的操作系统. 对于分时系统:假如a进程需要16个时间片,现在根据优先级只分配了1 ...
- mvc用UpdateModel报错
项目中使用UpdateModel时报错:未能更新类型“XXXXXX”的模型. 原因如下:表单Post时,有的参数为空,如a=1&b=2&=3.
- 云舒网络译:Rancher1.0正式版公布
编者注: Rancher Labs是一家容器技术基础设施提供商,总部位于美国硅谷,Rancher是一个高效易用的企业容器云平台. 云舒网络 http://www.cloudsoar.com/为Ranc ...
- Retimer、Redriver(Level Shifter)
重定时器Retimer和驱动器Redriver9(Level Shifter) 在高速串行通道的信号传输中,需要使用Redriver 和Retimer来保证信号传输的质量. Redriver,可以重新 ...