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.
More on Foundations, Threat Modeling and OWASP
What is DevSecOps, and how is it different from DevOps?BeginnerWhat does "shift-left" mean?BeginnerIf we shift left, why do we still need "shift-right" controls?IntermediateWhat are the phases of a secure SDLC and a security activity for each?BeginnerExplain the CIA triad with a DevOps example for each property.BeginnerWhat is threat modeling and when should you do it?Beginner