{"record":{"id":"1b4efeefd52c18a3","repo":"gchq/CyberChef","slug":"badly-formatted-ipv6-address","errorCode":null,"errorMessage":"Badly formatted IPv6 address.","messagePattern":"Badly formatted IPv6 address\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/lib/IP.mjs","lineNumber":370,"sourceCode":"        ipv6 = new Array(8);\n\n    for (let i = 0; i < 8; i++) {\n        if (isNaN(numBlocks[j])) {\n            ipv6[i] = 0;\n            if (i === (8-numBlocks.slice(j).length)) j++;\n        } else {\n            ipv6[i] = numBlocks[j];\n            j++;\n        }\n    }\n    return ipv6;\n\n    /**\n     * Converts a list of 3-8 numeric hex strings in the range 0-65535 to a list of numbers.\n     */\n    function parseBlocks(blocks) {\n        if (blocks.length < 3 || blocks.length > 8)\n            throw new OperationError(\"Badly formatted IPv6 address.\");\n        const numBlocks = [];\n        for (let i = 0; i < blocks.length; i++) {\n            numBlocks[i] = parseInt(blocks[i], 16);\n            if (numBlocks[i] < 0 || numBlocks[i] > 65535)\n                throw new OperationError(\"Block out of range.\");\n        }\n        return numBlocks;\n    }\n}\n\n/**\n * Converts an IPv6 address from numerical array format to string format.\n *\n * @param {number[]} ipv6\n * @param {boolean} compact - Whether or not to return the address in shorthand or not\n * @returns {string}\n *\n * @example","sourceCodeStart":352,"sourceCodeEnd":388,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/lib/IP.mjs#L352-L388","documentation":"Thrown by strToIpv6's inner parseBlocks when `ipStr.split(\":\")` yields fewer than 3 or more than 8 parts. Valid IPv6 text (including a single '::' compression) splits to 3-8 colon-separated groups; anything outside that is malformed. OperationError.","triggerScenarios":"Calling strToIpv6('ff00') (1 block), '1:2:3:4:5:6:7:8:9' (9 blocks), or an IPv4 address mistakenly passed in. Note 'ff00::' splits to ['ff00','',''] = 3 and is accepted.","commonSituations":"Missing colons; too many groups (>8); IPv4 address routed to the IPv6 parser; an address missing the '::' compression entirely.","solutions":["Validate the string against an IPv6 regex before calling strToIpv6.","Ensure 3-8 colon-separated groups after splitting.","Route IPv4 input to strToIpv4."],"exampleFix":"// before\nstrToIpv6(\"ff00\");\n\n// after\nstrToIpv6(\"ff00::\");","handlingStrategy":"validation","validationCode":"function assertIpv6Shape(ipStr) {\n  const blocks = ipStr.split(\":\");\n  if (blocks.length < 3 || blocks.length > 8) {\n    throw new Error(`IPv6 must split into 3-8 colon groups, got ${blocks.length}`);\n  }\n}\nassertIpv6Shape(ipStr);\nstrToIpv6(ipStr);","typeGuard":"const isIpv6Shape = ipStr => {\n  const n = ipStr.split(\":\").length;\n  return n >= 3 && n <= 8;\n};","tryCatchPattern":"try {\n  strToIpv6(ipStr);\n} catch (err) {\n  if (err instanceof OperationError && /Badly formatted IPv6 address/.test(err.message)) {\n    // wrong number of colon groups; correct the input\n  } else throw err;\n}","preventionTips":["Validate with an IPv6 regex before parsing.","Remember '::' compression is what allows 3-8 groups; a bare single group fails.","Route IPv4 input to strToIpv4."],"tags":["ip","ipv6","validation","parsing"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}