nodejs/node · error · InvalidArgumentError

UND_ERR_INVALID_ARG

UND_ERR_INVALID_ARG

Error message

Domain name too long (max 255 bytes)

What it means

Thrown by the address-encoding helper in socks5-utils when a domain name encodes to more than 255 UTF-8 bytes. RFC 1928 encodes the domain length as a single byte, so the domain must be at most 255 bytes. This is a hard protocol limit on the target address.

Source

Thrown at deps/undici/src/lib/core/socks5-utils.js:33

    const parts = address.split('.').map(Number)
    return {
      type: 0x01, // IPv4
      buffer: Buffer.from(parts)
    }
  }

  // Check if it's an IPv6 address
  if (net.isIPv6(address)) {
    return {
      type: 0x04, // IPv6
      buffer: parseIPv6(address)
    }
  }

  // Otherwise, treat as domain name
  const domainBuffer = Buffer.from(address, 'utf8')
  if (domainBuffer.length > 255) {
    throw new InvalidArgumentError('Domain name too long (max 255 bytes)')
  }

  return {
    type: 0x03, // Domain
    buffer: Buffer.concat([Buffer.from([domainBuffer.length]), domainBuffer])
  }
}

/**
 * Parse IPv6 address to buffer
 * @param {string} address - IPv6 address string
 * @returns {Buffer} 16-byte buffer
 */
function parseIPv6 (address) {
  const buffer = Buffer.alloc(16)
  let normalizedAddress = address

  // Expand an embedded IPv4 tail into the last two IPv6 groups.

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Validate the target hostname length before calling connect.
  2. Fix the upstream code that produced an overlong address.
  3. Use Buffer.byteLength(address,'utf8') to check, since multibyte chars inflate byte length.

Example fix

// before
client.connect(veryLongHostname, port)

// after
if (Buffer.byteLength(host, 'utf8') > 255) throw new Error('host too long for SOCKS5 DOMAIN ATYP')
client.connect(host, port)
Defensive patterns

Strategy: validation

Validate before calling

function assertHostLength(host) {
  if (Buffer.byteLength(host, 'utf8') > 255) {
    throw new Error('host exceeds 255-byte SOCKS5 DOMAIN limit')
  }
}

Prevention

When it happens

Trigger: Passing an extremely long hostname as the connect target; a malformed or concatenated address string; an address that is not really a hostname but a long blob.

Common situations: Bug constructing the target address; SNI/host header accidentally passed as the address; a very long generated subdomain.

Related errors


AI-assisted analysis of nodejs/node@1b2de5e052 (2026-08-13). Data as JSON: /api/errors/99123471c83474f2. Report an issue: GitHub.