Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8694

Show
Ignore:
Timestamp:
01/23/08 00:57:38 (6 months ago)
Author:
tobie
Message:

prototype: Prevent a potential security issue for cross-site ajax requests.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • spinoffs/prototype/branches/1-5-1/CHANGELOG

    r7068 r8694  
     1*1.5.1.2* (January 18, 2008) 
     2 
     3* Prevent a potential security issue for cross-site ajax requests. [sam, Tobie Langel] 
     4 
    15*1.5.1.1* (June 19, 2007) 
    26 
  • spinoffs/prototype/branches/1-5-1/Rakefile

    r7068 r8694  
    66PROTOTYPE_DIST_DIR = File.join(PROTOTYPE_ROOT, 'dist') 
    77PROTOTYPE_PKG_DIR  = File.join(PROTOTYPE_ROOT, 'pkg') 
    8 PROTOTYPE_VERSION  = '1.5.1.1
     8PROTOTYPE_VERSION  = '1.5.1.2
    99 
    1010task :default => [:dist, :package, :clean_package_source] 
  • spinoffs/prototype/branches/1-5-1/src/ajax.js

    r6558 r8694  
    188188       
    189189      var contentType = this.getHeader('Content-type'); 
    190       if (contentType && contentType.strip(). 
     190      if (contentType && this.isSameOrigin() && contentType.strip(). 
    191191        match(/^(text|application)\/(x-)?(java|ecma)script(;.*)?$/i)) 
    192192          this.evalResponse(); 
     
    206206  }, 
    207207   
     208  isSameOrigin: function() { 
     209    var m = this.url.match(/^\s*https?:\/\/[^/]*/); 
     210    return !m || (m[0] == new Template('#{protocol}//#{domain}#{port}').evaluate({ 
     211      protocol: location.protocol, 
     212      domain: document.domain, 
     213      port: location.port ? ':' + location.port : '' 
     214    })); 
     215  }, 
     216   
    208217  getHeader: function(name) { 
    209218    try { 
     
    215224    try { 
    216225      var json = this.getHeader('X-JSON'); 
    217       return json ? json.evalJSON() : null; 
     226      return json ? json.evalJSON(!this.isSameOrigin()) : null; 
    218227    } catch (e) { return null } 
    219228  }, 
  • spinoffs/prototype/branches/1-5-1/test/lib/jstest.rb

    r6600 r8694  
    147147end 
    148148 
    149 class NonCachingFileHandler < WEBrick::HTTPServlet::FileHandler 
    150   def do_GET(req, res) 
    151     super 
    152      
    153     res['Content-Type'] = case req.path 
    154       when /\.js$/   then 'text/javascript' 
    155       when /\.html$/ then 'text/html' 
    156       when /\.css$/  then 'text/css' 
    157       else 'text/plain' 
    158     end 
    159      
     149class WEBrick::HTTPServlet::AbstractServlet 
     150  def prevent_caching(res) 
    160151    res['ETag'] = nil 
    161152    res['Last-Modified'] = Time.now + 100**4 
     
    166157end 
    167158 
     159class BasicServlet < WEBrick::HTTPServlet::AbstractServlet 
     160  def do_GET(req, res) 
     161    prevent_caching(res) 
     162    res['Content-Type'] = "text/plain" 
     163     
     164    req.query.each do |k, v| 
     165      res[k] = v unless k == 'responseBody' 
     166    end 
     167    res.body = req.query["responseBody"] 
     168     
     169    raise WEBrick::HTTPStatus::OK 
     170  end 
     171   
     172  def do_POST(req, res) 
     173    do_GET(req, res) 
     174  end 
     175end 
     176 
     177class NonCachingFileHandler < WEBrick::HTTPServlet::FileHandler 
     178  def do_GET(req, res) 
     179    super 
     180    set_default_content_type(res, req.path) 
     181    prevent_caching(res) 
     182  end 
     183   
     184  def set_default_content_type(res, path) 
     185    res['Content-Type'] = case path 
     186      when /\.js$/   then 'text/javascript' 
     187      when /\.html$/ then 'text/html' 
     188      when /\.css$/  then 'text/css' 
     189      else 'text/plain' 
     190    end 
     191  end 
     192end 
     193 
    168194 
    169195class JavaScriptTestTask < ::Rake::TaskLib 
     
    186212      res.body = req["content-type"] 
    187213    end 
     214    @server.mount("/response", BasicServlet) 
    188215     
    189216    yield self if block_given? 
  • spinoffs/prototype/branches/1-5-1/test/unit/ajax.html

    r6558 r8694  
    174174        onComplete: function(transport) { assertNotEqual(0, transport.readyState) } 
    175175      }); 
     176    }}, 
     177 
     178    testIsSameOriginMethod: function() {with(this) { 
     179      var isSameOrigin = Ajax.Request.prototype.isSameOrigin; 
     180      assert(isSameOrigin.call({ url: '/foo/bar.html' }), '/foo/bar.html'); 
     181      assert(isSameOrigin.call({ url: window.location.toString() }), window.location); 
     182      assert(!isSameOrigin.call({ url: 'http://example.com' }), 'http://example.com'); 
     183 
     184      if (window.location.port == 4711) { 
     185        Ajax.Request.prototype.isSameOrigin = function() { 
     186          return false 
     187        }; 
     188 
     189        $("content").update('same origin policy'); 
     190        new Ajax.Request("/response", { 
     191          parameters: { 
     192            responseBody:   '$("content").update("<H2>Hello world!</H2>");',  
     193            'Content-Type': '           text/javascript     ' 
     194          }, 
     195          asynchronous: false, 
     196          method: 'get', 
     197          onException: function(e) { throw e }, 
     198          onComplete: function(transport) {  
     199            assertEqual("same origin policy", $("content").innerHTML); 
     200          } 
     201        }); 
     202 
     203        new Ajax.Request("/response", { 
     204          parameters: { 'X-JSON': '{});window.attacked = true;({}' }, 
     205          asynchronous: false, 
     206          method: 'get', 
     207          onComplete: function(transport, json) { 
     208            assertNull(json); 
     209          } 
     210        }); 
     211 
     212        Ajax.Request.prototype.isSameOrigin = isSameOrigin; 
     213      } else { 
     214        info('You must be running your tests from rake to test this feature.'); 
     215      } 
    176216    }} 
    177      
    178217  }, 'testlog'); 
    179218// ]]>