Interview Prep/DevSecOps/Foundations, Threat Modeling and OWASP

How do XSS attacks work and what are the defences?

IntermediateConceptFoundations, Threat Modeling and OWASP
Answer

XSS injects attacker-controlled script into a page viewed by other users (stored, reflected, DOM-based). The script runs with the victim's session, so it can steal tokens or act as the user.

Defences, in order: contextual output encoding (frameworks like React do this by default), avoid innerHTML and dangerouslySetInnerHTML, sanitise rich text with a vetted library (DOMPurify), a strict Content-Security-Policy (default-src 'self', nonces, no unsafe-inline), and HttpOnly plus Secure cookie flags to limit impact.

All DevSecOps questions Roadmap: Security in the pipeline

More on Foundations, Threat Modeling and OWASP