From 26d5241eaa667007bb6a2d3779f99376836bdbb5 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 29 Nov 2021 10:04:35 +0000 Subject: [PATCH] Merge pull request #5891 from overleaf/tm-unarchive-checksum-mismatches Test checksum against buffer, before converting to a JS string GitOrigin-RevId: 645d8a89a3881275ff555fda00eb4985677c6b34 --- services/docstore/app/js/DocArchiveManager.js | 9 +++++---- services/docstore/test/unit/js/DocArchiveManagerTests.js | 7 +++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/services/docstore/app/js/DocArchiveManager.js b/services/docstore/app/js/DocArchiveManager.js index edd920833d..6212bd5e3d 100644 --- a/services/docstore/app/js/DocArchiveManager.js +++ b/services/docstore/app/js/DocArchiveManager.js @@ -139,8 +139,8 @@ async function getDoc(projectId, docId) { key ) stream.resume() - const json = await _streamToString(stream) - const md5 = crypto.createHash('md5').update(json).digest('hex') + const buffer = await _streamToBuffer(stream) + const md5 = crypto.createHash('md5').update(buffer).digest('hex') if (sourceMd5 !== md5) { throw new Errors.Md5MismatchError('md5 mismatch when downloading doc', { key, @@ -149,6 +149,7 @@ async function getDoc(projectId, docId) { }) } + const json = buffer.toString() const doc = JSON.parse(json) const mongoDoc = {} @@ -251,11 +252,11 @@ async function destroyArchiveWithRetry(projectId, docId) { throw lastError } -async function _streamToString(stream) { +async function _streamToBuffer(stream) { const chunks = [] return new Promise((resolve, reject) => { stream.on('data', chunk => chunks.push(chunk)) stream.on('error', reject) - stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8'))) + stream.on('end', () => resolve(Buffer.concat(chunks))) }) } diff --git a/services/docstore/test/unit/js/DocArchiveManagerTests.js b/services/docstore/test/unit/js/DocArchiveManagerTests.js index 39369715d7..0d70c2e58b 100644 --- a/services/docstore/test/unit/js/DocArchiveManagerTests.js +++ b/services/docstore/test/unit/js/DocArchiveManagerTests.js @@ -265,6 +265,13 @@ describe('DocArchiveManager', function () { .to.eventually.be.fulfilled }) + it('should test md5 validity with the raw buffer', async function () { + await DocArchiveManager.promises.unarchiveDoc(projectId, docId) + expect(HashUpdate).to.have.been.calledWithMatch( + sinon.match.instanceOf(Buffer) + ) + }) + it('should throw an error if the md5 does not match', async function () { PersistorManager.getObjectMd5Hash.resolves('badf00d') await expect(