How do I prevent cross-site scripting (XSS)?
Treat everything from users as text. Let your framework escape output, avoid inserting raw HTML, and sanitize it when you truly need it. Then add a Content Security Policy as a second layer.
The fix
element.innerHTML = comment;element.textContent = comment;Check these places
- Search for
innerHTML,dangerouslySetInnerHTML,v-htmlanddocument.write. - If you must show user HTML, clean it with a maintained sanitizer such as DOMPurify.
- Check links: only allow
http:andhttps:addresses from users. - Set session cookies to
HttpOnly,SecureandSameSite. - Add a Content Security Policy that blocks inline scripts where you can.
Does React prevent XSS?
It escapes ordinary values for you, which covers most cases. It doesn't protect dangerouslySetInnerHTML or links with unsafe addresses.
Check your whole codebase
Cyber mode reads your code, finds the holes and tells you what to fix first. You approve every fix. Use it only on code you own or may test, and review each change, because AI can miss issues.