Skip to content
RCE Write-up Advanced

Hacking Cloudflare Pages part 2

The Cloudflare Pages build system processed user-controlled _redirects and _headers files as root without checking for symlinks, allowing attackers to read arbitrary files. Additionally, an internal API had a path traversal flaw that let attackers access other users' build data, and a version environment variable could be abused to execute arbitrary code as root.

ec0 Follow

Published on ec0.io

これらの脆弱性について日本語で読みたい場合は、こちらから読むことができます

ec0, on ec0.io

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

Context

Cloudflare Pages is a hosting product that builds a site from a repository and deploys the result. The build runs on Cloudflare's infrastructure, and the build system reads two special files from the repository, _redirects and _headers, to configure redirects and response headers for the deployed site. This write-up is a follow-up to an earlier investigation into the Pages build system, carried out with Sean while the author was at Assetnote. The follow-up was a collaboration with RyotaK, who brought a new vector for reading files as root inside the build system.

Setup

The work required a Pages project whose build settings the researcher controlled, including the build script and environment variables. The previous method for dumping source code and files from the build filesystem had been closed when the earlier reports were fixed, so the team could no longer review the current build scripts and tools. Regaining that access was the first step. No specific tool versions are given beyond the wrangler version discussed below.

Finding it

RyotaK's vector abused the handling of _redirects and _headers. The Pages build system processes these files as root and does not check whether they are symlinks. A build script could therefore create a symlink with one of those names pointing at a file the build user should not read, and the contents would then appear in the build results, where redirects and headers are shown.

Exploitation

Step 1: Read files as root through a symlink

The build script added a step like the following (the flaw has since been fixed):

Shell
ln -s /etc/shadow _redirects

During the build, _redirects becomes a symlink to /etc/shadow. When the build system processes redirects and headers and displays them in the build results, the target file's contents are exposed in the results panel. The same technique was used to dump the source code of the build agent for review, and the /proc/<pid>/environ trick was used to dump environment variables of system processes, including those of the root user.

Step 2: Study the internal API

With the dumped code and environment variables, the team found two further vulnerabilities. Internally, Pages uses an API that is not exposed to the Internet, used to pull and push data about the build environment and to submit build results back to Pages. A call looks like this:

Shell
https://api.pages.cloudflare.com/client/v4/accounts/[redacted]/storage/kv/namespaces/[redacted]/values/builds/5486590/logs

The API is normally protected by an authenticating proxy that restricts access to the caller's own project and namespace. The UUIDs in the accounts and namespace parts of the URL must match the values encoded in the JWT. The user does not control those values and cannot change them.

Step 3: Traverse out of the namespace

The API had a path traversal vulnerability that allowed requests outside the constraints of the user's account and namespace. A request such as the following was issued:

Text
https://api.pages.cloudflare.com/client/v4/accounts/[redacted]/storage/kv/namespaces/[redacted]/..%2F..%2f..%2f..%2f..%2f..%2Faccounts%[redacted]%2fstorage/kv/namespaces/[redacted]/keys

The encoded ..%2F sequences climb out of the caller's namespace and back into another account and namespace path. This exposed sensitive data such as keys used in other users' builds and build artifacts. In one example, keys were found that allowed access to another user's GitHub account.

Step 4: Control the wrangler version

Pages' functions support installs the wrangler npm package at build time to gather functions for deployment. Wrangler is a CLI for working with Cloudflare functions. It is installed as part of the build when the Pages build registers functions, a feature that was relatively new at the time of testing. The version installed could be controlled through the PAGES_WRANGLER_VERSION environment variable, which was passed to npm in a build tool step in /opt/pages/build_tool/steps/build_assets.py:

Python
  version = [env_var['value'] for env_var in env_vars if env_var['key'] == 'PAGES_WRANGLER_VERSION'][0]
  <.. snip ...>
  subprocess.run(['npm', 'install', f'wrangler@{version}'], cwd=WRANGLER_DIR, check=True, capture_output=True)

The version value is interpolated into the npm command after the package name and an @ symbol. That allows abuse of npm's feature for installing a specific version of a package from a git repository or from a fixed URL containing a tarball.

Step 5: Execute code as root

