Skip to content
XSS Write-up Intermediate

XSS using postMessage in Google Cloud Theia notebooks [Google VRP]

A webview in Google Cloud's Vertex AI Workbench Theia IDE accepted postMessage content that enabled script execution, allowing an attacker to run JavaScript in the victim's notebook origin. This could let the attacker fetch the default compute engine service account token from the metadata server and take control of the victim's project.

Sreeram KL Follow

Published on blog.geekycat.in

Bounty$3,134 View original

This write-up is a part of a series of write-ups about the bugs Sivanesh and I found in Google Cloud in 2022.

Sreeram KL, on blog.geekycat.in

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. 5 min read.

Context

This write-up is part of a series about bugs Sivanesh and I found in Google Cloud in 2022. The target here is Google Cloud's Vertex AI Workbench, specifically the option to create a workbench backed by Theia. Theia is the IDE that Google also uses in Cloud Shell, and prior research suggested that Theia implementations can be problematic.

A Theia workbench is served from a per-user subdomain of notebooks.googleusercontent.com, and the IDE includes a webview extension that renders HTML content inside the IDE. That webview is the feature at the centre of the first bug.

Setup

  1. Access to Google Cloud with the ability to create a Vertex AI Workbench instance using Theia.
  2. A deployed Theia instance workbench, reachable at a subdomain of the form {VICTIM-SUBDOMAIN}-dot-us-west1.notebooks.googleusercontent.com.
  3. A separate page under the attacker's control to host the proof of concept.

When a user-managed Vertex AI Workbench is created, it uses the project's default Compute Engine service account. That service account has access to all the project resources.

Finding it

After deploying a Theia instance workbench, I noticed that the installed version was not the latest. I searched for known vulnerabilities in that version. Several were known, but not all of them worked: either the specific feature had been removed from the installation (for example, the mini-browser), or exploiting them required unrealistic user interactions, such as uploading a file and opening it themselves.

CVE-2021-41038 was different. It exploits Theia's webview extension, and that extension was enabled in our deployment.

Exploitation

Step 1: Host the proof of concept

I created a page that opens the victim's webview URL and, after a delay, sends a postMessage to it:

HTML
<html>
  <body>
    <script>
      function exploit(){
        w = open("https://{VICTIM-SUBDOMAIN}-dot-us-west1.notebooks.googleusercontent.com/webview/index.html?id=a");
        window.setTimeout(function(){w.postMessage({channel: "content", args: {options: {allowScripts: true}, contents: "<img src=x onerror=\"document.write('XSS here! '+document.domain)\">"}}, "*");}, 5000);
      }
    </script>
    <button onclick="exploit()">Click here</button>
  </body>
</html>

The page opens the webview endpoint at /webview/index.html?id=a on the victim's notebook subdomain. After a five second timeout, it posts a message on the content channel whose arguments set allowScripts: true and supply HTML content containing an img element with an onerror handler. The handler writes the string XSS here! followed by document.domain into the document. The target origin is *.

Step 2: Trigger it

Clicking the button runs exploit(). The webview accepted the message and rendered the supplied HTML with scripts allowed, so the onerror handler executed. The alert popped, confirming the XSS.

Step 3: Turn the XSS into project compromise

Because the workbench runs with the project's default Compute Engine service account, and that account has access to all project resources, an attacker can use the XSS to fetch the service account token from the metadata server. With that token the attacker can compromise the entire project.

Obtaining the victim's instance subdomain

The attack needs the victim's notebook subdomain. The report lists two ways to get it:

  1. By default, every person's subdomain is leaked to several third-party domains via the referer. Some of those domains are:

    1. https://open-vsx.org
    2. https://openvsxorg.blob.core.windows.net
    3. https://schemastore.azurewebsites.net

    Anyone with access to these domains, or with a vulnerability in these servers, can leak the subdomain.

  2. Anyone with a "Notebook viewer" role in the victim's project can see the subdomain, and can then exploit this issue to gain editor access to the victim's project.

Root cause

