Unescaped Output and Formulas in Visualforce Pages
Formula expressions can be function calls or include information about platform objects, a user's environment, system environment, and the request environment. It’s important to be aware that the output that’s generated by expressions isn’t escaped during rendering. Since expressions are rendered on the server, it’s not possible to escape rendered data on the client using JavaScript or other client-side technology. This can lead to potentially dangerous situations if the formula expression references non-system data (that is, potentially hostile or editable data) and the expression itself is not wrapped in a function to escape the output during rendering.
1<apex:page standardController="Account">
2 <apex:form>
3 <apex:commandButton rerender="outputIt" value="Update It"/>
4 <apex:inputText value="{!myTextField}"/>
5 </apex:form>
6
7 <apex:outputPanel id="outputIt">
8 Value of myTextField is <apex:outputText value="{!myTextField}" escape="false"/>
9 </apex:outputPanel>
10</apex:page>1<script>alert('xss')- HTMLENCODE
- Encodes text and merge field values for use in HTML by replacing characters that are reserved in HTML, such as the greater-than sign (>), with HTML entity equivalents, such as >.
- JSENCODE
- Encodes text and merge field values for use in JavaScript by inserting escape characters, such as a backslash (\), before unsafe JavaScript characters, such as the apostrophe (').
- JSINHTMLENCODE
- Encodes text and merge field values for use in JavaScript inside HTML tags by replacing characters that are reserved in HTML with HTML entity equivalents and inserting escape characters before unsafe JavaScript characters. JSINHTMLENCODE(someValue) is a convenience function that is equivalent to JSENCODE(HTMLENCODE((someValue)). That is, JSINHTMLENCODE first encodes someValue with HTMLENCODE, and then encodes the result with JSENCODE.
- URLENCODE
- Encodes text and merge field values for use in URLs by replacing characters that are illegal in URLs, such as blank spaces, with the code that represent those characters as defined in RFC 3986, Uniform Resource Identifier (URI): Generic Syntax. For example, blank spaces are replaced with %20, and exclamation points are replaced with %21.
1<apex:outputText value=" {!HTMLENCODE(myTextField)}" escape="false"/>1<script>var ret = "{!$CurrentPage.parameters.retURL}";</script>1https://example.com/demo/redirect.html?retURL=%22foo%22%3Balert('xss')%3B%2F%2F1<script>var ret = "foo";alert('xss');//";</script>1<script>var ret = "{!JSENCODE($CurrentPage.parameters.retURL)}";</script>Formula tags can also be used to include platform object data. Although the data is taken directly from the user's organization, it must still be escaped before use to prevent users from executing code in the context of other users (potentially those with higher privilege levels). While these types of attacks must be performed by users within the same organization, they undermine the organization's user roles and reduce the integrity of auditing records. Additionally, many organizations contain data which has been imported from external sources and might not have been screened for malicious content.