Setting PAGES_WRANGLER_VERSION in the build settings to /opt/buildhome/repo/wrangler-3.0.0.tgz and including a malicious wrangler-3.0.0.tgz npm package with a modified wrangler binary in the build repository caused npm to install the attacker's package. In the example, the wrangler binary was modified to execute a reverse shell back to a host the team controlled. The modified wrangler2 binary was called as root with the user-controlled version, so the malicious package led to code execution as root.

Root cause

The report describes the symlink issue and two further vulnerabilities found afterwards. The build system processed _redirects and _headers as root without checking whether they were symlinks, so a repository could point them at arbitrary files and have the contents echoed into build output. The UUIDs in the account and namespace parts of the URL had to match the values encoded in the JWT, but encoded traversal sequences in the path still escaped the caller's namespace. And the build tool interpolated the PAGES_WRANGLER_VERSION value directly into an npm install command, letting a user-supplied string select an arbitrary package source, which npm then installed and executed as root.

Impact

An attacker with control of a Pages project could read files as root inside the build environment, including the build agent's source code and process environment variables. Through the API traversal, they could reach other users' namespaces and read sensitive data such as keys used in other users' builds and build artifacts, and in at least one case keys granting access to another user's GitHub account. Through the wrangler version, they could execute arbitrary code as root in the build system. The report does not give a severity rating or bounty amount.

Fix

The symlink issue with _redirects and _headers has been fixed. The report says the symlink issue has been fixed and does not describe fixes for the other two issues.

Variants to test

  1. On other build platforms, create _redirects or _headers style configuration files as symlinks to sensitive paths and check whether their contents appear in build logs or results. A positive result is the target file's contents shown in the output.
  2. On internal APIs reached through an authenticating proxy, insert encoded ..%2F sequences into path segments that are supposed to be fixed by a token. A positive result is a response containing data from a namespace or account other than your own.
  3. Where a build step interpolates a user-controlled version string into a package manager command, supply a path or URL to a tarball you control. A positive result is your package being installed and its code running during the build.
  4. Use /proc/<pid>/environ reads from within a build to dump environment variables of system processes. A positive result is environment data belonging to another user or the build service itself.

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

What happened, in plain words

Cloudflare Pages is a service that takes the files of a website stored in a code repository (a place like GitHub where the files of a project live) and turns them into a live website. To do that, Cloudflare runs a program called a build on its own computers: the build reads your files, prepares them, and publishes the result. This article is about a follow-up investigation into that build system, done by the author together with Sean (from an earlier round of research at Assetnote) and RyotaK, who brought a new idea for reading files from inside the build machine.

The investigation found a way back into the build system and then two further problems. First, two configuration files that a site owner can put in their repository were processed with the highest level of permission on the machine (the "root" user), without checking whether they were ordinary files or shortcuts pointing somewhere else. That let the researchers read files they should never have been able to see. Second, an internal programming interface (an API) used by the build system could be tricked into reading data belonging to other customers. Third, a setting that chooses which version of a helper tool gets installed during the build could be abused to install the attacker's own program and run it with root permission.

No severity rating or bounty amount is given for this report.

Background

If you have not read the earlier write-up that the author and Sean produced while the author was at Assetnote, about their first look at Cloudflare Pages, it is worth reading first. This article continues that work.

RyotaK contacted the team with a new way to dump files out of the build system as the root user, so the three of them decided to collaborate and take the build system apart again.

A few words that come up constantly below:

  • Repository: the folder of project files stored on a service such as GitHub. Cloudflare Pages reads it to build your site.
  • Build: an automatic job that runs on Cloudflare's computers, takes your repository, and produces the finished website.
  • Root: the all-powerful account on a Linux machine. A normal user cannot read many system files; root can read and change everything. Code running as root is the worst case in this kind of research.
  • Symlink (symbolic link): a file that is really a shortcut to another file. Opening the shortcut opens the target. Think of a note in a drawer that says "the real document is in the safe" instead of containing the document itself.

Regaining access

The team had previously found a way to dump the source code and various files from the build machine's filesystem, but that method stopped working once their earlier reports were fixed. Without it they could not review the current version of the build scripts and tools, so getting that visibility back was the first job.

RyotaK found a clever way to abuse a feature that lets Pages users set custom redirects or headers. Cloudflare Pages lets you place a file called _redirects or _headers in your repository to control how the published site redirects visitors and which HTTP headers it sends. (A redirect is an instruction that sends a browser from one address to another; a header is a small piece of extra information a web server attaches to its response.)