The webview extension accepts postMessage content on the content channel and honours an allowScripts option supplied by the sender, rendering attacker-controlled HTML with scripts enabled. The webview extension being enabled in the deployment is what made CVE-2021-41038 applicable here.

Impact

By using this XSS, an attacker could fetch the service account token from the metadata server and compromise the entire project, because the workbench uses the project's default Compute Engine service account, which has access to all project resources. The attacker needs the victim's instance subdomain, obtained through referer leakage to the third-party domains above or through a "Notebook viewer" role in the victim's project. In the second case, the issue turns viewer access into editor access to the project.

Bonus bug

Another behaviour we noticed in Theia was inspired by a bug in which the researcher describes how leaving a service unsecured in the internal cloud VPC can lead to security issues. In summary, although the port is only open inside the VPC:

  1. An attacker with low privileges in the project could exploit such behaviour to escalate privileges or move laterally.
  2. Admins tend to open port ranges to the internet for convenience.

To see how this affects Theia, consider how authorisation works. The Theia instance was hosted on a subdomain of notebooks.googleusercontent.com. When users try to access this domain, they are redirected through an OAuth-like flow where they are checked for permissions. If they have the necessary permissions, they are redirected back to the notebook domain and cookies get assigned.

However, port 8080, which Theia uses, is open inside the VPC. Anyone inside the VPC could access it by directly using the IP address of the notebook instead of the domain. Since the notebooks contain the authorisation token of the default Compute Engine service account, an attacker could use this to escalate privileges to the Project Editor level. Secondly, as noted above, it is common for admins to open port ranges to the internet for various reasons, and port 8080 is one of the most commonly opened ports.

Fix

We reported both issues to Google's Vulnerability Reward Program. For the bonus bug, Google was already aware and was in the process of removing Theia from the list of options for Vertex AI Workbench. That removal ended up being the patch for both bugs: Theia was removed from Vertex AI Workbench.

Reward

Google awarded us a $3133.70 bounty under their Vulnerability Reward Program.

  1. Other Theia deployments with the webview extension enabled: open /webview/index.html on the instance and post a content message with allowScripts: true and HTML containing an event handler. A positive result is script execution in the webview origin.
  2. Referer leakage of instance subdomains: load a Theia instance and watch outbound requests to third-party hosts such as extension registries and schema stores. A positive result is the notebook subdomain appearing in a Referer header sent to a domain you can observe.
  3. Direct access to the IDE port inside the VPC: from another workload in the same VPC, request the notebook's IP address on port 8080 instead of the notebooks.googleusercontent.com domain. A positive result is the IDE responding without the OAuth-like redirect and cookie assignment.
  4. Metadata token retrieval from the IDE origin: once script execution is achieved, request the metadata server from the page. A positive result is a service account token being returned to the attacker-controlled script.

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. 16 min read.

What happened, in plain words

Two security researchers, Sivanesh and Sreeram, spent part of 2022 looking for bugs in Google Cloud. This article is about one of those bugs, found in a Google Cloud product called Vertex AI Workbench. In plain terms: they found a way to make a victim's browser run code written by an attacker, inside a page that belongs to Google. That kind of bug is called cross-site scripting, or XSS. Because the page they could run code in had powerful access to the victim's Google Cloud project, the bug could have let an attacker take over the whole project. Google paid them a bounty of $3133.70 for reporting it, and Theia was removed from Vertex AI Workbench.

This version of the write-up is for someone who has never done security work. It explains every term as it comes up, walks through the proof of concept line by line, and keeps the same order as the researchers' own account.

Background: what the pieces are

Before the bug makes sense, you need a handful of ideas. Each one is simple on its own.

Google Cloud and projects

Google Cloud is Google's collection of rented computing services: storage, databases, virtual machines, and so on. Everything you create there lives inside a project, which is like a folder that holds all of one organisation's or one person's resources and controls who can touch them. When you create a resource in a project, you choose (or accept a default) service account, which is a kind of robot user identity that programs use to talk to Google's services on your behalf. Permissions are attached to accounts, human or robot.

