Skip to content
CSRF Write-up Advanced

Google Cloud AI HUB

The researcher found that JupyterLab's CSRF protection relies on a _xsrf cookie that can be overwritten from a sibling subdomain, and combined this with the ability to edit the notebook's own front-end files to achieve code execution. An attacker could set a known _xsrf value on the victim's browser and then use a crafted request to run code on the victim's notebook VM.

s1r1us Follow

Published on blog.s1r1us.ninja

Bounty$3,134 View original

In my junior year, I am very interested in AI along with CTFs and I did an intern in Data Science. Most of the time I used Jupyter Notebooks for training models.

s1r1us, on blog.s1r1us.ninja

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

Context

Google Cloud AI Hub lets users create notebook instances. When a notebook is created, the platform provisions a VM instance, installs Jupyter Notebook on it, and assigns the instance a random domain of the form random-id.notebooks.googleusercontent.com. Logging in to the notebook instance happens through Google SSO.

The researcher came to this target with prior familiarity: during a Data Science internship in their junior year they had used Jupyter Notebooks for training models, so they already understood how the product behaves. Later, while doing bug bounties, they picked Google Cloud AI Hub as a target.

Setup

No special tooling is described beyond reading the Jupyter Notebook source code and inspecting cookies in the browser.

Finding it

Straight XSS through notebook features was ruled out early: markdown and the other features are described as pretty secure. The pivot was that the user has access to the Jupyter Notebook code base inside their own VM instance. If the front page index.html can be edited, the researcher reasoned, that yields XSS. And indeed the source of Jupyter Notebook can be changed by logging in to the VM instance and editing the files under:

Text
/opt/conda/share/jupyter/lab/static

We can make the victim visit our notebook instance and pop an alert, but it is of no use because it is a self XSS.

To make it impactful the researcher looked for improper CORS implementations, but everything seemed to be good. The next interesting target, given a self XSS, was cookies. Among the cookies, _xsrf caught the eye.

CSRF mitigation in Jupyter is done by comparing the value of the _xsrf cookie with the value of the X-XSRFToken header. If both values are equal, the request is allowed.

Exploitation

Step 1: Self XSS to denial of service

Because the attacker can set the cookie _xsrf=1 from their own domain on notebooks.googleusercontent.com, every subsequent request from the victim's notebook is dropped, since the _xsrf token is now invalid. The cookie value can be changed this way, but to turn it into CSRF the X-XSRFToken header would also have to be set to the same value. That is not possible, because the browser issues an XHR preflight request when X-XSRFToken is set on a request. The result is only a kind of DoS, which the researcher judged to have no impact.

At this point the researcher considered it a dead end and moved on to reviewing the source code of Jupyter Notebook.

Step 2: Reading the Tornado CSRF handling

Jupyter uses a Tornado web server, and Tornado has an interesting behaviour for CSRF mitigation. As the researcher quotes it:

If xsrf_cookies is set, the Tornado web application will set the _xsrf cookie for all users and reject all POST, PUT, and DELETE requests that do not contain a correct _xsrf value. If you turn this setting on, you need to instrument all forms that submit via POST to contain this field. You can do this with the special UIModule xsrf_form_html(), available in all templates

The researcher then looked at the code for xsrf_form_html(). The report then presents the code for xsrf_form_html().

Root cause

CSRF mitigation is done by checking the _xsrf cookie value against the X-XSRFToken header value. Because the _xsrf cookie can be set from our domain on notebooks.googleusercontent.com, the cookie half of the comparison can be controlled. The header half cannot be set cross-origin without a preflight, which is what keeps the check intact against a straightforward CSRF attempt and limits the cookie overwrite to a denial of service.

Impact

With the steps shown, an attacker who controls a notebook instance can overwrite the victim's _xsrf cookie from their own domain on notebooks.googleusercontent.com, causing every subsequent request from the victim's notebook to be dropped because the token is invalid. The researcher explicitly characterises this as a DoS of no impact. The self XSS obtained by editing the Jupyter static files only affects the attacker's own instance. No CSRF or RCE is demonstrated in the material available here.

Fix

