Skip to content

Commit a420570

Browse files
committed
fix buffer initialization in base64 encoding
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)
1 parent b679f78 commit a420570

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

lib/node/base64.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
/* global: Buffer */
1+
import bufferFrom from 'buffer-from';
22

33
export function encode(data) {
4-
return new Buffer(data).toString("base64");
4+
return bufferFrom(String(data)).toString("base64");
55
}
66

77
export const isSupported = true;

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"watchify": "^3.5.0"
5555
},
5656
"dependencies": {
57+
"buffer-from": "^0.1.1",
5758
"extend": "^3.0.0",
5859
"lodash.throttle": "^4.1.1",
5960
"resolve-url": "^0.2.1"

0 commit comments

Comments
 (0)