mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
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:
parent
a757ed898b
commit
26d5241eaa
2 changed files with 12 additions and 4 deletions
|
@ -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)))
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in a new issue