Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

How do stored, reflected and DOM-based XSS differ, and how do Thymeleaf's th:text and th:utext relate to XSS?

Stored XSS saves a malicious script in data that is later shown to other users, reflected XSS echoes script from the current request straight back into the page, and DOM-based XSS happens entirely in client-side JavaScript that writes untrusted data into the page. In Thymeleaf, th:text escapes HTML so user input is shown as text, while th:utext outputs raw HTML and must only be used with trusted or sanitised content. Encoding must also match the context, and CSP limits the damage but does not replace encoding.

Why this question is asked

Cross-site scripting is still one of the most common web vulnerabilities, and it is serious: script running in our page can do anything the user can do, read anything the user can see and send it elsewhere. Interviewers ask about the three types because they arrive by different routes and are fixed in different places, and they ask about th:text and th:utext because that one letter decides whether a Spring page is safe. In this answer, we will recap what XSS is, look at each type with an example, see how Thymeleaf protects us and where it cannot, explain why the encoding depends on the context, and place CSP correctly as a second layer.

1A Quick Recap of XSS

XSS (cross-site scripting) happens when an attacker gets their own JavaScript to run in our page, in another user's browser. Because the script runs on our origin, the browser treats it as ours: it can read the page, call our APIs with the user's session, change what the user sees and, if tokens are reachable from JavaScript, steal them.

The root cause is always the same: untrusted data is placed into HTML or JavaScript in a way that lets it be interpreted as code instead of text. The three types differ in where the untrusted data comes from and where it is inserted.

2Stored, Reflected and DOM-Based XSS

Stored XSS is the most damaging. The attacker submits data, say a comment containing <script>...</script> or <img src=x onerror=...>, and the application stores it in the database. Every user who later views the comment page gets the script, with no further action from the attacker.

Reflected XSS uses the current request. A search page shows "Results for: " followed by the q parameter. If it inserts q without escaping, the attacker sends the victim a link with a script in q, and the page reflects it back. It needs the victim to follow a crafted link, but it needs no stored data.

DOM-based XSS never touches the server's HTML. Client-side code reads something attacker-controlled, such as location.hash or a query parameter, and writes it into the page with an unsafe API like innerHTML. The server may be perfectly safe; the bug is in our JavaScript, and the fix is to use textContent or a framework that escapes by default.

Three types of XSS
TypeWhere the script comes fromWhere to fix it
StoredSaved data shown to others (comments, names)Server-side output encoding, sanitising
ReflectedCurrent request (query, form, header)Server-side output encoding
DOM-basedClient-side sources (location.hash, postMessage)Client code: textContent, safe APIs

3Th:text Escapes, th:utext Does Not

Thymeleaf's th:text writes a value as text: it escapes the HTML special characters, so < becomes &lt;, > becomes &gt;, & becomes &amp; and quotes become entities. The browser then shows the characters instead of interpreting them as tags. th:utext (unescaped text) writes the value as it is, as HTML.

Java
// model: comment = "<img src=x onerror=alert(document.cookie)>"
<p th:text="${comment}"></p>
<!-- <p>&lt;img src=x onerror=alert(document.cookie)&gt;</p> shown as text, safe -->
<p th:utext="${comment}"></p>
<!-- <p><img src=x onerror=alert(document.cookie)></p> runs the script -->
<p>[[${comment}]]</p> <!-- inlined, escaped like th:text -->
<p>[(${comment})]</p> <!-- inlined, unescaped like th:utext -->

So th:text is the default for anything that came from a user, an external system or the database. th:utext is for HTML we trust, such as content written by our own editors, or user HTML that has been through an allowlist sanitiser. Notice that the inline forms follow the same rule, which is easy to miss in review: double brackets escape, bracket-parenthesis does not.

4Encoding Must Match the Context

HTML escaping is correct for text between tags, but data lands in other places too, and each has its own rules. Inside an HTML attribute, the value must be escaped and quoted. Inside a JavaScript block, HTML escaping is the wrong tool: a value like '; alert(1); ' breaks out of a JavaScript string, so the value must be JavaScript-encoded. Inside a URL, the value must be URL-encoded, and the URL's scheme must be checked, because a perfectly escaped javascript:alert(1) in an href still runs when clicked.

