Skip to content

Commit

Permalink
fix: replace hash routine md5 with sha256 (jestjs#12722)
Browse files Browse the repository at this point in the history
Co-authored-by: Dan Armbrust <daniel.armbrust@va.gov>
  • Loading branch information
2 people authored and F3n67u committed May 2, 2022
1 parent 30a0c97 commit b904b51
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@

### Fixes

- `[*]` Use `sha256` instead of `md5` as hashing algortihm for compatibility with FIPS systems ([#12722](https://github.com/facebook/jest/pull/12722))
- `[babel-jest]` [**BREAKING**] Pass `rootDir` as `root` in Babel's options ([#12689](https://github.com/facebook/jest/pull/12689))
- `[expect]` Move typings of `.not`, `.rejects` and `.resolves` modifiers outside of `Matchers` interface ([#12346](https://github.com/facebook/jest/pull/12346))
- `[expect]` Throw useful error if `expect.extend` is called with invalid matchers ([#12488](https://github.com/facebook/jest/pull/12488))
Expand Down
5 changes: 3 additions & 2 deletions packages/babel-jest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function getCacheKeyFromConfig(

const configPath = [babelOptions.config || '', babelOptions.babelrc || ''];

return createHash('md5')
return createHash('sha256')
.update(THIS_FILE)
.update('\0', 'utf8')
.update(JSON.stringify(babelOptions.options))
Expand All @@ -98,7 +98,8 @@ function getCacheKeyFromConfig(
.update(process.env.BABEL_ENV || '')
.update('\0', 'utf8')
.update(process.version)
.digest('hex');
.digest('hex')
.substring(0, 32);
}

function loadBabelConfig(
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-circus/src/__mocks__/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ interface Result extends ExecaSyncReturnValue {
}

export const runTest = (source: string) => {
const filename = createHash('md5').update(source).digest('hex');
const filename = createHash('sha256')
.update(source)
.digest('hex')
.substring(0, 32);
const tmpFilename = path.join(tmpdir(), filename);

const content = `
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-config/src/__tests__/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ afterEach(() => {

it('picks an id based on the rootDir', async () => {
const rootDir = '/root/path/foo';
const expected = createHash('md5')
const expected = createHash('sha256')
.update('/root/path/foo')
.update(String(Infinity))
.digest('hex');
.digest('hex')
.substring(0, 32);
const {options} = await normalize(
{
rootDir,
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,13 @@ const normalizeMissingOptions = (
projectIndex: number,
): Config.InitialOptionsWithRootDir => {
if (!options.id) {
options.id = createHash('md5')
options.id = createHash('sha256')
.update(options.rootDir)
// In case we load config from some path that has the same root dir
.update(configPath || '')
.update(String(projectIndex))
.digest('hex');
.digest('hex')
.substring(0, 32);
}

if (!options.setupFiles) {
Expand Down
10 changes: 6 additions & 4 deletions packages/jest-create-cache-key-function/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ function getGlobalCacheKey(files: Array<string>, values: Array<string>) {
]
.reduce(
(hash, chunk) => hash.update('\0', 'utf8').update(chunk || ''),
createHash('md5'),
createHash('sha256'),
)
.digest('hex');
.digest('hex')
.substring(0, 32);
}

function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction {
Expand All @@ -61,15 +62,16 @@ function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction {
const inferredOptions = options || configString;
const {config, instrument} = inferredOptions;

return createHash('md5')
return createHash('sha256')
.update(globalCacheKey)
.update('\0', 'utf8')
.update(sourceText)
.update('\0', 'utf8')
.update(config.rootDir ? relative(config.rootDir, sourcePath) : '')
.update('\0', 'utf8')
.update(instrument ? 'instrument' : '')
.digest('hex');
.digest('hex')
.substring(0, 32);
};
}

Expand Down
12 changes: 9 additions & 3 deletions packages/jest-haste-map/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,10 @@ export default class HasteMap extends EventEmitter {
}

private async setupCachePath(options: Options): Promise<void> {
const rootDirHash = createHash('md5').update(options.rootDir).digest('hex');
const rootDirHash = createHash('sha256')
.update(options.rootDir)
.digest('hex')
.substring(0, 32);
let hasteImplHash = '';
let dependencyExtractorHash = '';

Expand Down Expand Up @@ -344,8 +347,11 @@ export default class HasteMap extends EventEmitter {
id: string,
...extra: Array<string>
): string {
const hash = createHash('md5').update(extra.join(''));
return path.join(tmpdir, `${id.replace(/\W/g, '-')}-${hash.digest('hex')}`);
const hash = createHash('sha256').update(extra.join(''));
return path.join(
tmpdir,
`${id.replace(/\W/g, '-')}-${hash.digest('hex').substring(0, 32)}`,
);
}

static getModuleMapFromJSON(json: SerializableModuleMap): HasteModuleMap {
Expand Down
20 changes: 14 additions & 6 deletions packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,21 @@ class ScriptTransformer {
transformerCacheKey: string | undefined,
): string {
if (transformerCacheKey) {
return createHash('md5')
return createHash('sha256')
.update(transformerCacheKey)
.update(CACHE_VERSION)
.digest('hex');
.digest('hex')
.substring(0, 32);
}

return createHash('md5')
return createHash('sha256')
.update(fileData)
.update(transformOptions.configString)
.update(transformOptions.instrument ? 'instrument' : '')
.update(filename)
.update(CACHE_VERSION)
.digest('hex');
.digest('hex')
.substring(0, 32);
}

private _getCacheKey(
Expand Down Expand Up @@ -869,7 +871,10 @@ const stripShebang = (content: string) => {
* could get corrupted, out-of-sync, etc.
*/
function writeCodeCacheFile(cachePath: string, code: string) {
const checksum = createHash('md5').update(code).digest('hex');
const checksum = createHash('sha256')
.update(code)
.digest('hex')
.substring(0, 32);
writeCacheFile(cachePath, `${checksum}\n${code}`);
}

Expand All @@ -885,7 +890,10 @@ function readCodeCacheFile(cachePath: string): string | null {
return null;
}
const code = content.substring(33);
const checksum = createHash('md5').update(code).digest('hex');
const checksum = createHash('sha256')
.update(code)
.digest('hex')
.substring(0, 32);
if (checksum === content.substring(0, 32)) {
return code;
}
Expand Down

0 comments on commit b904b51

Please sign in to comment.