diff --git a/docs/contracts/class-a-dynu.md b/docs/contracts/class-a-dynu.md new file mode 100644 index 0000000..9e2786c --- /dev/null +++ b/docs/contracts/class-a-dynu.md @@ -0,0 +1,83 @@ +# Dynu DDNS API Contract Specification + +> [!NOTE] +> This document details the technical contract for Dynu's Dynamic DNS update endpoint. +> All findings below reflect empirical verification against Dynu production endpoints +> on October 29, 2026, executed via [`tests/contracts/test_dynu_contract.py`](file:///home/kiskaadee/Projects/active/ddns-monitor/tests/contracts/test_dynu_contract.py). + +--- + +## 1. Remote Interface & Transport + +* **Category:** Class A (Dedicated HTTP DDNS Protocol) +* **Base Endpoint:** `GET https://api.dynu.com/nic/update` +* **Transport:** HTTPS (Port 443) mandatory for credential safety; HTTP (Port 80) available for embedded/legacy devices. +* **Authentication Mechanisms:** + 1. *Query String Parameter:* `?password=` (plaintext or MD5/SHA256 hash). + 2. *HTTP Basic Authentication:* `Authorization: Basic base64(username:password)`. +* **Primary Query Parameters:** + * `hostname`: Fully qualified domain name (e.g., `node.dynu.net` or custom domain). + * `myip`: Target IPv4 address. + * `myipv6`: Target IPv6 address (or `no` to disable IPv6). + * `password`: IP Update Password or account password when not using HTTP Basic Auth. + * `offline`: `yes` marks the record as parked/offline. + +--- + +## 2. Empirical Verification: Claims vs. Reality + +Testing against live Dynu endpoints revealed several deviations from standard DynDNS2 specifications and generic documentation: + +| Dimension / Test Case | Standard Claim | Observed Reality (HTTP 200 OK) | Classification | +|---|---|---|---| +| **Empty / Unauthenticated Request** | Returns `badauth` or `401 Unauthorized` | Returns string **`unknown`** | **DISCREPANCY** | +| **Invalid Password (IP Mismatch)** | Returns `badauth` | Returns string **`badauth`** | **CONFIRMED** | +| **Invalid Password (IP Match)** | Returns `badauth` | Short-circuits to **`nochg`** without checking password | **DISCREPANCY** | +| **Nonexistent Hostname (Query Auth)** | Returns `nohost` | Returns string **`badauth`** | **DISCREPANCY** | +| **Successful Idempotent Update** | Returns `nochg ` | Returns bare string **`nochg`** | **DISCREPANCY** | +| **RFC 5737 Test IP (`198.51.100.42`)** | Sets record to test IP | Silently ignored; falls back to caller's WAN IP | **DISCREPANCY** | +| **Authoritative DNS Reflection** | Matches updated IP | Verified on `ns1.dynu.com` | **CONFIRMED** | + +--- + +## 3. Behavioral Mechanisms & Analysis + +### IP Match Short-Circuiting (The Idempotency Bypass) +If an update request arrives from an IP address that already matches the domain's existing `A` record, Dynu's gateway short-circuits the request and immediately returns `nochg` **before validating credentials**. +* Consequence: Sending a bogus password for a domain that already resolves to your public IP returns `nochg`, not `badauth`. +* Authentication (`badauth`) is only enforced when the requested update would actually mutate the record (or when targeting another user's domain whose IP differs). + +### Why Nonexistent Hostnames return `badauth` +Under query parameter authentication (`?hostname=&password=

