Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: subresource integrity #803

Merged
merged 1 commit into from
Aug 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: integrity
  • Loading branch information
jarrett-confrey committed Jul 28, 2021
commit f1a4a6fc6d249555e33576380d5eb7f5609e80dd
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ _Before_ submitting a pull request, please make sure the following is done…
8. Ensure the test suite passes via `yarn test`.

```sh-session
$ yarn test:prepare # build example and generate fixtures before running tests
$ yarn test
```

Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/ChunkExtractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,12 @@ class ChunkExtractor {
createChunkAsset({ filename, chunk, type, linkType }) {
const resolvedFilename =
typeof filename === 'object' && filename.name ? filename.name : filename
const resolvedIntegrity =
typeof filename === 'object' && filename.integrity ? filename.integrity : null

return {
filename: resolvedFilename,
integrity: resolvedIntegrity,
scriptType: getFileScriptType(resolvedFilename),
chunk,
url: this.resolvePublicUrl(resolvedFilename),
Expand Down
25 changes: 25 additions & 0 deletions packages/server/src/ChunkExtractor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,31 @@ describe('ChunkExtrator', () => {
`)
})

it('should add integrity if available in stats', () => {
const testExtractor = new ChunkExtractor({
stats: {
...stats,
namedChunkGroups: {
...stats.namedChunkGroups,
main: {
...stats.namedChunkGroups.main,
assets: stats.namedChunkGroups.main.assets.map(name => ({
name,
// pseudo hash - reversed name
integrity: name.split('').reverse().join(''),
})),
},
},
},
outputPath: targetPath,
})
expect(testExtractor.getScriptTags({ crossorigin: 'anonymous' }))
.toMatchInlineSnapshot(`
"<script id=\\"__LOADABLE_REQUIRED_CHUNKS__\\" type=\\"application/json\\" crossorigin=\\"anonymous\\">[]</script><script id=\\"__LOADABLE_REQUIRED_CHUNKS___ext\\" type=\\"application/json\\" crossorigin=\\"anonymous\\">{\\"namedChunks\\":[]}</script>
<script async data-chunk=\\"main\\" src=\\"/dist/node/main.js\\" integrity=\\"sj.niam\\" crossorigin=\\"anonymous\\"></script>"
`)
})

it('should add extra props if specified - object argument', () => {
extractor.addChunk('letters-A')
expect(extractor.getScriptTags({ nonce: 'testnonce' }))
Expand Down
16 changes: 14 additions & 2 deletions packages/webpack-plugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,20 @@ class LoadablePlugin {
return {
id: chunk.id,
files: [...chunk.files],
};
});
}
})

// update namedChunkGroups with integrity from webpack-subresource-integrity if available
Object.values(stats.namedChunkGroups).forEach(namedChunkGroup => {
namedChunkGroup.assets.forEach(namedChunkGroupAsset => {
if (!namedChunkGroupAsset.integrity) {
const asset = stats.assets.find(a => a.name === namedChunkGroupAsset.name) || {}
if (asset.integrity) {
namedChunkGroupAsset.integrity = asset.integrity
}
}
})
})

const result = JSON.stringify(stats, null, 2)

Expand Down