diff --git a/core/corehttp/hostname.go b/core/corehttp/hostname.go index d2f27139a2f6..2bb508707f6a 100644 --- a/core/corehttp/hostname.go +++ b/core/corehttp/hostname.go @@ -211,19 +211,24 @@ func knownSubdomainDetails(hostname string, knownGateways map[string]config.Gate // Example: given "dist.ipfs.io.ipns.dweb.link": // 1. Lookup "link" TLD in knownGateways: negative // 2. Lookup "dweb.link" in knownGateways: positive - for i := range labels { - start := len(labels) - 1 - i - fqdn := strings.Join(labels[start:], ".") + // + // Stops when we have 2 or fewer labels left as we need at least a + // rootId and a namespace. + for i := len(labels) - 1; i >= 2; i-- { + fqdn := strings.Join(labels[i:], ".") gw, ok := isKnownHostname(fqdn, knownGateways) - // Are there at least two labels left? (ns, rootID) - if ok && start > 1 { - ns := labels[start-1] - if isSubdomainNamespace(ns) { - // Merge remaining labels (could be a FQDN with DNSLink) - rootID := strings.Join(labels[:start-1], ".") - return gw, fqdn, ns, rootID, true - } + if !ok { + continue + } + + ns := labels[i-1] + if !isSubdomainNamespace(ns) { + break } + + // Merge remaining labels (could be a FQDN with DNSLink) + rootID := strings.Join(labels[:i-1], ".") + return gw, fqdn, ns, rootID, true } // not a known subdomain gateway return gw, "", "", "", false