`), Dynu uses the `hostname` parameter to locate the corresponding user account and fetch the registered IP Update Password hash. If the hostname does not exist on Dynu's system, credential resolution fails *before* zone authorization occurs, causing the API to return `badauth` rather than `nohost`. + +### Bare `nochg` Syntax +Standard DynDNS2 documentation dictates that idempotent updates return `nochg `. Dynu's server implementation frequently outputs only `nochg` with no following IP address. Upstream clients that expect an IP token following `nochg` fail to parse valid no-op responses. + +### Bogon / Documentation IP Filtering +Dynu's edge gateway actively inspects the `myip` parameter. Non-routable addresses, bogons, and RFC 5737 documentation networks (`198.51.100.0/24`) are discarded. Rather than returning an error status, Dynu discards the parameter and defaults to the connection's public source IPv4 address. If the existing record already matches the source IP, Dynu returns `nochg`. + +--- + +## 4. Adapter Implementation Requirements (Rust Engine) + +The Dynu plugin ([`src/plugins/dynu.rs`](file:///home/kiskaadee/Projects/active/ddns-monitor/src/plugins/dynu.rs)) must observe the following contract invariants: + +1. **Flexible `nochg` Matching:** + Do not split response tokens expecting `nochg `. Treat any response starting with `nochg` as an idempotent success: + ```rust + if body.starts_with("good") || body.starts_with("nochg") { + // Valid state reconciliation + } + ``` +2. **Error Handling for `unknown`:** + Account for `unknown` as an unauthenticated or missing-parameter error condition distinct from `badauth`. +3. **Pre-flight IP Validation:** + The `ddns-monitor` core must validate public routability (filtering RFC 1918, RFC 5737, and loopback addresses) prior to invoking provider plugins, preventing Dynu's silent fallback behavior. + +--- + +## 5. Untested Surfaces & Open Questions + +- [ ] **Dual-Stack IPv6 Behavior:** Does supplying `myipv6` alongside `myip` update both `A` and `AAAA` records atomically, and does omitting `myipv6` preserve existing `AAAA` records? +- [ ] **HTTP Basic Auth Semantics:** Does authenticating via HTTP Basic Auth (`Authorization: Basic ...`) return `nohost` for nonexistent domains instead of `badauth`? +- [ ] **Abuse Rate Limiting:** What request frequency threshold triggers the `abuse` status code? diff --git a/tests/contracts/test_dynu_contract.py b/tests/contracts/test_dynu_contract.py new file mode 100644 index 0000000..43f8409 --- /dev/null +++ b/tests/contracts/test_dynu_contract.py @@ -0,0 +1,120 @@ +import time +import urllib.error +import urllib.parse +import urllib.request + +import dns.exception +import dns.resolver + +DYNU_UPDATE_URL = "https://api.dynu.com/nic/update" + +def _query_dynu( + params: dict[str, str], + headers: dict[str, str] | None = None, + timeout: float = 10.0, +): + query_string = urllib.parse.urlencode(params) + url = f"{DYNU_UPDATE_URL}?{query_string}" if params else DYNU_UPDATE_URL + req = urllib.request.Request(url, headers=headers or {}) + try: + with urllib.request.urlopen(req, timeout=timeout) as resp: + body = resp.read().decode("utf-8", errors="replace").strip() + return resp.status, body + except urllib.error.HTTPError as e: + body = e.read().decode("utf-8", errors="replace").strip() + return e.code, body + +class TestDynuContract: + """Empirical verification of the Dynu Class A DDNS API contract.""" + + def test_reachability(self): + """Observed: Empty/unauthenticated call returns 200 OK with 'unknown' (not badauth).""" + status, body = _query_dynu({}) + assert status == 200, f"Expected HTTP 200, got {status} with body: {body}" + assert body.startswith("unknown"), f"Expected body to start with 'unknown', got: {body}" + + def test_bad_credentials_on_foreign_host(self): + """Observed: Invalid credentials for host whose IP differs from caller returns badauth.""" + params = { + "hostname": "test.dynu.net", + "password": "definitely_invalid_password_probe_12345", + } + status, body = _query_dynu(params) + assert status == 200, f"Expected HTTP 200, got {status}" + assert body.startswith("badauth"), f"Expected 'badauth', got: {body}" + + def test_bad_credentials_short_circuit_on_matching_ip(self, dynu_config): + """ + Observed: If caller's WAN IP already matches the domain's A record, Dynu + short-circuits and returns 'nochg' without checking password validity. + """ + params = { + "hostname": dynu_config["hostname"], + "password": "definitely_invalid_password_probe_12345", + } + status, body = _query_dynu(params) + assert status == 200, f"Expected HTTP 200, got {status}" + assert body.startswith("nochg"), ( + f"Expected short-circuit 'nochg' when IP matches, got: {body}" + ) + + def test_nonexistent_hostname(self, dynu_config): + """ + Observed: Under query auth (?password=...), nonexistent hostnames return 'badauth' + instead of 'nohost' because Dynu uses the hostname to resolve the credential hash. + """ + params = { + "hostname": "nonexistent-test-node-unowned-12345.dynu.net", + "myip": dynu_config["test_ipv4"], + "password": dynu_config["password"], + } + status, body = _query_dynu(params) + assert status == 200, f"Expected HTTP 200, got {status}" + assert body.startswith("badauth"), ( + f"Expected 'badauth' under query authentication for unowned host, got: {body}" + ) + + def test_valid_update_and_idempotency(self, dynu_config): + """ + Observed: + 1. Updating returns 'good' or 'nochg'. + 2. Idempotent repetition returns bare 'nochg' (without an IP address appended). + """ + target_ip = dynu_config["test_ipv4"] + params = { + "hostname": dynu_config["hostname"], + "myip": target_ip, + "password": dynu_config["password"], + } + + # Step 1: Initial update + status, body = _query_dynu(params) + assert status == 200, f"Expected HTTP 200, got {status}" + assert body.startswith(("good", "nochg")), ( + f"Expected 'good' or 'nochg', got: {body}" + ) + + # Safety pause to prevent abuse flagging + time.sleep(2.0) + + # Step 2: Idempotent repetition + status2, body2 = _query_dynu(params) + assert status2 == 200, f"Expected HTTP 200, got {status2}" + assert body2.startswith("nochg"), ( + f"Expected idempotent 'nochg' on second request, got: {body2}" + ) + + def test_authoritative_dns_propagation(self, dynu_config): + """Observed: Authoritative nameservers answer for the configured hostname.""" + hostname = dynu_config["hostname"] + + resolver = dns.resolver.Resolver() + try: + ns_ips = [str(rdata) for rdata in dns.resolver.resolve("ns1.dynu.com", "A")] + resolver.nameservers = ns_ips + except (dns.exception.DNSException, OSError): + pass # fallback to default configured nameserver if ns1.dynu.com resolution fails + + answers = resolver.resolve(hostname, "A") + resolved_ips = [str(rdata) for rdata in answers] + assert len(resolved_ips) > 0, f"Expected at least one A record for {hostname}"