Q19: What is Cross-Site Scripting (XSS) and how does it compromise token authentication?

Difficulty:MediumTopic:Security

XSS occurs when an attacker successfully injects malicious client-side script code into a web application. If malicious code executes inside the victim's DOM, it possesses full access to page context.

Impact on Tokens:

  • If tokens are stored in LocalStorage or SessionStorage, injected script code reads localStorage.getItem('token') and exfiltrates it to an attacker server.
  • If tokens are stored in httpOnly cookies, JavaScript cannot read the token string directly. However, the attacker can still trigger authenticated fetch requests from within the DOM context while the session is active.
Key Takeaways
  • LocalStorage offers zero protection against XSS token exfiltration.

  • httpOnly cookies mitigate direct token script extraction during XSS attacks.