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

Test checksum against buffer, before converting to a JS string

GitOrigin-RevId: 645d8a89a3881275ff555fda00eb4985677c6b34
This commit is contained in:
Brian Gough 2021-11-29 10:04:35 +00:00 committed by Copybot
parent a757ed898b
commit 26d5241eaa
2 changed files with 12 additions and 4 deletions

View file

@ -139,8 +139,8 @@ async function getDoc(projectId, docId) {
key key
) )
stream.resume() stream.resume()
const json = await _streamToString(stream) const buffer = await _streamToBuffer(stream)
const md5 = crypto.createHash('md5').update(json).digest('hex') const md5 = crypto.createHash('md5').update(buffer).digest('hex')
if (sourceMd5 !== md5) { if (sourceMd5 !== md5) {
throw new Errors.Md5MismatchError('md5 mismatch when downloading doc', { throw new Errors.Md5MismatchError('md5 mismatch when downloading doc', {
key, key,
@ -149,6 +149,7 @@ async function getDoc(projectId, docId) {
}) })
} }
const json = buffer.toString()
const doc = JSON.parse(json) const doc = JSON.parse(json)
const mongoDoc = {} const mongoDoc = {}
@ -251,11 +252,11 @@ async function destroyArchiveWithRetry(projectId, docId) {
throw lastError throw lastError
} }
async function _streamToString(stream) { async function _streamToBuffer(stream) {
const chunks = [] const chunks = []
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
stream.on('data', chunk => chunks.push(chunk)) stream.on('data', chunk => chunks.push(chunk))
stream.on('error', reject) stream.on('error', reject)
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8'))) stream.on('end', () => resolve(Buffer.concat(chunks)))
}) })
} }

View file

@ -265,6 +265,13 @@ describe('DocArchiveManager', function () {
.to.eventually.be.fulfilled .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 () { it('should throw an error if the md5 does not match', async function () {
PersistorManager.getObjectMd5Hash.resolves('badf00d') PersistorManager.getObjectMd5Hash.resolves('badf00d')
await expect( await expect(