gchq/CyberChef · error · Error

Protobuf input must be a byteArray or Uint8Array

Error message

Protobuf input must be a byteArray or Uint8Array

What it means

Thrown by the Protobuf constructor when `data` is neither a plain Array nor a Uint8Array. It is a plain `Error` (not OperationError), so callers must catch it themselves. Note that ArrayBuffer, TypedArrays other than Uint8Array, and Buffer (Node) will fail this check.

Source

Thrown at src/core/lib/Protobuf.mjs:27

 * integers (varint).
 *
 * @author GCHQ Contributor [3]
 * @copyright Crown Copyright 2019
 * @license Apache-2.0
 */
class Protobuf {

    /**
     * Protobuf constructor
     *
     * @param {byteArray|Uint8Array} data
     */
    constructor(data) {
        // Check we have a byteArray or Uint8Array
        if (data instanceof Array || data instanceof Uint8Array) {
            this.data = data;
        } else {
            throw new Error("Protobuf input must be a byteArray or Uint8Array");
        }

        // Set up masks
        this.TYPE = 0x07;
        this.NUMBER = 0x78;
        this.MSB = 0x80;
        this.VALUE = 0x7f;

        // Declare offset, length, and field type object
        this.offset = 0;
        this.LENGTH = data.length;
        this.fieldTypes = {};
    }

    // Public Functions

    /**
     * Encode a varint from a number

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Convert to a Uint8Array before construction: `new Protobuf(new Uint8Array(arrayBuffer))`.
  2. If you have a byte list, ensure it is a true Array of 0-255 integers.
  3. For Node Buffers, pass `new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength)`.

Example fix

// before
const pb = new Protobuf(arrayBuffer);
// after
const pb = new Protobuf(Array.from(new Uint8Array(arrayBuffer)));
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(data instanceof Array) && !(data instanceof Uint8Array)) {
  data = new Uint8Array(data instanceof ArrayBuffer ? data : new TextEncoder().encode(String(data)));
}
const pb = new Protobuf(data);

Type guard

function isByteArrayOrUint8Array(v) {
  return Array.isArray(v) || (v instanceof Uint8Array);
}

Try / catch

try {
  return new Protobuf(data);
} catch (e) {
  if (/must be a byteArray or Uint8Array/.test(e.message)) {
    return new Protobuf(new Uint8Array(data));
  }
  throw e;
}

Prevention

When it happens

Trigger: Constructing `new Protobuf(data)` with a string, number, plain object, ArrayBuffer, Int8Array/Float32Array, or null/undefined. Also a Node Buffer is not a Uint8Array in some environments and can fail.

Common situations: Passing an ArrayBuffer from a FileReader without wrapping in a Uint8Array view; passing raw string input from a recipe; feeding a JSON-parsed object; receiving data in a different TypedArray variant.

Related errors


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/b52d6c8b55c83e76. Report an issue: GitHub.