# Mattermost Server OAuth Flow Cross-Site Scripting

Reflected XSS in Mattermost OAuth mobile error page via unsanitized redirect_to query param concatenated into HTML, enabling session hijack and admin creation.

- Payout: $900
- Severity: high
- Program: Mattermost
- Found by: shielder
- Disclosed: 2021-08-06
- CVE: CVE-2021-37859
- Write-up quality: 7/10
- Original: https://hackerone.com/reports/1216203
- Library page: https://bugbountylibrary.com/r/h1%3A1216203
- Tags: area:web domain:web impact:ato impact:data-leak impact:privesc kind:bounty platform:hackerone sev:high tech:go technique:source-review vuln:xss

**Target:** Mattermost Server /oauth/{service}/mobile_login endpoint
**Root cause:** completeOAuth reads redirect_to from query into redirectURL and passes it to RenderMobileError, which concatenates it unsanitized into an <a href> inside RenderMobileMessage HTML output.
**Steps:**
1. Identify OAuth mobile flow endpoint `/oauth/shielder/mobile_login` which sets action=mobile and reads `redirect_to`.
2. Craft payload breaking out of href attribute: `redirect_to=%22%3E%3Cimg%20src=%22%22%20onerror=%22alert(1)%22%3E`.
3. Send victim link `https://<mattermost_url>/oauth/shielder/mobile_login?redirect_to=<payload>`.
4. Trigger OAuth error path so RenderMobileError renders the injected HTML; JS executes in Mattermost origin.
**How found:** Code review of web/oauth.go completeOAuth traced redirect_to into utils.RenderMobileError and RenderMobileMessage, spotting string concatenation into HTML without sanitization.
**Exploitation:** Attacker sends crafted link to a logged-in Mattermost user; when the OAuth flow errors, the reflected payload executes in the target origin, allowing the attacker to read chat contents (regular user) or create a new admin account (admin victim).
**Escalation:** Privilege-dependent impact: regular user session yields chat data theft; admin session yields full server takeover via new admin creation.
**Insight:** OAuth error/mobile fallback renderers often bypass the main templating engine's escaping; grep for RenderMobileError-style helpers that concatenate query params into raw HTML.

## Proof of concept (copied exactly from the original report)

**Malicious OAuth URL with XSS payload in redirect_to**

```
https://<mattermost_url>/oauth/shielder/mobile_login?redirect_to=%22%3E%3Cimg%20src=%22%22%20onerror=%22alert(%27zi0Black%20@%20Shielder%27)%22%3E
```

## Write-ups of the same bug (oldest first)
- 2021-07-26: [Mattermost Server v5.32 > v5.36 Reflected XSS in OAuth flow](https://bugbountylibrary.com/r/blog%3Ashielder-it-advisories-mattermost-server-reflected-xss-oauth) — zi0Black
- 2021-08-06: [Mattermost Server OAuth Flow Cross-Site Scripting](https://bugbountylibrary.com/r/h1%3A1216203) — shielder (this one)

