Veg*_*sen 12 javascript xss ajax xmlhttprequest
我想提供一个Javascript代码,它可以在任何包含它的网站上运行,但它总是需要在托管Javascript的服务器上获取更多数据(甚至修改数据).我知道出于明显的原因存在安全限制.
考虑在xyz.com上托管的index.html,其中包含以下内容:
<script type="text/javascript" src="http://abc.com/some.js"></script>
Run Code Online (Sandbox Code Playgroud)
some.js是否能够使用XMLHttpRequest将数据发布到abc.com?换句话说,abc.com是否被隐含信任,因为我们从那里加载了Javascript?
Jam*_*mes 18
some.js是否能够使用XMLHttpRequest将数据发布到abc.com?换句话说,abc.com是否被隐含信任,因为我们从那里加载了Javascript?
不,因为脚本被加载到一个单独的域,它将无法访问...
如果您信任数据源,那么JSONP可能是更好的选择.JSONP涉及将新的SCRIPT元素动态添加到页面,并将SRC设置为另一个域,并将回调设置为查询字符串中的参数.例如:
function getJSON(URL,success){
var ud = 'json'+(Math.random()*100).toString().replace(/\./g,'');
window[ud]= function(o){
success&&success(o);
};
document.getElementsByTagName('body')[0].appendChild((function(){
var s = document.createElement('script');
s.type = 'text/javascript';
s.src = URL.replace('callback=?','callback='+ud);
return s;
})());
}
getJSON('http://YOUR-DOMAIN.com/script.php?dataName=john&dataAge=99&callback=?',function(data){
var success = data.flag === 'successful';
if(success) {
alert('The POST to abc.com WORKED SUCCESSFULLY');
}
});
Run Code Online (Sandbox Code Playgroud)
因此,您需要托管自己的脚本,该脚本可以使用PHP/CURL发布到abc.com域,然后以JSONP格式输出响应:
我对PHP不太好,但可能是这样的:
<?php
/* Grab the variables */
$postURL = $_GET['posturl'];
$postData['name'] = $_GET['dataName'];
$postData['age'] = $_GET['dataAge'];
/* Here, POST to abc.com */
/* MORE INFO: http://uk3.php.net/curl & http://www.askapache.com/htaccess/sending-post-form-data-with-php-curl.html */
/* Fake data (just for this example:) */
$postResponse = 'blahblahblah';
$postSuccess = TRUE;
/* Once you've done that, you can output a JSONP response */
/* Remember JSON format == 'JavaScript Object Notation' - e.g. {'foo':{'bar':'foo'}} */
echo $_GET['callback'] . '({';
echo "'flag':' . $postSuccess . ',";
echo "'response':' . $postResponse . '})";
?>
Run Code Online (Sandbox Code Playgroud)
因此,您可以控制的服务器将充当客户端和abc.com之间的媒介,您将以JSON格式将响应发送回客户端,以便JavaScript可以理解和使用它...