The flaw was that the Pages build system processes these files as root and does not check whether they are symlinks. So, as part of setting up your build script (the list of commands the build runs), you could add a step like this. It has since been fixed.

Shell
ln -s /etc/shadow _redirects

Line by line:

  • ln -s is the Linux command that creates a symbolic link. The -s means "symbolic", as opposed to a hard link.
  • /etc/shadow is the system file on a Linux machine that holds password hashes for user accounts. A normal user is not allowed to read it; only root is.
  • _redirects is the name of the new shortcut being created in the current folder. So after this command, the file called _redirects is not a real file at all: it is a pointer to /etc/shadow.

During the build, the system then treated _redirects as the redirects configuration, read it as root, and showed the redirects and headers in the build results. Because the file was really a shortcut to /etc/shadow, the contents of that protected file appeared in the results panel, where the researcher could simply read them. The same trick was used to dump the source code of the build agent (the program that runs the build) so it could be reviewed, and the /proc/<pid>/environ trick was used to dump the environment variables of system processes, including those of the root user.

Two more terms worth knowing here:

  • Environment variable: a named setting that a program reads when it starts, such as a version number or a secret key. On Linux, /proc/<pid>/environ is a special file that shows the environment variables of a running process, identified by its process ID (<pid>).
  • Build agent: the software on Cloudflare's side that carries out the build steps.

Internal API path traversal

With the dumped code and environment variables in hand, the team found two more vulnerabilities with serious impact.

Internally, Cloudflare Pages uses an API that is not exposed to the Internet. An API is a way for one program to talk to another over the network, usually by sending a request to a URL and getting data back. This particular API is used to pull and push data about the build environment and to submit finished build results back to Pages. A call to it looks like this:

Shell
https://api.pages.cloudflare.com/client/v4/accounts/[redacted]/storage/kv/namespaces/[redacted]/values/builds/5486590/logs

Reading it piece by piece:

  • https://api.pages.cloudflare.com is the address of the internal API.
  • /client/v4/ is a version prefix in the path.
  • /accounts/[redacted]/ identifies one account. The long string is a UUID, a unique identifier made of letters and digits.
  • /storage/kv/namespaces/[redacted]/ identifies a storage namespace inside that account, again by UUID. A namespace is a separate storage area, like a labelled box that belongs to one customer.
  • /values/builds/5486590/logs asks for the logs of one particular build.

Normally the API is protected by an authenticating proxy: a piece of software that sits in front of the API, checks who is calling, and only lets the request through to the caller's own project and namespace. The check works because the caller carries a JWT, a signed token that proves who they are and what they may access. (A token is like a stamped visitor badge: the server can check the stamp without phoning anyone.) The UUIDs in the accounts and namespace parts of the URL must match the values encoded inside the JWT. The user does not control those values and cannot change them.

However, the API had a path traversal vulnerability. Path traversal means putting special sequences such as .. into a URL path so that the server walks up out of the folder it intended to serve and into a different one. A simple everyday comparison: a hotel clerk is told to fetch the folder labelled "room 12" from a shelf, but if the label can contain "go up one shelf", the clerk can be sent to a different guest's folder. Here, a request like the following was issued:

Text
https://api.pages.cloudflare.com/client/v4/accounts/[redacted]/storage/kv/namespaces/[redacted]/..%2F..%2f..%2f..%2f..%2f..%2Faccounts%[redacted]%2fstorage/kv/namespaces/[redacted]/keys

Line by line:

  • The URL starts the same way as the legitimate call, with the account UUID and a namespace UUID. This time the namespace is [redacted], the caller's own.
  • Then comes ..%2F repeated several times. .. means "parent folder" in a path, and %2F is the URL-encoded form of the / character. Encoding it stops the proxy from spotting a plain slash and treating the path as normal. Each ..%2F climbs one level up.
  • After climbing out, the path re-enters with accounts%[redacted]%2fstorage/kv/namespaces/[redacted]/keys, which points at a different account and namespace and asks for its keys.

The traversal let the request escape the caller's namespace and reach data belonging to others. That exposed sensitive data such as keys used in other users' builds, and build artifacts (the files a build produces). In one example, keys were found that allowed access to another user's GitHub account.

Arbitrary code execution as root user

