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
- Build KeyObjects with the factory APIs: crypto.createSecretKey(data), crypto.createPublicKey(...), crypto.createPrivateKey(...)
- Never call new crypto.KeyObject() from application code; treat it as internal
- 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
- Treat new crypto.KeyObject() as off-limits in application code
- Always obtain KeyObjects from createSecretKey/createPublicKey/createPrivateKey or generateKeyPair
- Lint for 'new crypto.KeyObject' occurrences in CI
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
- ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE
- Can not create private key from ${type} key
- can not create secret key from ${type} key
- Unsupported KeyObject type for structured clone: ${data.keyT
- ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/1d194b85756c73d9.
Report an issue: GitHub.