XSS (Cross-Site Scripting)

Securityintermediate

// Definition

An attack where attacker-controlled JavaScript executes in another user's browser, often via unescaped input rendered into HTML. Categories include reflected, stored, and DOM-based. Mitigated by output encoding and a strict Content Security Policy.

// Why it matters

XSS injects attacker-controlled script into a page other users load, hijacking sessions or defacing UI. For QA it's the canonical input-validation test: anywhere user input is later rendered — comments, names, search terms reflected back — is a candidate. Stored XSS is the dangerous variant because one bad input poisons every viewer.

// How to test

const payload = `<img src=x onerror="window.__xss=1">`
cy.visit('/profile/edit')
cy.get('[data-cy=display-name]').clear().type(payload)
cy.get('[data-cy=save]').click()

cy.visit('/profile')                  // reload where it renders
cy.window().should('not.have.property', '__xss') // script must NOT execute
cy.contains(payload)                  // it should appear as escaped text

// Common mistakes

  • Sanitising on display but not on storage (or vice-versa), missing one path
  • Escaping HTML but not JS/attribute/URL contexts (each needs its own encoding)
  • Trusting a WYSIWYG/markdown field because "it's just formatting"

// Related terms

Learn more · Non-Functional Testing Overview

Chapter 3 · Lesson 2: Common Vulnerability Categories — XSS, SQL Injection, CSRF