Post-Auth Stored XSS with User Interaction leads to Remote Code Execution
The toastr library was used without escaping, so a room name containing HTML could execute script when an admin edited the room. A validation bypass in createRoom let an attacker set that name, and the resulting admin account takeover could lead to remote code execution via a web hook.
Originally disclosed on HackerOne
- Disclosed after 3 months
Summary:
Unsafe usage of the toastr library leads to Stored XSS when combined with a validation bypass in the createRoom function. Targeting an admin account leads to Remote Code Execution.
Description:
The frontend uses the toastr library to display error messages to the user. However, it is used in an unsafe way which allows XSS when user input is reflected in an API error message. This happens for example when channel info is edited and the channel's name contains invalid characters.
To abuse this, an attacker can use a validation bypass in the createRoom function: the extraData parameter is merged with the room object without proper validation, which allows an attacker to override all previous properties such as the name or the owner. The attacker can use this to create a room that contains their XSS payload in the room's name.
Triggering the XSS requires multiple steps of user interaction, because there are few API endpoints that reflect user input back. One of them is the rooms.saveRoomSettings endpoint which calls the saveRoomSettings method which in turn uses the getValidRoomName function. This function checks the room's name and reflects the user-provided value back if it is not a valid name.
The error returned by the API is unsafely handled by passing it to the toastr library without escaping it or using the library's escapeHtml option. The handleError function passes the value to the toastr library, it escapes the details property but not the message and title property.
To gain Remote Code Execution capabilities on the server, an attacker can follow these steps to take over an admin account. The attacker can then use the newly gained admin privileges to create an incoming web hook that has a script. This allows them to execute commands or get a shell on the server, because the script is executed on the server without a security boundary in place (which seems to be intended).
Note: This issue is classified as Stored XSS because the payload is stored permanently in the database, but it could be argued that it is Reflected XSS because the payload is reflected by the API which then leads to the unsafe handling and execution of the payload.
Releases Affected:
We tested on 3.12.1, but it is hard to confirm since when Rocket.Chat is vulnerable because there are many parts of the code base involved.
Steps To Reproduce (from initial installation to vulnerability):
- Set up an instance of RocketChat 3.12.1, e.g. by cloning the repo and using Docker Compose:
git clone [email protected]:RocketChat/Rocket.Chat.gitcd Rocket.Chatgit checkout tags/3.12.1docker-compose up -d- Configure the instance with default settings
- Create a normal (non-admin) user with username
attackerand passwordattacker - Log in as the
attackeruser - Open the browser's developer tools and execute the following line of code:
Meteor.call('createChannel', 'valid-name', [], false, {}, { name: 'edit me <img src onerror=alert(origin)>' }) - Invite the admin to the newly created channel
- Log out and log in as an admin
- Edit the title of the newly created channel (e.g. change
metoyou) - Click the save button
- A dialog should pop up that shows the site's origin (e.g. http://localhost:3000), confirming that the XSS payload has been executed (this is only for the demo, the payload can be arbitrary JavaScript code)
- (The demo ends here but it is trivial to get RCE capabilities when having access to an admin account, as explained before)
Supporting Material/References:
The attached video shows the exploitation of the vulnerability with the attacker's view on the right and the victim's view (admin) on the left.
Suggested mitigation
- Restrict and validate the
extraDataparameter when creating a room - Use the
toastrlibrary with theescapeHtmloption or sanitize the message and title manually - Set a
Content-Security-Policyheader that prevents payload execution- preventing inline scripts might not be enough here because users can upload files
- a nonce-based CSP would fit best
Disclosure Policy
All reported issues are subject to a 90 day disclosure deadline. After 90 days elapse, parts of the bug report will become visible to the public.
Don't hesitate to ask if you have any questions or need further help with this issue.
Impact
An attacker can use this vulnerability to target an admin user and take over their account, which is already a high impact. The attacker can then use certain features that are available to admins in order to gain Remote Code Execution capabilities.
This gives them complete control over the Rocket.Chat instance and exposes all attached components, e.g. the database or any external system whose credentials are stored within Rocket.Chat settings. An attacker can read, change, or delete all items in the database, impacting confidentiality, integrity, and availability.
Attachments
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the error handler passed the API error's message and title to toastr without escaping, and createRoom merged unvalidated extraData into the room object, allowing the room name to contain HTML.
Impact
An attacker could take over an admin account and then use admin features to gain remote code execution, giving complete control over the instance and its data.
What the attacker needed
A normal user account; admin invited to the channel; admin edits and saves the room title
The fix
The report suggests restricting and validating extraData, using toastr's escapeHtml option or sanitising message and title, and setting a nonce-based Content-Security-Policy.
How it was found
- Set up a RocketChat 3.12.1 instance and configure default settings.
- Create a normal non-admin user and log in as that user.
- Use the browser console to call createChannel with extraData that sets a room name containing an HTML payload.
- Invite the admin to the newly created channel.
- Log out and log in as the admin, then edit the channel title and save.
- Observe the dialog showing the site origin, confirming script execution.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A web page can show error messages using a helper library. If that library inserts the message as raw HTML instead of plain text, any HTML in the message runs as code in the viewer's browser. Here an attacker could put HTML into a room name, and when an admin tried to edit that room the error message ran the attacker's code.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.