Skip to content

Commit

Permalink
Fix buffer initialization for Base64 encoding (#75)
Browse files Browse the repository at this point in the history
commit 3827da752d3b2eb86613b92ffb6715806d799fdc
Author: Marius <maerious@gmail.com>
Date:   Tue May 30 21:31:26 2017 +0200

    Transpile changes

commit 025aac69f827e6c0f51215609070bab6b76cc491
Author: Marius <maerious@gmail.com>
Date:   Tue May 30 21:30:41 2017 +0200

    Add test for number metadata

commit 35b470af8386ff96230e09408a3ba83c6831b3a0
Merge: 902e4bf 74e4500
Author: Marius <maerious@gmail.com>
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 <renee@kooi.me>
Date:   Tue May 30 17:18:44 2017 +0200

    fix lint

commit a420570
Author: Renée Kooi <renee@kooi.me>
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)
  • Loading branch information
Acconut committed May 30, 2017
1 parent 902e4bf commit 66694b4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
10 changes: 8 additions & 2 deletions lib.es5/node/base64.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 @@ -53,6 +53,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
5 changes: 3 additions & 2 deletions test/spec/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ describe("tus", function () {
metadata: {
foo: "hello",
bar: "world",
nonlatin: "słońce"
nonlatin: "słońce",
number: 100
},
withCredentials: true,
onProgress: function () {},
Expand All @@ -57,7 +58,7 @@ describe("tus", function () {
expect(req.requestHeaders["Upload-Length"]).toBe(11);
if (isBrowser) expect(req.withCredentials).toBe(true);
if (isNode || (isBrowser && "btoa" in window)) {
expect(req.requestHeaders["Upload-Metadata"]).toBe("foo aGVsbG8=,bar d29ybGQ=,nonlatin c8WCb8WEY2U=");
expect(req.requestHeaders["Upload-Metadata"]).toBe("foo aGVsbG8=,bar d29ybGQ=,nonlatin c8WCb8WEY2U=,number MTAw");
}

req.respondWith({
Expand Down

0 comments on commit 66694b4

Please sign in to comment.