Comment Renderer (HTML Injection)
Easy About 15 min Free, no account needed
Fix a stored-XSS hole in a comment renderer without breaking its output.
The brief
The bug in plain terms: This is stored cross-site scripting (XSS), where text a user types (here, a blog comment) is dropped straight into the page's HTML, so a comment containing
<script>or anonerrorhandler runs as real code in every other visitor's browser. With that foothold an attacker can steal session cookies, impersonate logged-in users, or deface the page for everyone who reads the comment. The trap: simply stripping out<script>tags or "bad words" is not enough, because there are countless ways to inject markup (attributes likeonerror, broken-out quotes, encoded variants), so the only reliable fix is to encode every user-supplied character so the browser always treats it as plain text, never as markup.
A blog renders user comments by stitching the author name and comment text straight into an HTML fragment:
return $"<div class=\"comment\"><span class=\"author\">{author}</span><p>{text}</p></div>";It works, until someone posts a comment like:
<script>alert('pwned')</script>That markup is delivered verbatim to every visitor's browser and executes as
code: a classic stored XSS (cross-site scripting) vulnerability. An attacker
can also break out of the author span with a payload like
"><img src=x onerror=alert(1)>.
Close the hole without changing the rendered markup for honest input: an
honest comment must still produce exactly the same fragment it does today, and
the page structure (the div, span and p) must stay intact no matter what a
visitor types. Work out what has to happen to user-supplied text before it
reaches the markup, and where, so the browser treats it as text and never as
code.
- Functional tests: honest comments must render exactly as before.
- Security tests: exploit payloads that currently inject markup must come back inert. The vulnerable starter fails every one of them.
In production code you would reach for your framework's built-in encoder rather than writing your own. Here the sandbox hides it from you on purpose. Knowing what a correct encoder must do is the point of the exercise.
Starter code
using System;
public class Solution
{
// Renders a user comment as an HTML fragment for the page.
public string RenderComment(string author, string text)
{
// BUG: user input is concatenated straight into the markup, so a comment like
// "<script>...</script>" is delivered to every visitor's browser as code.
return $"<div class=\"comment\"><span class=\"author\">{author}</span><p>{text}</p></div>";
}
}How it is graded
Functional tests and adversarial payloads both have to pass before the solution is accepted.
Related guides
What it practices
- Output encoding (XSS and formula injection)
- Encode or neutralize untrusted text for the context it lands in, such as HTML or spreadsheet cells.
Related katas
- Quarantine Spreadsheet Formulas Easy Pro Keep untrusted export cells from becoming spreadsheet commands, including whitespace and full-width prefixes.
- Filename Sanitizer Easy Free Reduce an uploaded filename to a safe, single path segment.
- Log Forging (CRLF) Easy Free Stop user input from forging extra log lines with embedded newlines.
- Mass Assignment Guard Easy Free Decide which submitted fields may be applied to a record using an allowlist, not a blocklist.