gchq/CyberChef · error · OperationError
Invalid encoding
Error message
Invalid encoding
What it means
Thrown by Decode Text run() when the selected encoding name does not resolve to a code page id in the CHR_ENC_CODE_PAGES map. The args are nominally constrained by the operation's option list, so through the UI this is effectively unreachable; it surfaces only when the operation is invoked programmatically with an encoding string not present in the registry.
Source
Thrown at src/core/operations/DecodeText.mjs:53
this.outputType = "string";
this.args = [
{
"name": "Encoding",
"type": "option",
"value": Object.keys(CHR_ENC_CODE_PAGES)
}
];
}
/**
* @param {ArrayBuffer} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const format = CHR_ENC_CODE_PAGES[args[0]];
if (!format) {
throw new OperationError("Invalid encoding");
}
return cptable.utils.decode(format, new Uint8Array(input));
}
}
export default DecodeText;
View on GitHub (pinned to 4290ea7539)
Solutions
- Use an exact encoding name from Object.keys(CHR_ENC_CODE_PAGES) (case-sensitive, e.g. 'UTF-8' not 'utf8').
- If building a recipe dynamically, validate the encoding against the registry before invoking.
- Regenerate the recipe in the current build so the option value matches the registry.
Example fix
// before
op.run(input, [{0: "utf8"}]);
// or
op.run(input, ["utf8"]); // not in registry
// after
op.run(input, ["UTF-8"]); // exact registry key Defensive patterns
Strategy: validation
Validate before calling
import { CHR_ENC_CODE_PAGES } from "src/core/lib/ChrEnc.mjs";
function isSupportedEncoding(name) {
return Object.prototype.hasOwnProperty.call(CHR_ENC_CODE_PAGES, name);
} Type guard
/** @returns {boolean} */
function isValidCodePage(name) {
return typeof name === "string"
&& Object.prototype.hasOwnProperty.call(CHR_ENC_CODE_PAGES, name);
} Try / catch
try {
out = decodeText.run(input, args);
} catch (e) {
if (e instanceof OperationError && e.message === "Invalid encoding") {
// pick a default (e.g. 'UTF-8') or surface supported list
args[0] = "UTF-8";
out = decodeText.run(input, args);
} else throw e;
} Prevention
- Use exact encoding names from Object.keys(CHR_ENC_CODE_PAGES) (case-sensitive).
- When invoking programmatically, validate the encoding against the registry first.
- Regenerate imported recipes in the current build to align option values.
- Default to 'UTF-8' as a safe fallback when the source encoding is unknown.
When it happens
Trigger: Calling run() directly with args[0] set to a string that is not a key of CHR_ENC_CODE_PAGES - e.g. 'utf-8' instead of 'UTF-8', a typo, an unsupported legacy code page name, or undefined after an arg mapping mistake.
Common situations: Programmatic/Node-API invocation passing a casing-mismatched or unregistered encoding name; a recipe imported with a stale encoding name removed in a newer build; an automation script hardcoding a charset string.
Related errors
- Invalid Base64 alphabet length (${alphabet.length}): ${alpha
- Byte length must be a positive integer
- unexpected character encountered: "${character}"
- Invalid 'Diff by' option.
- Invalid mode value
AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13).
Data as JSON: /api/errors/a3a7e312668ba9c3.
Report an issue: GitHub.