QA Interview Hub · Knowledge base · Quizzes · Українська

Penetration Testing › Client side: XSS, CSRF, CORS, clickjacking

XSS: reflected, stored and DOM

XSS is an injection of HTML or JavaScript that runs in the browser in the site's origin and acts on behalf of the user. There are reflected (from the request), stored (from the database) and DOM-based (an unsafe insertion in client-side JS) kinds. Protection is output encoding by context and safe APIs such as textContent; CSP is only a second line. HttpOnly does not remove XSS.

Pentest · Client side

A comment with a script tag runs for every visitor. Why does the browser trust that code?

Someone left text that looks like a script tag in a comment under an article, and now everyone who opens the page runs foreign code. The browser does not tell a developer’s script from a script on the page: everything that came from this site has the rights of this site. The lesson covers three kinds of XSS, the input context that changes the payload, and what really protects.

Mechanism. XSS (cross-site scripting) is an injection into HTML or JavaScript in the user’s browser. The script runs in the site’s origin, so it can read the page, send requests on behalf of the user with his cookies, change the interface (forge a login form), capture typed input. HttpOnly stops a script from reading the cookie, but it still sends requests on behalf of the user.

Three kinds of XSS: where the script comes from

Reflected: the server returns input at oncelinkresponsebrowserStored: input sits in the databaseformdatabasevisitorsDOM: the server never sees the fragmentURL with #page JSinnerHTML
Figure 1. Red marks where the script runs. In the DOM variant the server takes no part at all.
  • Reflected. A value from the request (?q=…) goes straight into the response. The attack is a link the victim has to open;
  • Stored. The value lies in the database (a comment, a profile name, a file name) and runs for everyone who sees the page. More dangerous: the victim needs to click nothing;
  • DOM-based. The unsafe insertion is done by JavaScript on the client: from a source (location.hash, document.referrer, postMessage) into a sink (innerHTML, document.write, eval). The part after # is not sent to the server, so server logs and a WAF do not see it.

Input context. What works depends on where the value ended up in the response. So a test starts with a harmless marker and looks at the source or the DOM to see how it was encoded:

<!-- 1. HTML body -->
<p>Hello, <img src=x onerror=alert(document.domain)></p>

<!-- 2. attribute -->
<input value="" autofocus onfocus=alert(1) x="">

<!-- 3. JavaScript string -->
<script>var q = '';alert(1);//';</script>

<!-- 4. URL attribute -->
<a href="javascript:alert(1)">profile</a>

These are textbook examples for a test environment or a lab. Each context needs its own encoding: in HTML text escape < and &, in an attribute quotes as well, in a JS string quotes and the backslash, in a URL check the scheme (https:, not javascript:). There is no single "one function".

A DOM XSS example. A page shows a greeting from the address:

// before
el.innerHTML = "Hello, " + decodeURIComponent(location.hash.slice(1));

// after
el.textContent = "Hello, " + decodeURIComponent(location.hash.slice(1));

textContent inserts text, not markup, so tags lose their force. If markup is really needed (an editor), it is passed through a sanitization library with an allowlist (for example, DOMPurify), not a home-made regular expression.

A real case. On 4 October 2005 Samy Kamkar released code on MySpace from his profile that added him as a friend, appended "but most of all, samy is my hero" to the profile, and copied itself onto the profile of everyone who opened an infected one. MySpace allowed part of HTML and CSS; the code bypassed the filters. In about 20 hours over a million profiles were infected and the site had to be taken down for a while. Kamkar pleaded guilty in 2007 and received probation and community service. On its own it was a "prank", but the self-propagating stored XSS showed that a foreign script in a page acts on behalf of every viewer.

What protects.

  • Output encoding by context and safe APIs (textContent, attributes through the DOM, template engines with auto-escaping). Frameworks (React, Angular, Vue) escape text by default; the risk is where they are bypassed: dangerouslySetInnerHTML, v-html, href with a user-supplied value;
  • CSP as a second line (next lesson). It does not fix the flaw, it makes execution harder;
  • Input validation helps but is not the main thing: the name "O’Brien" or a comment about <script> must be allowed, so protection is applied on output.

How QA checks it. On an authorized environment or in a lab (PortSwigger Web Security Academy, OWASP Juice Shop with DOM XSS in search, DVWA). Enter a unique marker with the characters "'<> into every field that is displayed later: profile, comments, file names, address, the Referer and User-Agent headers an admin sees. Look at how the marker is encoded in the source and the DOM. alert(document.domain) is enough as proof, without stealing sessions. "Blind" XSS (firing later in an admin panel) is tested only with a controlled test callback and by agreement.

Common mistakes.

  • Filtering only the script tag: img onerror, svg onload, attribute events work;
  • Encoding for HTML and inserting into a JavaScript string: this context needs different escaping;
  • Believing that HttpOnly or HTTPS eliminates XSS: they only limit part of the consequences.

Check yourself

The parameter ?name= lands in <input value="…">. Is escaping < and > enough?

Answer

No. In an attribute a quote is enough to close the value and add an event handler (" onfocus=alert(1) x="). For this context quotes and the ampersand are escaped and the attribute value is always quoted.

Why do a WAF and server logs not see DOM XSS taken from location.hash?

Answer

The browser does not send the part after # to the server: client-side JavaScript reads and inserts it. So the client code (sinks like innerHTML) has to be fixed and detection happens on the browser side.

In an interview. Junior: XSS is a foreign script in a page. Reflected comes from the request, stored sits in the database, the DOM variant arises in client JS. The defence is output encoding by context and safe APIs. Middle/Senior: I start with a marker and look at the context it landed in, and make the proof alert(document.domain). Separately I check DOM sinks, dangerouslySetInnerHTML and links with input; I treat CSP as a second layer, not a fix.

More articles in this chapter