Security Operations

A single-page app reads a search term from the URL fragment and writes it into the page with innerHTML. A tester sends a link whose fragment contains script, and it runs in the victim's browser. Server-side output encoding is already in place. Which fix addresses the root cause?

Show the answer

Correct answerB. Write the value with textContent instead of innerHTML

AWhy not this: HttpOnly stops scripts reading the cookie but the injected script still runs and can act in the page.

BWhy this is right: textContent inserts the value as plain text, so the browser builds no elements and runs no script from it.

CWhy not this: These prevent SQL injection in the data tier; the script here is injected in the browser.

DWhy not this: TLS protects data in transit and does not change how the page's script writes the fragment.

This is DOM-based XSS: the fragment never reaches the server, so server-side encoding cannot help. The flaw is the unsafe sink. Writing the value with textContent makes the browser treat it as text, so no markup or script is created.