From 9c408a2186c526502bef33d549658d1a92b06c5e Mon Sep 17 00:00:00 2001 From: lith Date: Tue, 10 Mar 2020 03:57:06 +0200 Subject: [PATCH] fixed a bug where base64 encoding containing slash will fail --- source/image-handler/image-request.js | 7 +++---- source/image-handler/test/test-image-request.js | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/source/image-handler/image-request.js b/source/image-handler/image-request.js index 2a47530da..0114c6fdb 100644 --- a/source/image-handler/image-request.js +++ b/source/image-handler/image-request.js @@ -240,9 +240,8 @@ class ImageRequest { decodeRequest(event) { const path = event["path"]; if (path !== undefined) { - const splitPath = path.split("/"); - const encoded = splitPath[splitPath.length - 1]; - const toBuffer = Buffer.from(encoded, 'base64'); + const strippedPath = path.charAt(0) === '/' ? path.substr(1, path.length) : path; + const toBuffer = Buffer.from(strippedPath, 'base64'); try { // To support European characters, 'ascii' was removed. return JSON.parse(toBuffer.toString()); @@ -300,4 +299,4 @@ class ImageRequest { } // Exports -module.exports = ImageRequest; \ No newline at end of file +module.exports = ImageRequest; diff --git a/source/image-handler/test/test-image-request.js b/source/image-handler/test/test-image-request.js index 9507dacef..ec0b8b57e 100644 --- a/source/image-handler/test/test-image-request.js +++ b/source/image-handler/test/test-image-request.js @@ -161,6 +161,20 @@ describe('setup()', function() { }) }); }); + describe('005/encodingWithSlashRequest', function () { + it(`should read image requests with base64 encoding having slash`, function () { + const event = { + path : 'eyJidWNrZXQiOiJlbGFzdGljYmVhbnN0YWxrLXVzLWVhc3QtMi0wNjY3ODQ4ODU1MTgiLCJrZXkiOiJlbnYtcHJvZC9nY2MvbGFuZGluZ3BhZ2UvMV81N19TbGltTl9MaWZ0LUNvcnNldC1Gb3ItTWVuLVNOQVAvYXR0YWNobWVudHMvZmZjMWYxNjAtYmQzOC00MWU4LThiYWQtZTNhMTljYzYxZGQzX1/Ys9mE2YrZhSDZhNmK2YHYqiAoMikuanBnIiwiZWRpdHMiOnsicmVzaXplIjp7IndpZHRoIjo0ODAsImZpdCI6ImNvdmVyIn19fQ==' + } + // Act + const imageRequest = new ImageRequest(); + const result = imageRequest.parseImageKey(event, 'Default'); + // Assert + const expectedResult = 'env-prod/gcc/landingpage/1_57_SlimN_Lift-Corset-For-Men-SNAP/attachments/ffc1f160-bd38-41e8-8bad-e3a19cc61dd3__سليم ليفت (2).jpg'; + assert.deepEqual(result, expectedResult); + + }) + }); }); // ---------------------------------------------------------------------------- // getOriginalImage()