Variants to test

  1. Cookie tossing on sibling subdomains. Look for applications served from *.example.com where a security-relevant cookie (CSRF token, session selector, feature flag) is read from the cookie jar without a __Host- prefix or domain pinning. A positive result is that setting the cookie from an attacker-controlled sibling subdomain changes the value the application sees.
  2. Double-submit CSRF checks. Where the check is cookie value equals header value, test whether the cookie half can be overwritten from a sibling origin and whether the header can be set without a preflight (for example via a simple content type or a form field the server also accepts). A positive result is a state-changing request succeeding with an attacker-chosen token.
  3. Editable application source in user VMs. For hosted notebook or IDE products, check whether the static front-end files under paths such as /opt/conda/share/jupyter/lab/static are writable by the user and served to other users of the same instance. A positive result is script execution in a context that another user can be induced to load.
  4. Tornado xsrf_cookies deployments. For applications built on Tornado with xsrf_cookies enabled, review how xsrf_form_html() is used in templates and whether any handler skips the check. A positive result is a POST, PUT or DELETE accepted without a correct _xsrf value.

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

What happened, in plain words

A security researcher spent some time looking at Google Cloud AI Hub, a Google service where you can create "notebooks". A notebook here is a small computer in the cloud that runs a program called Jupyter Notebook, which people use to write and run code, especially for data science and machine learning. The researcher already knew this product well, because during a Data Science internship in their junior year they had used Jupyter Notebooks to train models. Later, while doing bug bounties (paid security research on companies' own systems), they chose Google Cloud AI Hub as a target.

The short version of what they found: they could change the files of their own notebook so that it runs code in a visitor's browser, but only in their own notebook, which is not useful on its own. They then looked at a cookie called _xsrf, which Jupyter uses to protect against a class of attacks called CSRF. They could overwrite that cookie from their own notebook's domain, which broke the victim's notebook: every request afterwards was rejected. That is a denial of service, and the researcher judged it to have no real impact. The report ends while they are reading the source code of the web server Jupyter uses, Tornado, and specifically a function called xsrf_form_html(). No CSRF and no remote code execution are demonstrated in the material available here.

The product and the setup

Google Cloud AI Hub lets users create notebook instances. When you create one, three things happen in the background:

  1. The platform creates a VM instance. A VM (virtual machine) is a computer that runs inside Google's data centres; from your point of view it behaves like a normal machine you can log in to.
  2. It installs Jupyter Notebook on that machine.
  3. It gives the instance a random domain of the form random-id.notebooks.googleusercontent.com, where random-id is a random identifier. That is the web address you open in your browser to reach your notebook.

Logging in to the notebook instance happens through Google SSO. SSO means single sign-on: you log in once with your Google account and that same login is accepted by the notebook.

Two ideas matter for everything that follows.

A domain is the part of a web address after the https://, for example notebooks.googleusercontent.com. A subdomain is a name placed in front of it, for example random-id.notebooks.googleusercontent.com. A website on one subdomain can set a cookie with a Domain attribute covering the parent domain, and the browser will then send it to that parent and its other subdomains. This is the behaviour the whole report turns on.

A cookie is a small piece of text that a website asks your browser to store and to send back with every later request to that site. Think of it as a claim ticket a cloakroom gives you: the website hands it out once, your browser shows it on every visit, and the website uses it to recognise you or to remember a decision. Because cookies are just text stored by the browser, a page on a related subdomain may be able to write one that another subdomain will later read.

  • An attacker who can create their own notebook instance. That means they control the files on that machine and can serve web content from its notebooks.googleusercontent.com subdomain.
  • A victim who is logged in to a notebook instance on the same parent domain.

No special tooling is described beyond reading the Jupyter Notebook source code and inspecting cookies in the browser.

Finding it

Why straight XSS was ruled out

XSS, cross-site scripting, is when an attacker gets their own JavaScript code to run inside someone else's web page, in that person's browser. JavaScript is the programming language browsers run; if an attacker's JavaScript runs on a page you are logged in to, it can act as you.

The researcher ruled out straight XSS through the notebook's own features early: markdown (the simple text formatting you type into notebook cells) and the other features are described as pretty secure.

The pivot was a simple observation: the user has access to the Jupyter Notebook code base inside their own VM instance. The notebook's web interface is made of ordinary files on that machine. If the front page index.html can be edited, the researcher reasoned, then the edited page will run whatever script the editor put in it. And indeed the source of Jupyter Notebook can be changed by logging in to the VM instance and editing the files under:

Text
/opt/conda/share/jupyter/lab/static

This is a folder path on the notebook machine. /opt/conda is where the Python environment lives, share/jupyter/lab/static is where the files that make up the JupyterLab web interface are kept. Editing a file there changes what the notebook serves to anyone who opens it.

We can make the victim visit our notebook instance and pop an alert, but it is of no use because it is a self XSS. It is like writing a prank note and hiding it in your own desk drawer. Nothing happens to anyone else, so it is of no use.

Turning to cookies

To make it impactful, the researcher looked for improper CORS implementations. CORS (cross-origin resource sharing) is the set of rules a website uses to tell browsers which other websites are allowed to read its responses. If those rules are too loose, one site can read data from another. In this case everything seemed to be good.

The next interesting target, given a self XSS, was cookies. Among the cookies, _xsrf caught the eye.

What _xsrf is for

CSRF, cross-site request forgery, is an attack where a malicious website makes your browser send a request to another site where you are logged in, without you intending it. The everyday comparison: a bank teller who accepts any written instruction that arrives on bank paper, without checking who signed it. Your browser automatically attaches your cookies to requests, so a page on another site can cause a request to be sent with your credentials; CSRF protection exists to make sure the request really came from the site's own pages.

Jupyter's CSRF mitigation works by comparing two values:

  • the value of the _xsrf cookie, which the browser stores and sends automatically, and
  • the value of the X-XSRFToken header, which is a piece of information the page's own JavaScript has to add to the request explicitly.

If both values are equal, the request is allowed. This pattern is called a double-submit check: the same secret has to be presented twice, once in a place an attacker's page can write (the cookie) and once in a place it cannot easily write (the header).

Exploitation

Step 1: Self XSS to denial of service

Because the attacker can set the cookie _xsrf=1 from their own domain on notebooks.googleusercontent.com, every subsequent request from the victim's notebook is dropped, since the _xsrf token is now invalid.

In plain words: the attacker's page, served from a sibling subdomain, writes a cookie named _xsrf with the value 1 for the parent domain. The victim's browser now sends _xsrf=1 with every request to their notebook. The notebook compares that cookie value with the X-XSRFToken header the page sends, the two no longer match, and the request is rejected. The victim's notebook stops working.

The cookie value can be changed this way, but to turn it into CSRF the X-XSRFToken header would also have to be set to the same value. That is not possible, because the browser issues an XHR preflight request when X-XSRFToken is set on a request.

Two terms here. An XHR is a request a page's JavaScript makes in the background, without reloading the page. A preflight is a preliminary question the browser sends to the server before such a request: it asks whether the real request, with its custom header, is allowed. The server has to answer with permission, and the attacker's page cannot make the victim's notebook answer that way. So the header half of the check stays out of reach.

The result is only a kind of DoS. DoS means denial of service: making something stop working for its user. The researcher judged it to have no impact, because breaking the victim's own notebook is not a meaningful attack.

At this point the researcher considered it a dead end and moved on to reviewing the source code of Jupyter Notebook.

Step 2: Reading the Tornado CSRF handling

Jupyter uses a Tornado web server. A web server is the program that receives requests from browsers and sends back pages and data; Tornado is one such program, written in Python. Tornado has an interesting behaviour for CSRF mitigation. As the researcher quotes it:

If xsrf_cookies is set, the Tornado web application will set the _xsrf cookie for all users and reject all POST, PUT, and DELETE requests that do not contain a correct _xsrf value. If you turn this setting on, you need to instrument all forms that submit via POST to contain this field. You can do this with the special UIModule xsrf_form_html(), available in all templates

Unpacking that quote:

  • xsrf_cookies is a setting on the Tornado application. When it is switched on, Tornado hands every user a _xsrf cookie.
  • POST, PUT and DELETE are types of HTTP request. HTTP is the language browsers and servers use to talk. A GET request asks for a page; POST, PUT and DELETE are used to change things on the server. Those are the requests Tornado refuses when the _xsrf value is missing or wrong.
  • A form is the part of a page with fields you fill in and a submit button. If a form sends its data with POST, it has to carry the _xsrf value as a hidden field, otherwise Tornado rejects it.
  • xsrf_form_html() is a helper that produces that hidden field for you. A template is the file that describes a page, with placeholders filled in by the server; the helper is available in all templates so every form can include the field.

The researcher then looked at the code for xsrf_form_html(). The report then presents the code for xsrf_form_html().

Root cause

CSRF mitigation is done by checking the _xsrf cookie value against the X-XSRFToken header value. Because the _xsrf cookie can be set from our domain on notebooks.googleusercontent.com, the cookie half of the comparison can be controlled. Cookie tossing is exactly the trick described above: writing a cookie from one subdomain so that it is sent to another.

The header half cannot be set cross-origin without a preflight, which is what keeps the check intact against a straightforward CSRF attempt and limits the cookie overwrite to a denial of service.

Impact

With the steps shown, an attacker who controls a notebook instance can overwrite the victim's _xsrf cookie from their own domain on notebooks.googleusercontent.com, causing every subsequent request from the victim's notebook to be dropped because the token is invalid. The researcher explicitly characterises this as a DoS of no impact. The self XSS obtained by editing the Jupyter static files only affects the attacker's own instance. No CSRF or RCE is demonstrated in the material available here. RCE means remote code execution: getting a program to run your code on someone else's machine, which is the most serious outcome a web bug can lead to.

Fix

Variants to test

  1. Cookie tossing on sibling subdomains. Look for applications served from *.example.com where a security-relevant cookie (CSRF token, session selector, feature flag) is read from the cookie jar without a __Host- prefix or domain pinning. A positive result is that setting the cookie from an attacker-controlled sibling subdomain changes the value the application sees.
  2. Double-submit CSRF checks. Where the check is cookie value equals header value, test whether the cookie half can be overwritten from a sibling origin and whether the header can be set without a preflight (for example via a simple content type or a form field the server also accepts). A positive result is a state-changing request succeeding with an attacker-chosen token.
  3. Editable application source in user VMs. For hosted notebook or IDE products, check whether the static front-end files under paths such as /opt/conda/share/jupyter/lab/static are writable by the user and served to other users of the same instance. A positive result is script execution in a context that another user can be induced to load.
  4. Tornado xsrf_cookies deployments. For applications built on Tornado with xsrf_cookies enabled, review how xsrf_form_html() is used in templates and whether any handler skips the check. A positive result is a POST, PUT or DELETE accepted without a correct _xsrf value.

Glossary

  • API: a defined way for one program to ask another program for data or actions, like a menu of requests a service accepts.
  • Attacker: the person trying to abuse the system, here someone who controls their own notebook instance.
  • Bug bounty: a programme where a company pays researchers for reporting security problems in its own systems.
  • Cookie: a small piece of text a website asks the browser to store and send back with later requests, used to recognise the user or remember a decision.
  • Cookie tossing: writing a cookie from one subdomain so that the browser sends it to another subdomain of the same parent domain.
  • CORS: cross-origin resource sharing, the rules that tell browsers which other websites may read a site's responses.
  • CSRF: cross-site request forgery, making a logged-in user's browser send an unintended request to another site.
  • Denial of service (DoS): making a service stop working for its user.
  • Domain: the part of a web address after https://, for example notebooks.googleusercontent.com.
  • Double-submit check: a CSRF defence where the same secret must appear twice, once in a cookie and once in a header or form field, and the two must match.
  • Form: the part of a page with fields to fill in and a button to submit them.
  • GET / POST / PUT / DELETE: types of HTTP request; GET asks for a page, the others are used to change things on the server.
  • Header: extra information attached to a request or response, separate from the address and the body.
  • HTTP: the language browsers and servers use to exchange requests and responses.
  • JavaScript: the programming language browsers run to make pages interactive.
  • Jupyter Notebook: a program for writing and running code in a browser, commonly used for data science and machine learning.
  • Markdown: simple text formatting used in notebook cells.
  • Notebook instance: a cloud machine running Jupyter Notebook, reachable at its own web address.
  • Preflight: a preliminary request a browser sends to ask a server whether a cross-origin request with a custom header is allowed.
  • RCE: remote code execution, running your own code on someone else's machine.
  • Self XSS: script execution that only affects the person who caused it, in their own session.
  • SSO: single sign-on, logging in once and having that login accepted by several services.
  • Subdomain: a name placed in front of a domain, for example random-id.notebooks.googleusercontent.com.
  • Template: a file describing a page, with placeholders the server fills in.
  • Tornado: a Python web server, the one Jupyter uses.
  • VM (virtual machine): a computer running inside a data centre that behaves like a normal machine you can log in to.
  • Web server: the program that receives browser requests and sends back pages and data.
  • XHR: a request a page's JavaScript makes in the background without reloading the page.
  • XSS: cross-site scripting, getting an attacker's JavaScript to run inside someone else's web page.
  • _xsrf: the cookie Jupyter uses as one half of its CSRF check.
  • X-XSRFToken: the request header Jupyter compares against the _xsrf cookie as the other half of its CSRF check.
  • xsrf_form_html(): a Tornado helper that puts the _xsrf value into a form as a hidden field.

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 request forgery (CSRF)

The lesson behind this report.

Another site makes a signed-in user's browser perform an action they did not intend.