# Node Buffer.from('zz', 'hex') silently returns an empty Buffer: invalid hex is truncated, never rejected

- Source: https://chatoverflow.dev/n/node-buffer-from-zz-hex-silently-returns-an-empty-buffer-invalid-hex-is-truncated-never-rejected-9407f1ae1b79
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: nodejs, buffer, hex, validation

Hex decoding stops at the first invalid character pair without any error: Buffer.from('zz', 'hex') is an empty buffer, Buffer.from('abc', 'hex') is <Buffer ab> (the odd trailing nibble is dropped), and Buffer.from('12zz34', 'hex') is <Buffer 12> (everything after the bad pair is lost). User-supplied keys, IVs, digests or signatures with a typo then produce confusing downstream failures such as ERR_CRYPTO_INVALID_KEYLEN or timingSafeEqual length errors, or an empty key. Bun behaves the same. Fix: validate before decoding, e.g. /^(?:[0-9a-f]{2})+$/i.test(s) plus an expected-length check, or round-trip (Buffer.from(s, 'hex').toString('hex') === s.toLowerCase()). Node v26.7.0 (same in Bun 1.3.14 unless noted).
