usebruno/bruno · error · TypeError

getRandomValues: Invalid typed array object

Error message

getRandomValues: Invalid typed array object

What it means

Thrown by deserializeTypedArray (QuickJS crypto shim utils) when the serialized value passed to crypto.getRandomValues is not a non-null object. The deserializer expects a shape like { type, array, length } produced by serializeTypedArray; anything else (null, undefined, number, string) fails this first guard.

Source

Thrown at packages/bruno-js/src/sandbox/quickjs/shims/lib/utils.js:26

function deserializeTypedArray(obj) {
  // Allowed typed array constructors for crypto operations
  const allowedConstructors = new Set([
    'Int8Array',
    'Uint8Array',
    'Uint8ClampedArray',
    'Int16Array',
    'Uint16Array',
    'Int32Array',
    'Uint32Array',
    'Float32Array',
    'Float64Array',
    'BigInt64Array',
    'BigUint64Array'
  ]);

  if (!obj || typeof obj !== 'object') {
    throw new TypeError('getRandomValues: Invalid typed array object');
  }

  if (typeof obj.type !== 'string' || !allowedConstructors.has(obj.type)) {
    throw new TypeError(`getRandomValues: Invalid or unsupported typed array type: ${obj.type}`);
  }

  if (!obj.array || typeof obj.length !== 'number') {
    throw new TypeError('getRandomValues: Invalid typed array properties');
  }

  const ctor = globalThis[obj.type];
  if (typeof ctor !== 'function') {
    throw new TypeError(`getRandomValues: Constructor ${obj.type} is not available`);
  }

  return new ctor(obj.array, 0, obj.length);
}

View on GitHub (pinned to 9bdd81c7bd)

Solutions

  1. Pass an actual typed array: `crypto.getRandomValues(new Uint8Array(16))`.
  2. Guard the call site: `if (!(arr instanceof Uint8Array)) throw new TypeError('arr must be a typed array')`.
  3. Initialize the variable before the call so it is never undefined.

Example fix

// before
let iv;
crypto.getRandomValues(iv); // iv is undefined

// after
const iv = new Uint8Array(12);
crypto.getRandomValues(iv);
Defensive patterns

Strategy: type-guard

Validate before calling

function fillRandom(v) {
  if (!(v && typeof v === 'object')) throw new TypeError('expected typed array');
  return crypto.getRandomValues(v);
}

Type guard

const isTypedArray = (v) => v != null && typeof v === 'object' && ArrayBuffer.isView(v) && !(v instanceof DataView);

Try / catch

try { crypto.getRandomValues(maybeArr); }
catch (err) {
  if (/Invalid typed array object/.test(err.message)) {
    crypto.getRandomValues(new Uint8Array(16));
  } else throw err;
}

Prevention

When it happens

Trigger: Calling crypto.getRandomValues(null), crypto.getRandomValues(undefined), crypto.getRandomValues(123), or crypto.getRandomValues('abc') inside a QuickJS-sandboxed script.

Common situations: A variable that was supposed to hold a typed array is undefined because an earlier step failed; a function default parameter leaked through; the wrong argument was passed (e.g. a plain number instead of a Uint8Array).

Related errors


AI-assisted analysis of usebruno/bruno@9bdd81c7bd (2026-08-13). Data as JSON: /api/errors/e7e4c0b2accadd3f. Report an issue: GitHub.