The File uploading CSRF in Google Cloud Shell Editor
The Google Cloud Shell Editor's file upload endpoint lacked CSRF protection, allowing an attacker to upload files to a victim's home directory. By uploading a malicious HTML file and using the mini-browser feature, an attacker could execute JavaScript in the victim's context, potentially leading to persistent code execution via .bashrc modification.
Google Cloud Shell is an interactive shell environment for Google Cloud Platform that makes to learn and experiment with GCP and manage your projects and resources from a web browser. The cloud shell has a code editor, which called theia.
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 an interactive shell environment for Google Cloud Platform. It lets you learn and experiment with GCP and manage your projects and resources from a web browser. The Cloud Shell includes a code editor called Theia, which allows you to manipulate files in the Cloud Shell VM. More information is in the official docs at https://cloud.google.com/shell/docs/.
The editor is reachable through two kinds of URL:
https://970-dot-000000-dot-devshell.appspot.comhttps://devshell-vm-74ecb0ce-6db1-43df-a2f9-c77142a1e79e.cloudshell.dev:970/
These URLs are unique for every Google user, and the second one changes every time the user restarts the Cloud Shell VM. When the editor is opened through the first URL it is protected from CSRF attacks via the X-XSRF-PROTECTED header. When it is opened through the second URL, that protection was missing.
Setup
The researcher worked against their own Cloud Shell instance, using the editor's file upload feature and the /mini-browser feature. No special tooling is described beyond a browser, the ability to host a page, and the Cloud Shell editor itself.
Finding it
While investigating the file upload feature, the researcher found that it had no protection from CSRF attacks and that the request could be sent from any origin. The upload request looks like this:
POST /files HTTP/1.1
Host: devshell-vm-74ecb0ce-6db1-43df-a2f9-c77142a1e79e.cloudshell.dev:970
Content-Length: 337
Cookie: auth_cookie
Content-Type: multipart/form-data; boundary=------[redacted]
------[redacted]
Content-Disposition: form-data; name="target"
file:///home/userhome/folder
------[redacted]
Content-Disposition: form-data; name="upload"; filename="the_filename"
the_content_of_file
------[redacted]--
This is a standard multipart request. Because the server accepts it from any origin, it will create the file named the_filename with the content the_content_of_file in /home/userhome/folder.
Exploitation
Step 1: Bypass the multipart field name quoting
When trying different variants of the upload request, the researcher found that requests with the following line were executed successfully and files were created in the home directory:
Content-Disposition: form-data; name="upload; filename=the_filename; x"
The difference from the original request is very small, but it makes the attack possible: an HTML input element can be created with upload; filename=the_filename; x as the name attribute, and that is a valid name. With quote-separated fields this would not be possible. The HTML form used to craft the upload request is:
<form action="https://<user_devshell_domain>/files" enctype="multipart/form-data" method="POST" target="upload_iframe">
<input name="target" value="file:///home/userhome/folder">
<input name="upload; filename=the_filename; x" value="the content of file">
</form>
This lets an attacker create files in the victim's devshell VM with attacker-controlled content. Two questions remained before the attack was complete:
- How to know the domain name of the victim's Cloud Shell editor to send the request to.
- What content can be loaded via CSRF, and what kind of attack is possible with this bug: XSS, or RCE via CSRF?
Step 2: Learn the victim's Cloud Shell hostname
Opening the editor URL without credentials, https://devshell-vm-74ecb0ce-6db1-43df-a2f9-c77142a1e79e.cloudshell.dev:970/, returns a 302 redirect to auth. Opening a host that is not yours also returns you to your own editor after authorisation. So if an auth request to an attacker's VM is initiated in an iframe, the user will be redirected to their own editor anyway.
The result of that redirect can be hijacked via CSP reports. The attacker crafts a page containing an iframe with the authorisation request. The page must also have CSP rules that allow all domains except *.appspot.com and *.cloudshell.dev. When a user opens the page, they are authorised in the iframes and redirected to the editor domains, and the attacker receives CSP reports with the blocked URIs. According to the CSP rules of the page, those reports contain the user's Cloud Shell editor domains. That solves the first problem.
Step 3: Turn the uploaded file into XSS and persistence
HTML files from the VM can be opened with the /mini-browser feature, at a URL of the form:
https://970-dot-000000-dot-devshell.appspot.com/mini-browser/home/myusername/uploaded.html
Opening the uploaded HTML there showed the researcher's uploaded HTML, and the JavaScript in it was executed. The file could also be loaded in an iframe by clicking in the interface. Through this XSS, an attacker can place malicious code in .bashrc or other system files in the user's home directory, and that code will run every time the VM is restarted.
Root cause
The editor served through the *.cloudshell.dev:970 URL lacked the X-XSRF-PROTECTED header protection that the *.appspot.com URL applied, so the /files upload endpoint accepted cross-origin multipart requests carrying the victim's auth_cookie. The multipart parser also accepted a name attribute containing semicolons and a filename fragment (upload; filename=the_filename; x), which made the request constructible from a plain HTML form, where a quoted field name could not be used. The /mini-browser feature opened the uploaded HTML and its JavaScript executed.
Impact
An attacker who can get a victim to open a crafted page can create files with arbitrary content in the victim's Cloud Shell VM, learn the victim's editor hostname through CSP reports, and execute JavaScript in the editor origin via /mini-browser. That XSS allows placing malicious code in .bashrc or other system files in the user's home directory, which runs every time the VM is restarted.
Fix
The report does not describe the fix that was applied. The protection that was missing is the X-XSRF-PROTECTED header check that the *.appspot.com URL already enforced, and
Variants to test
- Send the same multipart upload from a cross-origin page to the
*.cloudshell.dev:970host and check whether the file appears in the target folder. A positive result is a created file with the supplied name and content. - Try field names containing semicolons and a
filenamefragment, such asupload; filename=the_filename; x, in a plain HTML form. A positive result is a file created without a quoted field name. - Load an uploaded HTML file through
/mini-browserand check whether its JavaScript executes. A positive result is script execution in the editor origin. - Use a page with CSP rules that block
*.appspot.comand*.cloudshell.devand an iframe pointing at the editor. A positive result is CSP reports containing the victim's editor hostname.
What happened, in plain words
A security researcher found a way to make a victim's browser quietly create files inside the victim's own Google Cloud Shell, a free Linux machine that Google runs for you in the cloud and that you reach through your web browser. The trick relied on two weaknesses that combined: the editor part of Cloud Shell accepted file uploads from any website, and it accepted a slightly malformed upload form that a normal web page could build. Once a file was planted, the researcher could open it through a built-in preview feature and get their JavaScript to run as if it came from Google's own editor. From there, code could be placed in a startup file so it ran again every time the victim's machine restarted. Google set the P1 priority on the report and paid a 5,000 USD bounty.
This article walks through the same steps the researcher took, explaining each concept from scratch. You only need to know how to use a browser to follow it.
Background: what Cloud Shell and Theia are
Google Cloud Shell is an interactive shell environment for Google Cloud Platform. In plain terms, it is a Linux computer that Google lends you for free, running somewhere in a data centre, which you control through a terminal inside your browser. You use it to learn and experiment with Google Cloud Platform (GCP) and to manage your projects and resources without installing anything on your own laptop.
Cloud Shell also includes a code editor called Theia. Theia is a program that runs in the browser and lets you browse, open, edit, create and delete files on that remote Linux machine, much like a text editor on your desktop but pointed at the cloud machine instead. More information about Cloud Shell is in the official docs at https://cloud.google.com/shell/docs/.
The editor could be reached through two different kinds of web address (URL):
https://970-dot-000000-dot-devshell.appspot.comhttps://devshell-vm-74ecb0ce-6db1-43df-a2f9-c77142a1e79e.cloudshell.dev:970/
Both addresses are unique for every Google user, and the second one changes every time the user restarts the Cloud Shell VM. Think of the first address as a stable nickname and the second as a direct phone number that gets reassigned whenever the machine reboots.
When the editor was opened through the first address, it was protected from CSRF attacks via the X-XSRF-PROTECTED header. When it was opened through the second address, that protection was missing. That difference is the seed of everything that follows.
Two concepts you need before going further
CSRF (Cross-Site Request Forgery) is a class of attack where a malicious website makes your browser send a request to another website where you are already logged in. Your browser automatically attaches your login cookie to every request aimed at that site, so the receiving site cannot tell whether you clicked a button yourself or a hidden form on an attacker's page did it for you. A useful comparison: imagine a bank that accepts written withdrawal slips and trusts any slip that arrives in an envelope with your name on it. If someone can post a slip in your name, the bank pays out. CSRF is exactly that, with the browser as the postal service.
A cookie is a small piece of data that a website asks your browser to store and to send back on every later request to that site. It is how a site remembers that you are logged in. In the request below, the cookie is called auth_cookie; it is the credential that proves the request comes from a logged-in user.
A header is a line of metadata at the top of an HTTP request or response, separate from the body. Headers carry things like the content type, the cookies, and security flags. X-XSRF-PROTECTED is a custom header that the editor's front end was expected to add to state-changing requests; the server could then reject any request that lacked it, because a random attacker page cannot add custom headers to a cross-origin request without the browser first asking permission (a mechanism called CORS).
Setup
The researcher worked against their own Cloud Shell instance, using the editor's file upload feature and the /mini-browser feature. No special tooling is described beyond a browser, the ability to host a page, and the Cloud Shell editor itself.
Finding it
While investigating the file upload feature, the researcher found that it had no protection from CSRF attacks and that the request could be sent from any origin. "Origin" here means the combination of scheme, host and port that a web page comes from, for example https://example.com. Browsers normally restrict what a page from one origin can do to another origin, but a plain HTML form submission is one of the operations that is allowed across origins, which is why CSRF is possible at all.
The upload request looks like this:
POST /files HTTP/1.1
Host: devshell-vm-74ecb0ce-6db1-43df-a2f9-c77142a1e79e.cloudshell.dev:970
Content-Length: 337
Cookie: auth_cookie
Content-Type: multipart/form-data; boundary=------[redacted]
------[redacted]
Content-Disposition: form-data; name="target"
file:///home/userhome/folder
------[redacted]
Content-Disposition: form-data; name="upload"; filename="the_filename"
the_content_of_file
------[redacted]--
Line by line, in plain words:
POST /files HTTP/1.1says "I am sending data to the path/fileson the server, using version 1.1 of the HTTP protocol". POST is the verb used when you submit a form or upload something, as opposed to GET, which is used when you simply ask for a page.Host: devshell-vm-74ecb0ce-6db1-43df-a2f9-c77142a1e79e.cloudshell.dev:970names the server the request is aimed at. This is the second kind of editor URL from the background section, the one that changes on every restart.Content-Length: 337tells the server how many bytes of body follow, so it knows when the body ends.Cookie: auth_cookieis the victim's login credential, attached automatically by the browser. In a real request this would be a long opaque string; here it is shown as a placeholder.Content-Type: multipart/form-data; boundary=------[redacted]says the body is a multipart form, the same format a browser uses when you upload a file through a web page. Theboundaryis a random-looking string used as a separator between the parts, so the server can tell where one field ends and the next begins.- The first part,
Content-Disposition: form-data; name="target", followed by a blank line and thenfile:///home/userhome/folder, is a field calledtarget. Its value is afile://URL, which is a way of writing a path on the local filesystem. So this field says "put the upload in this folder". - The second part,
Content-Disposition: form-data; name="upload"; filename="the_filename", followed by a blank line and thenthe_content_of_file, is the file itself. Thefilenameparameter tells the server what to call the new file, and the body of the part is its content. - The final
------[redacted]--line, with two trailing hyphens, marks the end of the multipart body.
This is a standard multipart request. Because the server accepts it from any origin, it will create the file named the_filename with the content the_content_of_file in /home/userhome/folder.
Exploitation
Step 1: Bypass the multipart field name quoting
When trying different variants of the upload request, the researcher found that requests with the following line were executed successfully and files were created in the home directory:
Content-Disposition: form-data; name="upload; filename=the_filename; x"
Compare this with the original line Content-Disposition: form-data; name="upload"; filename="the_filename". In the original, the field name is upload and the filename is a separate parameter, both wrapped in double quotes. In the variant, everything is inside one pair of quotes: the field name becomes the literal string upload; filename=the_filename; x. The server accepted this variant and created the file.
The difference from the original request is very small, but it makes the attack possible. Here is why. When a browser builds a multipart form from an HTML page, it takes each input's name attribute and puts it into the Content-Disposition line. But a name containing semicolons and spaces is perfectly legal in HTML, so an attacker can write upload; filename=the_filename; x as the name attribute and the browser will send it verbatim. With quote-separated fields this would not be possible, because the attacker cannot inject a quote into the field name without breaking the syntax.
The HTML form used to craft the upload request is:
<form action="https://<user_devshell_domain>/files" enctype="multipart/form-data" method="POST" target="upload_iframe">
<input name="target" value="file:///home/userhome/folder">
<input name="upload; filename=the_filename; x" value="the content of file">
</form>
Line by line:
<form ...>opens an HTML form.action="https://<user_devshell_domain>/files"is the address the form will be submitted to;<user_devshell_domain>is a placeholder for the victim's own editor hostname, which the attacker does not know yet and which the next step solves.enctype="multipart/form-data"tells the browser to encode the form as a multipart body, the same format as the raw request above.method="POST"says to use the POST verb.target="upload_iframe"says to submit the form into a named iframe instead of navigating the whole page, so the victim sees nothing happen.- The first
<input>is thetargetfield, with the valuefile:///home/userhome/folder, the destination folder. - The second
<input>is the clever one. Itsnameis the literal stringupload; filename=the_filename; x, and itsvalueisthe content of file. When the browser serialises this, it produces the malformedContent-Dispositionline shown above, and the server accepts it as an upload of a file calledthe_filename.
This lets an attacker create files in the victim's devshell VM with attacker-controlled content. Two questions remained before the attack was complete:
- How to know the domain name of the victim's Cloud Shell editor to send the request to.
- What content can be loaded via CSRF, and what kind of attack is possible with this bug: XSS, or RCE via CSRF?
XSS (Cross-Site Scripting) means getting your own JavaScript to run inside a page that belongs to someone else's site, so that the browser treats it as part of that site. RCE (Remote Code Execution) means getting your own commands to run on someone else's machine. The researcher was asking: is this just script in a page, or can it become commands on the victim's Linux box?
Step 2: Learn the victim's Cloud Shell hostname
The attacker cannot send the form until they know the victim's editor address, because the address contains a random-looking identifier and changes on every restart. Opening the editor URL without credentials, https://devshell-vm-74ecb0ce-6db1-43df-a2f9-c77142a1e79e.cloudshell.dev:970/, returns a 302 redirect to auth. A 302 is an HTTP status code meaning "the thing you asked for is somewhere else, go there instead"; here the browser is sent to Google's login page. Opening a host that is not yours also returns you to your own editor after authorisation. So if an auth request to an attacker's VM is initiated in an iframe, the user will be redirected to their own editor anyway.
An iframe is a rectangular window inside a web page that loads another web page. The attacker can put an iframe on their page pointing at a Cloud Shell editor address, and the victim's browser will follow the redirects and end up loading the victim's own editor inside that frame.
The result of that redirect can be hijacked via CSP reports. CSP (Content Security Policy) is a set of rules a page can send to the browser saying which sources of content are allowed. One of its features is the ability to tell the browser "if you block something because of these rules, send me a report about it". The attacker crafts a page containing an iframe with the authorisation request. The page must also have CSP rules that allow all domains except *.appspot.com and *.cloudshell.dev. When a user opens the page, they are authorised in the iframes and redirected to the editor domains, and the attacker receives CSP reports with the blocked URIs. According to the CSP rules of the page, those reports contain the user's Cloud Shell editor domains. That solves the first problem.
In everyday terms: the attacker sets up a security camera that only films the one thing they are not allowed to see, and the camera emails them a description of whatever it caught. The victim's browser does the filming, because it is the one that knows where the redirect ended up.
Step 3: Turn the uploaded file into XSS and persistence
HTML files from the VM can be opened with the /mini-browser feature, at a URL of the form:
https://970-dot-000000-dot-devshell.appspot.com/mini-browser/home/myusername/uploaded.html
This URL uses the first kind of editor address, the stable one, and points at a file called uploaded.html in the user's home directory. The /mini-browser path is a preview feature: it takes a file from the VM and shows it in the browser. Opening the uploaded HTML through /mini-browser executed its JavaScript.
Opening the uploaded HTML there showed the researcher's uploaded HTML, and the JavaScript in it was executed. The file could also be loaded in an iframe by clicking in the interface. That is the XSS: attacker-supplied script running inside the editor's origin.
Through this XSS, an attacker can place malicious code in .bashrc or other system files in the user's home directory, and that code will run every time the VM is restarted. .bashrc is a configuration file that the Bash shell reads and executes every time it starts, so anything written there runs automatically. That turns a one-off script execution into persistence: the attacker's code survives reboots of the VM.
Root cause
The editor served through the *.cloudshell.dev:970 URL lacked the X-XSRF-PROTECTED header protection that the *.appspot.com URL applied, so the /files upload endpoint accepted cross-origin multipart requests carrying the victim's auth_cookie. The multipart parser also accepted a name attribute containing semicolons and a filename fragment (upload; filename=the_filename; x), which made the request constructible from a plain HTML form, where a quoted field name could not be used. The /mini-browser feature then served the uploaded HTML from the editor origin, giving script execution in that origin.
Three small gaps stacked into a serious problem: a missing header check, a lenient parser, and a preview feature that trusted files it served.
Impact
An attacker who can get a victim to open a crafted page can create files with arbitrary content in the victim's Cloud Shell VM, learn the victim's editor hostname through CSP reports, and execute JavaScript in the editor origin via /mini-browser. That XSS allows placing malicious code in .bashrc or other system files in the user's home directory, which runs every time the VM is restarted.
Fix
The report does not describe the fix that was applied. The protection that was missing is the X-XSRF-PROTECTED header check that the *.appspot.com URL already enforced, and
Variants to test
If you want to reproduce the reasoning on your own account, these are the checks the researcher's work suggests:
- Send the same multipart upload from a cross-origin page to the
*.cloudshell.dev:970host and check whether the file appears in the target folder. A positive result is a created file with the supplied name and content. - Try field names containing semicolons and a
filenamefragment, such asupload; filename=the_filename; x, in a plain HTML form. A positive result is a file created without a quoted field name. - Load an uploaded HTML file through
/mini-browserand check whether its JavaScript executes. A positive result is script execution in the editor origin. - Use a page with CSP rules that block
*.appspot.comand*.cloudshell.devand an iframe pointing at the editor. A positive result is CSP reports containing the victim's editor hostname.
Glossary
- API: a defined set of addresses and rules that one program uses to talk to another; here, the
/filesendpoint that accepts uploads. - Bash: the command-line shell on most Linux systems; it reads
.bashrcwhen it starts. .bashrc: a configuration file in a user's home directory that Bash executes on startup, so anything in it runs automatically.- Boundary: a random string used in a multipart body to separate one part from the next.
- Browser: the program you use to view web pages; it stores cookies and enforces origin and CSP rules.
- Cloud Shell: Google's interactive Linux environment, reachable from a browser, for working with Google Cloud Platform.
- Cookie: a small piece of data a site asks the browser to store and resend on later requests, commonly used to remember that you are logged in.
- CORS: a browser mechanism that lets a page ask permission to read responses from another origin; without it, cross-origin requests are limited.
- CSP (Content Security Policy): rules a page sends to the browser listing which sources of content are allowed, optionally with a reporting address for blocked items.
- CSP report: a message the browser sends to the address named in a CSP policy when it blocks something.
- CSRF (Cross-Site Request Forgery): making a victim's browser send a request to a site where they are logged in, using their stored cookie, without their intent.
Content-Disposition: the header line in a multipart part that names the field and, for files, the filename.Content-Length: the header stating how many bytes the request body contains.Content-Type: the header stating what format the body is in, such asmultipart/form-data.- devshell: the internal name used in the Cloud Shell editor's hostnames.
- Editor origin: the scheme, host and port the editor is served from; script running there is treated as part of the editor.
file://URL: a way of writing a path on the local filesystem rather than a web address.- Header: a line of metadata at the top of an HTTP request or response, separate from the body.
- Host: the header naming the server a request is aimed at.
- HTML form: the part of a web page that collects values and submits them to a server, possibly as a multipart body.
- HTTP: the protocol browsers and servers use to exchange requests and responses.
- Iframe: a window inside a web page that loads another web page.
- JavaScript: the programming language browsers run inside web pages.
/mini-browser: the editor feature that previews files from the VM in the browser.- Multipart form: a request body format that carries several named parts, used for file uploads, with each part separated by a boundary.
- Origin: the combination of scheme, host and port a page comes from, such as
https://example.com. - P1: the priority Google set on this report.
- Persistence: keeping an attacker's code running across restarts or reboots.
- POST: the HTTP verb used to submit data to a server, as opposed to GET, which asks for a page.
- RCE (Remote Code Execution): getting your own commands to run on someone else's machine.
- Redirect (302): an HTTP response telling the browser to go to a different address instead.
- Theia: the code editor included in Cloud Shell, running in the browser.
- URL: a web address, such as
https://example.com/path. - VM (virtual machine): a computer simulated in software, here the Linux machine behind Cloud Shell.
- XSS (Cross-Site Scripting): getting your own JavaScript to run inside a page belonging to someone else's site.
X-XSRF-PROTECTED: a custom header the editor's front end was expected to add to state-changing requests, so the server could reject requests that lacked it.