WeCodeAcademy

Cross-Site Scripting (XSS)

In cross-site scripting, an attacker gets their own script to run in other people's browsers, usually through a comment, name or search box that the site later shows to other users without cleaning it. The script can then read the page, act as the victim, or steal their session.

The defence is output escaping: before placing user text into a page, turn the special characters <, >, & and quotes into harmless entities, so the browser shows them as text instead of running them as code. Most modern frameworks (such as React) do this by default; the danger returns when you bypass it. A Content Security Policy adds a second layer.

Examplepython
Open in editor »
Quick check

What does escaping user text before showing it in HTML do?

Your turn: write the code

Escape the username and print the safe HTML paragraph <p>Hello, NAME</p> with the escaped name.

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.