Cross-site scripting

When a value the client sent is placed into a page without escaping, the browser reads it as markup, and markup can carry a script. This set checks that you know why escaping is the fix, and why a header is a second line of defence.

4 questions 1 task Read the lesson

Questions

Each one is marked as you go, and the explanation arrives straight after, which is the part worth reading.

4 questions, marked as you go.

1 What makes a reflected value become a script in the browser?
2 What does html.escape(q) change about the value?
3 Why can curl show the bug without running the payload?
4 What does Content-Security-Policy: default-src none add?

Tasks

These need your own device. Run the command in Termux, then paste what it printed. The marking looks at what the output means, so spacing, version numbers and ordering do not matter. If you have not installed anything yet, the lesson covers the setup.

Show a payload reflected raw, then escaped

This builds a page fragment twice, once with the value placed in as-is and once with the same value escaped. The value is a script tag. Run it and paste both lines.

Run this in Termux On your own device. Nothing on this page executes it. The command runs in your terminal, and this box only reads what you paste back.

python3 -c "import html; q='<script>alert(1)</script>'; print('raw :', '<p>'+q+'</p>'); print('safe :', '<p>'+html.escape(q)+'</p>')"
Marking your own work Both halves of this are checked in your browser, with no server involved, so nothing stops you from pasting output you did not run. The tasks are written so that the easiest way to pass one is to actually do it and read the result, which is the whole intention.

All sets Read the lesson