A browser trusts everything that arrives from a website’s origin equally. If a page from your company’s domain contains a piece of JavaScript, the browser runs it with all the privileges that domain has: it can read the page, act as the logged-in user, call the site’s APIs and change what the user sees. The browser does not ask who wrote the code. Cross-site scripting, usually shortened to XSS, is what happens when an attacker manages to get their code into that trusted stream.

The name is historical and slightly misleading. Not every case involves another site, and the effect is better described as “attacker-controlled content being executed as code inside your site”. It is a form of injection: user-supplied data ends up interpreted by the browser as markup or script instead of as plain text.

What an attacker gains

The consequences depend on what the vulnerable page can do. Running in a victim’s session, injected code can typically read anything visible on the page, submit forms and call APIs as the user, alter the page to show a fake login prompt, or capture keystrokes on that page. If session cookies are not protected with the HttpOnly flag, it can read them and send them away, letting the attacker take over the session. On an admin page, XSS can mean creating new admin accounts. On a banking or trading portal, it can mean initiating actions the user never intended.

Three ways the code gets in

Stored XSS. The malicious input is saved by the application, for example in a product review, a support ticket, a user’s display name or an uploaded file’s metadata, and later shown to other users. Everyone who views it is affected. Stored XSS in an internal tool is especially dangerous, because the people viewing support tickets or admin dashboards tend to be privileged staff.

Reflected XSS. The input is part of a request, often a URL parameter such as a search term, and the server echoes it straight back into the response page. The attacker has to get the victim to open a crafted link, usually via phishing. Nothing is stored; the payload travels inside the link.

DOM-based XSS. The server never sees the problem. The page’s own JavaScript reads something attacker-controllable, such as part of the URL after the hash sign, and writes it into the page in a way that causes it to be interpreted as HTML. Single-page applications with heavy client-side logic are prone to this, and server-side scanners often miss it.

The core defence: encode for the context

The fundamental fix is to make sure that when user data is placed into a page, it is treated as text, not as markup. This is done by output encoding: converting characters that have special meaning into a safe representation. In ordinary HTML text, for instance, the less-than sign is converted to the entity <, so the browser displays it rather than treating it as the start of a tag.

The catch is that the right encoding depends on where in the page the data lands. A value placed in the body text, inside an HTML attribute, inside a JavaScript string, inside a URL, or inside a CSS value each has different special characters. Encoding meant for HTML body text does not make a value safe inside a JavaScript block. This is called context-aware encoding, and it is why hand-rolled escaping functions so often fail.

Modern frameworks handle much of this automatically. React, Angular and Vue escape values inserted into templates by default, and server-side template engines such as those in Django or Spring typically auto-escape too. XSS in these frameworks usually comes from deliberately bypassing the protection: functions or directives whose names warn that they insert raw HTML, building HTML strings manually, or putting user data into URL attributes where a value starting with a scheme like javascript: can execute. A good review habit is to search the codebase for every use of the raw-HTML escape hatches and justify each one.

When you must allow some HTML

Sometimes users legitimately need formatting, such as a rich-text editor for blog posts or support replies. Encoding everything would break that. The answer is sanitisation with a well-maintained library that parses the HTML and keeps only an allow-list of harmless tags and attributes, such as bold, italics, lists and links with safe URL schemes. Writing your own sanitiser with regular expressions is a known path to trouble, because browsers parse malformed HTML in surprising ways.

Content Security Policy as a safety net

A Content Security Policy (CSP) is an HTTP response header that tells the browser which sources of scripts and other resources the page is allowed to use. A strict policy can forbid inline scripts entirely and allow only scripts carrying a per-response random value (a nonce) or a matching hash. If an attacker manages to inject markup, the browser refuses to run it because it lacks the nonce.

CSP is powerful but often deployed weakly. Policies that allow unsafe-inline, or that trust whole content delivery networks hosting thousands of third-party libraries, provide little protection. Rolling out a strict policy on an older application takes work, so teams commonly start in report-only mode, collect violation reports for a few weeks, fix legitimate inline code, and then enforce.

Supporting measures

  • Mark session cookies HttpOnly so scripts cannot read them. This does not stop XSS from acting as the user, but it stops simple session theft.
  • Set correct content types on responses, and serve user uploads from a separate domain so an uploaded file cannot run with your main site’s privileges.
  • Validate input formats where they are strict, such as phone numbers or PIN codes. This narrows the attack surface, though free-text fields still rely on encoding.
  • Require re-authentication or an OTP for high-impact actions such as changing a registered mobile number, so injected code cannot complete them silently.

The underlying rule is the same as for every injection flaw: data should stay data. Frameworks get you most of the way by default. Most real XSS bugs live in the places where someone turned that default off, so knowing where those places are in your codebase is the most practical defence of all.

Written by the RP Strativa Team.

Leave a Reply

Trending