00:00

#0901

HTML Tag Injection

Easy+50 XPA03:2021 InjectionCWE-79
XSSOutput EncodingHTML

Scenario

Your application renders user-submitted comments in an HTML page by interpolating username and comment directly into a template.

Neither field is HTML-escaped, allowing attackers to inject arbitrary HTML and JavaScript.

A stored XSS attack in a comment field can steal session cookies from every user who views the page.

Stored XSS in comment fields is one of the most common web vulnerabilities. It can lead to session hijacking, credential theft, and drive-by malware delivery.

Your Tasks

  1. Inspect renderComment — it interpolates username and comment with no escaping.
  2. Fix it: escape the following characters in both fields before inserting into HTML:
  3. & → & < → < > → > " → " ' → '
  4. Return {html: escapedHtmlString}.

Examples

Example 1Script tag injected via comment (bug)

renderComment('alice', '<script>alert(1)</script>')
// html contains raw <script> tag — XSS!

Example 2Script tag safely escaped

renderComment('alice', '<script>alert(1)</script>')
// html contains &lt;script&gt;alert(1)&lt;/script&gt;

Constraints

  • Both username and comment must be escaped
  • Return an object {html: string} — not a bare string
  • Plain text with no special characters must appear unchanged in output

Hint

References

solution.js
Ln 1, Col 1UTF-8JavaScript
Sandbox ready
0/0/0not run