XSS vulnerability is mainly linked to two parts of the web app:
Source
- such as user input fieldSink
- displays the input data
Front-end
Sanitize and validate the user input on the front-end using JavaScript.
Input Validation
e.g email field won’t accept input if the email format is invalid:
Input Sanitization
We should always ensure that we do not allow any input with JavaScript code in it.
We can utilize the DOMPurify JavaScript library, as follows:
This will escape any special characters with a \
.
Direct Input
Never let user input directly with certain HTML tags:
- JavaScript code
<script></script>
- CSS Style Code
<style></style>
- Tag/Attribute Fields
<div name='INPUT'></div>
- HTML Comments
<!-- -->
We should avoid using JavaScript functions that allow changing raw text of HTML fields like:
DOM.innerHTML
DOM.outerHTML
document.write()
document.writeln()
document.domain
And the following jQuery functions:
html()
parseHTML()
add()
append()
prepend()
after()
insertAfter()
before()
insertBefore()
replaceAll()
replaceWith()
Back-end
Input Validation
An example of E-Mail validation on a PHP back-end is the following:
Input Sanitization
For example, for a PHP back-end, we can use the addslashes
function to sanitize user input by escaping special characters with a backslash:
For a NodeJS back-end, we can also use the DOMPurify library as we did with the front-end, as follows:
Output HTML Encoding
We have to encode any special characters into their HTML codes, which is helpful if we need to display the entire user input without introducing XSS vulnerability.