Relying on HTML, XML and other types of encoding to validate user input can result in the browser executing malicious code.
The use of certain encoding functions will prevent some, but not all cross-site scripting attacks. Depending on the context in which the data appear, characters beyond the basic <, >, &, and " that are HTML-encoded and those beyond <, >, &, ", and ' that are XML-encoded may take on meta-meaning. Relying on such encoding functions is equivalent to using a weak blacklist to prevent cross-site scripting and might allow an attacker to inject malicious code that will be executed in the browser. Because accurately identifying the context in which the data appear statically is not always possible, the HP Fortify Secure Coding Rulepacks reports cross-site scripting findings even when encoding is applied and presents them as Cross-Site Scripting: Poor Validation issues.
Cross-site scripting (XSS) vulnerabilities occur when:
1. Data enters a web application through an untrusted source. In the case of DOM-based XSS, data is read from a URL parameter or other value within the browser and written back into the page with client-side code. In the case of Reflected XSS, the untrusted source is typically a web request, while in the case of Persisted (also known as Stored) XSS it is typically a database or other back-end datastore.
2. The data is included in dynamic content that is sent to a web user without being validated for malicious code. In the case of DOM Based XSS, malicious code gets executed as part of DOM (Document Object Model) creation, whenever the victim's browser parses the HTML page.
The malicious content sent to the web browser often takes the form of a segment of JavaScript, but may also include HTML, Flash or any other type of code that the browser may execute. The variety of attacks based on XSS is almost limitless, but they commonly include transmitting private data like cookies or other session information to the attacker, redirecting the victim to web content controlled by the attacker, or performing other malicious operations on the user's machine under the guise of the vulnerable site.
Example: The following JavaScript code segment reads an employee ID, eid
, from an HTTP request, escapes it, and displays it to the user.
<SCRIPT>
var pos=document.URL.indexOf("eid=")+4;
document.write(escape(document.URL.substring(pos,document.URL.length)));
</SCRIPT>
eid
contains only standard alphanumeric text. If eid
has a value that includes meta-characters or source code, then the code will be executed by the web browser as it displays the HTTP response.[1] Standards Mapping - OWASP Top 10 2007 - (OWASP 2007) A1 Cross Site Scripting (XSS)
[2] Standards Mapping - OWASP Top 10 2010 - (OWASP 2010) A2 Cross-Site Scripting (XSS)
[3] Standards Mapping - OWASP Top 10 2004 - (OWASP 2004) A4 Cross Site Scripting
[4] Standards Mapping - Security Technical Implementation Guide Version 3 - (STIG 3) APP3510 CAT I, APP3580 CAT I
[5] Standards Mapping - Web Application Security Consortium 24 + 2 - (WASC 24 + 2) Cross-site Scripting, Content Spoofing
[6] Standards Mapping - Common Weakness Enumeration - (CWE) CWE ID 82, CWE ID 83, CWE ID 87, CWE ID 692
[7] HTML 4.01 Specification W3
[8] Standards Mapping - SANS Top 25 2009 - (SANS 2009) Insecure Interaction - CWE ID 116
[9] Standards Mapping - Payment Card Industry Data Security Standard Version 1.2 - (PCI 1.2) Requirement 6.3.1.1, Requirement 6.5.1
[10] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 - (PCI 1.1) Requirement 6.5.4
[11] Standards Mapping - Payment Card Industry Data Security Standard Version 2.0 - (PCI 2.0) Requirement 6.5.7
[12] Standards Mapping - FIPS200 - (FISMA) SI
[13] Understanding Malicious Content Mitigation for Web Developers CERT