Skip to content

About

Cloudflare Worker alternative to acme-dns

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

acme-worker

A Cloudflare Worker that implements a minimal acme-dns-compatible /update endpoint, proxying DNS-01 challenge TXT record updates to the Cloudflare DNS API.

The runtime and DNS backend are Cloudflare-specific, but the /update endpoint implements the acme-dns protocol, making it compatible with any ACME client or tool that supports acme-dns.

How it works

Request flow

ACME client (Caddy, acme.sh, …)
  │
  │  POST /update  {subdomain, txt}
  │  X-Api-User: <ACME_USER>  X-Api-Key: <ACME_SECRET>
  ▼
Cloudflare Worker  (this repo)
  │  1. Rejects requests with wrong or missing X-Api-User/X-Api-Key → 401
  │  2. Strips the _acme-challenge. prefix and checks the bare
  │     domain against ACME_ALLOWED_DOMAINS → 403 if not listed
  │  3. Calls Cloudflare DNS API to upsert the TXT record
  ▼
Cloudflare DNS API
  │  Sets _acme-challenge.<domain> TXT = <token>
  ▼
CA (Let's Encrypt, ZeroSSL, …)
  Looks up _acme-challenge.<domain>, finds the token, issues the cert

The _acme-challenge. prefix is baked into the worker — ACME_ALLOWED_DOMAINS contains only the bare domain(s), keeping the allowlist readable.

Security model

Standard DNS-01 automation requires the ACME client to hold zone-wide DNS credentials, so a compromised web server also compromises the DNS zone. EFF's 2018 deep-dive covers the full scope of that exposure.

This worker reduces the credential scope on the client side: the ACME client holds only ACME_USER/ACME_SECRET, not the Cloudflare API token. A compromised web server can update _acme-challenge.* TXT records via the worker, but has no direct access to other record types. Worker secrets can be encrypted at rest where they cannot be read back through the Cloudflare dashboard or API, and in this case, exfiltration of those secrets would imply a breach of the Cloudflare platform itself.

Differences from acme-dns

acme-dns uses CNAME delegation: _acme-challenge.example.com is pointed at a subdomain on a dedicated acme-dns server, which runs its own authoritative DNS and never requires access to the primary zone. This worker writes TXT records directly into the Cloudflare zone via the API — no CNAME is required, but the Cloudflare token must have DNS edit access to the zone.

acme-dns issues per-domain credentials (one username/password pair per registered subdomain), so a leaked credential is scoped to a single entry. This worker uses a single ACME_USER/ACME_SECRET pair across all allowed domains, which is simpler to operate but means a leaked credential covers all domains in ACME_ALLOWED_DOMAINS.

The API shape is identical: both require X-Api-User and X-Api-Key. The username/password fields in client credential configs map to ACME_USER and ACME_SECRET respectively.

Setup

1. Install dependencies

npm install

2. Set secrets

wrangler secret put CF_API_TOKEN        # Cloudflare API token (Zone > DNS > Edit)
wrangler secret put CF_ZONE_ID          # Zone ID for your domain
wrangler secret put ACME_USER            # Username clients will send in X-Api-User
wrangler secret put ACME_SECRET         # Password clients will send in X-Api-Key
wrangler secret put ACME_ALLOWED_DOMAINS  # Comma-separated bare domains, e.g. example.com,sub.example.com

3. Deploy

wrangler deploy

Local development

Create a .dev.vars file (gitignored) with your secrets:

CF_API_TOKEN=your-api-token
CF_ZONE_ID=your-zone-id
ACME_USER=your-username
ACME_SECRET=your-shared-secret
ACME_ALLOWED_DOMAINS=example.com

Then run:

wrangler dev

API

POST /update

Headers:

Header Value
X-Api-User Must match ACME_USER
X-Api-Key Must match ACME_SECRET
Content-Type application/json

Body:

{
  "subdomain": "_acme-challenge.example.com",
  "txt": "<challenge-token>"
}

Responses:

Status Meaning
200 Success — {"txt": "<token>"}
400 Missing or empty txt field
401 Invalid or missing X-Api-User or X-Api-Key
403 subdomain not in ACME_ALLOWED_DOMAINS
404 Route not found
502 Cloudflare DNS API error

Testing

curl -X POST https://<worker-url>/update \
  -H "X-Api-User: <your-username>" \
  -H "X-Api-Key: <your-secret>" \
  -H "Content-Type: application/json" \
  -d '{"subdomain":"_acme-challenge.example.com","txt":"testtoken123"}'

Expected response:

{"txt":"testtoken123"}

Verify the TXT record appears in the Cloudflare DNS dashboard before configuring a client.

Client configuration

Any acme-dns-compatible client can be pointed at this worker's URL. Examples:

Caddy

{
  acme_dns acmedns https://<worker-url>
}

With a credential block:

{
  "username": "<ACME_USER>",
  "password": "<ACME_SECRET>",
  "subdomain": "_acme-challenge.example.com",
  "server_url": "https://<worker-url>",
  "fulldomain": "_acme-challenge.example.com",
  "allowfrom": []
}

### acme.sh

```bash
export ACMEDNS_UPDATE_URL="https://<worker-url>/update"
acme.sh --issue --dns dns_acmedns -d example.com

Certbot (certbot-dns-acmedns)

Point api in your acmedns.json credentials file at https://<worker-url>.

About

Cloudflare Worker alternative to acme-dns

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages