```
- Directly as a script tag or other HTML tag that supports a javascript event handler
- Breaking out of an html tag and creating another html tag that is a javascript event handler
Because of this, user input within an html context needs to be prevented from breaking out of a quoted context or from injecting html tags. This is done with HTML encoding.
HTML Encoding
In order to force a string character to be interpreted as data rather than markup, a [character reference](http://www.w3.org/TR/html5/syntax.html#character-references) should be used. There are two common ways to denote a character reference:
- *numeric character references* represent the character by an ampersand (&), the pound sign (\#) followed by either the decimal unicode point value, or an "x" and the hexadecimal unicode value. Finally, a semicolon (;) closes out the character reference. This allows every unicode character to be referenced.
- *entity character references* represent a subset of commonly used special characters by an ampersand (&) an ascii mnemonic for the character's name, and an (optional) closing semicolon.
HTML Encoding is the process of replacing characters by their character references and HTML decoding is the reverse.
```html
<a href="www.salesforce.com">link
<a href="www.salesforce.com">link
```
The HTML parser generates the following DOM:
```html
```
which the browser renders as:
```html
link
link
link
link
```
- For link1, because the bracket is replaced by its character reference, the less than sign is treated as a string literal. The closing tag \ is viewed as a redundant closing tag and is not rendered in the DOM at all.
- In link2, an escaped character immediately follows the opening tag, but the HTML Parser is expecting a tagname which is markup, as this is impossible the HTML parser bails on tag processing and interprets the opening tag as text. The closing tag is swallowed as in link1.
- In link3, the anchor tag is successfully parsed but as the "h" in "href" is escaped, the href is not interpreted as an attribute and the result is an anchor tag without an href, the link text appears but is not clickable.
- In link4, because a portion of an attribute value is encoded, the character references are decoded to "www.sales" in the DOM and the link is clickable, successfully navigating to www.salesforce.com
Therefore if developers html encode user data prior to HTML rendering, the data will always render as text and never as markup. In general, only a subset of characters are html encoded: those characters that can allow an attacker to inject their own tags or break out of a quoted attribute value:
|Common Name|Symbol|Decimal Numeric|Hex Numeric|Entity|
|-----------|------|---------------|-----------|------|
|Ampersand|&|&\#38;|&\#x26;|&|
|Less than Symbol|\<|&\#60;|&\#x3c;|<|
|Greater than Symbol|\>|&\#62;|&\#x3e;|>|
|Single Quote|'|&\#39;|&\#27;|N/A|
|Double Quote|"|&\#34;|&\#22;|"|
When using unquoted attribute values or when failing to close tags other characters need to be escaped. However in this case the set of characters that would need to be escaped are browser dependent and may change with new browser versions. Developers should ensure that all HTML tags are balanced and that all attribute values are quoted. User data within an HTML context should only appear as the text content of an existing tag or within a quoted attribute value.
- HTML Parsing Contexts
The HTML parser operates in several contexts: the most important of which are normal, raw text, and escapable raw text contexts.
PCDATA or Normal Context Parsing
For most tags the PCDATA (in HTML 4), or normal (in HTML 5) parsing context applies. In this context the HTML parser tries to balance nested tags and performs HTML decoding prior to DOM rendering.
For example, the HTML parser converts:
```html
in alpha. Still in alpha
in beta
```
into the following DOM:
```html
" Still in alpha"
in beta
```
For PC Data parsing, keep in mind that
- Because tags and attribute values are balanced, the parser will not allow a data within an attribute value to inject a new tag or close out an existing tag, as per the example above. The only way to escape a quoted attribute value is to close out the quote OR enter a CDATA context.
- All character references are decoded, and are therefore unsafe to be used as inputs into further HTML rendering contexts without applying an additional round of encoding.
Raw Text or CDATA Parsing
For \');