Google Cloud Shell XSS
Theia webviews in Google Cloud Shell did not enforce origin isolation and accepted postMessage commands from any origin, allowing an attacker to inject scripts and run terminal commands. The report also found that user files could be embedded cross-site due to missing embedding protection and a SameSite=None cookie.
This is an attempt to make a better write up than 575924 – (CVE-2021-41038) XSS in @theia/plugin-ext webview :) (It’s a Theia bug but Google paid for improving the security of the project after some discussion.)
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.
Context
Google Cloud Shell is a browser-based environment that gives users a shell and an editor in the cloud. The IDE part is not written by Google: it embeds the Theia IDE from a subdomain of cloudshell.dev. That subdomain can request access to the GCP API via postMessage(), so bugs in Theia can affect the security of Google Cloud. The author notes that cloudshell.dev is not on the Public Suffix List, and that it used to be on appspot.com, which is listed. He is not sure when the API opt-in was added, and thinks it used to have credentials by default.
Theia implements VS Code style webviews. These are iframes used to render arbitrary HTML, and they can be driven via postMessage() from any origin. The report links the message listener and the assignment in Theia's plugin-ext package.
Setup
No accounts, roles or tool versions are described. The work is source review of Theia and of the JavaScript served from https://www.gstatic.com/_/cloudshell/_/js/, plus live testing against https://shell.cloud.google.com/. The precondition is that the attacker can send a message to the webview.
Finding it
Webviews are supposed to use a unique origin so that the sandbox restrictions are enforced. In Theia they do not, because of allow-same-origin. The report points to the relevant lines in main.js and to a write-up on bringing VS Code webviews to the web.
Google Cloud Shell does not isolate webviews into their own origins as Theia's documentation describes, and the webviews are allowed to run terminal commands anyway via postMessage():
window.parent.postMessage({
target: 'ID from URL of webview',
channel: 'onmessage',
data: {
text: "echo ':)'",
type: 'RUN_IN_TERMINAL'
}
});
The comment in the report next to the message format notes that children are trusted: rename channel to command and remove target.
Cloud Shell does use CSP frame-ancestors to block embedding of the IDE and its webviews:
Content-Security-Policy: frame-ancestors 'self' https://*.corp.google.com:* https://*.sandbox.google.com:* https://*.googleplex.com:* https://byteboard.googleplex.com https://byteboard.dev https://edge.byteboard.dev https://enginterview.withgoogle.com https://console.cloud.google.com https://ide.cloud.google.com https://shell.cloud.google.com https://ssh.cloud.google.com;
To prevent webview hijacking and XSS, Theia now checks that messages come from window.parent or from a child of the webview. The report links the change. It also notes that other web IDEs may still be vulnerable if their webviews have no embedding protection, as with vscode-webview.net (CVE-2022-24526), or if they copied from VS Code. VS Code itself now uses MessageChannel and only sends to window.parent.
Exploitation
Step 1: get Theia to open a webview automatically
An attacker needs to send a message to the webview. Reading the code served from https://www.gstatic.com/_/cloudshell/_/js/, the author found that the opencloudcodewelcome URL parameter makes Cloud Shell embed Theia and open a webview automatically:
https://shell.cloud.google.com/?show=ide&opencloudcodewelcome=true
The report notes that this now uses COOP allow-popups, so the opener cannot be exploited.
Step 2: send the XSS payload from the opener
Any opener can send a message to the embedded webview:
w[2][0].postMessage(
{
channel: 'content',
args: {
options: {allowScripts: true},
contents: '<script>document.write(document.domain)</script>'
}
},
'*'
);
The message uses the content channel and passes allowScripts: true together with HTML in contents. The script writes the document domain. The target origin is '*'.
Root cause
The webview is meant to run in a unique origin so that sandbox restrictions apply, but allow-same-origin is present, so the sandbox does not isolate it. Cloud Shell does not put webviews into their own origins, and webviews can run terminal commands via postMessage(). Theia now checks that messages come from window.parent or a child of the webview, a change the report links. The CSP frame-ancestors policy limits which sites can embed the IDE and its webviews, but it does not stop an allowed embedder, or a page that can reach the webview, from posting messages to it.
Impact
An attacker who can get a victim to load the crafted Cloud Shell URL and can send messages to the embedded webview can execute script in the webview context. The webview is not isolated and can run terminal commands via postMessage(). The report hedges on the API side: the cloudshell.dev subdomain can request access to the GCP API via postMessage(), and the author is not sure when that opt-in was added, thinking it used to have credentials by default.
Embed users content from Google Cloud Shell in cross-site iframes (Fixed)
There is no embedding protection on /_cloudshell/, and a SameSite None cookie is used for CloudShellAuthorization. That makes it possible to embed files on an attacker-controlled website:
iframe.src = '8080-SERVER.cloudshell.dev/_cloudshell/file?path=/entrypoint.sh';
When the Theia subdomain is known (DNS is normally insecure), this allows attacks such as The Human Side Channel and element leaks. It would be worse if SERVER could be leaked without network access. That is prevented because the redirector blocks embedding and has COOP:
https://ssh.cloud.google.com/devshell/proxy?port=8080&devshellProxyPath=%2Ffoo
Fix
Theia changed its message handling so that messages are only accepted from window.parent or a child of the webview. The cross-site embedding of /_cloudshell/ content is marked as fixed in the report. VS Code moved to MessageChannel and only sends to window.parent.
Variants to test
- Other Theia-based IDEs: check whether webviews use
allow-same-originand whether the message listener validates the sender.
What happened, in plain words
Google Cloud Shell is a service that gives you a small computer in the cloud, which you reach through your web browser. You get a command line (a place where you type commands) and a code editor, all running on Google's machines rather than on your own laptop. The editor part of Cloud Shell is not written by Google. It is a piece of software called Theia, an open-source code editor that runs in the browser, and Google loads it from a subdomain of cloudshell.dev.
A subdomain is a smaller name under a bigger one: if example.com is the main address, shop.example.com is a subdomain. Websites are made of separate pieces called frames: a page can show another page inside a rectangular window, like a picture inside a picture frame. A webview is a special kind of frame used to display content that a plugin or extension supplies, and it can show any HTML, the language web pages are written in.
Frames talk to each other with a browser feature called postMessage(). It is like passing a note through a slot in a door: one window hands a message to another window, and the receiving window decides what to do with it. The problem the researcher found is that a message could make the webview display HTML with scripts enabled, and Theia now checks that messages come from window.parent or a child of the webview. Running a script that an attacker wrote inside someone else's page is called cross-site scripting, or XSS. It is like someone slipping a written instruction into your shopping list so that the shop assistant follows it as if you had written it.
The researcher also found a second, smaller problem: some files served by Cloud Shell could be shown inside a page on any other website, which can leak small pieces of information. That part was fixed.
The pieces involved
Google Cloud Shell and Theia
Cloud Shell is a browser-based environment that gives users a shell and an editor in the cloud. The IDE part is not written by Google: it embeds the Theia IDE from a subdomain of cloudshell.dev. An IDE is an integrated development environment, the program where you write and run code. That subdomain can request access to the GCP API via postMessage(), so bugs in Theia can affect the security of Google Cloud.
An API (application programming interface) is a way for one program to ask another program to do something, like a waiter taking your order to the kitchen. GCP is Google Cloud Platform, Google's collection of cloud services. So the Theia subdomain is allowed to ask Google Cloud to do things on the user's behalf, which is why a bug in Theia matters beyond the editor itself.
The author notes that cloudshell.dev is not on the Public Suffix List, and that it used to be on appspot.com, which is listed. The Public Suffix List is a public register of the endings of domain names, used by browsers to decide which parts of a name belong to the same site. Being on it, or not, changes how strictly the browser separates one subdomain from another. He is not sure when the API opt-in was added, and thinks it used to have credentials by default. Credentials here means the proof of who you are, such as a login token, that the API uses to decide whether to obey a request.
Webviews and how they are built
Theia implements VS Code style webviews. VS Code is Microsoft's code editor, and its webviews are iframes used to render arbitrary HTML. An iframe is the browser's built-in way to put one web page inside another. These webviews can be driven via postMessage() from any origin. An origin is the combination of protocol, host and port that identifies where a page came from, for example https://shell.cloud.google.com; "from any origin" means the code did not check who was sending the message. The report links the message listener and the assignment in Theia's plugin-ext package. A listener is the part of the code that waits for incoming messages, and the assignment is the line that stores what the message contains.
Setup
No accounts, roles or tool versions are described. The work is source review of Theia and of the JavaScript served from https://www.gstatic.com/_/cloudshell/_/js/, plus live testing against https://shell.cloud.google.com/. Source review means reading the code that a website sends to your browser, the same code your browser downloads when you open the page. The only precondition that matters is that the attacker can get a victim to load a Cloud Shell URL that embeds Theia and opens a webview, and can then send messages to that webview. The precondition is that the attacker can send a message to the webview.
Finding it
The sandbox that was not switched on
Webviews are supposed to use a unique origin so that the sandbox restrictions are enforced. A sandbox is a set of limits placed on a frame so that even if bad code runs inside it, it cannot reach outside. Giving the frame a unique origin means the browser treats it as coming from nowhere in particular, so it cannot read data that belongs to the real site. In Theia they do not, because of allow-same-origin. That is a setting on the sandbox that tells the browser to keep the frame's normal origin instead of a unique one, which cancels much of the isolation. The report points to the relevant lines in main.js and to a write-up on bringing VS Code webviews to the web.
Running terminal commands from a message
Google Cloud Shell does not isolate webviews into their own origins as Theia's documentation describes, and the webviews are allowed to run terminal commands anyway via postMessage():
window.parent.postMessage({
target: 'ID from URL of webview',
channel: 'onmessage',
data: {
text: "echo ':)'",
type: 'RUN_IN_TERMINAL'
}
});
Line by line:
window.parent.postMessage({starts sending a message to the window that contains the current frame.window.parentmeans "the page that holds me".target: 'ID from URL of webview',names which webview the message is for. The ID is a value that appears in the webview's address.channel: 'onmessage',picks which internal channel the message travels on, a bit like choosing which mailbox to drop the letter into.data: {begins the actual content of the message.text: "echo ':)'",is the command to run.echois a terminal command that prints whatever follows it, so this would print a smiley face.type: 'RUN_IN_TERMINAL'tells the receiver what kind of request this is: run the text in the terminal.- The closing
});finishes the call.
The comment in the report next to the message format notes that children are trusted: rename channel to command and remove target. A child frame is a frame inside the current one. The comment notes that children are trusted: rename channel to command and remove target.
The rule that limits who may embed the page
Cloud Shell does use CSP frame-ancestors to block embedding of the IDE and its webviews:
Content-Security-Policy: frame-ancestors 'self' https://*.corp.google.com:* https://*.sandbox.google.com:* https://*.googleplex.com:* https://byteboard.googleplex.com https://byteboard.dev https://edge.byteboard.dev https://enginterview.withgoogle.com https://console.cloud.google.com https://ide.cloud.google.com https://shell.cloud.google.com https://ssh.cloud.google.com;
CSP stands for Content Security Policy, a set of rules a website sends to the browser in a header, which is a small piece of information attached to the response. frame-ancestors lists which pages are allowed to put this page inside a frame. 'self' means the same origin as the page itself. The entries with * are wildcards, meaning any subdomain of that name, and :* means any port number. The rest are specific named sites. Anything not on the list is refused by the browser, so an attacker's page cannot simply frame the editor.
To prevent webview hijacking and XSS, Theia now checks that messages come from window.parent or from a child of the webview. Hijacking here means taking control of a webview that is not yours. The report links the change. It also notes that other web IDEs may still be vulnerable if their webviews have no embedding protection, as with vscode-webview.net (CVE-2022-24526), or if they copied from VS Code. A CVE is a public catalogue number given to a known security flaw so people can refer to it unambiguously. VS Code itself now uses MessageChannel and only sends to window.parent. MessageChannel is a browser feature that creates a private, two-way pipe between two windows, so messages cannot be picked up by anyone else.
Exploitation
Step 1: get Theia to open a webview automatically
An attacker needs to send a message to the webview. Reading the code served from https://www.gstatic.com/_/cloudshell/_/js/, the author found that the opencloudcodewelcome URL parameter makes Cloud Shell embed Theia and open a webview automatically:
https://shell.cloud.google.com/?show=ide&opencloudcodewelcome=true
A URL parameter is the part of an address after the question mark, made of name and value pairs joined by &. Here show=ide asks for the editor view and opencloudcodewelcome=true asks for the welcome webview to open by itself. The parameter makes Cloud Shell embed Theia and open a webview automatically.
The report notes that this now uses COOP allow-popups, so the opener cannot be exploited. COOP is Cross-Origin-Opener-Policy, a header that controls whether the page that opened this one keeps a reference to it. The opener is the window that opened the current one.
Step 2: send the XSS payload from the opener
Any opener can send a message to the embedded webview:
w[2][0].postMessage(
{
channel: 'content',
args: {
options: {allowScripts: true},
contents: '<script>document.write(document.domain)</script>'
}
},
'*'
);
Line by line:
w[2][0].postMessage(sends a message to a particular window.wis a list of window references the attacker's page holds,[2]picks one entry and[0]picks a frame inside it. In plain terms, this is a window reference held by the attacker's page.{opens the message object.channel: 'content',selects the channel used for displaying content, as opposed to the terminal channel used earlier.args: {opens the arguments, the details of the request.options: {allowScripts: true},switches on script execution inside the content that is about to be displayed. Without this, the webview would show the HTML but not run any script in it.contents: '<script>document.write(document.domain)</script>'is the HTML to display. The<script>tag marks JavaScript, the language browsers run.document.writeinserts text into the page, anddocument.domainis the current page's domain name. So the script writes the current page's domain onto the screen.},closes the arguments.'*'is the target origin. The star means "I do not care who receives this", so the browser delivers the message regardless of which site the receiving window belongs to.);closes the call.
The message uses the content channel and passes allowScripts: true together with HTML in contents. The script writes the document domain, which demonstrates script execution inside the webview. The target origin is '*'.
Root cause
The webview is meant to run in a unique origin so that sandbox restrictions apply, but allow-same-origin is present, so the sandbox does not isolate it. Cloud Shell does not put webviews into their own origins, and webviews can run terminal commands via postMessage(). Theia now checks that messages come from window.parent or a child of the webview, a change the report links. The CSP frame-ancestors policy limits which sites can embed the IDE and its webviews, but it does not stop an allowed embedder, or a page that can reach the webview, from posting messages to it.
Impact
An attacker who can get a victim to load the crafted Cloud Shell URL and can send messages to the embedded webview can execute script in the webview context. Context here means the environment the script runs in, with whatever access that environment has. The webview is not isolated and can run terminal commands via postMessage(). The report hedges on the API side: the cloudshell.dev subdomain can request access to the GCP API via postMessage(), and the author is not sure when that opt-in was added, thinking it used to have credentials by default.
Embed users content from Google Cloud Shell in cross-site iframes (Fixed)
There is no embedding protection on /_cloudshell/, and a SameSite None cookie is used for CloudShellAuthorization. A cookie is a small piece of data a site stores in your browser and sends back with later requests, often used to remember who you are. SameSite is a cookie setting that says whether the cookie may be sent when the request comes from another site; None means it may be sent even then. So a request made from an attacker's page still carries the user's authorisation cookie.
That makes it possible to embed files on an attacker-controlled website:
iframe.src = '8080-SERVER.cloudshell.dev/_cloudshell/file?path=/entrypoint.sh';
Line by line:
iframe.src =sets the address that an iframe on the attacker's page should load.'8080-SERVER.cloudshell.dev/_cloudshell/file?path=/entrypoint.sh'is that address.8080-SERVERstands for the user's own Cloud Shell subdomain, whereSERVERis the part that varies per user. The path/_cloudshell/fileasks for a file, and?path=/entrypoint.shnames the file to return.entrypoint.shis a shell script, a list of commands the computer can run.
When the Theia subdomain is known (DNS is normally insecure), this allows attacks such as The Human Side Channel and element leaks. DNS is the system that turns names like cloudshell.dev into the numeric addresses computers use; the author's point is that the per-user part is not a well-kept secret. A side channel is a way of learning something without reading it directly, by observing indirect effects such as timing or size. An element leak is a related trick where a page learns something about content it cannot read, for example by measuring how a frame behaves.
It would be worse if SERVER could be leaked without network access. That is prevented because the redirector blocks embedding and has COOP:
https://ssh.cloud.google.com/devshell/proxy?port=8080&devshellProxyPath=%2Ffoo
A redirector is a service that forwards a request to the right place. This address asks the proxy to reach port 8080 on the user's machine and to pass along the path %2Ffoo, which is the encoded form of /foo. Because this endpoint refuses to be framed and sets COOP, an attacker's page cannot use it to discover the subdomain name.
Fix
Theia changed its message handling so that messages are only accepted from window.parent or a child of the webview. The cross-site embedding of /_cloudshell/ content is marked as fixed in the report. VS Code moved to MessageChannel and only sends to window.parent.
Variants to test
Glossary
- API: a defined way for one program to ask another program to do something, like a waiter carrying orders to a kitchen.
- Child frame: a frame that sits inside another frame, the way a small picture sits inside a bigger one.
- Cookie: a small piece of data a website stores in your browser and sends back with later requests, often to remember who you are.
- COOP (Cross-Origin-Opener-Policy): a header that controls whether the window that opened a page keeps a reference to it.
- Credentials: the proof of identity a program uses, such as a login token, when asking a service to act for a user.
- CSP (Content Security Policy): rules a website sends to the browser saying what the page may load and who may embed it.
- CVE: a public catalogue number for a known security flaw, so people can refer to it unambiguously.
- DNS: the system that turns names like
example.cominto the numeric addresses computers use. - Element leak: a trick where a page learns something about content it cannot read by observing how a frame behaves.
- frame-ancestors: the part of a CSP that lists which pages may put this page inside a frame.
- GCP (Google Cloud Platform): Google's collection of cloud services.
- HTML: the language web pages are written in.
- IDE: integrated development environment, the program where you write and run code.
- iframe: a browser feature that puts one web page inside another.
- JavaScript: the programming language browsers run to make pages interactive.
- Listener: the part of a program that waits for incoming messages and reacts to them.
- MessageChannel: a browser feature that creates a private two-way pipe between two windows.
- Opener: the window that opened the current window.
- Origin: the combination of protocol, host and port that identifies where a page came from, such as
https://shell.cloud.google.com. - postMessage(): the browser function windows use to send messages to each other.
- Public Suffix List: a public register of domain endings that browsers use to decide which parts of a name belong to the same site.
- Redirector: a service that forwards a request to the right place.
- SameSite: a cookie setting that says whether the cookie may be sent when the request comes from another site;
Nonemeans it may. - Sandbox: a set of limits on a frame so that bad code inside it cannot reach outside.
- Side channel: learning something without reading it directly, by observing indirect effects such as timing or size.
- Source review: reading the code a website sends to your browser, the same code your browser downloads when you open the page.
- Subdomain: a smaller name under a bigger one, such as
shop.example.comunderexample.com. - Terminal: the command line where you type commands for the computer to run.
- Theia: an open-source code editor that runs in the browser.
- URL parameter: a name and value pair after the question mark in an address, joined by
&. - VS Code: Microsoft's code editor.
- Webview: a frame used to display content supplied by a plugin or extension, which can show any HTML.
- XSS (cross-site scripting): running a script an attacker wrote inside someone else's page, so the browser treats it as part of that page.