# Take over subdomains of r2.dev using R2 custom domains

Unverified zone could be added as R2 custom domain, letting attacker take over any r2.dev subdomain and serve their own bucket contents.

- Payout: $1,125
- Severity: medium
- Program: Cloudflare
- Found by: albertspedersen
- Disclosed: 2022-09-28
- Write-up quality: 7/10
- Original: https://hackerone.com/reports/1700276
- Library page: https://bugbountylibrary.com/r/h1%3A1700276
- Tags: area:cloud domain:cloud impact:dos impact:integrity kind:bounty platform:hackerone sev:medium tech:cdn technique:recon vuln:access-control vuln:misconfiguration vuln:subdomain-takeover

**Target:** Cloudflare R2 custom domains / SSL for SaaS custom hostname API
**Root cause:** API did not verify zone ownership/active status before adding the domain as an SSL for SaaS custom hostname, and *.r2.dev already CNAMEs to the fallback origin.
**Steps:**
1. Add `r2.dev` to your Cloudflare account, stop at zone ownership verification.
2. Use mitmproxy script to flip `workers.r2_publicbuckets` flag client-side in `/api/v4/accounts/[0-9a-f]{32}/flags` response.
3. Create R2 bucket, add `albert.r2.dev` as custom domain in Domain Access.
4. Wait, refresh; status becomes Active; enable domain if 'Not allowed'.
5. Visit `albert.r2.dev` and observe it serves your bucket contents.
**How found:** Researcher noticed R2 public buckets feature gated by a client-side flag; removed server check let them access UI, then observed custom domain became Active without completing zone ownership verification, hypothesizing the API lacked zone-active validation.
**Exploitation:** Attacker adds target zone (e.g. r2.dev) without completing ownership verification, then registers a subdomain as an R2 custom domain; because the API skips zone-active check and *.r2.dev already points to the fallback origin, the subdomain resolves to attacker's bucket. Also usable to block legitimate owners by pre-registering their domain.
**Escalation:** Same technique can block a victim's domain from being used as R2 custom domain (denial) and hijack existing pub-<id>.r2.dev bucket subdomains.
**Insight:** When a feature is gated by a dashboard flag, toggling it client-side often reveals backend endpoints that skip validation; also test whether SaaS custom-hostname APIs validate zone ownership before activation, especially on shared wildcard zones.

## Proof of concept (copied exactly from the original report)

**mitmproxy script toggling R2 public buckets flag**

```
import json
import mitmproxy
import re


class R2PublicBuckets:
	async def response(self, flow: mitmproxy.http.HTTPFlow):
		if re.match(r'https?://dash\.cloudflare\.com/api/v4/accounts/[0-9a-f]{32}/flags', flow.request.url):
			data = json.loads(flow.response.text)
			data['result']['workers']['r2_publicbuckets'] = True
			flow.response.text = json.dumps(data, separators=(',', ':'))

addons = [
	R2PublicBuckets()
]
```

**Steps to add albert.r2.dev custom domain**

```
1. Add `r2.dev` to your Cloudflare account and follow the steps until you're asked to complete zone ownership verification.

2. Create an R2 bucket if you don't already have one and add e.g. `albert.r2.dev` as a custom domain in the "Domain Access" section.
```

