\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18586}{}\doxysubsubsection{Safer Node.\+js Buffer API}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18586}
$\ast$$\ast$\+Use the new Node.\+js Buffer APIs ({\ttfamily Buffer.\+from}, {\ttfamily Buffer.\+alloc}, {\ttfamily Buffer.\+alloc\+Unsafe}, {\ttfamily Buffer.\+alloc\+Unsafe\+Slow}) in all versions of Node.\+js.$\ast$$\ast$

{\bfseries{Uses the built-\/in implementation when available.}}\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18587}{}\doxysection{install}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18587}

\begin{DoxyCode}{0}
\DoxyCodeLine{npm install safe-\/buffer }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18588}{}\doxysection{usage}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18588}
The goal of this package is to provide a safe replacement for the node.\+js {\ttfamily Buffer}.

It\textquotesingle{}s a drop-\/in replacement for {\ttfamily Buffer}. You can use it by adding one {\ttfamily require} line to the top of your node.\+js modules\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var Buffer = require('safe-\/buffer').Buffer}
\DoxyCodeLine{}
\DoxyCodeLine{// Existing buffer code will continue to work without issues:}
\DoxyCodeLine{}
\DoxyCodeLine{new Buffer('hey', 'utf8')}
\DoxyCodeLine{new Buffer([1, 2, 3], 'utf8')}
\DoxyCodeLine{new Buffer(obj)}
\DoxyCodeLine{new Buffer(16) // create an uninitialized buffer (potentially unsafe)}
\DoxyCodeLine{}
\DoxyCodeLine{// But you can use these new explicit APIs to make clear what you want:}
\DoxyCodeLine{}
\DoxyCodeLine{Buffer.from('hey', 'utf8') // convert from many types to a Buffer}
\DoxyCodeLine{Buffer.alloc(16) // create a zero-\/filled buffer (safe)}
\DoxyCodeLine{Buffer.allocUnsafe(16) // create an uninitialized buffer (potentially unsafe) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18589}{}\doxysection{api}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18589}
\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18590}{}\doxysubsection{Class Method\+: Buffer.\+from(array)}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18590}

\begin{DoxyItemize}
\item {\ttfamily array} \{Array\}
\end{DoxyItemize}

Allocates a new {\ttfamily Buffer} using an {\ttfamily array} of octets.


\begin{DoxyCode}{0}
\DoxyCodeLine{const buf = Buffer.from([0x62,0x75,0x66,0x66,0x65,0x72]);}
\DoxyCodeLine{  // creates a new Buffer containing ASCII bytes}
\DoxyCodeLine{  // ['b','u','f','f','e','r'] }

\end{DoxyCode}


A {\ttfamily Type\+Error} will be thrown if {\ttfamily array} is not an {\ttfamily Array}.\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18591}{}\doxysubsection{Class Method\+: Buffer.\+from(array\+Buffer\mbox{[}, byte\+Offset\mbox{[}, length\mbox{]}\mbox{]})}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18591}

\begin{DoxyItemize}
\item {\ttfamily array\+Buffer} \{Array\+Buffer\} The {\ttfamily .buffer} property of a {\ttfamily Typed\+Array} or a {\ttfamily new Array\+Buffer()}
\item {\ttfamily byte\+Offset} \{Number\} Default\+: {\ttfamily 0}
\item {\ttfamily length} \{Number\} Default\+: {\ttfamily array\+Buffer.\+length -\/ byte\+Offset}
\end{DoxyItemize}

When passed a reference to the {\ttfamily .buffer} property of a {\ttfamily Typed\+Array} instance, the newly created {\ttfamily Buffer} will share the same allocated memory as the Typed\+Array.


\begin{DoxyCode}{0}
\DoxyCodeLine{const arr = new Uint16Array(2);}
\DoxyCodeLine{arr[0] = 5000;}
\DoxyCodeLine{arr[1] = 4000;}
\DoxyCodeLine{}
\DoxyCodeLine{const buf = Buffer.from(arr.buffer); // shares the memory with arr;}
\DoxyCodeLine{}
\DoxyCodeLine{console.log(buf);}
\DoxyCodeLine{  // Prints: <Buffer 88 13 a0 0f>}
\DoxyCodeLine{}
\DoxyCodeLine{// changing the TypedArray changes the Buffer also}
\DoxyCodeLine{arr[1] = 6000;}
\DoxyCodeLine{}
\DoxyCodeLine{console.log(buf);}
\DoxyCodeLine{  // Prints: <Buffer 88 13 70 17> }

\end{DoxyCode}


The optional {\ttfamily byte\+Offset} and {\ttfamily length} arguments specify a memory range within the {\ttfamily array\+Buffer} that will be shared by the {\ttfamily Buffer}.


\begin{DoxyCode}{0}
\DoxyCodeLine{const ab = new ArrayBuffer(10);}
\DoxyCodeLine{const buf = Buffer.from(ab, 0, 2);}
\DoxyCodeLine{console.log(buf.length);}
\DoxyCodeLine{  // Prints: 2 }

\end{DoxyCode}


A {\ttfamily Type\+Error} will be thrown if {\ttfamily array\+Buffer} is not an {\ttfamily Array\+Buffer}.\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18592}{}\doxysubsection{Class Method\+: Buffer.\+from(buffer)}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18592}

\begin{DoxyItemize}
\item {\ttfamily buffer} \{Buffer\}
\end{DoxyItemize}

Copies the passed {\ttfamily buffer} data onto a new {\ttfamily Buffer} instance.


\begin{DoxyCode}{0}
\DoxyCodeLine{const buf1 = Buffer.from('buffer');}
\DoxyCodeLine{const buf2 = Buffer.from(buf1);}
\DoxyCodeLine{}
\DoxyCodeLine{buf1[0] = 0x61;}
\DoxyCodeLine{console.log(buf1.toString());}
\DoxyCodeLine{  // 'auffer'}
\DoxyCodeLine{console.log(buf2.toString());}
\DoxyCodeLine{  // 'buffer' (copy is not changed) }

\end{DoxyCode}


A {\ttfamily Type\+Error} will be thrown if {\ttfamily buffer} is not a {\ttfamily Buffer}.\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18593}{}\doxysubsection{Class Method\+: Buffer.\+from(str\mbox{[}, encoding\mbox{]})}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18593}

\begin{DoxyItemize}
\item {\ttfamily str} \{String\} String to encode.
\item {\ttfamily encoding} \{String\} Encoding to use, Default\+: `\textquotesingle{}utf8'\`{}
\end{DoxyItemize}

Creates a new {\ttfamily Buffer} containing the given Java\+Script string {\ttfamily str}. If provided, the {\ttfamily encoding} parameter identifies the character encoding. If not provided, {\ttfamily encoding} defaults to `\textquotesingle{}utf8'\`{}.


\begin{DoxyCode}{0}
\DoxyCodeLine{const buf1 = Buffer.from('this is a tést');}
\DoxyCodeLine{console.log(buf1.toString());}
\DoxyCodeLine{  // prints: this is a tést}
\DoxyCodeLine{console.log(buf1.toString('ascii'));}
\DoxyCodeLine{  // prints: this is a tC)st}
\DoxyCodeLine{}
\DoxyCodeLine{const buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex');}
\DoxyCodeLine{console.log(buf2.toString());}
\DoxyCodeLine{  // prints: this is a tést }

\end{DoxyCode}


A {\ttfamily Type\+Error} will be thrown if {\ttfamily str} is not a string.\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18594}{}\doxysubsection{Class Method\+: Buffer.\+alloc(size\mbox{[}, fill\mbox{[}, encoding\mbox{]}\mbox{]})}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18594}

\begin{DoxyItemize}
\item {\ttfamily size} \{Number\}
\item {\ttfamily fill} \{Value\} Default\+: {\ttfamily undefined}
\item {\ttfamily encoding} \{String\} Default\+: {\ttfamily utf8}
\end{DoxyItemize}

Allocates a new {\ttfamily Buffer} of {\ttfamily size} bytes. If {\ttfamily fill} is {\ttfamily undefined}, the {\ttfamily Buffer} will be {\itshape zero-\/filled}.


\begin{DoxyCode}{0}
\DoxyCodeLine{const buf = Buffer.alloc(5);}
\DoxyCodeLine{console.log(buf);}
\DoxyCodeLine{  // <Buffer 00 00 00 00 00> }

\end{DoxyCode}


The {\ttfamily size} must be less than or equal to the value of `require(\textquotesingle{}buffer').k\+Max\+Length{\ttfamily (on 64-\/bit architectures,}k\+Max\+Length{\ttfamily is }(2$^\wedge$31)-\/1{\ttfamily ). Otherwise, a \mbox{[}}Range\+Error{\ttfamily \mbox{]}\mbox{[}\mbox{]} is thrown. A zero-\/length Buffer will be created if a}size\`{} less than or equal to 0 is specified.

If {\ttfamily fill} is specified, the allocated {\ttfamily Buffer} will be initialized by calling {\ttfamily buf.\+fill(fill)}. See \mbox{[}{\ttfamily buf.\+fill()}\mbox{]}\mbox{[}\mbox{]} for more information.


\begin{DoxyCode}{0}
\DoxyCodeLine{const buf = Buffer.alloc(5, 'a');}
\DoxyCodeLine{console.log(buf);}
\DoxyCodeLine{  // <Buffer 61 61 61 61 61> }

\end{DoxyCode}


If both {\ttfamily fill} and {\ttfamily encoding} are specified, the allocated {\ttfamily Buffer} will be initialized by calling {\ttfamily buf.\+fill(fill, encoding)}. For example\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');}
\DoxyCodeLine{console.log(buf);}
\DoxyCodeLine{  // <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64> }

\end{DoxyCode}


Calling {\ttfamily Buffer.\+alloc(size)} can be significantly slower than the alternative {\ttfamily Buffer.\+alloc\+Unsafe(size)} but ensures that the newly created {\ttfamily Buffer} instance contents will {\itshape never contain sensitive data}.

A {\ttfamily Type\+Error} will be thrown if {\ttfamily size} is not a number.\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18595}{}\doxysubsection{Class Method\+: Buffer.\+alloc\+Unsafe(size)}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18595}

\begin{DoxyItemize}
\item {\ttfamily size} \{Number\}
\end{DoxyItemize}

Allocates a new {\itshape non-\/zero-\/filled} {\ttfamily Buffer} of {\ttfamily size} bytes. The {\ttfamily size} must be less than or equal to the value of `require(\textquotesingle{}buffer').k\+Max\+Length{\ttfamily (on 64-\/bit architectures,}k\+Max\+Length{\ttfamily is}(2$^\wedge$31)-\/1{\ttfamily ). Otherwise, a \mbox{[}}Range\+Error{\ttfamily \mbox{]}\mbox{[}\mbox{]} is thrown. A zero-\/length Buffer will be created if a}size\`{} less than or equal to 0 is specified.

The underlying memory for {\ttfamily Buffer} instances created in this way is {\itshape not initialized}. The contents of the newly created {\ttfamily Buffer} are unknown and {\itshape may contain sensitive data}. Use \mbox{[}{\ttfamily buf.\+fill(0)}\mbox{]}\mbox{[}\mbox{]} to initialize such {\ttfamily Buffer} instances to zeroes.


\begin{DoxyCode}{0}
\DoxyCodeLine{const buf = Buffer.allocUnsafe(5);}
\DoxyCodeLine{console.log(buf);}
\DoxyCodeLine{  // <Buffer 78 e0 82 02 01>}
\DoxyCodeLine{  // (octets will be different, every time)}
\DoxyCodeLine{buf.fill(0);}
\DoxyCodeLine{console.log(buf);}
\DoxyCodeLine{  // <Buffer 00 00 00 00 00> }

\end{DoxyCode}


A {\ttfamily Type\+Error} will be thrown if {\ttfamily size} is not a number.

Note that the {\ttfamily Buffer} module pre-\/allocates an internal {\ttfamily Buffer} instance of size {\ttfamily Buffer.\+pool\+Size} that is used as a pool for the fast allocation of new {\ttfamily Buffer} instances created using {\ttfamily Buffer.\+alloc\+Unsafe(size)} (and the deprecated {\ttfamily new Buffer(size)} constructor) only when {\ttfamily size} is less than or equal to {\ttfamily Buffer.\+pool\+Size $>$$>$ 1} (floor of {\ttfamily Buffer.\+pool\+Size} divided by two). The default value of {\ttfamily Buffer.\+pool\+Size} is {\ttfamily 8192} but can be modified.

Use of this pre-\/allocated internal memory pool is a key difference between calling {\ttfamily Buffer.\+alloc(size, fill)} vs. {\ttfamily Buffer.\+alloc\+Unsafe(size).fill(fill)}. Specifically, {\ttfamily Buffer.\+alloc(size, fill)} will {\itshape never} use the internal Buffer pool, while {\ttfamily Buffer.\+alloc\+Unsafe(size).fill(fill)} {\itshape will} use the internal Buffer pool if {\ttfamily size} is less than or equal to half {\ttfamily Buffer.\+pool\+Size}. The difference is subtle but can be important when an application requires the additional performance that {\ttfamily Buffer.\+alloc\+Unsafe(size)} provides.\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18596}{}\doxysubsection{Class Method\+: Buffer.\+alloc\+Unsafe\+Slow(size)}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18596}

\begin{DoxyItemize}
\item {\ttfamily size} \{Number\}
\end{DoxyItemize}

Allocates a new {\itshape non-\/zero-\/filled} and non-\/pooled {\ttfamily Buffer} of {\ttfamily size} bytes. The {\ttfamily size} must be less than or equal to the value of `require(\textquotesingle{}buffer').k\+Max\+Length{\ttfamily (on 64-\/bit architectures,}k\+Max\+Length{\ttfamily is }(2$^\wedge$31)-\/1{\ttfamily ). Otherwise, a \mbox{[}}Range\+Error{\ttfamily \mbox{]}\mbox{[}\mbox{]} is thrown. A zero-\/length Buffer will be created if a}size\`{} less than or equal to 0 is specified.

The underlying memory for {\ttfamily Buffer} instances created in this way is {\itshape not initialized}. The contents of the newly created {\ttfamily Buffer} are unknown and {\itshape may contain sensitive data}. Use \mbox{[}{\ttfamily buf.\+fill(0)}\mbox{]}\mbox{[}\mbox{]} to initialize such {\ttfamily Buffer} instances to zeroes.

When using {\ttfamily Buffer.\+alloc\+Unsafe()} to allocate new {\ttfamily Buffer} instances, allocations under 4KB are, by default, sliced from a single pre-\/allocated {\ttfamily Buffer}. This allows applications to avoid the garbage collection overhead of creating many individually allocated Buffers. This approach improves both performance and memory usage by eliminating the need to track and cleanup as many {\ttfamily Persistent} objects.

However, in the case where a developer may need to retain a small chunk of memory from a pool for an indeterminate amount of time, it may be appropriate to create an un-\/pooled Buffer instance using {\ttfamily Buffer.\+alloc\+Unsafe\+Slow()} then copy out the relevant bits.


\begin{DoxyCode}{0}
\DoxyCodeLine{// need to keep around a few small chunks of memory}
\DoxyCodeLine{const store = [];}
\DoxyCodeLine{}
\DoxyCodeLine{socket.on('readable', () => \{}
\DoxyCodeLine{  const data = socket.read();}
\DoxyCodeLine{  // allocate for retained data}
\DoxyCodeLine{  const sb = Buffer.allocUnsafeSlow(10);}
\DoxyCodeLine{  // copy the data into the new allocation}
\DoxyCodeLine{  data.copy(sb, 0, 0, 10);}
\DoxyCodeLine{  store.push(sb);}
\DoxyCodeLine{\}); }

\end{DoxyCode}


Use of {\ttfamily Buffer.\+alloc\+Unsafe\+Slow()} should be used only as a last resort {\itshape after} a developer has observed undue memory retention in their applications.

A {\ttfamily Type\+Error} will be thrown if {\ttfamily size} is not a number.\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18597}{}\doxysubsection{All the Rest}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18597}
The rest of the {\ttfamily Buffer} API is exactly the same as in node.\+js. \href{https://nodejs.org/api/buffer.html}{\texttt{ See the docs}}.\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18598}{}\doxysection{Related links}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18598}

\begin{DoxyItemize}
\item \href{https://github.com/nodejs/node/issues/4660}{\texttt{ Node.\+js issue\+: Buffer(number) is unsafe}}
\item \href{https://github.com/nodejs/node-eps/pull/4}{\texttt{ Node.\+js Enhancement Proposal\+: Buffer.\+from/\+Buffer.alloc/\+Buffer.\+zalloc/\+Buffer() soft-\/deprecate}}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18599}{}\doxysection{Why is $<$tt$>$\+Buffer$<$/tt$>$ unsafe?}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18599}
Today, the node.\+js {\ttfamily Buffer} constructor is overloaded to handle many different argument types like {\ttfamily String}, {\ttfamily Array}, {\ttfamily Object}, {\ttfamily Typed\+Array\+View} ({\ttfamily Uint8\+Array}, etc.), {\ttfamily Array\+Buffer}, and also {\ttfamily Number}.

The API is optimized for convenience\+: you can throw any type at it, and it will try to do what you want.

Because the Buffer constructor is so powerful, you often see code like this\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{// Convert UTF-\/8 strings to hex}
\DoxyCodeLine{function toHex (str) \{}
\DoxyCodeLine{  return new Buffer(str).toString('hex')}
\DoxyCodeLine{\} }

\end{DoxyCode}


{\itshape {\bfseries{But what happens if {\ttfamily to\+Hex} is called with a {\ttfamily Number} argument?}}}\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18600}{}\doxysubsection{Remote Memory Disclosure}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18600}
If an attacker can make your program call the {\ttfamily Buffer} constructor with a {\ttfamily Number} argument, then they can make it allocate uninitialized memory from the node.\+js process. This could potentially disclose TLS private keys, user data, or database passwords.

When the {\ttfamily Buffer} constructor is passed a {\ttfamily Number} argument, it returns an {\bfseries{UNINITIALIZED}} block of memory of the specified {\ttfamily size}. When you create a {\ttfamily Buffer} like this, you {\bfseries{MUST}} overwrite the contents before returning it to the user.

From the \href{https://nodejs.org/api/buffer.html\#buffer_new_buffer_size}{\texttt{ node.\+js docs}}\+:

\begin{quote}
{\ttfamily new Buffer(size)}


\begin{DoxyItemize}
\item {\ttfamily size} Number
\end{DoxyItemize}

The underlying memory for {\ttfamily Buffer} instances created in this way is not initialized. {\bfseries{The contents of a newly created {\ttfamily Buffer} are unknown and could contain sensitive data.}} Use {\ttfamily buf.\+fill(0)} to initialize a Buffer to zeroes. \end{quote}
(Emphasis our own.)

Whenever the programmer intended to create an uninitialized {\ttfamily Buffer} you often see code like this\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var buf = new Buffer(16)}
\DoxyCodeLine{}
\DoxyCodeLine{// Immediately overwrite the uninitialized buffer with data from another buffer}
\DoxyCodeLine{for (var i = 0; i < buf.length; i++) \{}
\DoxyCodeLine{  buf[i] = otherBuf[i]}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18601}{}\doxysubsection{Would this ever be a problem in real code?}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18601}
Yes. It\textquotesingle{}s surprisingly common to forget to check the type of your variables in a dynamically-\/typed language like Java\+Script.

Usually the consequences of assuming the wrong type is that your program crashes with an uncaught exception. But the failure mode for forgetting to check the type of arguments to the {\ttfamily Buffer} constructor is more catastrophic.

Here\textquotesingle{}s an example of a vulnerable service that takes a JSON payload and converts it to hex\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{// Take a JSON payload \{str: "{}some string"{}\} and convert it to hex}
\DoxyCodeLine{var server = http.createServer(function (req, res) \{}
\DoxyCodeLine{  var data = ''}
\DoxyCodeLine{  req.setEncoding('utf8')}
\DoxyCodeLine{  req.on('data', function (chunk) \{}
\DoxyCodeLine{    data += chunk}
\DoxyCodeLine{  \})}
\DoxyCodeLine{  req.on('end', function () \{}
\DoxyCodeLine{    var body = JSON.parse(data)}
\DoxyCodeLine{    res.end(new Buffer(body.str).toString('hex'))}
\DoxyCodeLine{  \})}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{server.listen(8080) }

\end{DoxyCode}


In this example, an http client just has to send\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  "{}str"{}: 1000}
\DoxyCodeLine{\} }

\end{DoxyCode}


and it will get back 1,000 bytes of uninitialized memory from the server.

This is a very serious bug. It\textquotesingle{}s similar in severity to the \href{http://heartbleed.com/}{\texttt{ the Heartbleed bug}} that allowed disclosure of Open\+SSL process memory by remote attackers.\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18602}{}\doxysubsection{Which real-\/world packages were vulnerable?}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18602}
\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18603}{}\doxysubsubsection{$<$a href=\char`\"{}https\+://www.\+npmjs.\+com/package/bittorrent-\/dht\char`\"{}$>$$<$tt$>$bittorrent-\/dht$<$/tt$>$$<$/a$>$}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18603}
\href{https://github.com/mafintosh}{\texttt{ Mathias Buus}} and I (\href{http://feross.org/}{\texttt{ Feross Aboukhadijeh}}) found this issue in one of our own packages, \href{https://www.npmjs.com/package/bittorrent-dht}{\texttt{ {\ttfamily bittorrent-\/dht}}}. The bug would allow anyone on the internet to send a series of messages to a user of {\ttfamily bittorrent-\/dht} and get them to reveal 20 bytes at a time of uninitialized memory from the node.\+js process.

Here\textquotesingle{}s \href{https://github.com/feross/bittorrent-dht/commit/6c7da04025d5633699800a99ec3fbadf70ad35b8}{\texttt{ the commit}} that fixed it. We released a new fixed version, created a \href{https://nodesecurity.io/advisories/68}{\texttt{ Node Security Project disclosure}}, and deprecated all vulnerable versions on npm so users will get a warning to upgrade to a newer version.\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18604}{}\doxysubsubsection{$<$a href=\char`\"{}https\+://www.\+npmjs.\+com/package/ws\char`\"{}$>$$<$tt$>$ws$<$/tt$>$$<$/a$>$}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18604}
That got us wondering if there were other vulnerable packages. Sure enough, within a short period of time, we found the same issue in \href{https://www.npmjs.com/package/ws}{\texttt{ {\ttfamily ws}}}, the most popular Web\+Socket implementation in node.\+js.

If certain APIs were called with {\ttfamily Number} parameters instead of {\ttfamily String} or {\ttfamily Buffer} as expected, then uninitialized server memory would be disclosed to the remote peer.

These were the vulnerable methods\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{socket.send(number)}
\DoxyCodeLine{socket.ping(number)}
\DoxyCodeLine{socket.pong(number) }

\end{DoxyCode}


Here\textquotesingle{}s a vulnerable socket server with some echo functionality\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{server.on('connection', function (socket) \{}
\DoxyCodeLine{  socket.on('message', function (message) \{}
\DoxyCodeLine{    message = JSON.parse(message)}
\DoxyCodeLine{    if (message.type === 'echo') \{}
\DoxyCodeLine{      socket.send(message.data) // send back the user's message}
\DoxyCodeLine{    \}}
\DoxyCodeLine{  \})}
\DoxyCodeLine{\}) }

\end{DoxyCode}


{\ttfamily socket.\+send(number)} called on the server, will disclose server memory.

Here\textquotesingle{}s \href{https://github.com/websockets/ws/releases/tag/1.0.1}{\texttt{ the release}} where the issue was fixed, with a more detailed explanation. Props to \href{https://github.com/3rd-Eden}{\texttt{ Arnout Kazemier}} for the quick fix. Here\textquotesingle{}s the \href{https://nodesecurity.io/advisories/67}{\texttt{ Node Security Project disclosure}}.\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18605}{}\doxysubsection{What\textquotesingle{}s the solution?}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18605}
It\textquotesingle{}s important that node.\+js offers a fast way to get memory otherwise performance-\/critical applications would needlessly get a lot slower.

But we need a better way to {\itshape signal our intent} as programmers. {\bfseries{When we want uninitialized memory, we should request it explicitly.}}

Sensitive functionality should not be packed into a developer-\/friendly API that loosely accepts many different types. This type of API encourages the lazy practice of passing variables in without checking the type very carefully.\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18606}{}\doxysubsubsection{A new API\+: $<$tt$>$\+Buffer.\+alloc\+Unsafe(number)$<$/tt$>$}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18606}
The functionality of creating buffers with uninitialized memory should be part of another API. We propose {\ttfamily Buffer.\+alloc\+Unsafe(number)}. This way, it\textquotesingle{}s not part of an API that frequently gets user input of all sorts of different types passed into it.


\begin{DoxyCode}{0}
\DoxyCodeLine{var buf = Buffer.allocUnsafe(16) // careful, uninitialized memory!}
\DoxyCodeLine{}
\DoxyCodeLine{// Immediately overwrite the uninitialized buffer with data from another buffer}
\DoxyCodeLine{for (var i = 0; i < buf.length; i++) \{}
\DoxyCodeLine{  buf[i] = otherBuf[i]}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18607}{}\doxysubsection{How do we fix node.\+js core?}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18607}
We sent \href{https://github.com/nodejs/node/pull/4514}{\texttt{ a PR to node.\+js core}} (merged as {\ttfamily semver-\/major}) which defends against one case\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var str = 16}
\DoxyCodeLine{new Buffer(str, 'utf8') }

\end{DoxyCode}


In this situation, it\textquotesingle{}s implied that the programmer intended the first argument to be a string, since they passed an encoding as a second argument. Today, node.\+js will allocate uninitialized memory in the case of {\ttfamily new Buffer(number, encoding)}, which is probably not what the programmer intended.

But this is only a partial solution, since if the programmer does {\ttfamily new Buffer(variable)} (without an {\ttfamily encoding} parameter) there\textquotesingle{}s no way to know what they intended. If {\ttfamily variable} is sometimes a number, then uninitialized memory will sometimes be returned.\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18608}{}\doxysubsection{What\textquotesingle{}s the real long-\/term fix?}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18608}
We could deprecate and remove {\ttfamily new Buffer(number)} and use {\ttfamily Buffer.\+alloc\+Unsafe(number)} when we need uninitialized memory. But that would break 1000s of packages.

\sout{We believe the best solution is to\+:}

\sout{1. Change {\ttfamily new Buffer(number)} to return safe, zeroed-\/out memory}

\sout{2. Create a new API for creating uninitialized Buffers. We propose\+: {\ttfamily Buffer.\+alloc\+Unsafe(number)}}\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18609}{}\doxysubsubsection{Update}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18609}
We now support adding three new APIs\+:


\begin{DoxyItemize}
\item {\ttfamily Buffer.\+from(value)} -\/ convert from any type to a buffer
\item {\ttfamily Buffer.\+alloc(size)} -\/ create a zero-\/filled buffer
\item {\ttfamily Buffer.\+alloc\+Unsafe(size)} -\/ create an uninitialized buffer with given size
\end{DoxyItemize}

This solves the core problem that affected {\ttfamily ws} and {\ttfamily bittorrent-\/dht} which is {\ttfamily Buffer(variable)} getting tricked into taking a number argument.

This way, existing code continues working and the impact on the npm ecosystem will be minimal. Over time, npm maintainers can migrate performance-\/critical code to use {\ttfamily Buffer.\+alloc\+Unsafe(number)} instead of {\ttfamily new Buffer(number)}.\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18610}{}\doxysubsection{Conclusion}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18610}
We think there\textquotesingle{}s a serious design issue with the {\ttfamily Buffer} API as it exists today. It promotes insecure software by putting high-\/risk functionality into a convenient API with friendly \char`\"{}developer ergonomics\char`\"{}.

This wasn\textquotesingle{}t merely a theoretical exercise because we found the issue in some of the most popular npm packages.

Fortunately, there\textquotesingle{}s an easy fix that can be applied today. Use {\ttfamily safe-\/buffer} in place of {\ttfamily buffer}.


\begin{DoxyCode}{0}
\DoxyCodeLine{var Buffer = require('safe-\/buffer').Buffer }

\end{DoxyCode}


Eventually, we hope that node.\+js core can switch to this new, safer behavior. We believe the impact on the ecosystem would be minimal since it\textquotesingle{}s not a breaking change. Well-\/maintained, popular packages would be updated to use {\ttfamily Buffer.\+alloc} quickly, while older, insecure packages would magically become safe from this attack vector.\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18611}{}\doxysection{links}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18611}

\begin{DoxyItemize}
\item \href{https://github.com/nodejs/node/pull/4514}{\texttt{ Node.\+js PR\+: buffer\+: throw if both length and enc are passed}}
\item \href{https://nodesecurity.io/advisories/67}{\texttt{ Node Security Project disclosure for {\ttfamily ws}}}
\item \href{https://nodesecurity.io/advisories/68}{\texttt{ Node Security Project disclosure for{\ttfamily bittorrent-\/dht}}}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18612}{}\doxysection{credit}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18612}
The original issues in {\ttfamily bittorrent-\/dht} (\href{https://nodesecurity.io/advisories/68}{\texttt{ disclosure}}) and {\ttfamily ws} (\href{https://nodesecurity.io/advisories/67}{\texttt{ disclosure}}) were discovered by \href{https://github.com/mafintosh}{\texttt{ Mathias Buus}} and \href{http://feross.org/}{\texttt{ Feross Aboukhadijeh}}.

Thanks to \href{https://github.com/evilpacket}{\texttt{ Adam Baldwin}} for helping disclose these issues and for his work running the \href{https://nodesecurity.io/}{\texttt{ Node Security Project}}.

Thanks to \href{https://github.com/jhiesey}{\texttt{ John Hiesey}} for proofreading this README and auditing the code.\hypertarget{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18613}{}\doxysection{license}\label{md_sfcode_node_modules_npm_registry_fetch_node_modules_safe_buffer_README_autotoc_md18613}
MIT. Copyright (C) \href{http://feross.org}{\texttt{ Feross Aboukhadijeh}} 