XSS attack ( Cross Site Scripting ) is a code injection attack that allows an attacker to inject malicious code into a website. If the browser interprets this injected data as code, XSS attack will be a success. Note that XSS is a misnomer. The injection of any script is called XSS. XSS attacks always execute in the browser.

How does XSS work

Http get requests where query string is used to pass input parameters to webapi and http form post requests can be used for code injection.

An attacker could use a script to fetch the cookies stored in the victim’s browser and send them to the attacker’s machine. The attacker could then use those cookies for session hijacking. 

Even without the script tag, the attacker can inject html elements . Events like onclick, onmouseover related the html element could trigger malicious script , for example 

<img src="http://attackersite.com/someimage.jpg" onmouseover=alert(document.cookie);>

Types of XSS

Note that Reflected XSS is much more difficult to detect since these attacks target a user directly (not stored in db ). For example, an attacker can create a malicious url and send it to the user via email, web based advertisements.

XSS risks

The code injected by XSS is executed as legitimate application code giving the attacker full control over the web application executing in the user's browser ie the entire application is compromised with any (read/write/monitoring) , (server side/client side) operation possible.

Attack vectors commonly utilized in XSS attacks

How to prevent XSS attacks

Note that HTML, javascript, and CSS all need to be escaped differently as they are parsed differently by the browser. Note that You cannot simply escape everything or else your own scripts and HTML markup will not work. Escaping will ensure that "the user input will be interpreted as text and not code" In other words, you are telling the browser that the data being sent should be interpreted as text only and not in any other way. Hence even if code injection is successful, it will just be interpreted as text and not executable code. It is always better to use an escaping library (for example https://owasp.org/www-project-enterprise-security-api/)

How does React framework prevent XSS

React APIs autoencode: React framework abstracts away the details of the browser’s DOM and provides a higher-level API to render components. It handles all of the details of putting data into the DOM. Components use the React APIs or the JSX templating language to define what should be rendered. Under the hood, React instructs the browser to create proper elements and update the DOM. React APIs will automatically autoescape so that malicious code if any is just seen as text by the browser. Let's consider the react create element API.

React.createElement("h1", {}, 'This is example comment');

This API creates and returns a new React element of the given type.

Whether components are generated using react API or JSX code react applies auto-escaping. React also does URL sanitization. As already explained in the article, URLs that use javascript: as the scheme, instead of http: or https: pose XSS risk. 

Rendering HTML in react:  Real-world applications often run into requirements where they need to render dynamic HTML code. That HTML code typically originates from untrusted sources, such as user-provided data. For example, Rich text editors generate HTML as output. When we put the output data into DOM using React APIs, React will ensure the output is properly encoded. If the output gets encoded HTML will be shown as text whereas the requirement is that the output should be considered HTML code, not text. 

For this use case, React exposes the innerHTML attribute through the dangerouslySetInnerHTML property. The only point is that name of the property will warn the user's that this property is not safe to use. By itself, the API does not provide any protection. HTML sanitization should be done to protect against XSS attack in this case as output encoding will encode everything and cannot be used. Sanitization will remove malicious code. DOMPurify is a lightweight Javascript-based HTML sanitizer that can be used for HTML sanitization.

Escape hatches in React: React offers a higher-level API to render components. Direct access to DOM is generally not required. However higher-level API is not always enough. In certain scenarios, developers need to have direct access to the native DOM elements. To support such use cases, React offers an escape hatch, which provides the application direct access to native DOM. In React, two concrete escape hatches give access to native DOM elements: findDOMNode and createRef. When using DOM directly , the auto escaping capability of React is not there, and therefore HTML Sanitization or escaping becomes the responsibility of the developer.

 

XSS tools

XSS libraries