Cloudflare-wide IP spoofing with Cloudflare Workers
A Cloudflare Workers bug allowed a worker to set the CF-Connecting-IP header on outgoing requests to other Cloudflare-protected sites, so those sites saw the worker's chosen IP instead of the real visitor's IP. An attacker could use this to impersonate any IP address, potentially accessing IP-restricted functionality.
Published on njbooher.github.io
Cloudflare is a distributed CDN and WAF. Customers configure their servers to only accept connections from Cloudflare servers and then change their DNS to point to Cloudflare’s edge servers.
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
Cloudflare is a distributed CDN and WAF. Customers configure their origin servers to accept connections only from Cloudflare's servers, then point their DNS at Cloudflare's edge. Cloudflare proxies each request to the backend and adds the CF-Connecting-IP header so the origin can identify the original visitor.
Cloudflare Workers is a serverless code hosting service. You deploy JavaScript to Cloudflare's edge servers, where it runs in the v8 runtime. A minimal hello world worker looks like this:
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
return new Response("Hello world")
}
Worker code can also make requests to external sites from inside the event handler:
async function handleRequest(request) {
return fetch(new Request('https://example.com'));
}
That outgoing request is made either by Google Compute Engine (in development) or by Cloudflare's edge server (in production), and the CF-Connecting-IP header on it is set to an IPv6 address representing the worker. The JavaScript code is not allowed to modify this header.
Setup
No special account setup, roles or tooling are described. The work was done by experimenting with sample code taken from the Workers documentation, and by observing the headers that arrived at other Cloudflare-fronted endpoints, including the /cdn-cgi/trace magic route.
Finding it
While experimenting with sample code from the documentation, I came across a sample like this:
async function handleRequest(request) {
return fetch(new Request('https://example.com', request));
}
The difference from the earlier example is the second parameter to Request. The Workers documentation describes this as an optional object with settings to apply to the Request.
When I pointed this sample at another Cloudflare worker, my IP address was sent in the CF-Connecting-IP header, rather than the fixed IPv6 address that the worker normally sets. That was unexpected: the runtime is supposed to own that header and the worker code is not supposed to be able to change it.
Further testing against the /cdn-cgi/trace magic route showed the behaviour was not specific to Workers. Sites behind Cloudflare's CDN and WAF also received my IP in the CF-Connecting-IP header for requests made by my worker code. The behaviour held even when my worker code modified query and POST parameters, as long as it did not tamper with the request headers.
Exploitation
Step 1: pass the incoming request as the Request init object
The trigger is the second argument to Request. Instead of constructing a fresh request with only a URL, the sample forwards the incoming request object as the init settings:
async function handleRequest(request) {
return fetch(new Request('https://example.com', request));
}
Because the incoming request carries the visitor's headers, those headers are applied to the outgoing sub-request. Cloudflare then trusts the CF-Connecting-IP value on the outgoing sub-request instead of replacing it with the worker's own IPv6 address.
Step 2: confirm the header on another worker
Pointing the sample at a second Cloudflare worker showed my own IP address arriving in CF-Connecting-IP, not the fixed IPv6 address normally set by the worker. This confirmed the header was being taken from the incoming request rather than generated by the platform.
Step 3: confirm it applies to ordinary Cloudflare-fronted sites
I repeated the test against the /cdn-cgi/trace magic route. Testing against the /cdn-cgi/trace magic route showed the effect was not limited to Workers-to-Workers traffic: sites behind Cloudflare's CDN and WAF also received my IP in CF-Connecting-IP for requests made by my worker code.
Step 4: vary the request body and query string
The behaviour persisted when the worker code modified query and POST parameters, provided it did not touch the request headers. In other words, the spoofing survives ordinary request rewriting; only header tampering breaks it.
Step 5: trigger it from a victim's browser
Because the worker only needs to receive a request from the victim, any request to my worker was enough, even one generated by an image embedded on a page. My worker could then make requests to any other site behind Cloudflare and present the victim's IP address as its own.
Step 6: cache per-IP responses
Some sites expose additional functionality to users from specific IP addresses. Using the Workers KV data store, I could cache the response to the sub-request so that the result obtained under a given spoofed IP could be checked afterwards.
Root cause
Normally, when a visitor sends a request to a Cloudflare-protected site containing a CF-Connecting-IP header, Cloudflare drops the header and replaces it with the visitor's actual IP before forwarding to the origin. When a Cloudflare worker makes a request to a Cloudflare-protected site, however, Cloudflare trusts the header value supplied by the worker and passes it on to the origin.
The report does not describe the internal cause. What it shows is that Cloudflare trusts the CF-Connecting-IP value supplied by a worker's sub-request and passes it to the origin. The protection that exists for visitor traffic, stripping and rewriting the header at the edge, is not applied to the worker's own sub-requests.
Impact
An attacker who controls a Cloudflare worker could make requests to any other site behind Cloudflare while claiming the IP address of any visitor who requested the worker. The victim does not need to do anything beyond loading a page that references the worker, for example through an embedded image. Sites that grant extra functionality based on source IP address would then grant it to the attacker under the victim's identity. The report does not describe any specific site or feature that was abused this way; the KV caching technique is presented as a way to check for such functionality.
Fix
Cloudflare committed a fix and deployed it globally.
Variants to test
- Forward the incoming request as the
Requestinit object to a second worker and inspectCF-Connecting-IPthere. A positive result is the visitor's IP appearing instead of the worker's fixed IPv6 address. - Repeat against
/cdn-cgi/traceon a Cloudflare-fronted host. A positive result is the trace output showing the visitor's IP inCF-Connecting-IP. - Rewrite query and POST parameters in the worker before the sub-request, leaving headers untouched. A positive result is the spoofed header surviving the rewrite.
- Trigger the worker from an image embedded in a page and observe the sub-request at a Cloudflare-protected origin. A positive result is the origin seeing the browser visitor's IP rather than the worker's.
- Cache the sub-request response in Workers KV. A positive result is being able to check afterwards whether the origin exposed functionality to that IP.
What happened, in plain words
Cloudflare is a company that sits between visitors and lots of websites. When you visit a site that uses Cloudflare, your request goes to Cloudflare first, and Cloudflare passes it on to the site's own server. Cloudflare also offers a service called Workers, which lets people run small pieces of JavaScript code on Cloudflare's own machines. The bug in this report let that code tell a website it was coming from the IP address of whoever had just loaded the worker. In other words, the worker could pretend to be you.
This matters because some websites give extra features to visitors from particular IP addresses, for example an office network or a company's own range. If a worker can claim a visitor's IP, it can borrow features meant for that visitor's address.
Background: what Cloudflare does
Cloudflare describes itself as a distributed CDN and WAF. Both terms are worth unpacking.
- A CDN (content delivery network) is a set of servers spread around the world that sit in front of a website. Visitors are sent to the nearest one, which makes pages load faster and absorbs traffic.
- A WAF (web application firewall) is a filter that inspects incoming requests and blocks ones that look malicious.
A typical customer sets things up in two steps. First, they configure their own servers (the origin servers, the machines that actually hold the website) to accept connections only from Cloudflare's servers. That way nobody can skip the filter and talk to the origin directly. Second, they change their DNS, the directory that maps a domain name to an IP address, so that the name points at Cloudflare's edge servers instead of their own.
From then on, Cloudflare proxies every request: the visitor talks to Cloudflare, and Cloudflare talks to the origin on the visitor's behalf. A proxy is like a receptionist who takes all your calls and passes on the message.
Because the origin now only sees Cloudflare's machines, it needs another way to know who the real visitor is. Cloudflare solves this by adding a header to each forwarded request:
CF-Connecting-IP
A header is a small labelled line of extra information attached to a web request or response, alongside the address being requested. CF-Connecting-IP is simply a header whose value is meant to be the visitor's IP address. An IP address is the number that identifies a device on the internet, a bit like a postal address for a computer.
Background: what Cloudflare Workers are
Cloudflare Workers is a serverless code hosting service. Serverless does not mean there is no server; it means you do not manage one. You upload code and the provider runs it for you on demand. In this case you deploy JavaScript to Cloudflare's edge servers, where it runs in the v8 runtime, the same JavaScript engine that powers the Chrome browser.
A minimal hello world worker looks like this:
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
return new Response("Hello world")
}
Line by line:
addEventListener("fetch", ...)tells the platform: whenever a request arrives, run the function I am giving you."fetch"is the name of the event, andeventis an object describing what happened.event.respondWith(handleRequest(event.request))says: the reply to this request is whateverhandleRequestreturns.event.requestis the incoming request itself, including its headers.async function handleRequest(request)declares a function that takes the incoming request and can wait for slow things (that is whatasyncmeans).return new Response("Hello world")builds a reply containing the textHello worldand sends it back.
Worker code can also make requests to external sites from inside the event handler:
async function handleRequest(request) {
return fetch(new Request('https://example.com'));
}
Line by line:
async function handleRequest(request)is the same function as before.new Request('https://example.com')builds a brand new outgoing request aimed athttps://example.com.fetch(...)sends that request and returns the response, which the function hands back to the visitor.
That outgoing request is made either by Google Compute Engine (in development) or by Cloudflare's edge server (in production). In both cases the CF-Connecting-IP header on it is set to an IPv6 address representing the worker. IPv6 is the newer, longer form of IP address. The important part is that the platform fills in this header itself, and the JavaScript code is not allowed to modify it. The runtime is the authority on who the request came from.
Setup
No special account setup, roles or tooling are described. The work was done by experimenting with sample code taken from the Workers documentation, and by observing the headers that arrived at other Cloudflare-fronted endpoints, including the /cdn-cgi/trace magic route. A magic route is a special URL that Cloudflare answers itself rather than passing to the website; /cdn-cgi/trace returns a short plain-text summary of what Cloudflare saw in your request, which makes it a handy mirror for checking headers.
Finding it
While experimenting with sample code from the documentation, I came across a sample like this:
async function handleRequest(request) {
return fetch(new Request('https://example.com', request));
}
Line by line:
async function handleRequest(request)takes the incoming request.new Request('https://example.com', request)builds an outgoing request tohttps://example.com, but this time passes a second argument: the incomingrequestobject.fetch(...)sends it.
The difference from the earlier example is that second parameter to Request. The Workers documentation describes it as an optional object with settings to apply to the Request. Think of it as a form of defaults: instead of starting from a blank request, you start from an existing one and copy its settings over.
When I pointed this sample at another Cloudflare worker, my IP address was sent in the CF-Connecting-IP header, rather than the fixed IPv6 address that the worker normally sets. That was unexpected: the runtime is supposed to own that header and the worker code is not supposed to be able to change it. Seeing my own address there was unexpected, since the runtime is supposed to set that header itself.
Further testing against the /cdn-cgi/trace magic route showed the behaviour was not specific to Workers. Sites behind Cloudflare's CDN and WAF also received my IP in the CF-Connecting-IP header for requests made by my worker code. The behaviour held even when my worker code modified query and POST parameters, as long as it did not tamper with the request headers. A query parameter is the part of a URL after the ?, such as ?page=2. A POST parameter is data sent in the body of a form submission rather than in the URL.
Exploitation
Step 1: pass the incoming request as the Request init object
The trigger is the second argument to Request. Instead of constructing a fresh request with only a URL, the sample forwards the incoming request object as the init settings:
async function handleRequest(request) {
return fetch(new Request('https://example.com', request));
}
Because the incoming request carries the visitor's headers, those headers are applied to the outgoing sub-request. A sub-request is a request made by the worker on its own behalf, rather than one made directly by the visitor. The runtime then trusts the CF-Connecting-IP value it finds there instead of overwriting it with the worker's own IPv6 address.
Step 2: confirm the header on another worker
Pointing the sample at a second Cloudflare worker showed my own IP address arriving in CF-Connecting-IP, not the fixed IPv6 address normally set by the worker. This confirmed the header was being taken from the incoming request rather than generated by the platform.
Step 3: confirm it applies to ordinary Cloudflare-fronted sites
I repeated the test against the /cdn-cgi/trace magic route. The trace output showed my IP in CF-Connecting-IP for requests made by the worker, so the effect was not limited to Workers-to-Workers traffic: any site behind Cloudflare's CDN and WAF received the same spoofed value. Spoofing means presenting false information about yourself, here a false source address.
Step 4: vary the request body and query string
The behaviour persisted when the worker code modified query and POST parameters, provided it did not touch the request headers. In other words, the spoofing survives ordinary request rewriting; only header tampering breaks it.
Step 5: trigger it from a victim's browser
Because the worker only needs to receive a request from the victim, any request to my worker was enough, even one generated by an image embedded on a page. My worker could then make requests to any other site behind Cloudflare and present the victim's IP address as its own. The victim does nothing unusual: loading a page that happens to include the image is sufficient.
Step 6: cache per-IP responses
Some sites expose additional functionality to users from specific IP addresses. Using the Workers KV data store, I could cache the response to the sub-request so that the result obtained under a given spoofed IP could be checked afterwards. Workers KV is a simple key-value store: you save a value under a name and read it back later. Caching means keeping a copy so you do not have to fetch it again. Here it lets the response to the sub-request be cached so the result can be checked.
Root cause
Normally, when a visitor sends a request to a Cloudflare-protected site containing a CF-Connecting-IP header, Cloudflare drops the header and replaces it with the visitor's actual IP before forwarding to the origin. This stops anyone from simply writing a fake address into the header themselves.
When a Cloudflare worker makes a request to a Cloudflare-protected site, however, Cloudflare trusts the header value supplied by the worker and passes it on to the origin. The protection that exists for visitor traffic, stripping and rewriting the header at the edge, is not applied to the worker's own sub-requests.
The mistake is in how the runtime treats the CF-Connecting-IP header on worker sub-requests. The platform is meant to set that header itself and forbid worker code from modifying it, but when the incoming request object is reused as the Request init object, the visitor's headers are carried into the outgoing request and the runtime accepts the resulting CF-Connecting-IP value rather than replacing it.
Impact
An attacker who controls a Cloudflare worker could make requests to any other site behind Cloudflare while claiming the IP address of any visitor who requested the worker. The victim does not need to do anything beyond loading a page that references the worker, for example through an embedded image. Sites that grant extra functionality based on source IP address would then grant it to the attacker under the victim's identity. The report does not describe any specific site or feature that was abused this way; the KV caching technique is presented as a way to check for such functionality.
Fix
Cloudflare committed a fix and deployed it globally. The report does not describe the code change itself.
Variants to test
- Forward the incoming request as the
Requestinit object to a second worker and inspectCF-Connecting-IPthere. A positive result is the visitor's IP appearing instead of the worker's fixed IPv6 address. - Repeat against
/cdn-cgi/traceon a Cloudflare-fronted host. A positive result is the trace output showing the visitor's IP inCF-Connecting-IP. - Rewrite query and POST parameters in the worker before the sub-request, leaving headers untouched. A positive result is the spoofed header surviving the rewrite.
- Trigger the worker from an image embedded in a page and observe the sub-request at a Cloudflare-protected origin. A positive result is the origin seeing the browser visitor's IP rather than the worker's.
- Cache the sub-request response in Workers KV keyed by the spoofed IP. A positive result is being able to retrieve, after the fact, content that the origin only serves to that IP.
Timeline
- June 17, 2020 09:45 AM: reported to Cloudflare
- June 17, 2020 10:04 AM: acknowledged by Cloudflare
- June 19, 2020: bounty received from Cloudflare
- June 24, 2020: fix committed by Cloudflare and deployed globally
- January 5, 2021: published with approval of Cloudflare
Glossary
- CDN: a network of servers spread around the world that sit in front of a website to speed it up and absorb traffic.
- WAF: a filter that inspects incoming web requests and blocks ones that look malicious.
- Origin server: the machine that actually holds and serves a website, as opposed to the proxy in front of it.
- DNS: the system that maps domain names to IP addresses.
- Proxy: an intermediary that receives a request and passes it on to the real destination on your behalf.
- Header: a labelled line of extra information attached to a web request or response.
- IP address: the number that identifies a device on the internet.
- IPv6: the newer, longer form of IP address.
- Serverless: a way of running code where the provider manages the servers and you only supply the code.
- Runtime: the environment that executes code, here the v8 JavaScript engine.
- Event handler: a function that runs when a particular thing happens, such as a request arriving.
- Sub-request: a request made by a worker on its own behalf, rather than one made directly by the visitor.
- Magic route: a special URL answered by Cloudflare itself rather than passed to the website.
- Query parameter: the part of a URL after the
?that carries extra values. - POST parameter: data sent in the body of a form submission rather than in the URL.
- Spoofing: presenting false information about yourself, such as a false source address.
- Workers KV: a simple key-value data store where values are saved under names and read back later.
- Caching: keeping a copy of something so it does not have to be fetched again.
- Trust boundary: the line between two parts of a system that are expected to trust each other, and where that trust can be abused.
- Bounty: a payment made by a company to someone who reports a security problem to it.