{"record":{"id":"c88192f88b9c47b5","repo":"honojs/hono","slug":"err-invalid-ip-address","errorCode":"ERR_INVALID_IP_ADDRESS","errorMessage":"Invalid IPv4 address: ${ipv4}","messagePattern":"Invalid IPv4 address: (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/utils/ipaddr.ts","lineNumber":77,"sourceCode":" */\nexport const distinctRemoteAddr = (remoteAddr: string): AddressType => {\n  if (IPV4_REGEX.test(remoteAddr)) {\n    return 'IPv4'\n  }\n  if (remoteAddr.includes(':')) {\n    // Domain can't include `:`\n    return 'IPv6'\n  }\n}\n\nconst createInvalidIPAddressError = (message: string): InvalidIPAddressError => {\n  const error = new TypeError(message) as InvalidIPAddressError\n  error.code = INVALID_IP_ADDRESS_ERROR_CODE\n  return error\n}\n\nconst throwInvalidIPv4Address = (ipv4: string): never => {\n  throw createInvalidIPAddressError(`Invalid IPv4 address: ${ipv4}`)\n}\n\nconst throwInvalidIPv6Address = (ipv6: string): never => {\n  throw createInvalidIPAddressError(`Invalid IPv6 address: ${ipv6}`)\n}\n\nconst parseIPv4ToBinary = (\n  ipv4: string,\n  start: number,\n  end: number,\n  onInvalid: () => never\n): bigint => {\n  let result = 0n\n  let octets = 0\n  let octet = 0\n  let digits = 0\n  let firstDigit = 0\n","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/honojs/hono/blob/e2740d5a1bd0b4254e517e3af8b60789284bc7bd/src/utils/ipaddr.ts#L59-L95","documentation":"Hono's ipaddr utility throws a TypeError (code ERR_INVALID_IP_ADDRESS) when a string passed to convertIPv4ToBinary cannot be parsed as a dotted-quad IPv4 address. It is used by CIDR/IP matchers, so malformed IPv4 inputs (wrong octet count, out-of-range octets, non-numeric characters) surface here.","triggerScenarios":"Calling convertIPv4ToBinary (directly or via CIDR matching in ipRestriction / range utilities) with strings like '1.2.3', '1.2.3.4.5', '256.1.1.1', '1.2.3.abc', or an IPv6 string accidentally passed to the IPv4 path.","commonSituations":"Feeding user-supplied or header-derived strings (x-forwarded-for entries) into CIDR matchers without validation; config typos in allowRules/denyRules such as '192.168.1' or '192.168.1.256'; passing a hostname instead of an IP.","solutions":["Validate with a regex or net.isIP-style check before converting","Trim/split x-forwarded-for and pick the first well-formed entry","Fix the typo in your configured IP/CIDR rules","Branch on ':' in the address to route IPv6 strings to convertIPv6ToBinary"],"exampleFix":"// before\nconst bin = convertIPv4ToBinary(addr) // throws on '1.2.3.4.5'\n\n// after\nconst IPV4_RE = /^(25[0-5]|2[0-4]\\d|1?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|1?\\d?\\d)){3}$/\nif (IPV4_RE.test(addr)) {\n  const bin = convertIPv4ToBinary(addr)\n} else {\n  // handle invalid address\n}","handlingStrategy":"type-guard","validationCode":"const IPV4_RE = /^(25[0-5]|2[0-4]\\d|1?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|1?\\d?\\d)){3}$/\nif (IPV4_RE.test(addr)) convertIPv4ToBinary(addr)","typeGuard":"const isIPv4 = (s: string): boolean =>\n  /^((25[0-5]|2[0-4]\\d|1?\\d?\\d)\\.){3}(25[0-5]|2[0-4]\\d|1?\\d?\\d)$/.test(s.trim())","tryCatchPattern":"try {\n  return convertIPv4ToBinary(addr)\n} catch (e) {\n  if (e instanceof TypeError && (e as InvalidIPAddressError).code === 'ERR_INVALID_IP_ADDRESS') {\n    // treat as invalid input: skip or reject\n    return null\n  }\n  throw e\n}","preventionTips":["Validate IP strings before CIDR/binary conversion","Lint your configured IP rules for typos","Split x-forwarded-for and take the first valid entry"],"tags":["hono","ipv4","ipaddr","typeerror","validation"],"backgroundTag":"invalid-ip-address","analyzedSha":"e2740d5a1bd0b4254e517e3af8b60789284bc7bd","analyzedAt":"2026-08-28T10:18:08.750Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}