Find the Security Mistake
Find the Mistake Medium
Question
This code has a security vulnerability. Find and fix it.
Code
const userInput = getUserInput();
document.getElementById("output").innerHTML = userInput;
Hint
Think about what happens if userInput contains HTML tags.
Explanation
Using innerHTML with user input is an XSS vulnerability. If userInput contains '<script>alert(1)</script>', the script executes. The fix is to use textContent instead: document.getElementById('output').textContent = userInput. This treats the input as plain text, not HTML.
Related Resources
Related Lessons
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.