Skip to content
Security2 min read

What is XSS (cross-site scripting)?

Short answer

XSS (cross-site scripting) is a vulnerability where a site shows user input on a page without encoding it, so an attacker can inject JavaScript that runs in the browser of every visitor who opens that page. The script can steal login sessions, change content or redirect visitors.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. Types of XSS
  2. Example: vulnerable code
  3. The fix: encode output
  4. Extra layers of protection
  5. On ArabyHost

The idea is simple: a browser can’t tell code the site owner wrote from code a visitor typed in that was then shown on the page. If the site doesn’t encode input, every comment or search field becomes a way to run foreign JavaScript under your site’s name. The worst case is an admin opening a page with stored XSS: the script then runs with admin rights and can, for example, create a new admin account for the attacker.

Types of XSS

Type Where the code lives Example
Stored In the database, shown to every visitor A malicious comment on a post
Reflected In the URL, runs for whoever opens it A crafted search link sent to the victim
DOM-based In JavaScript running in the browser A script that reads part of the URL and writes it into the page

Example: vulnerable code

echo "Results for: " . $_GET['q'];

If someone opens search.php?q=<script>alert(document.cookie)</script>, the script runs in their browser. A real attacker swaps alert for code that sends the session cookie to their own server.

The fix: encode output

echo "Results for: " . htmlspecialchars($_GET['q'], ENT_QUOTES, 'UTF-8');

Now < and > appear as plain text and the browser doesn’t treat them as code. The general rule: encode every output for the context it appears in.

  • WordPress: esc_html() for text, esc_attr() for HTML attributes, esc_url() for links.
  • JavaScript: use element.textContent instead of element.innerHTML for any user-supplied text.

Extra layers of protection

  1. Content Security Policy (CSP): a header like Content-Security-Policy: script-src 'self' blocks inline and unapproved external scripts.
  2. HttpOnly cookies so JavaScript can’t read them.
  3. Keep WordPress plugins updated, since many published XSS flaws live in them.
  4. A WAF to block known patterns.

On ArabyHost

Imunify360 security on ArabyHost servers includes rules that block many known XSS attempts, but output encoding still belongs in your code. Start by updating plugins and themes, and follow the secure WordPress guide. If your site throws an error after a code change, the Araby AI assistant in the panel can read the error log and explain the cause.

FAQ

What's the difference between XSS and SQL injection?

SQL injection targets the database on the server; XSS targets the visitor's browser. The root cause is the same: untrusted input treated as code.

Do HttpOnly cookies protect against XSS?

They stop scripts from reading the session cookie, which closes the simplest route to account takeover. But malicious code can still perform actions as the visitor within the page, so output encoding is still essential.

How can I test my site for XSS?

Try entering a word wrapped in a simple HTML tag, such as the bold tag b, into search and comment fields. If the word shows up bold instead of the tag appearing as text, output isn't being encoded and the field may be vulnerable.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.