Skip to content

Commit fc85b8f

Browse files
fix: replace hash routine md5 with sha256 (#12722)
Co-authored-by: Dan Armbrust <[email protected]>
1 parent c1a57cb commit fc85b8f

File tree

8 files changed

+43
-20
lines changed

8 files changed

+43
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767

6868
### Fixes
6969

70+
- `[*]` Use `sha256` instead of `md5` as hashing algortihm for compatibility with FIPS systems ([#12722](https://github.com/facebook/jest/pull/12722))
7071
- `[babel-jest]` [**BREAKING**] Pass `rootDir` as `root` in Babel's options ([#12689](https://github.com/facebook/jest/pull/12689))
7172
- `[expect]` Move typings of `.not`, `.rejects` and `.resolves` modifiers outside of `Matchers` interface ([#12346](https://github.com/facebook/jest/pull/12346))
7273
- `[expect]` Throw useful error if `expect.extend` is called with invalid matchers ([#12488](https://github.com/facebook/jest/pull/12488))

packages/babel-jest/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function getCacheKeyFromConfig(
7878

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

81-
return createHash('md5')
81+
return createHash('sha256')
8282
.update(THIS_FILE)
8383
.update('\0', 'utf8')
8484
.update(JSON.stringify(babelOptions.options))
@@ -98,7 +98,8 @@ function getCacheKeyFromConfig(
9898
.update(process.env.BABEL_ENV || '')
9999
.update('\0', 'utf8')
100100
.update(process.version)
101-
.digest('hex');
101+
.digest('hex')
102+
.substring(0, 32);
102103
}
103104

104105
function loadBabelConfig(

packages/jest-circus/src/__mocks__/testUtils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ interface Result extends ExecaSyncReturnValue {
3131
}
3232

3333
export const runTest = (source: string) => {
34-
const filename = createHash('md5').update(source).digest('hex');
34+
const filename = createHash('sha256')
35+
.update(source)
36+
.digest('hex')
37+
.substring(0, 32);
3538
const tmpFilename = path.join(tmpdir(), filename);
3639

3740
const content = `

packages/jest-config/src/__tests__/normalize.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ afterEach(() => {
6868

6969
it('picks an id based on the rootDir', async () => {
7070
const rootDir = '/root/path/foo';
71-
const expected = createHash('md5')
71+
const expected = createHash('sha256')
7272
.update('/root/path/foo')
7373
.update(String(Infinity))
74-
.digest('hex');
74+
.digest('hex')
75+
.substring(0, 32);
7576
const {options} = await normalize(
7677
{
7778
rootDir,

packages/jest-config/src/normalize.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,13 @@ const normalizeMissingOptions = (
333333
projectIndex: number,
334334
): Config.InitialOptionsWithRootDir => {
335335
if (!options.id) {
336-
options.id = createHash('md5')
336+
options.id = createHash('sha256')
337337
.update(options.rootDir)
338338
// In case we load config from some path that has the same root dir
339339
.update(configPath || '')
340340
.update(String(projectIndex))
341-
.digest('hex');
341+
.digest('hex')
342+
.substring(0, 32);
342343
}
343344

344345
if (!options.setupFiles) {

packages/jest-create-cache-key-function/src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ function getGlobalCacheKey(files: Array<string>, values: Array<string>) {
4949
]
5050
.reduce(
5151
(hash, chunk) => hash.update('\0', 'utf8').update(chunk || ''),
52-
createHash('md5'),
52+
createHash('sha256'),
5353
)
54-
.digest('hex');
54+
.digest('hex')
55+
.substring(0, 32);
5556
}
5657

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

64-
return createHash('md5')
65+
return createHash('sha256')
6566
.update(globalCacheKey)
6667
.update('\0', 'utf8')
6768
.update(sourceText)
6869
.update('\0', 'utf8')
6970
.update(config.rootDir ? relative(config.rootDir, sourcePath) : '')
7071
.update('\0', 'utf8')
7172
.update(instrument ? 'instrument' : '')
72-
.digest('hex');
73+
.digest('hex')
74+
.substring(0, 32);
7375
};
7476
}
7577

packages/jest-haste-map/src/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,10 @@ export default class HasteMap extends EventEmitter {
298298
}
299299

300300
private async setupCachePath(options: Options): Promise<void> {
301-
const rootDirHash = createHash('md5').update(options.rootDir).digest('hex');
301+
const rootDirHash = createHash('sha256')
302+
.update(options.rootDir)
303+
.digest('hex')
304+
.substring(0, 32);
302305
let hasteImplHash = '';
303306
let dependencyExtractorHash = '';
304307

@@ -344,8 +347,11 @@ export default class HasteMap extends EventEmitter {
344347
id: string,
345348
...extra: Array<string>
346349
): string {
347-
const hash = createHash('md5').update(extra.join(''));
348-
return path.join(tmpdir, `${id.replace(/\W/g, '-')}-${hash.digest('hex')}`);
350+
const hash = createHash('sha256').update(extra.join(''));
351+
return path.join(
352+
tmpdir,
353+
`${id.replace(/\W/g, '-')}-${hash.digest('hex').substring(0, 32)}`,
354+
);
349355
}
350356

351357
static getModuleMapFromJSON(json: SerializableModuleMap): HasteModuleMap {

packages/jest-transform/src/ScriptTransformer.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,21 @@ class ScriptTransformer {
116116
transformerCacheKey: string | undefined,
117117
): string {
118118
if (transformerCacheKey) {
119-
return createHash('md5')
119+
return createHash('sha256')
120120
.update(transformerCacheKey)
121121
.update(CACHE_VERSION)
122-
.digest('hex');
122+
.digest('hex')
123+
.substring(0, 32);
123124
}
124125

125-
return createHash('md5')
126+
return createHash('sha256')
126127
.update(fileData)
127128
.update(transformOptions.configString)
128129
.update(transformOptions.instrument ? 'instrument' : '')
129130
.update(filename)
130131
.update(CACHE_VERSION)
131-
.digest('hex');
132+
.digest('hex')
133+
.substring(0, 32);
132134
}
133135

134136
private _getCacheKey(
@@ -869,7 +871,10 @@ const stripShebang = (content: string) => {
869871
* could get corrupted, out-of-sync, etc.
870872
*/
871873
function writeCodeCacheFile(cachePath: string, code: string) {
872-
const checksum = createHash('md5').update(code).digest('hex');
874+
const checksum = createHash('sha256')
875+
.update(code)
876+
.digest('hex')
877+
.substring(0, 32);
873878
writeCacheFile(cachePath, `${checksum}\n${code}`);
874879
}
875880

@@ -885,7 +890,10 @@ function readCodeCacheFile(cachePath: string): string | null {
885890
return null;
886891
}
887892
const code = content.substring(33);
888-
const checksum = createHash('md5').update(code).digest('hex');
893+
const checksum = createHash('sha256')
894+
.update(code)
895+
.digest('hex')
896+
.substring(0, 32);
889897
if (checksum === content.substring(0, 32)) {
890898
return code;
891899
}

0 commit comments

Comments
 (0)