Vertex AI Workbench and Theia

Vertex AI Workbench is a Google Cloud service for data science work: you get a ready-made environment where you can write and run code. One of the options when creating a workbench was to back it with Theia. Theia is an IDE, an integrated development environment: a program in your browser that looks like a code editor, with file trees, a terminal and panels. Google also uses Theia in Cloud Shell, its browser-based command line. The researchers had read earlier research suggesting that Theia setups can be insecure, so it was a natural place to look.

Subdomains

A subdomain is a name that sits in front of a domain. In mail.example.com, mail is the subdomain and example.com is the domain. Each Theia workbench was served from its own subdomain of notebooks.googleusercontent.com, of the form {VICTIM-SUBDOMAIN}-dot-us-west1.notebooks.googleusercontent.com. The {VICTIM-SUBDOMAIN} part is a placeholder for the random-looking name that identifies one particular user's notebook. us-west1 is a Google Cloud region (a data centre location).

The webview extension

Theia, like many editors, supports extensions: add-on features. One of them is the webview, a small embedded browser pane inside the IDE that can display HTML content. HTML is the language web pages are written in. A webview that renders HTML supplied by someone else is a classic place for trouble, because HTML can carry scripts.

Scripts, JavaScript and XSS

A script is a small program that runs inside a web page, almost always written in JavaScript. Scripts are what make pages interactive. The security model of the web is that a page from one site should not be able to run its scripts inside another site's page, because then it could read that site's data. Cross-site scripting (XSS) is the bug class where an attacker manages to get their script running inside someone else's page anyway. Once that happens, the attacker's script runs with the same privileges as the site itself: it can read what the site can read and do what the site can do.

A useful everyday comparison: imagine a bank clerk who will read out any note handed to them, without checking who wrote it.

postMessage

Browsers give pages a controlled way to send each other messages: the postMessage API. An API (application programming interface) is simply a defined set of commands one piece of software offers to another. postMessage lets one window (a tab, a popup, an iframe) hand a data object to another window. The receiving page decides what to do with the message. If it trusts the message too much, and the message contains HTML, the receiver can end up rendering attacker HTML. That is the shape of the bug here.

Cookies and tokens

A cookie is a small piece of data a website asks your browser to store and send back on every request to that site. It is how sites remember that you are logged in. A token is similar in spirit: a string that proves who you are or what you are allowed to do. The researchers mention two kinds: cookies assigned after a login-like check, and the service account's authorisation token, which is a credential the notebook holds so it can act as the robot user.

The metadata server

Cloud virtual machines can ask a special internal address, the metadata server, questions about themselves, including "what credentials am I running as?". It is meant to be reachable only from inside the machine. If an attacker gets code running on that machine (or in a page served from it), they can ask the metadata server for the machine's token. That is the escalation step in this story.

Setup: what the researchers needed

  1. Access to Google Cloud with permission to create a Vertex AI Workbench instance using Theia.
  2. A deployed Theia instance workbench, reachable at a subdomain of the form {VICTIM-SUBDOMAIN}-dot-us-west1.notebooks.googleusercontent.com.
  3. A separate page under the attacker's control to host the proof of concept.

A proof of concept (PoC) is a small, deliberately harmless demonstration that a bug is real: the minimum needed to show the problem, not a weaponised attack.

One more fact matters for everything that follows. When a user-managed Vertex AI Workbench is created, it uses the project's default Compute Engine service account. That service account has access to all the project resources. In everyday terms: the notebook is not a lowly guest in the project; it is a keyholder with a key to every room.

Finding it: how the bug was spotted

After deploying a Theia instance workbench, the researcher noticed that the installed version was not the latest. That is a normal first move in security testing: old software has a public list of known bugs, so you check whether any of them apply. The researchers searched for known vulnerabilities in that version.

Several were known, but not all of them worked. Two reasons came up:

  • Either the specific feature had been removed from the installation (for example, the mini-browser), so the bug had nothing to attach to.
  • Or exploiting them required unrealistic user interactions, such as uploading a file and opening it themselves. A bug that only works if the victim does something contrived is much less serious in practice.

CVE-2021-41038 was different. A CVE is a public catalogue number for a known vulnerability, so researchers everywhere can refer to the same bug by the same name. This one exploits Theia's webview extension, and that extension was enabled in the deployment. The feature was present, so the bug applied.

Exploitation

Step 1: Host the proof of concept

The researcher created a page that opens the victim's webview URL and, after a delay, sends a postMessage to it:

HTML
<html>
  <body>
    <script>
      function exploit(){
        w = open("https://{VICTIM-SUBDOMAIN}-dot-us-west1.notebooks.googleusercontent.com/webview/index.html?id=a");
        window.setTimeout(function(){w.postMessage({channel: "content", args: {options: {allowScripts: true}, contents: "<img src=x onerror=\"document.write('XSS here! '+document.domain)\">"}}, "*");}, 5000);
      }
    </script>
    <button onclick="exploit()">Click here</button>
  </body>
</html>

Line by line, in plain words:

  • <html>, <body>: the usual skeleton of a web page.
  • <script>: starts a block of JavaScript that the browser will run.
  • function exploit(){: defines a named block of code, exploit, that runs only when called.
  • w = open("https://{VICTIM-SUBDOMAIN}-dot-us-west1.notebooks.googleusercontent.com/webview/index.html?id=a");: opens a new browser window at the victim's webview page. w is a handle to that new window, so the script can talk to it later. The path /webview/index.html?id=a is the webview endpoint; id=a is a query parameter, a small piece of data passed in the URL.
  • window.setTimeout(function(){ ... }, 5000);: waits five seconds (5000 milliseconds) before running the inner function. The delay gives the new window time to load. Without it, the message might arrive before the page is ready to receive it.
  • w.postMessage({...}, "*");: sends a message to the opened window. The first argument is the data; the second is the target origin, which says which sites are allowed to receive it. "*" means "any site", the loosest possible setting.
  • {channel: "content", args: {options: {allowScripts: true}, contents: "..."}}: the message body. It names a channel called content, and in its arguments it sets allowScripts: true (asking the receiver to permit scripts in the content) and supplies contents, the HTML to render.
  • contents: "<img src=x onerror=\"...\">": the HTML is an image tag whose source, x, does not exist. Browsers fire an error event when an image fails to load, and the onerror attribute says what to run at that moment. This is a standard trick for smuggling script into HTML: the script is not in a <script> tag, it is in an error handler.
  • document.write('XSS here! '+document.domain): the handler writes the text XSS here! followed by document.domain, which is the current site's domain name, into the page. Printing the domain proves which site the code is running in.
  • <button onclick="exploit()">Click here</button>: a button that calls exploit when clicked, so the researcher can start the demonstration by hand.

Why this step was taken: the whole bug depends on the webview trusting a message from another window. The PoC is the smallest page that opens the webview and sends exactly the message the webview is willing to act on.

Step 2: Trigger it

Clicking the button runs exploit(). The webview accepted the message and rendered the supplied HTML with scripts allowed, so the onerror handler executed. The alert popped, confirming the XSS.

In plain terms: the webview received a note from a stranger, believed it, and read it out loud. The text XSS here! plus the domain appearing on screen is the proof that attacker-supplied script ran inside the Google-hosted page.

Step 3: Turn the XSS into project compromise

Because the workbench runs with the project's default Compute Engine service account, and that account has access to all project resources, an attacker can use the XSS to fetch the service account token from the metadata server. With that token the attacker can compromise the entire project.

Why this matters: XSS on a random blog would be annoying. XSS on a page that sits next to a keyholder identity is serious. The script now runs in the notebook's own origin, so it can make the same requests the notebook can, including the request to the metadata server that hands back the service account token. The token is the key; the project is the building.

Obtaining the victim's instance subdomain

The attack needs the victim's notebook subdomain, because the PoC has to open that exact URL. The report lists two ways to get it:

  1. By default, every person's subdomain is leaked to several third-party domains via the referer. Some of those domains are:

    1. https://open-vsx.org
    2. https://openvsxorg.blob.core.windows.net
    3. https://schemastore.azurewebsites.net

    Anyone with access to these domains, or with a vulnerability in these servers, can leak the subdomain.

  2. Anyone with a "Notebook viewer" role in the victim's project can see the subdomain, and can then exploit this issue to gain editor access to the victim's project.

The referer (yes, spelled that way in the HTTP standard) is a header your browser attaches to many requests, saying which page you came from. It exists so sites can see their traffic sources. Here it has a side effect: when the notebook page loads resources from those third-party domains, the notebook's subdomain travels along in the referer. A role in Google Cloud is a named bundle of permissions; "Notebook viewer" means read-only access to the notebook. The second route is the interesting one: someone who was only supposed to look can use this bug to become an editor.

Root cause

The webview extension accepts postMessage content on the content channel and honours an allowScripts option supplied by the sender, rendering attacker-controlled HTML with scripts enabled. The webview extension being enabled in the deployment is what made CVE-2021-41038 applicable here.

In everyday terms: the clerk at the desk accepts instructions from anyone who walks in, and one of the instructions is "and please ignore the rule about not running scripts". The receiver should have checked who sent the message and refused to enable scripts for untrusted content. It did neither.

Impact

By using this XSS, an attacker could fetch the service account token from the metadata server and compromise the entire project, because the workbench uses the project's default Compute Engine service account, which has access to all project resources. The attacker needs the victim's instance subdomain, obtained through referer leakage to the third-party domains above or through a "Notebook viewer" role in the victim's project. In the second case, the issue turns viewer access into editor access to the project.

Put simply: the bug does not just run a script; it hands the attacker the project's keys. The only precondition is knowing the notebook's address, and the report shows two realistic ways to learn it.

Bonus bug

Another behaviour the researchers noticed in Theia was inspired by a bug in which the researcher describes how leaving a service unsecured in the internal cloud VPC can lead to security issues. A VPC (virtual private cloud) is a private network inside the cloud: machines in it can talk to each other, and by default the outside world cannot reach in. In summary, although the port is only open inside the VPC:

  1. An attacker with low privileges in the project could exploit such behaviour to escalate privileges or move laterally.
  2. Admins tend to open port ranges to the internet for convenience.

A port is a numbered door on a machine that a particular service listens on; port 8080 is a common choice for web applications. "Escalate privileges" means gaining more permissions than you started with. "Move laterally" means hopping from one machine to another inside the network, the way an intruder walks from room to room.

To see how this affects Theia, consider how authorisation works. The Theia instance was hosted on a subdomain of notebooks.googleusercontent.com. When users try to access this domain, they are redirected through an OAuth-like flow where they are checked for permissions. If they have the necessary permissions, they are redirected back to the notebook domain and cookies get assigned.

Authorisation is the process of deciding whether you are allowed to do something. An OAuth-like flow is the familiar pattern where you are bounced to a login or consent page and then sent back to where you started, with proof of who you are. Here, the bounce is the permission check, and the cookies assigned at the end are the proof that you passed it.

However, port 8080, which Theia uses, is open inside the VPC. Anyone inside the VPC could access it by directly using the IP address of the notebook instead of the domain. An IP address is the numeric address of a machine on a network, the equivalent of a street address. The domain name route goes through the permission check; the IP address route does not. Since the notebooks contain the authorisation token of the default Compute Engine service account, an attacker could use this to escalate privileges to the Project Editor level. Secondly, as noted above, it is common for admins to open port ranges to the internet for various reasons, and port 8080 is one of the most commonly opened ports.

So the bonus bug is a second, independent way in: skip the front door with its permission check and knock on the side door that only the internal network was supposed to see. If that side door is ever opened to the internet, which the report says is common, the protection disappears entirely.

Fix

The researchers reported both issues to Google's Vulnerability Reward Program. A Vulnerability Reward Program (also called a bug bounty) is a scheme where a company pays researchers for reporting security bugs responsibly instead of exploiting them. For the bonus bug, Google was already aware and was in the process of removing Theia from the list of options for Vertex AI Workbench. That removal ended up being the patch for both bugs: Theia was removed from Vertex AI Workbench.

A patch is a fix. Removing the feature removes the attack surface: if there is no Theia workbench, there is no webview to send messages to and no port 8080 to reach.

Reward

Google awarded the researchers a $3133.70 bounty under their Vulnerability Reward Program.

If you want to look for similar problems elsewhere, the report suggests four directions. These are starting points, not instructions to attack anything you do not own or have permission to test.

  1. Other Theia deployments with the webview extension enabled: open /webview/index.html on the instance and post a content message with allowScripts: true and HTML containing an event handler. A positive result is script execution in the webview origin.
  2. Referer leakage of instance subdomains: load a Theia instance and watch outbound requests to third-party hosts such as extension registries and schema stores. A positive result is the notebook subdomain appearing in a Referer header sent to a domain you can observe.
  3. Direct access to the IDE port inside the VPC: from another workload in the same VPC, request the notebook's IP address on port 8080 instead of the notebooks.googleusercontent.com domain. A positive result is the IDE responding without the OAuth-like redirect and cookie assignment.
  4. Metadata token retrieval from the IDE origin: once script execution is achieved, request the metadata server from the page. A positive result is a service account token being returned to the attacker-controlled script.

Glossary

  • API: a defined set of commands one piece of software offers to another so they can work together.
  • Authorisation: the process of deciding whether a user or program is allowed to perform an action.
  • Bounty: money a company pays a researcher for responsibly reporting a security bug.
  • Cookie: a small piece of data a website asks your browser to store and send back on later requests, commonly used to keep you logged in.
  • CVE: a public catalogue number assigned to a known vulnerability so everyone can refer to the same bug.
  • Cross-site scripting (XSS): a bug where an attacker gets their script to run inside another site's page, with that site's privileges.
  • Domain: the main name of a website, such as example.com.
  • Escalate privileges: gain more permissions than you started with.
  • Extension: an add-on feature that plugs into a larger program, such as an editor.
  • HTML: the language web pages are written in, describing text, images and structure.
  • IDE: integrated development environment, a program for writing and running code, often in the browser.
  • IP address: the numeric address of a machine on a network.
  • JavaScript: the programming language that runs scripts inside web pages.
  • Metadata server: an internal address cloud machines can query for information about themselves, including their credentials.
  • Move laterally: move from one compromised machine to another inside a network.
  • OAuth-like flow: a login or consent pattern where you are redirected away and back, receiving proof of identity at the end.
  • Patch: a fix for a bug.
  • Port: a numbered door on a machine that a particular service listens on.
  • postMessage: a browser API that lets one window send a data message to another window.
  • Project: in Google Cloud, the container that holds resources and controls who can access them.
  • Proof of concept (PoC): a minimal demonstration that a bug is real.
  • Referer: an HTTP header telling the server which page the request came from.
  • Role: a named bundle of permissions in Google Cloud, such as "Notebook viewer".
  • Script: a small program, usually JavaScript, that runs inside a web page.
  • Service account: a robot user identity that programs use to act on your behalf in the cloud.
  • Subdomain: a name in front of a domain, as in mail.example.com.
  • Target origin: in postMessage, the site allowed to receive the message; * means any site.
  • Theia: an IDE that runs in the browser, used by Google Cloud Shell and, at the time, by Vertex AI Workbench.
  • Token: a string that proves identity or permission, used by software instead of a password.
  • VPC: a private network inside the cloud that machines can use to talk to each other.
  • Vulnerability Reward Program: a company scheme that pays researchers for reporting security bugs.
  • Webview: an embedded browser pane inside an application that can display HTML content.

More by Sreeram KL

View all

2 more disclosed reports, best first.

More from Google

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.