A comment feature must accept any characters, including angle brackets, and later displays each comment in an HTML body and inside an inline JavaScript string. Which control BEST prevents stored cross-site scripting?
Show the answer
Correct answerD. Encode each value for its output context when rendered
AWhy not this: Encryption at rest protects stored data from disclosure but the decrypted comment would still execute when rendered.
BWhy not this: HTML entity encoding at input does not protect the JavaScript string context and corrupts the stored data.
CWhy not this: A blocklist is easily evaded and conflicts with the requirement to accept any characters.
DWhy this is right: Context-specific encoding at output makes the data inert in both the HTML and JavaScript contexts.
Output encoding applied at render time, matched to each context (HTML body, JavaScript string), neutralizes the data wherever it is displayed while still allowing arbitrary input. A single encoding applied at input fits only one context and leaves the other exposed.