iOfficeAI/OfficeCLI · error · ArgumentException

Could not resolve host '{host}'.

Error message

Could not resolve host '{host}'.

What it means

Thrown by the guarded ConnectCallback when DNS returned an empty address list for the host (addresses.FirstOrDefault() is null). It is a fallback: GetHostAddressesAsync usually throws outright on NXDOMAIN, but this guard catches the empty-result case so a no-address host produces a clear message rather than a NullReferenceException downstream.

Source

Thrown at src/officecli/Core/SsrfGuard.cs:54

    public static SocketsHttpHandler CreateGuardedHandler(string what)
    {
        return new SocketsHttpHandler
        {
            AllowAutoRedirect = true,
            MaxAutomaticRedirections = 10,
            ConnectCallback = async (ctx, ct) =>
            {
                var host = ctx.DnsEndPoint.Host;
                var addresses = await Dns.GetHostAddressesAsync(host, ct).ConfigureAwait(false);
                foreach (var addr in addresses)
                {
                    if (!IsPublicAddress(addr))
                        throw new ArgumentException(
                            $"Refusing to fetch {what} from non-public address '{addr}' (host '{host}'). " +
                            $"Remote {what} sources must resolve to a public IP (SSRF protection).");
                }
                var target = addresses.FirstOrDefault()
                    ?? throw new ArgumentException($"Could not resolve host '{host}'.");
                var socket = new Socket(SocketType.Stream, ProtocolType.Tcp) { NoDelay = true };
                try
                {
                    await socket.ConnectAsync(target, ctx.DnsEndPoint.Port, ct).ConfigureAwait(false);
                    return new NetworkStream(socket, ownsSocket: true);
                }
                catch
                {
                    socket.Dispose();
                    throw;
                }
            }
        };
    }

    /// <summary>
    /// Shared cap on a single remote fetch, to bound memory use on a hostile or
    /// accidentally-huge response. Without it the default HttpClient buffering

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Verify the hostname with `nslookup` / `dig` from the same machine.
  2. Correct the typo or use the fully-qualified domain name.
  3. Check network connectivity and DNS configuration.

Example fix

// before
picture = "https://exmaple.com/x.png"  // typo: exmaple
// after
picture = "https://example.com/x.png"
Defensive patterns

Strategy: try-catch

Validate before calling

async Task<bool> HostResolves(string url)
{
    try { return (await System.Net.Dns.GetHostAddressesAsync(new Uri(url).Host)).Length > 0; }
    catch { return false; }
}

Try / catch

try { bytes = await client.GetByteArrayAsync(url); }
catch (System.ArgumentException ex) when (ex.Message.Contains("Could not resolve host"))
{ /* hostname has no DNS records; fix the URL */ }
catch (System.Net.Sockets.SocketException) { /* NXDOMAIN / network error */ }

Prevention

When it happens

Trigger: An image=/file= URL whose hostname does not resolve (typo, non-existent domain), or DNS returned zero A/AAAA records.

Common situations: Typo in the hostname; DNS not configured / offline; the host is a short name that only resolves inside a specific network.

Understand the failure class

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/00494274af3ebaeb. Report an issue: GitHub.