Buffer.alloc() and Buffer.allocUnsafe()

The two ways to create a new Buffer of a given size: alloc() fills it with zeros, while allocUnsafe() skips that step and may contain old data.

Sending or logging an allocUnsafe buffer before every byte is overwritten can leak passwords or keys from earlier memory.

In more detail

allocUnsafe() is faster, but its bytes may be left over from memory used earlier, including secrets. Use it only when you overwrite every byte immediately, and use alloc() everywhere else.

Related terms

Taught in

References