Reflected and Stored XSS in Server-Rendered Output
Follow immediate and saved messages into server-rendered pages, and choose output handling for the way each value is used.
You’re reading a preview. Premium includes the full lesson and specialist practice. View the full curriculum.
A customer leaves a support message: Thanks, team! A staff member opens the ticket, and the app displays those words. Now the customer sends <b>Thanks, team!</b> instead. Does the staff page show the angle brackets, or create a bold element?
That difference is the starting point for cross-site scripting, or XSS. The risk appears when someone else's content is interpreted as active page content. We'll first inspect a message displayed immediately, then a saved message opened by staff.
| Where the value goes | What the browser should receive for a plain-text feature |
|---|---|
| HTML text output | The literal customer message |
| A stored ticket body | The same text rule when it is read later |
| A link or another output context | A separate check appropriate to that use |
Reflected describes the immediate request-and-response path. Stored describes a value saved before a later page displays it. We'll locate the actual output operation in both.
Start with the source and the eventual audience
List every input channel, not only obvious text boxes. Query parameters, saved ticket bodies, imported customer names, filenames, log fields, and integration responses can all reach a page. A field becomes dangerous when its eventual consumer grants it more interpretation or authority than its writer should possess.
<?php
// Unsafe reflected output: request data becomes HTML source.
$note = $_GET['note'] ?? '';
echo '<section class="popup">';
echo '<p>' . $note . '</p>';
echo '</section>';
?>
Here the note does not need a database round trip. It is reflected into paragraph content. A crafted value that introduces elements or event attributes can be interpreted by the HTML parser. A map popup or search preview is still a browser rendering surface even if the enclosing feature is not called a comment system.
Select each stop. Safe storage and safe display answer different questions.
Write. The body is untrusted even when the customer submits it through a normal form.
{
"subject": "Billing question",
"body": "<b>customer marker</b>"
}
Store. Binding prevents SQL grammar injection but preserves the body’s bytes and provenance.
INSERT INTO tickets (subject, body)
VALUES (?, ?);
Display. An unescaped body directive grants HTML interpretation to an external writer.
<h1 th:text="${ticket.subject}"></h1>
<section th:utext="${ticket.body}"></section>
Review question: The defect is at the output interpretation boundary; persistence does not make input trusted.