DOM XSS, Rich Text, and Context-Aware Defenses
Follow a saved note through JSON to the dashboard and see why textContent and innerHTML give the same string different jobs.
You’re reading a preview. Premium includes the full lesson and specialist practice. View the full curriculum.
A dashboard loads yesterday's search terms from an API. The response is valid JSON, and every record came from the database. One term happens to contain <b>sale</b>.
JSON preserves that string; it does not decide how the dashboard will display it. If the page joins it into HTML and assigns it to innerHTML, the browser reads markup. If the page assigns it to textContent, the browser displays the characters.
Someone writes a term → storage → JSON response → dashboard formatter → browser output
We'll follow one saved term through that journey. DOM XSS names the browser-side interpretation step. The same finding can also be stored XSS because the term was saved earlier.
Follow provenance through the API
A storage system does not change who originally controlled a value. Query terms and user identifiers can be stored in DynamoDB, a relational table, or a log service and still be attacker-influenced. Determine which fields are actually controllable, which ingestion paths exist, and which users view them.
// Server: serializing data is appropriate for this API response.
const records = await analytics.readForTenant(actor.tenantId);
res.json({
records: records.map(row => ({
term: row.term,
visitor: row.visitor,
count: row.count,
})),
});
// Browser: the response is parsed back into strings and numbers.
const response = await fetch('/api/analytics', { credentials: 'same-origin' });
const data = await response.json();
const markup = data.records
.map(row => '<tr><td>' + row.term + '</td><td>' + row.visitor + '</td></tr>')
.join('');
document.querySelector('#analytics-body').innerHTML = markup;
The response may be valid JSON with the correct content type. The unsafe transition occurs later: decoded strings become HTML source. Escaping needed by JSON syntax disappears when JSON is parsed. An API validator that accepts strings does not prove that those strings are appropriate markup.
Select each stop and keep the original writer in view.
Ingest. A log stores a caller-controlled term.
await logs.insert({ term: request.query.term });
Transport. The API serializes the saved string as data.
res.json({ records: await logs.readScoped(actor.tenantId) });
Interpret. The formatter grants HTML interpretation inside the reader’s origin.
const html = records.map(r => "<td>" + r.term + "</td>").join("");
table.innerHTML = html;
Review question: Stored delivery and DOM execution describe different parts of the same trace.