Quick revision
The short answer
We accept rich text, but we never trust it: an allowlist sanitiser such as the OWASP Java HTML Sanitizer keeps only the tags and attributes the feature needs, restricts link URLs to http and https and adds rel attributes to links. We store the original and a sanitised version, or sanitise at render time with a versioned policy, and render the sanitised HTML as raw HTML in exactly one place. A strict Content-Security-Policy is the second layer in case anything slips through.
What to remember
In this answer, we designed safe rich-text comments. An allowlist sanitiser such as the OWASP Java HTML Sanitizer keeps only the needed tags, allows only href on links with http or https, and adds rel attributes. We store the original and a sanitised version with its policy version, or sanitise at render time, render raw HTML in exactly one fragment, and keep a strict CSP as the second layer.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We accept rich text, but we never trust it: an allowlist sanitiser such as the OWASP Java HTML Sanitizer keeps only the tags and attributes the feature needs, restricts link URLs to http and https and adds rel attributes to links. We store the original and a sanitised version, or sanitise at render time with a versioned policy, and render the sanitised HTML as raw HTML in exactly one place. A strict Content-Security-Policy is the second layer in case anything slips through.
Why this question is asked
Rich-text comments are where XSS defences get tested for real. Plain text is easy: escape it with th:text and the problem disappears. But as soon as users may write bold text, lists and links, we have to output their HTML as HTML, and that is exactly what an attacker wants. Interviewers ask this design question to see whether we know the difference between escaping and sanitising, and whether we can design the storage and rendering so that one mistake does not become a vulnerability. In this answer, we will see why escaping alone does not work here, design the sanitiser policy, decide when to sanitise and what to store, keep raw rendering in one place, and add CSP as a backstop.
1Why Escaping Alone Does Not Work Here
With th:text, a comment containing <b>great</b> would be shown literally, angle brackets and all. The feature needs those tags to work, so the comment must be output with th:utext, which writes HTML as it is. That also means any <script>, any onerror attribute and any javascript: link would work too.
The answer is to change what we output, not how we output it. A sanitiser parses the user's HTML and rebuilds it with only the elements and attributes we explicitly allow, dropping everything else. A denylist, removing known-bad tags such as script, does not work, because HTML has too many ways to run script: event attributes, SVG, style tricks, malformed markup that browsers repair in surprising ways. An allowlist only keeps what we know is safe.
2Designing the Sanitiser Policy
The OWASP Java HTML Sanitizer builds a policy from an allowlist. We allow only what the feature needs: bold, italic, paragraphs, line breaks, lists and links. Links are the risky part, so we allow only the href attribute on a, only http and https URLs, and we add rel attributes so that linked pages cannot control our tab and search engines do not reward spam links.
Notice what is not in the policy: no style attribute, no class, no img, no iframe. Each one could be justified for some feature, and each one widens the attack surface, so we add them only when there is a real need and a review. The exact output formatting can differ slightly between library versions, so tests should check structure rather than exact strings.
3When to Sanitise and What to Store
There are two sound designs. In the first, we sanitise on input and store both the original HTML and the sanitised version, along with the policy version that produced it. The page renders the stored sanitised version, which is fast. If the policy changes, for example we tighten it after a new bypass, a background job re-sanitises every stored original with the new policy.
In the second, we store only the original and sanitise at render time with the current policy. This always applies the latest policy without a migration job, at the cost of sanitising on every view; caching the result keyed by comment id and policy version brings that cost down. Either way, keeping the original means we never lose what the user wrote, and versioning the policy means we always know which rules produced what we serve.
What we should not do is store only the sanitised version from years ago and trust it forever. Sanitisers get bypasses fixed, and policies change; old sanitised output was produced by old rules.
- User submits HTMLFrom the rich-text editor
- Store originalNever shown directly
- Sanitise with policy v2Allowlist, http/https links, rel added
- Store sanitised + policy versionOr sanitise at render, cached
- Policy version current?Checked on read or by a jobYesRender as raw HTMLNoRe-sanitise original
- Render in one template fragmentOnly place th:utext is used
6 steps in order. Play them, or step through them one at a time.
4Rendering Raw HTML in Exactly One Place
The sanitised HTML has to be output with th:utext, and that is the risky line in the codebase. So we make it the only such line: a single Thymeleaf fragment renders comment bodies, it takes a type that can only be created by the sanitiser, and everything else in the application keeps using th:text.
The benefit is reviewability. A code search for th:utext finds one occurrence, and a test can assert that. If the rich-text content is ever needed elsewhere, such as in an email or an API response for a mobile app, it goes through the same sanitised value; an API that returns rich text should document that it is sanitised HTML, so clients do not apply their own unsafe rendering to the original.
5A Strict CSP as the Second Layer
Even a good sanitiser can have a bypass, and a future developer might add a th:utext somewhere else. A strict Content-Security-Policy limits the damage: with script-src restricted to our own files and per-request nonces, and no 'unsafe-inline', an injected script or event handler does not run. object-src 'none' and base-uri 'self' close other common tricks.
CSP is the backstop, not the defence. The sanitiser decides what HTML is allowed; CSP makes sure that if it gets something wrong, the result is a broken comment rather than a stolen session.
- EditorProduces only the allowed formatting (convenience, not security)
- Allowlist sanitiserKeeps allowed tags; http/https links with rel
- StorageOriginal + sanitised + policy version
- Single render fragmentOnly th:utext; everything else escaped
- Strict CSPInjected script does not run
5 layers, from the top down. Step through them one at a time.
6Follow-up questions
Can we trust the rich-text editor to produce safe HTML?
No. The editor runs in the attacker's browser, and they can send any HTML directly to our endpoint. The editor limits what honest users produce; the server-side sanitiser is the security control.
Why not use Markdown instead of HTML?
Markdown is a good choice and often simpler for users. But most Markdown renderers allow raw HTML and links, so the rendered output still needs an allowlist sanitiser or a renderer configured to forbid raw HTML and unsafe link schemes.
Why add noopener and noreferrer?
Without noopener, a page opened from a link with target=_blank can access window.opener and navigate our tab to a phishing page. noreferrer stops our URL from leaking to the linked site, and nofollow tells search engines not to reward links posted in comments.
7Conclusion
In this answer, we designed safe rich-text comments. An allowlist sanitiser such as the OWASP Java HTML Sanitizer keeps only the needed tags, allows only href on links with http or https, and adds rel attributes. We store the original and a sanitised version with its policy version, or sanitise at render time, render raw HTML in exactly one fragment, and keep a strict CSP as the second layer.
Open with
Because users need bold, lists and links, I cannot just escape the comment with th:text, so the design is about making the HTML safe before it is ever output raw.
Then, if they want more
The core is an allowlist sanitiser, for example the OWASP Java HTML Sanitizer. The policy allows only the tags the feature needs, like p, b, i, lists and a, allows only the href attribute on links, restricts URLs to http and https so javascript links are removed, and adds rel nofollow, noopener and noreferrer.
Everything else, scripts, event attributes, images, style, is dropped, and a denylist would not work because there are too many ways to run script in HTML. For storage, I would keep the original HTML and the sanitised version with the policy version, so if we tighten the policy later, a job can re-sanitise from the originals.
Sanitising at render time with caching is a valid alternative. Then I would render the sanitised HTML with th:utext in exactly one template fragment, fed by a SafeHtml type that only the sanitiser creates, and keep th:text everywhere else, so a code search finds one raw output.
Finally, a strict CSP with nonces and no unsafe-inline is the second layer, so if the sanitiser ever misses something, the injected script does not run.
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 "Users can write rich-text comments with bold, links and lists in your Spring...": https://scrivio-interview-library.netlify.app/spring-boot/web-security-threats/156081918b/