liveBook · Manning

Cross-site scripting attack

As explained in Web Component Development with Modern Libraries and Tooling

A security attack in which injected script executes in a user's web context through insufficiently sanitized content.

A cross-site scripting attack can occur when an attribute value is interpolated into an HTML string and assigned to innerHTML. Prefer textContent when inserting user-controlled text; innerHTML is acceptable only for trusted HTML. section 3.1.2

Unsafe versus safer rendering

A component reading name from <hello-world name="Mark"> can render safely with textContent. Replacing that DOM construction with this.innerHTML = `<p>Hello, ${name}!</p>` allows an XSS attack if name contains hostile markup. section 3.1.2

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Contrasts unsafe innerHTML interpolation with textContent and limits innerHTML to trusted HTML.
Examples
Uses a hello-world custom element whose name attribute becomes either textContent or interpolated innerHTML.
What it assumes
Assumes basic custom-element attributes and DOM rendering while demonstrating both rendering alternatives.
What you can do after
Apply DOM APIs to render attribute values as text and recognize when interpolated HTML creates an XSS risk.
How it connects
Connects cross-site scripting attacks to innerHTML, textContent, web components, and DOM attributes.

Also explained in

Compare how 6 books explain cross-site scripting attack →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Component Development with Modern Libraries and Tooling. The section links point to the parts of the book it draws on.