Skip to content

Fix: tunnel route dns creates CNAME in wrong zone when user has multiple domains#1610

Open
StringerBell69 wants to merge 1 commit intocloudflare:masterfrom
StringerBell69:fix-dns-route-zone-resolution
Open

Fix: tunnel route dns creates CNAME in wrong zone when user has multiple domains#1610
StringerBell69 wants to merge 1 commit intocloudflare:masterfrom
StringerBell69:fix-dns-route-zone-resolution

Conversation

@StringerBell69
Copy link

Description

This PR fixes a bug in cloudflared tunnel route dns where the CLI would incorrectly create a CNAME record in the wrong zone if the account manages multiple domains.

The Issue

Previously, the RouteTunnel function blindly sent the route PUT request using the ZoneID embedded in the cert.pem upon login. If a user was logged in with cert.pem tied to domain-A.com, but ran cloudflared tunnel route dns <tunnel> app.domain-B.com, the API would attempt to create the DNS record inside the domain-A.com namespace (e.g. creating app.domain-B.com.domain-A.com).

The Fix

  1. Introduced ListZones() in cfapi to correctly fetch all authorized zones attached to the user's account (leveraging fetchExhaustively to avoid missing pagination records).
  2. Modified RouteTunnel to perform an in-memory suffix match (strings.HasSuffix(route.Hostname(), "."+zone.Name) || route.Hostname() == zone.Name) against the verified zones list from the API.
  3. Once the matching zone is discovered, it dynamically constructs the correct endpoint (/zones/<ZoneID>/tunnels/<id>/routes) instead of falling back to the login certificate's default zone.

Added comprehensive automated unit tests covering the exact matching, subdomain suffix mapping, and fallback logic if a zone fails to map.

When users have multiple domains, tunnel route dns would incorrectly use the default zoneID from the login certificate, creating an invalid CNAME record (e.g. app.domain1.com.domain2.com).

This fix introduces ListZones in cfapi to fetch all valid zones for the account and explicitly checks if the provided hostname exactly matches or is a subdomain of a discovered zone, preventing this behavior and dynamically adjusting the endpoint to the correct Zone ID.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant