Escaping X InnerHTML
Tags: html, javascript, beginners, datascience
Published: 2022-03-06 04:00:20

####_any comments HIGHLY appreciated; feedback/questions welcome!_####
---

## Escape and Data Channels
_What is escape code? Why do we use it?_

  Escape code attempts to solve a complex yet foundational issue on how a computer reads and handles data as a stream, like printing or executing. When we see a " _quote_, just one, it's just some text, but to a computer, it is an entire command, a declaration of data! When we want to pass data, it's required to specify what type and how we need the computer to read it.

Escape code example:
`'&#60;imgsrc&#61;&#34;x&#34;onerror&#61;&#34;alert&#40;1&#41;&#34;&#62;';
`

   When providing a user an input field, the computer has to take the input and store it somehow, in what we know as a variable. Then when we go back and tell the computer to read the variable; we call said variable, and we have our assignment/call loop. When we assign an input DIRECTLY to a variable and call it we are _executing_ code.

  In a smart component, we cannot avoid the manipulation of data through these streams and therefore MUST use some form of _escape code_. Our escape code can help us interpret strings and inputs properly, not just allowing us to write code freely, but also protect users against _xSS_


## innerHTML and _Injection_
_wait, what?_

- First, we need to know how innerHTML works and then we move to why we can inject JS to this element.

- Secondly, Injection is defined as running JavaScript from the client-side invoked by the client

Now, when our JS source for a site reads innerHTML data, it doesn't just send some data for an HTML file to execute. The innerHTML executes the value defined by the expression, this means if you use 

` element.innerHTML = <input /> `

When we put `<script> </script>` tags inside our input, we can VERY easily inject code, directly to the site, like so:
`const HACKS=` `<script deferred> alert("xScriptatK");</script>`
_You need to defer because as the page loads the DOM has already executed_

This is how most hackers steal site data: cookies, localStorage, sometimes make fetch() requests for altering data, and then return data to a remote location.


---
---


## **ESCAPING X INNERHTML** 
**_All Together Now:_**

When we want to dynamically set web-data and content we need to use a markup of  _escaped_ characters. 

We have a few options:
1. `textContent`
Renders a string of escaped characters, our HACKS var from earlier would render as a string and not execute.

2. Sanitize 3rd Party Content
Let's say we have an input field, and the user fills it with the following, `'<img src="x" onerror=HACKS()>'` This would send an error and allow them to inject code from the error call, in this case, its `HACKS()` Instead, we call a sanitizer on the content with an escape method it will become:

`app.innerHTML =
'&#60;imgsrc&#61;&#34;x&#34;error&#61;&#34;alert&#40;1&#41;&#34;&#62;';

This is the encoded syntax of 
`<img src="x" onerror="HACKS">`
<img src="x" onerror="HACKS">

(our hacks)

and we remove any markup and get our source back without any injection.

`var sanitizeHTML = function (str) {
	return str.replace(/[^\w. ]/gi, function (c) {
		return '&#' + c.charCodeAt(0) + ';';
	});
};`



This code takes user-submitted (str) and encodes it (sanitizes it) and returns another string(str) the cleaned-up version.


`const Hac = '" onerror="alert(\'XSS Attack\')"';`

`//Inject our dynamic element into the UI
app.innerHTML = '<img src="' + sanitizeHTML(thirdPartySrc) + '">';`
This is just an example of encoding a property, if you want to allow some markup you will need a library of whitelisted elements/methods.


3. Use a framework/library
Using a library like REACT, Angular, or jQuery.
Libraries tend use #2 under the hood, and use understandable syntax to make your code more understandable, and writeable. This is the best option in my opinion, as they are VERY well tested and should be considered reliable by any coder worth his/her/they/whomst/nonbinary markup.

**Roll Credits**
_Now next time you are using a library/framework you can appreciate all the work that is done for you by escape code, handled by a library. 
HUGE THANKS TO:_
[Element.innerHTML](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML)
[gomakethings](https://gomakethings.com/how-to-sanitize-third-party-content-with-vanilla-js-to-prevent-cross-site-scripting-xss-attacks/)
[Decoder](https://www.utilities-online.info/urlencode)
Without these resources I would have no idea where to start with this, and they were highly informative in this learning process.