denoland/deno · error · TypeError

ERR_INVALID_ARG_VALUE

ERR_INVALID_ARG_VALUE

Error message

The argument 'type' is invalid. Received ${inspected}

What it means

KeyObject's constructor only accepts the internal type strings 'secret', 'public', and 'private'. It is an internal API meant to be called by the crypto factories; constructing crypto.KeyObject directly with any other type string (e.g. an algorithm name like 'rsa') throws ERR_INVALID_ARG_VALUE for 'type'.

Source

Thrown at ext/node/polyfills/internal/crypto/keys.ts:157

        buffer,
      );
    }
    return buffer;
  },
);

const kConsumePublic = 0;
const kConsumePrivate = 1;
const kCreatePublic = 2;
const kCreatePrivate = 3;

class KeyObject {
  [kKeyType]: any;
  [kHandle]: any;

  constructor(type: any, handle: any) {
    if (type !== "secret" && type !== "public" && type !== "private") {
      throw new ERR_INVALID_ARG_VALUE("type", type);
    }

    if (typeof handle !== "object") {
      throw new ERR_INVALID_ARG_TYPE("handle", "object", handle);
    }

    this[kKeyType] = type;
    this[kHandle] = handle;
  }

  get type(): any {
    return this[kKeyType];
  }

  static from(key: CryptoKey): KeyObject {
    if (!isCryptoKey(key)) {
      throw new ERR_INVALID_ARG_TYPE("key", "CryptoKey", key);
    }

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Build KeyObjects with the factory APIs: crypto.createSecretKey(data), crypto.createPublicKey(...), crypto.createPrivateKey(...)
  2. Never call new crypto.KeyObject() from application code; treat it as internal
  3. If you only have raw bytes, createSecretKey(bytes) gives a valid secret KeyObject

Example fix

// before
const k = new crypto.KeyObject('secret', rawBytes);
// after
const k = crypto.createSecretKey(rawBytes);
Defensive patterns

Strategy: type-guard

Type guard

const KEY_OBJECT_TYPES = ['secret', 'public', 'private'];
function isKeyObjectType(t) {
  return KEY_OBJECT_TYPES.includes(t);
}

Prevention

When it happens

Trigger: new crypto.KeyObject('rsa', handle) or new crypto.KeyObject('Secret', handle) — application code calling the constructor directly instead of using the factory APIs.

Common situations: Attempting to hand-fabricate a KeyObject around raw key data; porting code from libraries that poke at Node internals; assuming the constructor validates algorithm names.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/1d194b85756c73d9. Report an issue: GitHub.