Skip to content

Commit 66694b4

Browse files
committed
Fix buffer initialization for Base64 encoding (#75)
commit 3827da752d3b2eb86613b92ffb6715806d799fdc Author: Marius <[email protected]> Date: Tue May 30 21:31:26 2017 +0200 Transpile changes commit 025aac69f827e6c0f51215609070bab6b76cc491 Author: Marius <[email protected]> Date: Tue May 30 21:30:41 2017 +0200 Add test for number metadata commit 35b470af8386ff96230e09408a3ba83c6831b3a0 Merge: 902e4bf 74e4500 Author: Marius <[email protected]> Date: Tue May 30 21:26:52 2017 +0200 Merge branch 'fix/buffer-leak' of https://github.com/goto-bus-stop/tus-js-client into buffer commit 74e4500 Author: Renée Kooi <[email protected]> Date: Tue May 30 17:18:44 2017 +0200 fix lint commit a420570 Author: Renée Kooi <[email protected]> Date: Tue May 30 15:43:13 2017 +0200 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 902e4bf commit 66694b4

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

lib.es5/node/base64.js

+8-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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
@@ -53,6 +53,7 @@
5353
"watchify": "^3.5.0"
5454
},
5555
"dependencies": {
56+
"buffer-from": "^0.1.1",
5657
"extend": "^3.0.0",
5758
"lodash.throttle": "^4.1.1",
5859
"resolve-url": "^0.2.1"

test/spec/upload.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ describe("tus", function () {
3535
metadata: {
3636
foo: "hello",
3737
bar: "world",
38-
nonlatin: "słońce"
38+
nonlatin: "słońce",
39+
number: 100
3940
},
4041
withCredentials: true,
4142
onProgress: function () {},
@@ -57,7 +58,7 @@ describe("tus", function () {
5758
expect(req.requestHeaders["Upload-Length"]).toBe(11);
5859
if (isBrowser) expect(req.withCredentials).toBe(true);
5960
if (isNode || (isBrowser && "btoa" in window)) {
60-
expect(req.requestHeaders["Upload-Metadata"]).toBe("foo aGVsbG8=,bar d29ybGQ=,nonlatin c8WCb8WEY2U=");
61+
expect(req.requestHeaders["Upload-Metadata"]).toBe("foo aGVsbG8=,bar d29ybGQ=,nonlatin c8WCb8WEY2U=,number MTAw");
6162
}
6263

6364
req.respondWith({

0 commit comments

Comments
 (0)