HOWTO: Prevent Cross-Site Scripting Security Issues


The information in this article applies to:


SUMMARY

Dynamically generated HTML pages can introduce security risks if inputs are not validated either on the way in or on the way out. Malicious script can be embedded within input submitted to Web pages and appear to browsers as originating from a trusted source. This problem is referred to as a cross-site security scripting issue. This article discusses cross-site scripting security issues, the ramifications, and prevention.


MORE INFORMATION

The Problem

The underlying problem is that many Web pages display input that is not validated. If input is not validated, then malicious script can be embedded within the input. If a server-side script then displays this non-validated input the the script runs on the browser as though the trusted site generated it.

Ramifications

If input to your dynamic Web pages is not validated, you may encounter the following: Which Web pages are at risk? Essentially, the problem affects dynamic page creation based on input that was not validated. Typical examples include the following types of Web pages:

Prevention

Presented here are a few approaches to preventing cross-site scripting security attacks. Evaluate your specific situation to determine which techniques will work best for you. It is important to note that in all techniques, you are validating data that you receive from input, and not your trusted script. Essentially, prevention means that you follow good coding practice by running sanity checks on your input to your routines.

The following general approaches for preventing cross-site scripting attacks are presented here: When filtering or encoding, you need to specify a character set for your Web pages to ensure that your filter is checking for the appropriate special characters. The data inserted into your Web pages should filter out byte sequences that are considered special based on the specific character set. A popular charset is ISO 8859-1, which was the default in early versions of HTML and HTTP. You must take into account localization issues when changing these parameters.

Encode Output Based on Input Parameters for Special Characters
Encode data received as input when you write it out as HTML. This technique is effective on data that was not validated for some reason during input. By using techniques such as URLEncode and HTMLEncode, you can prevent malicious script from executing.

The following code snippets show using URLEncode and HTMLEncode from Active Server Pages (ASP) pages:

<%

      var BaseURL = http://www.mysite.com/search2.asp?searchagain=;

      Response.write("<a href=\"" + BaseUrl +

      Server.URLEncode(Request.QueryString("SearchString")) +

      "\">click-me</a>");

%>

<% Response.Write("Hello visitor <I>" +

      Server.HTMLEncode(Request.Form("UserName")) +

      "</I>");

%> 
If you encode the HTML and URLs, you may need to specify the code page as you would if you were to filter data.

It is important to note that calling HTMLEncode on the string that is about to be displayed will prevent any script in it from being executed and thus preventing the problem.

Filter Input Parameters for Special Characters
Filtering input works by removing some or all special characters from your input. Special characters are characters that enable script to be generated within an HTML stream. Special characters include the following:

< > " ' % ; ) ( & + - 
Note that your individual situation may warrant the filtering of additional characters or strings beyond the special characters.

While filtering can be an effective technique, there are a few caveats: Here is a sample filter written in JavaScript demonstrating how to remove special characters:

function RemoveBad(strTemp) { 

    strTemp = strTemp.replace(/\<|\>|\"|\'|\%|\;|\(|\)|\&|\+|\-/g,""); 

    return strTemp;

} 



 
This code would process user input before storing it for later use.

<% Session("StoredPreference") = RemoveBad(Request.Cookies("UserColor"));

         var TempStr = RemoveBad(Request.QueryString("UserName"));

         var SQLString = "INSERT TableName VALUES (' " +

         RemoveBad(Request.Form("UserName") + " ' )";

 
Filter Output Based on Input Parameters for Special Characters
This technique is similar to filtering input except that you filter characters that are written out to the client. While this can be an effective technique, it may present a problem for Web pages that write out HTML elements.

For example, on a page that writes out <TABLE> elements, a generic function that removes the special characters would strip the < and > characters ruining the <TABLE> tag. Therefore, in order for this technique to be useful, you would only filter data passed in or data that was previously entered by a user and stored in a database.

Possible Sources of Malicious Data
While the problem applies to any page that uses input to dynamically generate HTML, the following are some possible sources of malicious data to help you spot check for potential security risks:

Conclusion

In conclusion, the following are key points to remember regarding the cross-site scripting security problem:


REFERENCES

For more information, see the following advisory from the Computer Emergency Response Team (CERT) at Carnegie Mellon University:

http://www.cert.org/advisories/CA-2000-02.html
For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
Q253117 HOWTO: Prevent Internet Explorer and Outlook Express CSSI Vulnerability
Q253119 HOWTO: Review ASP Code for CSSI Vulnerability
Q253120 HOWTO: Review Visual InterDev Generated Code for CSSI Vulnerability
Q253121 HOWTO: Review MTS/ASP Code for CSSI Vulnerability

Additional query words:

Keywords : kbGrpASP kbDSupport kbCSSI
Version :
Platform :
Issue type : kbhowto
Technology :


Last Reviewed: February 4, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.