Skip to content

Commit

Permalink
fix buffer initialization in base64 encoding
Browse files Browse the repository at this point in the history
If a number was passed to `encode()`, the buffer would be created with
uninitialised memory. This patch casts anything that's passed in to
a string first and then uses the safe `Buffer.from` API. `Buffer.from`
was added in Node v4 but the `buffer-from` module ponyfills it for older
Node versions.

See [nodejs/node#4660](nodejs/node#4660)
  • Loading branch information
goto-bus-stop committed May 30, 2017
1 parent b679f78 commit a420570
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/node/base64.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global: Buffer */
import bufferFrom from 'buffer-from';

export function encode(data) {
return new Buffer(data).toString("base64");
return bufferFrom(String(data)).toString("base64");
}

export const isSupported = true;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"watchify": "^3.5.0"
},
"dependencies": {
"buffer-from": "^0.1.1",
"extend": "^3.0.0",
"lodash.throttle": "^4.1.1",
"resolve-url": "^0.2.1"
Expand Down

0 comments on commit a420570

Please sign in to comment.