Cloudflare Pages has a feature called functions, which lets a site run small pieces of code on Cloudflare's servers. To support it, the wrangler npm package is installed during the page build to gather the functions for deployment. Wrangler is a command-line tool for working with Cloudflare functions, and npm is the standard package manager for JavaScript: a program that downloads and installs other programs from a public catalogue.

Wrangler is installed as part of the build whenever the Pages build registers any functions, a feature that was relatively new to Pages at the time of testing. The version of wrangler could be controlled through the PAGES_WRANGLER_VERSION environment variable, which was passed to npm in a build tool step in /opt/pages/build_tool/steps/build_assets.py:

Python
  version = [env_var['value'] for env_var in env_vars if env_var['key'] == 'PAGES_WRANGLER_VERSION'][0]
  <.. snip ...>
  subprocess.run(['npm', 'install', f'wrangler@{version}'], cwd=WRANGLER_DIR, check=True, capture_output=True)

Line by line:

  • The first line searches the list of environment variables for the one whose key is PAGES_WRANGLER_VERSION and takes its value, storing it in a variable called version. In plain words: "find the version number the user asked for".
  • <.. snip ...> marks code that was left out of the excerpt.
  • The last line runs the command npm install wrangler@<version> in the wrangler directory. The f'wrangler@{version}' part is a Python f-string, which builds a piece of text by inserting the value of version after wrangler@. subprocess.run is Python's way of launching another program, here npm. check=True makes Python raise an error if npm fails, and capture_output=True collects npm's output instead of printing it.

As you can see, the user controls the version value, and it is inserted into the npm command right after the package name and an @ symbol. That lets the attacker abuse an npm feature: npm can install a specific version of a package not only from its public catalogue but also from a git repository or from a fixed URL containing a tarball (a single archive file, like a zipped folder).

So the researchers set PAGES_WRANGLER_VERSION in the build settings to /opt/buildhome/repo/wrangler-3.0.0.tgz, and included a malicious wrangler-3.0.0.tgz npm package with a modified wrangler binary in the build repository. In their example, they modified the wrangler binary so that it opened a reverse shell back to a host they controlled. A reverse shell is a connection in which the compromised machine reaches out to the attacker and then hands over a command prompt, so the attacker can type commands on the victim machine.

The modified wrangler2 binary was called as root with the user-controlled version, so using the malicious wrangler package led to code execution as root. In other words, the attacker's own program ran with the highest permission on the build machine.

Summary

The author hopes this brief write-up is helpful to others in their travels on the Internet, and thanks Sean and RyotaK for their collaboration and skill, and the Cloudflare Pages security team for being a pleasure to work with in reporting these vulnerabilities.

Glossary

  • API: a way for one program to talk to another over a network, usually by sending a request to a URL and receiving data back.
  • Authenticating proxy: software that sits in front of a service, checks who is calling, and only forwards requests that are allowed.
  • Build: an automatic job that runs on a server, takes a project's files, and produces the finished website.
  • Build agent: the software on the hosting provider's side that carries out the build steps.
  • Build artifact: a file produced by a build.
  • Environment variable: a named setting a program reads when it starts, such as a version number or a secret key.
  • Functions: a Cloudflare Pages feature that lets a site run small pieces of code on Cloudflare's servers.
  • Header: a small piece of extra information a web server attaches to its response.
  • JWT: a signed token that proves who a caller is and what they may access; the server can check the signature without contacting anyone else.
  • Namespace: a separate storage area belonging to one customer, like a labelled box.
  • npm: the standard package manager for JavaScript, which downloads and installs programs from a public catalogue.
  • Path traversal: putting special sequences such as .. into a URL path so the server walks out of the folder it meant to serve and into another one.
  • Redirect: an instruction that sends a browser from one address to another.
  • Reverse shell: a connection in which the compromised machine reaches out to the attacker and hands over a command prompt.
  • Root: the all-powerful account on a Linux machine, able to read and change everything.
  • Symlink: a file that is really a shortcut to another file; opening the shortcut opens the target.
  • Tarball: a single archive file, like a zipped folder, often used to distribute software.
  • Token: a piece of data that proves identity or permission, like a stamped visitor badge.
  • UUID: a long unique identifier made of letters and digits.
  • Wrangler: a command-line tool for working with Cloudflare functions.

More from Cloudflare

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.