zoukankan      html  css  js  c++  java
  • JSONP for cross-site Callbacks 【转】

    转自:http://weblog.west-wind.com/posts/2007/Jul/04/JSONP-for-crosssite-Callbacks


    JSONP is an unofficial protocol that allows making cross domain calls by generating script tags in the current document and expecting a result back to calls a specified callback handler.

    The client JavaScript code to make a JSONP call looks like this:

    function jsonp(url,callback,name, query)
    {                
        if (url.indexOf("?") > -1)
            url += "&jsonp=" 
        else
            url += "?jsonp=" 
        url += name + "&";
        if (query)
            url += encodeURIComponent(query) + "&";   
        url += new Date().getTime().toString(); // prevent caching        
        
        var script = document.createElement("script");        
        script.setAttribute("src",url);
        script.setAttribute("type","text/javascript");                
        document.body.appendChild(script);
    }

    The idea is that this code adds a <script> tag dynamically to the page and when this code loads it causes the JavaScript that the server loads to be executed. Because it uses the DOM and a <script> tag this gets around the cross site scripting limitations of the XHR object.

    The server is expected to return JSON data, but with a slight twist: It has to prepend the callback function name, which is passed as part of the url. The syntax for a JSONP Url is simply the URL with a jsonp= query string parameter that defines the the callback function:

    http://someserver.com/mypage.aspx?jsonp=callbackFunction

    If the server wants to return a result like:

    { "x": 10, "y": 15}

    the result will be turned into (assuming the callback specified is callbackFunction):

    callbackFunction( { "x": 10, "y": 15} )

    If you're implementing this in an ASPX.NET page it might look like this:

    public partial class JSONP : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["jsonp"]) )
                this.JsonPCallback();
        }
    
        public void JsonPCallback()
        {
            string Callback = Request.QueryString["jsonp"];
            if (!string.IsNullOrEmpty(Callback))
            {
                // *** Do whatever you need
                Response.Write(Callback + "( {"x":10 , "y":100} );");
            }
    
            Response.End();
        }
    }

    The client will now have  script tag that gets added to the document and the script executes on load and in effect calls the callbackFunction with the parameter:

    function callbackFunction(Result)
    {
        alert(Result.x + "  " + Result.y) ;
    }

    And there's a cross domain callback.

    Note that this callback requires that the server KNOWS it's receiving a JSONP request because the server has to prepend the callback handler name supplied so the code can fire in the client.

    Yeah it seems like an Exploit

    I'm not sold on the idea, because it is basically exploiting what seems to be a security hole in the browser and one that apparently won't be plugged as many services like Adsense rely on it. But all this talk about preventing cross site scripting are pretty much moot by allowing this sort of hack to work. But then again this implementation doesn't do anything that couldn't be done before or changes any of the security issues - it just takes advantage of this functionality.

    Still - it feel... wrong <g>...

    It is what it is and it seems this approach is getting more popular in being able to retrieve JSON content from various public sites. It certainly is something that should be useful for ASP.NET developers who internally need to make client callbacks to their own internal sites which seems a frequent request.

  • 相关阅读:
    Linux Hung Task分析
    Linux内存都去哪了:(1)分析memblock在启动过程中对内存的影响
    《Linux/UNIX系统编程手册》第63章 IO多路复用、信号驱动IO以及epoll
    Linux内核和用户空间通信之netlink
    Linux soft lockup分析
    一款DMA性能优化记录:异步传输和指定实时信号做async IO
    Linux下时钟框架实践---一款芯片的时钟树配置
    使用Kernel NetEm和tc模拟复杂网络环境
    使用Flame Graph进行系统性能分析
    sigsuspend()阻塞:异步信号SIGIO为什么会被截胡?
  • 原文地址:https://www.cnblogs.com/shrekuu/p/4466104.html
Copyright © 2011-2022 走看看