【发布时间】:2015-09-30 08:16:24
【问题描述】:
如何检查从 PhoneGap 应用发送的 postMessage 的来源?
我在PhoneGap中设置了一个iframe:
<iframe id="receiver" src="http://example.com/receiver.html" width="90" height="90"></iframe>
在 PhoneGap javascript 中,发送消息:
var receiver = document.getElementById('receiver').contentWindow;
receiver.postMessage('from phonegap app', 'http://example.com');
在receiver.html中,一些用于接收消息的javascript:
function receiveMessage(e) { alert(e.data) }
window.addEventListener('message', receiveMessage);
为了安全,在function receiveMessage我应该检查来源:
function receiveMessage(e) {
if (e.origin !== "something here")
return;
else
alert(e.data);
但由于消息来自 PhoneGap 应用,因此来源始终只是 file://。
检查file:// 是否可以接受安全性?还是我应该使用其他值?
如果重要,我在应用程序的 config.xml 中有 <access origin="http://example.com" />。
【问题讨论】:
标签: cordova security postmessage