gchq/CyberChef · error · OperationError
Badly formatted IPv6 address.
Error message
Badly formatted IPv6 address.
What it means
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.
Source
Thrown at src/core/lib/IP.mjs:370
ipv6 = new Array(8);
for (let i = 0; i < 8; i++) {
if (isNaN(numBlocks[j])) {
ipv6[i] = 0;
if (i === (8-numBlocks.slice(j).length)) j++;
} else {
ipv6[i] = numBlocks[j];
j++;
}
}
return ipv6;
/**
* Converts a list of 3-8 numeric hex strings in the range 0-65535 to a list of numbers.
*/
function parseBlocks(blocks) {
if (blocks.length < 3 || blocks.length > 8)
throw new OperationError("Badly formatted IPv6 address.");
const numBlocks = [];
for (let i = 0; i < blocks.length; i++) {
numBlocks[i] = parseInt(blocks[i], 16);
if (numBlocks[i] < 0 || numBlocks[i] > 65535)
throw new OperationError("Block out of range.");
}
return numBlocks;
}
}
/**
* Converts an IPv6 address from numerical array format to string format.
*
* @param {number[]} ipv6
* @param {boolean} compact - Whether or not to return the address in shorthand or not
* @returns {string}
*
* @exampleView on GitHub (pinned to 4290ea7539)
Solutions
- Validate the string against an IPv6 regex before calling strToIpv6.
- Ensure 3-8 colon-separated groups after splitting.
- Route IPv4 input to strToIpv4.
Example fix
// before
strToIpv6("ff00");
// after
strToIpv6("ff00::"); Defensive patterns
Strategy: validation
Validate before calling
function assertIpv6Shape(ipStr) {
const blocks = ipStr.split(":");
if (blocks.length < 3 || blocks.length > 8) {
throw new Error(`IPv6 must split into 3-8 colon groups, got ${blocks.length}`);
}
}
assertIpv6Shape(ipStr);
strToIpv6(ipStr); Type guard
const isIpv6Shape = ipStr => {
const n = ipStr.split(":").length;
return n >= 3 && n <= 8;
}; Try / catch
try {
strToIpv6(ipStr);
} catch (err) {
if (err instanceof OperationError && /Badly formatted IPv6 address/.test(err.message)) {
// wrong number of colon groups; correct the input
} else throw err;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Missing colons; too many groups (>8); IPv4 address routed to the IPv6 parser; an address missing the '::' compression entirely.
Related errors
- IPv6 CIDR must be less than 128
- More than 4 blocks.
- Invalid input. Enter either a CIDR range (e.g. 10.0.0.0/24)
- Invalid IPv6 address
- Byte length must be a positive integer
AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13).
Data as JSON: /api/errors/1b4efeefd52c18a3.
Report an issue: GitHub.