From f15c39357930c109ed32695d1343ca09ffa5ed4f Mon Sep 17 00:00:00 2001 From: Volker Mische Date: Fri, 19 Jan 2018 00:39:03 +0100 Subject: [PATCH] Treat `=` as end of Base64 encoded string Node.js treats an equal sign as the end of a Base64 encoded string. Hence `Buffer.from('=bad')` results in an empty string. --- index.js | 4 +++- test/node/test-buffer-alloc.js | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 0adbef6..6944c05 100644 --- a/index.js +++ b/index.js @@ -1560,9 +1560,11 @@ Buffer.prototype.fill = function fill (val, start, end, encoding) { // HELPER FUNCTIONS // ================ -var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g +var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_=]/g function base64clean (str) { + // Node takes equal signs as end of the Base64 encoding + str = str.split('=')[0] // Node strips out invalid characters like \n and \t from the string, base64-js does not str = str.trim().replace(INVALID_BASE64_RE, '') // Node converts strings with length < 2 to '' diff --git a/test/node/test-buffer-alloc.js b/test/node/test-buffer-alloc.js index b2ffed1..c813a23 100644 --- a/test/node/test-buffer-alloc.js +++ b/test/node/test-buffer-alloc.js @@ -459,10 +459,10 @@ assert.strictEqual( assert.strictEqual(b.toString('latin1', 0, pos), 'Madness?! This is node.js!'); } -/* + // Regression test for https://github.com/nodejs/node/issues/3496. assert.strictEqual(Buffer.from('=bad'.repeat(1e4), 'base64').length, 0); -*/ + // Regression test for https://github.com/nodejs/node/issues/11987. assert.deepStrictEqual(Buffer.from('w0 ', 'base64'), Buffer.from('w0', 'base64')); -- 2.34.1