diff --git a/README.md b/README.md index a22c337..cdd9499 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ If you want one update to refresh both an exact record and a wildcard companion, ## Various setup paths - [docs/cloudflare-setup.md](docs/cloudflare-setup.md): easiest setup path for most users - [docs/synology-setup.md](docs/synology-setup.md): step-by-step DSM walkthrough with screenshots +- [docs/security-model.md](docs/security-model.md): current trust model, limitations, and hardening options - [Advanced local setup](#advanced-local-setup): local clone, Wrangler, and explicit D1 workflows - [JSON API](#json-api): script and automation usage @@ -60,6 +61,7 @@ If you want one update to refresh both an exact record and a wildcard companion, - JSON API for scripts and automation (`POST /update` with OpenAPI docs) - IPv4 (A) and IPv6 (AAAA) support - Optional D1-backed audit log with scheduled cleanup +- Default per-IP rate limiting for update endpoints - Hostname allowlist to limit what records can be changed - Configurable proxied/DNS-only mode and TTL @@ -73,6 +75,10 @@ The easiest path is: If you are not using Synology and just want the API, deploy the Worker first and then use the JSON example in [JSON API](#json-api). +The default deploy path also enables a fixed-window rate limit of 10 update requests per minute per client IP. That is meant to slow down bad password guessing, runaway retry loops, and damage from a leaked secret without requiring extra Cloudflare bindings. + +If you need to understand what this setup does and does not protect, read [docs/security-model.md](docs/security-model.md). The short version is that this worker is designed for trusted clients you control, not for strong per-device identity or zero-trust exposure on its own. + ## Advanced local deploy Use this path if you want to deploy manually, need explicit local control over Wrangler, or want to manage D1 from your own machine. @@ -179,10 +185,23 @@ These non-secret variables live in `wrangler.jsonc` and can be overridden per-en | Variable | Default | Description | |---|---|---| -| `DDNS_ALLOWED_HOSTNAMES` | none | Required. Comma-separated hostnames this worker may update, e.g. `nas.example.com,home.example.com`. Wildcard companions such as `*.nas.example.com` are supported as explicit entries. | +| `DDNS_ALLOWED_HOSTNAMES` | `nas.example.com,*.nas.example.com` | Comma-separated hostnames this worker may update. Replace the default with your real hostname list before production use. Wildcard companions such as `*.nas.example.com` are supported as explicit entries. | | `DDNS_PROXIED` | `"false"` | Whether DNS records are proxied through Cloudflare. Most NAS setups need `"false"` (DNS-only) for direct IP access on non-standard ports. | | `DDNS_TTL` | `"1"` | DNS record TTL in seconds. `"1"` means automatic. Valid range: 60-86400. | | `DDNS_LOG_RETENTION_DAYS` | `"30"` | How many days of update logs to keep in D1. A cron job runs every 6 hours to prune older rows. | +| `DDNS_RATE_LIMIT_MAX_REQUESTS` | `"10"` | Maximum number of update requests allowed per client IP within the current fixed window. Set to `"0"` to disable rate limiting. | +| `DDNS_RATE_LIMIT_WINDOW_SECONDS` | `"60"` | Fixed-window size for DDNS rate limiting, in seconds. | + +## Shared secret rotation + +If you think `DDNS_SHARED_SECRET` was exposed, rotate it immediately: + +1. Generate a new random secret. +2. Update `DDNS_SHARED_SECRET` in Cloudflare. +3. Update every caller that stores the old secret, including DSM and any scripts using `X-DDNS-Secret`. +4. Trigger a manual test request and confirm the worker returns `good ` or `nochg `. + +If you use the D1 audit log, review recent rows for unusual request volume, repeated authentication failures, or unexpected update sources. The built-in per-IP rate limit reduces how fast a leaked secret can be abused, but it does not replace rotating the secret. ## Using the worker @@ -210,6 +229,8 @@ Then add a DDNS entry: DSM will call the worker whenever it detects an IP change. The worker responds with `good ` or `nochg ` on success. +If DSM or another client starts retrying too quickly, the worker now returns HTTP `429` with a `Retry-After` header. The Synology-compatible body stays `911` so the client treats it as a retryable server-side problem. + If you want one request for `nas.example.com` to also update `*.nas.example.com`, include both in `DDNS_ALLOWED_HOSTNAMES`, for example `nas.example.com,*.nas.example.com`. The worker treats the wildcard entry as a second managed DNS record and updates both records together. ### JSON API @@ -227,6 +248,8 @@ Wildcard records work the same way here: if `DDNS_ALLOWED_HOSTNAMES` contains bo OpenAPI documentation is served at the worker's root URL (`/`). +If a client IP exceeds the default fixed-window rate limit, `POST /update` returns HTTP `429` with a JSON error body and a `Retry-After` header. + ### Health check ``` diff --git a/docs/security-model.md b/docs/security-model.md new file mode 100644 index 0000000..c3839ba --- /dev/null +++ b/docs/security-model.md @@ -0,0 +1,99 @@ +# Security model + +This worker gives Synology DSM and simple scripts a way to update Cloudflare DNS records without storing your Cloudflare API token on every client. That is the main security win: clients only know a DDNS-specific shared secret, while the worker keeps the real Cloudflare credentials on the server side. + +The tradeoff is that the current design uses bearer-secret authentication, not per-client identity. If a caller knows the shared secret and asks to update an allowed hostname, the worker treats that caller as authorized. + +This document explains what the current implementation protects, and where its limits are, and what hardening steps make sense if you need a tighter security model. + +## What the worker protects today + +The current design is meant for a small number of clients you control, such as one Synology NAS and a few scripts. It narrows the blast radius compared to giving those clients direct Cloudflare DNS credentials. + +At a high level, each update request flows through these checks: + +```text +Client + | + | shared secret + hostname + optional IP override + v +Worker + | + | 1. authenticate shared secret + | 2. require hostname in DDNS_ALLOWED_HOSTNAMES + | 3. apply per-client-IP rate limit + | 4. update only the matching DNS records in this zone + v +Cloudflare DNS API + +Optional side path: +Worker -> D1 audit log / rate-limit state +``` + +Those checks give you a few concrete protections: + +- Clients never receive your Cloudflare API token or zone-wide DNS permissions. +- The hostname allowlist limits updates to names you explicitly approved in `DDNS_ALLOWED_HOSTNAMES`. +- The built-in fixed-window rate limit slows down password guessing and runaway retry loops from a single client IP. +- The optional D1 audit log gives you a record of update attempts and outcomes for later review. + +## Where the current design is intentionally weak + +The main limit is that `DDNS_SHARED_SECRET` is a bearer secret. Possession is enough. There is no second factor, no per-device secret, and no policy tied to the Synology `username` field. + +That leads to a few practical consequences: + +- All clients share the same trust level. If one client leaks the secret, every allowed hostname protected by that secret is at risk. +- The Synology-compatible `GET /nic/update` endpoint carries the secret in the `password` query parameter because DSM expects a DynDNS2-style URL. Query-string secrets are easier to leak through screenshots, copied URLs, client-side logs, proxy logs, and similar tooling than header-based secrets. +- The worker accepts a valid caller-supplied IP override: `myip` on `GET /nic/update` and `ip` on `POST /update`. If the secret leaks, an attacker can point an allowed hostname at another IP address. The worker falls back to `CF-Connecting-IP` only when the supplied override is missing or invalid. +- The DSM `username` field is currently ignored. It exists for compatibility, not authorization, so it does not contain or scope access. + +This means the worker is a good fit for trusted clients under one operator, but it is not the same thing as strong client identity. + +## What the current controls are good at + +For the intended use case, the current controls still solve real problems. + +- Shared-secret auth is simple enough for Synology DSM and other basic HTTP clients. +- Hostname allowlisting prevents a compromised client from editing arbitrary records across the zone. +- Rate limiting reduces the speed of abuse when the secret is guessed or leaked. +- Secret rotation is simple: change `DDNS_SHARED_SECRET`, update the clients you control, and test again. + +For many home-lab and small-operator setups, that is an acceptable balance between compatibility and containment. + +## What to do if the secret is exposed + +Treat a leaked `DDNS_SHARED_SECRET` like a leaked password with zone-specific impact. + +1. Generate a new long random secret. +2. Update `DDNS_SHARED_SECRET` in Cloudflare. +3. Update every client that stores it, including DSM and any scripts using `X-DDNS-Secret`. +4. Review recent DDNS activity if you enabled D1 logging. +5. Run a manual update and confirm the worker returns a normal success response again. + +The built-in rate limit helps slow abuse, but it does not remove the need to rotate the secret. + +## Hardening options if you need more than this + +If your threat model is stronger than "trusted clients I control," the next improvements are fairly clear. + +### Safer default for non-Synology clients: use the JSON API + +If you control the caller and do not need Synology's legacy DynDNS2 shape, prefer `POST /update` with `X-DDNS-Secret`. A header-based secret is still a bearer secret, but it avoids putting the secret in the URL. + +### Stronger operator controls + +Depending on the client, you can also add more layers around the worker: + +- richer audit logging such as caller IP, user agent, or Cloudflare Ray ID +- Cloudflare WAF or custom rules in front of the worker +- a separate stronger-auth path for non-Synology automation clients +- mutual TLS (mTLS) or client certificates for environments that can support them + +These are useful additions, but they are not drop-in replacements for Synology DSM compatibility. + +## Bottom line + +This project currently offers constrained shared-secret authentication, hostname scoping, rate limiting, and optional audit logging. That is a meaningful improvement over handing every DDNS client full Cloudflare DNS credentials. + +It does not offer strong per-device identity, and it does not guarantee that updates always reflect the caller's own network IP. If you need those guarantees, treat the current worker as a compatibility-focused base and plan for stronger client-specific authentication on top. \ No newline at end of file diff --git a/docs/synology-setup.md b/docs/synology-setup.md index d048640..28056b2 100644 --- a/docs/synology-setup.md +++ b/docs/synology-setup.md @@ -12,6 +12,8 @@ Before you start, make sure the worker is already configured with: If you have not finished the Cloudflare side yet, go to [cloudflare-setup.md](./cloudflare-setup.md) first. +If you want the security tradeoffs behind this setup, read [security-model.md](./security-model.md). The short version is that DSM compatibility uses a shared secret, not a per-device identity model. + ## What you need in front of you - Your Synology NAS DSM web interface @@ -112,4 +114,12 @@ That makes one request for `nas.example.com` update both DNS records. ### DSM keeps retrying after setup -Open the worker logs in Cloudflare and check whether the response is `badauth`, `nohost`, or `911`. Those map to bad password, disallowed hostname, or a server-side problem. \ No newline at end of file +Open the worker logs in Cloudflare and check whether the response is `badauth`, `nohost`, or `911`. Those map to bad password, disallowed hostname, or a server-side problem. + +### I think the shared secret leaked + +Create a new `DDNS_SHARED_SECRET` in Cloudflare first, then come back to DSM and replace the password in the DDNS entry with the new value. + +After saving the new password, run the DSM test again. The old secret should stop working immediately once the Worker is using the new one. + +If the Worker starts returning HTTP `429` with a `911` body, the built-in rate limit has probably slowed down repeated requests from the same client IP. Wait for the `Retry-After` period to pass, then test again with the new secret. \ No newline at end of file