Lesson 2 · Unit 02
Cross-site scripting
User text that runs as script in other people's browsers
What you practise
- Stored and reflected XSS, why the browser runs it, escaping on output, Content-Security-Policy,
HttpOnlycookies
Try a question from this lesson
Pick one
A comment box saves what you type and pastes it into the page's HTML for every visitor. Someone posts a comment containing a script tag. What do visitors' browsers do?
Correct answer
Run the script as if the site itself wrote it
Why
The browser receives one page of HTML and cannot know that part of it came from a stranger. A script tag is a script tag, so it runs, with everything the site itself is allowed to do.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
The browser cannot tell
Script inside a page runs with the site's full power: it can read the page, act as the logged-in visitor and send data anywhere.
Getting your script into someone else's page is cross-site scripting (XSS). Stored XSS is saved and served to everyone. Reflected XSS bounces off a URL parameter to whoever opens a crafted link.
<p>Nice post!</p>
<p><script>stealSession()</script></p>Concept
Limit the damage
Two response headers act as a second line of defence.
A Content-Security-Policy tells the browser which scripts may run. With script-src 'self', only script files from the site's own origin run: inline script is refused.
HttpOnly on a cookie hides it from JavaScript, so a script cannot copy the session id out.
Neither fixes the bug. They make it less useful.
Content-Security-Policy:
script-src 'self'
Set-Cookie: sid=abc123; HttpOnly; Secure10 more exercises in the app
- 5× multiple choice
- 1× type the answer
- 2× true or false
- 1× fill the blanks
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.