What is Cross-Site Scripting (XSS)?

How attackers hide scripts in your web pages to steal your visitors’ logins, and the simple rules that stop it.

Cross-Site Scripting (XSS) is when an attacker sneaks their own JavaScript into your website. When a visitor opens the page, the attacker’s code runs in the visitor’s browser as if your site sent it.

How it works

Many sites show text that users type in: comments, search results, contact forms, profile names. If the site displays that text without cleaning it first, an attacker can type code instead of words. Everyone who views that page then runs the code.

What attackers can do

  • Steal login cookies and take over accounts
  • Show fake login boxes to collect passwords
  • Redirect visitors to scam or malware sites
  • Change what your page says

How to protect your site

  • Escape all user input before showing it on a page
  • Add a Content-Security-Policy header to block unknown scripts
  • Keep WordPress, themes and plugins updated
  • Mark login cookies as HttpOnly so scripts can’t read them
Key takeaway

Never trust text that users type. Clean it before it reaches your pages.

Not sure whether your own site is exposed? Request a free security check or write to us.