Cross Site Scripting (XSS)

Mobile Application Design and Development [./]
Spring 2010 — INFO 152 (CCN 42504)

Erik Wilde, UC Berkeley School of Information
2010-02-19

Creative Commons License [http://creativecommons.org/licenses/by/3.0/]

This work is licensed under a CC
Attribution 3.0 Unported License
[http://creativecommons.org/licenses/by/3.0/]

Contents Erik Wilde: Cross Site Scripting (XSS)

Contents

Erik Wilde: Cross Site Scripting (XSS)

(2) Abstract

Many mobile applications use data from a variety of sources, the most popular example being map-based applications with often combine map data from one site with placemarks and other map overlays from a different source. Depending on the implementation, this design might be limited by the Same-Origin Policy implemented by clients, which originated in an attempt to reduce the risks of Cross-Site Scripting (XSS). In this practical lecture, we look at some of the workarounds that are possible, specifically at JSON with Padding (JSONP), which is a client-based approach, and at reverse proxying, which is a server-based approach.



XSS Risk and Mitigation

Outline (XSS Risk and Mitigation)

  1. XSS Risk and Mitigation [4]
  2. Client-Side Solution [5]
  3. Server-Side Solution [3]
XSS Risk and Mitigation Erik Wilde: Cross Site Scripting (XSS)

(4) XSS Risk: Cookie Theft

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <script src="jquery.js" type="text/javascript"></script>
  <script src="jquery.cookie.js" type="text/javascript"></script>
  <script type="text/javascript">
   $(document).ready(function() {
    $.cookie("username", "Mr.X");
    $.cookie("password", "SuperSecret");
    $("#remote").load("include.html");
   });
  </script>
 </head>
 <body>
  <p>XSS Demo:</p>
  <div id="remote"></div>
 </body>
</html>
<p>This is included content ...</p>
<script type="text/javascript">
 alert("... and this is injected scripting stealing your cookie:");
 alert( document.cookie );
</script>


XSS Risk and Mitigation Erik Wilde: Cross Site Scripting (XSS)

(5) Same-Origin Policy



XSS Risk and Mitigation Erik Wilde: Cross Site Scripting (XSS)

(6) Mobile Applications and XSS



XSS Risk and Mitigation Erik Wilde: Cross Site Scripting (XSS)

(7) Allowing XSS



Client-Side Solution

Outline (Client-Side Solution)

  1. XSS Risk and Mitigation [4]
  2. Client-Side Solution [5]
  3. Server-Side Solution [3]
Client-Side Solution Erik Wilde: Cross Site Scripting (XSS)

(9) XMLHttpRequest Restrictions



Client-Side Solution Erik Wilde: Cross Site Scripting (XSS)

(10) JSON is JavaScript



Client-Side Solution Erik Wilde: Cross Site Scripting (XSS)

(11) JSON with padding (JSONP)



Client-Side Solution Erik Wilde: Cross Site Scripting (XSS)

(12) JSONP Example

jsonFlickrFeed({
 "title":"The MobApp2010 Pool, with geodata",
 "link":"http://www.flickr.com/photos/",
 "description":"",
 "modified":"2010-01-31T06:10:25Z",
 "generator":"http://www.flickr.com/",
 "items":[
  {
   "title":"DSC00809",
   "link":"http://www.flickr.com/photos/47030217@N06/4318229442/",
   "media":{
    "m":"http://farm5.static.flickr.com/4028/4318229442_5ac597fdf5_m.jpg"
   },
   "date_taken":"2010-01-30T21:14:39-08:00",
   "description":"<p><a href=\"http://www.flickr.com/people/47030217@N06/\">stoodle246<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/47030217@N06/4318229442/\" title=\"DSC00809\"><img src=\"http://farm5.static.flickr.com/4028/4318229442_5ac597fdf5_m.jpg\" width=\"240\" height=\"180\" alt=\"DSC00809\" /><\/a><\/p> ",
   "published":"2010-01-31T06:10:25Z",
   "author":"nobody@flickr.com (stoodle246)",
   "author_id":"47030217@N06",
   "tags":"",
   "latitude":"37.873633",
   "longitude":"-122.256975"
  },


Client-Side Solution Erik Wilde: Cross Site Scripting (XSS)

(13) JSONP Handling in jQuery

  // Handle JSONP Parameter Callbacks
  if ( s.dataType === "jsonp" ) {
   if ( type === "GET" ) {
    if ( !jsre.test( s.url ) ) {
     s.url += (rquery.test( s.url ) ? "&" : "?") + (s.jsonp || "callback") + "=?";
    }
   } else if ( !s.data || !jsre.test(s.data) ) {
    s.data = (s.data ? s.data + "&" : "") + (s.jsonp || "callback") + "=?";
   }
   s.dataType = "json";
  }

  // Build temporary JSONP function
  if ( s.dataType === "json" && (s.data && jsre.test(s.data) || jsre.test(s.url)) ) {
   jsonp = s.jsonpCallback || ("jsonp" + jsc++);

   // Replace the =? sequence both in the query string and the data
   if ( s.data ) {
    s.data = (s.data + "").replace(jsre, "=" + jsonp + "$1");
   }

   s.url = s.url.replace(jsre, "=" + jsonp + "$1");

   // We need to make sure
   // that a JSONP style response is executed properly
   s.dataType = "script";

   // Handle JSONP-style loading
   window[ jsonp ] = window[ jsonp ] || function( tmp ) {
    data = tmp;
    success();
    complete();
    // Garbage collect
    window[ jsonp ] = undefined;

    try {
     delete window[ jsonp ];
    } catch(e) {}

    if ( head ) {
     head.removeChild( script );
    }
   };
  }


Server-Side Solution

Outline (Server-Side Solution)

  1. XSS Risk and Mitigation [4]
  2. Client-Side Solution [5]
  3. Server-Side Solution [3]
Server-Side Solution Erik Wilde: Cross Site Scripting (XSS)

(15) HTTP Server Configuration



Server-Side Solution Erik Wilde: Cross Site Scripting (XSS)

(16) Server Requirements



Server-Side Solution Erik Wilde: Cross Site Scripting (XSS)

(17) Mapping Remote Resources

RewriteEngine On
ProxyRequests Off
<Proxy *>
	Order deny,allow
	Allow from all
</Proxy>
RewriteRule ^/staticmap(.*) http://maps.google.com/maps/api/staticmap$1 [P]


Erik Wilde: Cross Site Scripting (XSS)

(18) Conclusions



2010-02-19 Mobile Application Design and Development [./]
Spring 2010 — INFO 152 (CCN 42504)