From c71dbf3ae4e04aea3594a4528aadf36bb2411c6d Mon Sep 17 00:00:00 2001 From: hasezoey Date: Mon, 8 May 2023 17:06:35 +0200 Subject: [PATCH] fix(MongoBinaryDownload::makeMD5check): actually hash the content of the file, not the path --- .../src/util/MongoBinaryDownload.ts | 4 ++-- .../src/util/__tests__/MongoBinaryDownload.test.ts | 8 ++++---- packages/mongodb-memory-server-core/src/util/utils.ts | 9 +++++++++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/mongodb-memory-server-core/src/util/MongoBinaryDownload.ts b/packages/mongodb-memory-server-core/src/util/MongoBinaryDownload.ts index 9c96b9757..f16669ae3 100644 --- a/packages/mongodb-memory-server-core/src/util/MongoBinaryDownload.ts +++ b/packages/mongodb-memory-server-core/src/util/MongoBinaryDownload.ts @@ -10,7 +10,7 @@ import MongoBinaryDownloadUrl from './MongoBinaryDownloadUrl'; import { HttpsProxyAgent } from 'https-proxy-agent'; import resolveConfig, { envToBool, ResolveConfigVariables } from './resolveConfig'; import debug from 'debug'; -import { assertion, mkdir, pathExists, md5 } from './utils'; +import { assertion, mkdir, pathExists, md5FromFile } from './utils'; import { DryMongoBinary } from './DryMongoBinary'; import { MongoBinaryOpts } from './MongoBinary'; import { clearLine } from 'readline'; @@ -156,7 +156,7 @@ export class MongoBinaryDownload { const signatureContent = (await fspromises.readFile(archiveMD5Path)).toString('utf-8'); const regexMatch = signatureContent.match(/^\s*([\w\d]+)\s*/i); const md5SigRemote = regexMatch ? regexMatch[1] : null; - const md5SigLocal = md5(mongoDBArchive); + const md5SigLocal = await md5FromFile(mongoDBArchive); log(`makeMD5check: Local MD5: ${md5SigLocal}, Remote MD5: ${md5SigRemote}`); if (md5SigRemote !== md5SigLocal) { diff --git a/packages/mongodb-memory-server-core/src/util/__tests__/MongoBinaryDownload.test.ts b/packages/mongodb-memory-server-core/src/util/__tests__/MongoBinaryDownload.test.ts index 845e7203e..d5044e540 100644 --- a/packages/mongodb-memory-server-core/src/util/__tests__/MongoBinaryDownload.test.ts +++ b/packages/mongodb-memory-server-core/src/util/__tests__/MongoBinaryDownload.test.ts @@ -130,7 +130,7 @@ describe('MongoBinaryDownload', () => { const fileWithReferenceMd5 = '/another/path'; jest.spyOn(fspromises, 'readFile').mockResolvedValueOnce(`${someMd5} fileName`); - jest.spyOn(utils, 'md5').mockImplementationOnce(() => someMd5); + jest.spyOn(utils, 'md5FromFile').mockResolvedValueOnce(someMd5); jest.spyOn(fspromises, 'unlink').mockResolvedValue(void 0); const du = new MongoBinaryDownload({ downloadDir: '/', checkMD5: true }); @@ -142,12 +142,12 @@ describe('MongoBinaryDownload', () => { expect(du.download).toBeCalledWith(urlToMongoDBArchivePath); expect(fspromises.readFile).toBeCalledWith(fileWithReferenceMd5); expect(fspromises.unlink).toBeCalledTimes(1); - expect(utils.md5).toBeCalledWith(mongoDBArchivePath); + expect(utils.md5FromFile).toBeCalledWith(mongoDBArchivePath); }); it('makeMD5check throws an error if md5 of downloaded mongoDBArchive is NOT the same as in the reference result', async () => { jest.spyOn(fspromises, 'readFile').mockResolvedValueOnce(`someMD5 fileName`); - jest.spyOn(utils, 'md5').mockImplementationOnce(() => 'anotherMD5'); + jest.spyOn(utils, 'md5FromFile').mockResolvedValueOnce('anotherMD5'); const du = new MongoBinaryDownload({ downloadDir: '/', checkMD5: true }); jest.spyOn(du, 'download').mockResolvedValue(''); @@ -163,7 +163,7 @@ describe('MongoBinaryDownload', () => { it('false value of checkMD5 attribute disables makeMD5check validation', async () => { jest.spyOn(fspromises, 'readFile').mockResolvedValueOnce(`someMD5 fileName`); - jest.spyOn(utils, 'md5').mockImplementationOnce(() => 'anotherMD5'); + jest.spyOn(utils, 'md5FromFile').mockResolvedValueOnce('anotherMD5'); const du = new MongoBinaryDownload({ downloadDir: '/', checkMD5: false }); const result = await du.makeMD5check('', ''); diff --git a/packages/mongodb-memory-server-core/src/util/utils.ts b/packages/mongodb-memory-server-core/src/util/utils.ts index 248a7d254..5ed333889 100644 --- a/packages/mongodb-memory-server-core/src/util/utils.ts +++ b/packages/mongodb-memory-server-core/src/util/utils.ts @@ -370,3 +370,12 @@ export function uuidv4(): string { export function md5(content: BinaryLike): string { return createHash('md5').update(content).digest('hex'); } + +/** + * Helper function to have md5 generation and definition in one place for a file + * @param file the location of a file to read for a hash + * @returns a md5 of the input file + */ +export async function md5FromFile(file: string): Promise { + return md5(await fspromises.readFile(file)); +}