Universal XSS with Playlist feature
Brave for iOS leaked two internal security tokens into page JavaScript and then built a script by string concatenation using a page-supplied node tag, so a malicious page could inject code. An attacker could run script in the main frame of any site, giving universal XSS.
Originally disclosed on HackerOne
- Disclosed after 18 months
Summary:
Brave iOS has three weaknesses described below. By combining them, Universal XSS can be achieved.
- Exposure of UserScriptManager.securityToken
Playlist.js embeds the exact value of the $<notifyNode> into HTMLVideoElement.prototype.setAttribute. By reading the value, an attacker can retrieve the hidden security token.
- Exposure of UserScriptManager.messageHandlerToken
Also, WindowRenderHelper.js embeds the exact value of the $<handler> into W{securityToken}.postMessage. By reading the value, an attacker can retrieve the hidden message handler token.
- UXSS in PlaylistHelper through nodeTag
PlaylistHelper.swift concatenates strings to build a JavaScript code and executes it on the mainframe of a WebView. Then, nodeTag given from a webpage is directly included in the code. So, if the nodeTag, named as tagId in JS world, passed from the page contained ');alert(document.location);//, unintended alert() is executed on the mainframe.
Products affected:
- Brave iOS 1.32.3 and higher (include the latest Nightly)
Steps To Reproduce:
- Visit the Google page: https://sites.google.com/view/nishimunea-brave-uxss1/page
- This page contains a cross origin malicious page https://csrf.jp/brave/playlist.php in an iframe
- The iframe exploits the above three weaknesses to send a message to playlistHelper
- Push
Add to Brave PlaylistandOpenbutton in the setting menu - An alert dialog is appear on the sites.google.com
Supporting Material/References:
- Demonstration movie is attached
Impact
- Universal XSS on the arbitrary domains
Attachments
Context
Brave for iOS ships a Playlist feature: a way to save media from a page and play it back later inside the browser. To make that work, the app injects JavaScript into web content and communicates with the native side through a message handler. The injected scripts are built from templates that contain placeholders, and the native code substitutes real values into those placeholders before the script runs.
Two of those substituted values are secrets: a security token used to namespace the user script, and a message handler token used to address the native message handler. The report shows that both secrets leak into the page's JavaScript context, and that a third flaw in PlaylistHelper.swift lets a page inject code into the main frame. Combined, the three give Universal XSS on arbitrary domains.
The affected product is Brave iOS 1.32.3 and higher, including the latest Nightly at the time of the report. The asset is the brave-ios repository.
Setup
No accounts or special tooling are needed beyond a Brave iOS build in the affected range and a way to host two pages: an attacker-controlled page and a cross-origin page loaded in an iframe. The researcher used a Google Sites page as the top-level page and a page on csrf.jp as the cross-origin iframe.
The preconditions are:
- The victim opens the attacker's page in Brave iOS.
- The page embeds the cross-origin malicious page in an iframe.
- The victim uses the Playlist UI:
Add to Brave Playlist, thenOpenin the settings menu.
Finding it
The researcher identified three separate weaknesses and then chained them.
1. Exposure of UserScriptManager.securityToken
In Playlist.js, the value of the $<notifyNode> placeholder is embedded directly into a call to HTMLVideoElement.prototype.setAttribute. Because the substituted value ends up in the page's JavaScript context, a page can read it back and recover the hidden security token.
2. Exposure of UserScriptManager.messageHandlerToken
In WindowRenderHelper.js, the value of the $<handler> placeholder is embedded directly into a W{securityToken}.postMessage call. Reading that value from the page recovers the hidden message handler token.
3. UXSS in PlaylistHelper through nodeTag
PlaylistHelper.swift builds a JavaScript string by concatenation and executes it on the main frame of a WebView. A value called nodeTag, supplied by the web page, is inserted into that string without escaping. In the JavaScript world the value is named tagId. If the page supplies a nodeTag containing ');alert(document.location);//, the resulting script executes the injected alert() on the main frame.
Chained, they let a cross-origin iframe send a message to the native Playlist helper and inject script into the top-level page.
Exploitation
Step 1: Open the attacker page
The victim visits the Google Sites page:
https://sites.google.com/view/nishimunea-brave-uxss1/page
This page is the target of the eventual Universal XSS: the injected script is meant to run in the context of sites.google.com, not in the context of the attacker's own origin.
Step 2: Load the cross-origin malicious page in an iframe
The Google Sites page contains a cross-origin iframe pointing at:
https://csrf.jp/brave/playlist.php
Step 3: Exploit the three weaknesses from the iframe
The iframe uses the three weaknesses described above to send a message to playlistHelper. The iframe exploits the three weaknesses described above to send a message to playlistHelper.
Step 4: Trigger the Playlist UI
The victim presses Add to Brave Playlist and then Open in the settings menu. This is the user interaction that drives the native Playlist helper to build and run the script containing the attacker-controlled nodeTag.
Step 5: Observe the alert on the top-level origin
An alert dialog appears on sites.google.com. The script ran in the main frame of the top-level page, not in the iframe, which is what makes this a Universal XSS rather than a same-origin issue.
A demonstration movie, uxss_with_playlist.mov, is attached to the report.
Root cause
There are three distinct mistakes.
-
Secret values placed in page-readable JavaScript. The
$<notifyNode>placeholder inPlaylist.jsand the$<handler>placeholder inWindowRenderHelper.jsare substituted with the security token and the message handler token respectively, and both end up in code that runs in the page's context. Any script on the page can read them back. The tokens are meant to be hidden, but nothing prevents the page from observing them once they are embedded. -
String concatenation to build executable JavaScript.
PlaylistHelper.swiftassembles a JavaScript program by concatenating strings and then executes it on the main frame of a WebView. ThenodeTagvalue, calledtagIdin JavaScript, comes from the web page and is inserted into that program without escaping or validation. A value containing');alert(document.location);//breaks out of the surrounding string literal and runs as code. -
Trusting page-supplied data across an origin boundary. The helper runs on the main frame, so code injected through
nodeTagexecutes with the privileges of the top-level origin, not the origin that supplied the value. That is what turns a page-controlled string into Universal XSS.
Impact
Universal XSS on arbitrary domains. Because the injected script runs in the main frame of whatever page the victim is viewing, an attacker who can get the victim to load the malicious iframe and then use the Playlist UI can execute script in the context of the top-level site. The report's demonstration uses sites.google.com as the top-level origin.
The reproduction requires the victim to press Add to Brave Playlist and then Open in the settings menu.
Fix
The report does not describe a fix. Possible mitigations based on the root causes are:
- Stop embedding the security token and the message handler token in page-readable JavaScript. Keep them out of any script that runs in the page's context, or otherwise ensure the page cannot read them back.
- Stop building JavaScript by concatenating page-supplied values. Pass
nodeTagas data (for example, as an argument to a function or via a structured message) rather than interpolating it into source text, and validate it against an expected format. - Treat anything coming from a web page as untrusted when it crosses into the native side, especially when the result is executed on the main frame.
Variants to test
- Other injected scripts with placeholder substitution. Look for other user scripts in the same codebase where a secret or token is substituted into a string that runs in the page. A positive result is a page script that can read the substituted value back at runtime.
- Other native-to-JavaScript string builders. Search for Swift or Objective-C code that concatenates page-supplied values into a JavaScript string and evaluates it. A positive result is a value containing a quote and a statement separator that changes the executed behaviour.
- Other page-supplied identifiers used in native helpers. Values like element tags, IDs, class names or URLs that are passed from the page into native code and then into an evaluation context are candidates. A positive result is code execution in the main frame rather than in the supplying frame.
- Cross-origin iframe to top-frame escalation. Any feature where a cross-origin iframe can send a message to a native handler that then acts on the main frame is worth testing. A positive result is an effect visible on the top-level origin, as in the alert on
sites.google.comhere.
Hunting notes
This kind of bug tends to hide where a browser or hybrid app bridges web content and native code: injected user scripts, message handlers, and helpers that build JavaScript strings from page data. The signals during recon are template placeholders that get substituted with secrets, native code that concatenates strings into evaluateJavaScript-style calls, and any feature that acts on the main frame based on data from a subframe.
The researcher showed impact by demonstrating the full chain end to end: a cross-origin iframe on an attacker page, the Playlist UI interaction, and an alert firing on the top-level sites.google.com origin. That demonstration, plus the three clearly separated root causes, is what makes the case for Universal XSS rather than a same-origin scripting issue.
What happened, in plain words
Brave is a web browser. On iPhone and iPad it is called Brave for iOS. Like every browser, it takes the web pages you visit and turns them into something you can see and click. Some pages contain video or audio, and Brave has a feature called Playlist that lets you save that media and watch or listen to it later inside the browser, a bit like saving a song to a playlist in a music app.
To make Playlist work, the browser has to do two things at once. It has to run the page's own code (the JavaScript that websites use to make pages interactive), and it has to talk to the app itself (the native side, the part written in Swift, Apple's programming language for iOS apps). The bridge between those two worlds is where this bug lives.
The researcher found three separate mistakes in Brave for iOS. Each one alone is fairly limited. Put together, they let a malicious web page run its own code inside a completely different website that the victim is viewing, for example Google Sites. That is called Universal XSS, or UXSS for short. "XSS" stands for cross-site scripting: tricking a browser into running attacker code as if it belonged to a trusted site. "Universal" means it is not tied to one site; it can hit any domain the victim happens to be on.
The affected versions are Brave iOS 1.32.3 and higher, including the latest Nightly build at the time. Nightly is the experimental, frequently updated version of the browser that testers use. The code involved lives in the brave-ios repository, the public place where Brave keeps the source code for its iOS browser.
Some background you need first
Before the three weaknesses make sense, here are the ideas they rely on.
JavaScript. The programming language that runs inside web pages. When you click a button and something changes on the page without reloading, JavaScript did that.
Injected scripts. A browser can add its own JavaScript into a page you visit, alongside the page's own scripts. Brave does this to power features like Playlist. The page and the browser's script end up sharing the same little world inside the browser tab, called the page's JavaScript context. Anything in that shared world can, in principle, be read by the page.
Templates and placeholders. A template is a piece of text with blanks in it, like a form letter with "Dear ____". The blanks here look like $<notifyNode> and $<handler>. Before the script runs, the app fills each blank with a real value. This is convenient, but it means whatever gets filled in becomes part of the script the page can see.
Tokens. A token is a short secret string, like a password or a ticket number, used to prove that a message really comes from a trusted sender. Think of a cloakroom ticket: only the person holding the matching ticket gets the coat back. Brave uses two tokens here: a security token that namespaces its user script (keeps it separate from other scripts) and a message handler token used to address the native message handler, the internal mailbox the page uses to talk to the app.
API. An API is a defined way for two pieces of software to talk to each other, like a restaurant menu: you order from the list, the kitchen knows what to do, and you do not need to know how the kitchen works. The native message handler is an internal API between the web page and the Brave app.
iframe. A web page can contain another web page inside a rectangular frame, like a picture-in-picture window. The inner page is called an iframe. If the inner page comes from a different website than the outer one, it is cross-origin: the browser deliberately stops the inner page from reading the outer page's contents, to protect privacy. Origin means the combination of scheme, host and port, roughly "which website this is".
Main frame. The outer, top-level page. Code that runs in the main frame runs with the identity and permissions of the top-level website, which is exactly what an attacker wants.
String concatenation. Gluing pieces of text together end to end. That is the heart of the third weakness.
Setup
Nothing exotic is needed to reproduce this. You need a Brave iOS build in the affected range, plus two web pages: one controlled by the attacker (the top-level page the victim visits) and one cross-origin page loaded inside an iframe. The researcher used a Google Sites page as the top-level page and a page on csrf.jp as the cross-origin iframe.
The preconditions are:
- The victim opens the attacker's page in Brave iOS.
- The page embeds the cross-origin malicious page in an iframe.
- The victim uses the Playlist interface:
Add to Brave Playlist, thenOpenin the settings menu.
Finding it
The researcher spotted three separate weaknesses and then chained them together. Here is each one.
1. Exposure of UserScriptManager.securityToken
In a file called Playlist.js, the value that fills the $<notifyNode> placeholder is placed directly into a call to HTMLVideoElement.prototype.setAttribute. That is a standard browser function for setting an attribute on a video element. Because the filled-in value ends up in the page's JavaScript context, any script on the page can read it back and recover the hidden security token. The secret was supposed to stay hidden, but it was written into a place the page can inspect.
2. Exposure of UserScriptManager.messageHandlerToken
In a file called WindowRenderHelper.js, the value that fills the $<handler> placeholder is placed directly into a W{securityToken}.postMessage call. postMessage is the browser's built-in way for scripts to send messages to each other. Reading that value from the page recovers the hidden message handler token, the second secret.
3. UXSS in PlaylistHelper through nodeTag
PlaylistHelper.swift builds a JavaScript string by gluing pieces of text together and then runs it on the main frame of a WebView. A WebView is the component inside an app that displays web content. A value called nodeTag, supplied by the web page, is inserted into that string without any escaping. In the JavaScript world the value is named tagId. If the page supplies a nodeTag containing:
');alert(document.location);//
then the resulting script runs the injected alert() on the main frame. alert() is the browser function that pops up a dialog box. document.location is the current page's address. So the dialog would show the address of the top-level page, proving the code ran there.
Why does that little string work? The surrounding code has put the value inside quotes, as text. The leading '); closes the quote and the statement, so everything after it is treated as new instructions rather than as text. alert(document.location); is then a complete instruction. The trailing // starts a comment, which tells JavaScript to ignore the rest of the line, so the leftover closing quote from the original code does not cause an error. It is like writing a note inside quotation marks and having the reader treat the end of your note as the end of the sentence, then follow your own instructions.
Chained, they let a cross-origin iframe send a message to the native Playlist helper and inject script into the top-level page.
Exploitation
Step 1: Open the attacker page
The victim visits the Google Sites page:
https://sites.google.com/view/nishimunea-brave-uxss1/page
This page is the target of the eventual Universal XSS. The injected script is meant to run in the context of sites.google.com, not in the context of the attacker's own origin. That distinction is the whole point: running code on your own site is harmless, running it on someone else's site is the attack.
Step 2: Load the cross-origin malicious page in an iframe
The Google Sites page contains a cross-origin iframe pointing at:
https://csrf.jp/brave/playlist.php
Think of it as a visitor in a waiting room who cannot enter the office, but who has found a copy of the staff badge and can therefore send messages that look official.
Step 3: Exploit the three weaknesses from the iframe
The iframe uses the three weaknesses to send a message to playlistHelper. The iframe exploits the three weaknesses described above to send a message to playlistHelper.
Step 4: Trigger the Playlist UI
The victim presses Add to Brave Playlist and then Open in the settings menu. This is the user interaction that drives the native Playlist helper to build and run the script containing the attacker-controlled nodeTag. The reproduction requires this tap.
Step 5: Observe the alert on the top-level origin
An alert dialog appears on sites.google.com. The script ran in the main frame of the top-level page, not in the iframe. That is what makes this a Universal XSS rather than a same-origin issue: the code crossed from the attacker's iframe into the victim's top-level site.
A demonstration movie, uxss_with_playlist.mov, is attached to the report.
Root cause
There are three distinct mistakes.
-
Secret values placed in page-readable JavaScript. The
$<notifyNode>placeholder inPlaylist.jsand the$<handler>placeholder inWindowRenderHelper.jsare filled with the security token and the message handler token respectively, and both end up in code that runs in the page's context. Any script on the page can read them back. The tokens are meant to be hidden, but nothing prevents the page from observing them once they are embedded. It is like writing the safe combination on the outside of the safe. -
String concatenation to build executable JavaScript.
PlaylistHelper.swiftassembles a JavaScript program by gluing strings together and then runs it on the main frame of a WebView. ThenodeTagvalue, calledtagIdin JavaScript, comes from the web page and is inserted into that program without escaping or validation. A value containing');alert(document.location);//breaks out of the surrounding string literal and runs as code. -
Trusting page-supplied data across an origin boundary. The helper runs on the main frame, so code injected through
nodeTagexecutes with the privileges of the top-level origin, not the origin that supplied the value. That is what turns a page-controlled string into Universal XSS.
Impact
Universal XSS on arbitrary domains. Because the injected script runs in the main frame of whatever page the victim is viewing, an attacker who can get the victim to load the malicious iframe and then use the Playlist interface can execute script in the context of the top-level site. The report's demonstration uses sites.google.com as the top-level origin.
The reproduction requires the victim to press Add to Brave Playlist and then Open in the settings menu.
Fix
The report does not describe a fix. Possible mitigations based on the root causes are:
- Stop embedding the security token and the message handler token in page-readable JavaScript. Keep them out of any script that runs in the page's context, or otherwise ensure the page cannot read them back.
- Stop building JavaScript by gluing page-supplied values together. Pass
nodeTagas data (for example, as an argument to a function or via a structured message) rather than inserting it into source text, and validate it against an expected format. - Treat anything coming from a web page as untrusted when it crosses into the native side, especially when the result is executed on the main frame.
Variants to test
- Other injected scripts with placeholder substitution. Look for other user scripts in the same codebase where a secret or token is filled into a string that runs in the page. A positive result is a page script that can read the filled-in value back at runtime.
- Other native-to-JavaScript string builders. Search for Swift or Objective-C code that glues page-supplied values into a JavaScript string and evaluates it. A positive result is a value containing a quote and a statement separator that changes the executed behaviour.
- Other page-supplied identifiers used in native helpers. Values like element tags, IDs, class names or URLs that are passed from the page into native code and then into an evaluation context are candidates. A positive result is code execution in the main frame rather than in the supplying frame.
- Cross-origin iframe to top-frame escalation. Any feature where a cross-origin iframe can send a message to a native handler that then acts on the main frame is worth testing. A positive result is an effect visible on the top-level origin, as in the alert on
sites.google.comhere.
Hunting notes
This kind of bug tends to hide where a browser or hybrid app bridges web content and native code: injected user scripts, message handlers, and helpers that build JavaScript strings from page data. The signals during recon are template placeholders that get filled with secrets, native code that glues strings into evaluateJavaScript-style calls, and any feature that acts on the main frame based on data from a subframe.
The researcher showed impact by demonstrating the full chain end to end: a cross-origin iframe on an attacker page, the Playlist interface interaction, and an alert firing on the top-level sites.google.com origin. That demonstration, plus the three clearly separated root causes, is what makes the case for Universal XSS rather than a same-origin scripting issue.
Glossary
- API: a defined way for two pieces of software to talk to each other, like a menu you order from.
- Attribute: a setting attached to an element on a web page, such as a video's source or size.
- Cross-origin: coming from a different website (different scheme, host or port) than the page around it.
- CSS: the language that controls how a web page looks; not central here, but part of the same web toolkit.
- Escaping: marking special characters in text so they are treated as plain text rather than as code.
- Hybrid app: an app that mixes native code with web content shown in a WebView.
- iframe: a web page embedded inside another web page, like a picture-in-picture window.
- Injected script: JavaScript that the browser adds into a page alongside the page's own scripts.
- JavaScript: the programming language that runs inside web pages to make them interactive.
- JavaScript context: the shared little world inside a browser tab where a page's scripts run and can see each other's values.
- Main frame: the outer, top-level page; code there runs with the top-level site's identity.
- Message handler: an internal mailbox that lets web content send messages to the native app.
- Native side: the part of an app written in the platform's own language (Swift on iOS) rather than in web code.
- Nightly: the experimental, frequently updated build of a browser used by testers.
- Node: an element on a web page, such as a video, a button or a paragraph.
- Origin: the combination of scheme, host and port that identifies which website something belongs to.
- Placeholder: a blank in a template, like
$<notifyNode>, that gets filled with a real value before use. - Playlist: Brave's feature for saving media from a page and playing it back later inside the browser.
- postMessage: the browser's built-in way for scripts to send messages to each other.
- Repository: a public place where source code is stored and shared.
- Same-origin policy: the browser rule that stops one website's scripts from reading another website's content.
- Security token: a short secret string used to prove a message comes from a trusted sender.
- Swift: Apple's programming language for iOS and macOS apps.
- Template: a piece of text with blanks in it that get filled before use.
- Token: a short secret string, like a cloakroom ticket, used to prove identity or permission.
- Universal XSS (UXSS): cross-site scripting that is not tied to one site and can run attacker code in the context of any domain the victim is viewing.
- User script: a script the browser injects into pages to add its own features.
- Validation: checking that a value matches an expected format before using it.
- WebView: the component inside an app that displays web content.
- XSS (cross-site scripting): tricking a browser into running attacker code as if it belonged to a trusted site.