Thymeleaf helps with each of these when we use the right construct. Attribute processors such as th:value and th:title escape for attributes. With th:inline="javascript", [[${value}]] inside a script block is output as a properly escaped JavaScript literal. Link expressions @{...} URL-encode parameters. But no template engine can know that a user-supplied URL should not start with javascript:, so that check is ours.

XML
<script th:inline="javascript">
const name = [[${user.name}]]; // "O'Brien</script>" -> "O\'Brien<\/script>" as a JS string
</script>
<a th:href="@{/search(q=${q})}">again</a> <!-- /search?q=%3Cscript%3E... -->
<a th:href="${profile.website}">site</a> <!-- "javascript:alert(1)" -> still clickable: validate scheme -->

Check the exact escaping output for your Thymeleaf version; the point is that each context has a matching mechanism and HTML escaping alone is not enough.

5Where CSP Fits

A Content-Security-Policy with a strict script-src, using nonces and no 'unsafe-inline', tells the browser to refuse inline scripts and scripts from unlisted sources. If an injection slips through, the injected script may simply not run. That is a valuable second layer, especially for large applications where one forgotten th:utext is likely somewhere.

But CSP does not replace encoding. Policies are often weakened for legacy code or widgets, older browsers support less, and some injections, such as injected HTML that changes a form's action or adds a fake login form, do not need script at all. Encoding stops the injection; CSP limits what a missed injection can do.

6Follow-up questions

Is escaping on input instead of output a good idea?

Generally no. The same data may be shown in HTML, JSON, a PDF and an email, each needing different encoding, and escaping on input corrupts data for the other uses. Store the original, encode on output for the context. Sanitising rich text with an allowlist is a separate, deliberate step.

Do React and Angular prevent XSS?

They escape values in templates by default, which removes most XSS. The escape hatches, such as dangerouslySetInnerHTML in React or bypassSecurityTrustHtml in Angular, have the same role as th:utext and need the same care.

Does HttpOnly on the session cookie stop XSS?

It stops the script from reading the cookie, which is useful. But the script can still make requests with the user's session from inside the page, so the attacker can still act as the user.

7Conclusion

In this answer, we saw that stored XSS comes from saved data, reflected XSS from the current request and DOM-based XSS from client-side code. Thymeleaf's th:text escapes HTML and is the default for untrusted data, while th:utext and [(...)] output raw HTML for trusted or sanitised content only. Encoding must match the context, with JavaScript inlining, URL encoding and scheme checks where needed, and CSP is a second layer that limits damage but never replaces encoding.

What to say in the interviewAbout 90 seconds aloud

Open with

All three are about untrusted data being interpreted as script in our page, but they come from different places. Stored XSS is when the script is saved, for example in a comment or a display name, and served to every user who views it, so it is the most damaging.

Then, if they want more

Reflected XSS is when the page echoes something from the current request, like a search term, without escaping, so the attacker needs the victim to click a crafted link.

DOM-based XSS happens only in the browser: our JavaScript reads something like location.hash and writes it into the page with innerHTML, so the server can be completely safe and the bug is in client code.

In Thymeleaf, th:text escapes HTML, so angle brackets become entities and user input is shown as text. th:utext writes raw HTML, so I only use it for trusted content or HTML that went through an allowlist sanitiser, and I watch for the inline form with bracket-parenthesis, which is also unescaped.

I would also point out that encoding depends on the context. HTML escaping is right for text, but inside a script block you need JavaScript encoding, which Thymeleaf does with th:inline javascript, URLs need URL encoding, and a user-supplied link needs a scheme check because javascript URLs survive escaping.

Finally, a strict CSP is a good second layer that can stop injected scripts from running, but it does not replace correct encoding.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
OWASP Top Ten
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do stored, reflected and DOM-based XSS differ, and how do Thymeleaf's th:text and...": https://scrivio-interview-library.netlify.app/spring-boot/web-security-threats/372c52be98/