-
Notifications
You must be signed in to change notification settings - Fork 922
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Adding SRI hash(es) for HMR script resources (#1268)
* feat(test-dev): Refactor test-dev logging mechanism Removes the hard overwriting of console from jest and instead pipes it into a file that can be analyzed if something failes. * feat(snowflake-build): Add synchronized SRI hash generator capability * feat(build-snowflake): Adds integrity checks for HMR script injections * tests(smoke): Replace snapshots with script tags including the SRI hash resolves #1214 * fix(test-dev): Add special permission for opening file * feat(snowpack): Refactor module and add more unit tests
- Loading branch information
Showing
7 changed files
with
71 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ test/build/**/build | |
test/create-snowpack-app/test-install | ||
test/esinstall/**/web_modules | ||
yarn-error.log | ||
**/*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {createHash} from 'crypto'; | ||
|
||
type SupportedSRIAlgorithm = 'sha512' | 'sha384' | 'sha256'; | ||
const DEFAULT_CRYPTO_HASH = 'sha384'; | ||
const EMPTY_BUFFER = Buffer.from(''); | ||
|
||
export const generateSRI = ( | ||
buffer: Buffer = EMPTY_BUFFER, | ||
hashAlgorithm: SupportedSRIAlgorithm = DEFAULT_CRYPTO_HASH, | ||
) => `${hashAlgorithm}-${createHash(hashAlgorithm).update(buffer).digest('base64')}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const {generateSRI} = require('../../snowpack/lib/build/import-sri'); | ||
|
||
const EMPTY = Buffer.from(''); | ||
|
||
test('empty buffer with SHA256', () => { | ||
expect(generateSRI(EMPTY, 'sha256')).toBe('sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU='); | ||
}); | ||
|
||
test('empty buffer with SHA384', () => { | ||
expect(generateSRI(EMPTY, 'sha384')).toBe( | ||
'sha384-OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb', | ||
); | ||
}); | ||
|
||
test('empty buffer with SHA512', () => { | ||
expect(generateSRI(EMPTY, 'sha512')).toBe( | ||
'sha512-z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg==', | ||
); | ||
}); | ||
|
||
test('verify that SHA384 is default SRI hash algorithm', () => { | ||
expect(generateSRI(EMPTY, 'sha384')).toBe(generateSRI(EMPTY)); | ||
}); | ||
|
||
test('undefined should return empty hash result', () => { | ||
expect(generateSRI()).toBe(generateSRI(EMPTY)); | ||
}); |
f57ca8a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: