Просмотр исходного кода

Merge pull request #5891 from overleaf/tm-unarchive-checksum-mismatches

Test checksum against buffer, before converting to a JS string

GitOrigin-RevId: 645d8a89a3881275ff555fda00eb4985677c6b34
Brian Gough 4 лет назад
Родитель
Сommit
26d5241eaa

+ 5 - 4
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)))
   })
 }

+ 7 - 0
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(